include<18f452.h>
#include<string.h>
#use delay(clock=4000000)
#fuses NOLVP,NOWDT,NOPROTECT,HS
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, BITS=8, PARITY=N,stream=PUMP,ERRORS)
//#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,BITS=8,PARITY=N,stream=PUMP,FORCE_SW,RESTART_WDT)
#bit LCD_RS = 0x8.2 //RD2
#bit LCD_RW = 0x8.0 //RD0
#bit LCD_EN = 0x8.3 //RD3
#bit RELAY1 = 0x5.0 //RA1
#bit RELAY2 = 0x5.1 //RA2
#bit RELAY3 = 0x5.2 //RA3
#bit RELAY4 = 0x5.3 //RA4
#byte LCD_DATA = 8
#define LCD_STROBE() ((LCD_EN = 1),(LCD_EN=0))
unsigned char buffer[35],response_flag = 0,Pulse_Count,ch,m,i,j;
int1 relay1_flag,int0_flag;
int16 UNITS;
int16 int_unit[5],t_unit,l_unit,r_unit;
unsigned char num[11];
//rb
//int16 uc1,uc2;
//unsigned int8 old_portb=0,port_b;
//int1 change,ca,ne;
void lcd_string(char * s);
void lcd_write(unsigned char c);
void lcd_clear(void);
void lcd_string(char * s);
void lcd_character(char c);
void lcd_char(unsigned char c);
void lcd_init();
void recharge();
void Read_response();
void char_inte(unsigned char a,b,c);
void send_cmgf(unsigned char *str);
void lcd_numdsply();
void clear_1stline();
void clear_2ndline();
void calc_units(int16 num);
void calc_rs(int16 num);
void get_unit(int16 unit[]);
void char_unit(unsigned char unit[]);
void TX_data(int16 no);
void rs_disp(unsigned int num );
unsigned char str22[16]="---WELCOME TO--";
unsigned char str23[17]="NOC Meter";
#INT_EXT
void Pulse_isr()
{
Pulse_Count++;
if(Pulse_Count == 16)//in place of 10 unit change on 18-7-2014 -32 in naturetech
{
Pulse_Count++;
int0_flag = 1;
Pulse_Count = 0;
UNITS++;
}
}
void calc_units(int16 num)
{
int16 unit_buff[5],x1,x2,x3,x4;
x1 = num/100;
x1 = x1+0x30;
unit_buff[1] = x1;
num = num%100;
num = num*10;
x2 = num/100;
x2 = x2+0x30;
unit_buff[2] = x2;
num = num%100;
num = num*10;
x3 = num/100;
x3 = x3+0x30;
unit_buff[3] = x3;
num = num%100;
num = num*10;
x4 = num/100;
x4 = x4+0x30;
unit_buff[4] = x4;
lcd_char(unit_buff[1]);
lcd_char(unit_buff[2]);
lcd_char(unit_buff[3]);
lcd_char(unit_buff[4]);
lcd_char(' ');
}
void char_inte(unsigned char a,b,c)
{
unsigned char new[3];
new[0]=a;
new[1]=b;
new[2]=c;
for(int i=0;i<3;i++)//for reading the buffer unit and save it to int
{
if(new[i]=='1')
int_unit[i]=1;
if(new[i]=='2')
int_unit[i]=2;
if(new[i]=='3')
int_unit[i]=3;
if(new[i]=='4')
int_unit[i]=4;
if(new[i]=='5')
int_unit[i]=5;
if(new[i]=='6')
int_unit[i]=6;
if(new[i]=='7')
int_unit[i]=7;
if(new[i]=='8')
int_unit[i]=8;
if(new[i]=='9')
int_unit[i]=9;
if(new[i]=='0')
int_unit[i]=0;
}
int16 a1=0,b1=0,c1=0;
a1=int_unit[0] * 100;
b1=int_unit[1] * 10;
c1=int_unit[2];
a1= a1+b1+c1;
r_unit=a1;
}
void calc_rs(int16 num)
{
int16 unit_buff[4],x1,x2,x3;
x1 = num/100;
x1 = x1+0x30;
unit_buff[1] = x1;
num = num%100;
num = num*10;
x2 = num/100;
x2 = x2+0x30;
unit_buff[2] = x2;
num = num%100;
num = num*10;
x3 = num/100;
x3 = x3+0x30;
unit_buff[3] = x3;
lcd_char(unit_buff[1]);
lcd_char(unit_buff[2]);
lcd_char(unit_buff[3]);
}
void main()
{
set_tris_d(0x00);
set_tris_a(0x00);
set_tris_c(0x80);
Pulse_Count = 0;
UNITS = 0;
r_unit=0;
t_unit=0;
l_unit=0;
lcd_init();
enable_interrupts(GLOBAL);
enable_interrupts(INT_EXT);
ext_int_edge(H_TO_L);
lcd_clear();
lcd_write(0x80);
lcd_string(str22);
lcd_write(0xc0);
lcd_string(str23);
while(TRUE)
{
lcd_clear();
calc_rs(UNITS);
delay_ms(2000);
}
}
void send_cmgf(unsigned char *str)
{
//unsigned char t;
while(*str!='\0')
{
putchar(*str);
str++;
}
putchar(0x0d);
while(getchar()!='K');
}
void lcd_write(unsigned char c)
{
delay_us(40);
LCD_DATA = ((c & 0xf0));
LCD_STROBE();
LCD_DATA = ( ( c << 4 ) & 0xf0 );
LCD_STROBE();
}
void lcd_char(unsigned char c)
{
delay_us(40);
LCD_DATA = ((c & 0xf0));
LCD_RS = 1;
LCD_STROBE();
LCD_DATA = ( ( c << 4 ) & 0xf0 );
LCD_RS = 1;
LCD_STROBE();
}
void clear_1stline()
{
lcd_write(0x80);
for(m=0;m<16;m++)
{
lcd_char(' ');
}
}
void clear_2ndline()
{
lcd_write(0xc0);
for(m=0;m<16;m++)
{
lcd_char(' ');
}
}
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x01);
delay_ms(2);
}
/* write a string of chars to the LCD */
void lcd_string(char * s)
{
LCD_RS = 1; // write characters
while(*s)
lcd_char(*s++);
}
/* write one character to the LCD */
void lcd_character(char c)
{
LCD_RS = 1; // write characters
lcd_char( c );
}
void lcd_init()
{
char init_value;
// ADCON1 = 0x06; // Disable analog pins on PORTA
init_value = 0x30; //0x03;
set_tris_a(0x00);
set_tris_d(0x00);
LCD_RS = 0;
LCD_EN = 0;
LCD_RW = 0;
delay_ms(15); // wait 15mSec after power applied,
LCD_DATA = init_value;
LCD_STROBE();
delay_ms(5);
LCD_STROBE();
delay_us(200);
LCD_STROBE();
delay_us(200);
LCD_DATA = 0x20; //0x02; // Four bit mode
LCD_STROBE();
lcd_write(0x28); // Set interface length
lcd_write(0x0c); // Display On, Cursor On, Cursor Blink
lcd_clear(); // Clear screen
lcd_write(0x06); // Set entry Mode
}
void recharge()
{
if(t_unit > 0)
{
RELAY1 = 0; //on while recharge
relay1_flag = 1;
clear_1stline();
lcd_write(0x80);
lcd_write(0x89);
calc_rs(t_unit);
}
}