#include <dongco.h>
#include <lcd4bit.c>
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#define ccp1 PIN_C2
#define ccp2 PIN_C1
#define en PIN_C4
//UNSIGNED int8 ctimer=0;
float tocdo=0, tocdodat;
float xung=0, xungmoi = 0,xungcu =0 ;
float dem = 0, w=0;
void ngatkenhA();
void NGAT_TIMER1();
float xung21(FLOAT);
#INT_TIMER1
void NGAT_TIMER1()
{
IF(dem >= 4)
{
disable_interrupts (INT_EXT) ;
setup_timer_1 (T1_DISABLED) ;
tocdo = 3.45 * xungmoi;
xungmoi = 0;
set_timer1 (15535) ;
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_1) ;
enable_interrupts (INT_EXT) ;
dem = 0;
}
dem = dem + 1;
w = tocdo;
}
#INT_EXT
void ngatkenhA(VOID)
{
IF (!input (PIN_B0))
{
xungmoi = xungmoi + 1;
}
}
void main()
{
   lcd_init();char string[32];
gets(string);
tocdodat = 120;
lcd_init();
set_tris_b(0b00000001);
set_tris_c(0b10000000);
set_tris_d(0x00);
output_d(0x00);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
set_timer1(15535);
enable_interrupts(INT_TIMER1);// ngat timer 1
enable_interrupts(INT_EXT); // ngat ngoai
ext_INT_edge(H_TO_L);
enable_interrupts ( GLOBAL ) ;
   while(TRUE)
   {
     
lcd_putcmd (0x80) ;
printf (lcd_putchar, "tocdo = %f",w);
printf (" %f\n\r", tocdo);
delay_ms (500);
IF (tocdo < tocdodat)
{
output_high (en) ;
output_low (ccp1) ;
output_high (ccp2) ;
lcd_putcmd (0xc0) ;
printf (lcd_putchar, "Che DO :0n-thuan");
}
IF (tocdo > tocdodat)
{
output_low (en) ;
lcd_putcmd (0xc0) ;
printf (lcd_putchar, "Che DO :0ff ");
}

   }

}
