kumnan
member
คะแนน 0
ออฟไลน์
กระทู้: 5
|
|
« เมื่อ: เมษายน 20, 2012, 02:27:59 am » |
|
#include <16F887.h>
#FUSES NOWDT //No Watch Dog Timer #FUSES EC_IO //External clock #FUSES NOPUT //No Power Up Timer #FUSES NOMCLR //Master Clear pin used for I/O #FUSES NOPROTECT //Code not protected from reading #FUSES NOCPD //No EE protection #FUSES NOBROWNOUT //No brownout reset #FUSES NOIESO //Internal External Switch Over mode disabled #FUSES NOFCMEN //Fail-safe clock monitor disabled #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O #FUSES NODEBUG //No Debug mode for ICD #FUSES NOWRT //Program memory not write protected #FUSES BORV40 //Brownout reset at 4.0V
#use delay(clock=20000000)
// 74145 PIN #define SCAN_A PIN_D0 #define SCAN_B PIN_D1 #define SCAN_C PIN_D2 #define SCAN_D PIN_D3 // LCD PIN #define LD1 PIN_A0 #define LD2 PIN_A1 #define LD3 PIN_A2 #define LD4 PIN_A3 #define EN PIN_A4 #define RS PIN_A5 // KEYPAD PIN #define COL1 PIN_B1 #define COL2 PIN_B2 #define COL3 PIN_B3 #define ROW1 PIN_B4 #define ROW2 PIN_B5 #define ROW3 PIN_B6 #define ROW4 PIN_B7 // SERVO MOTOR PIN #define SERVO PIN_D4
#define SERVO2 PIN_D5
#include "LCD4.c" #include "key.c"
unsigned int16 count_sensor; unsigned int16 count_set1; unsigned int16 count_set2;
unsigned int16 count_7seg1; unsigned int16 count_7seg2;
unsigned int8 BUF7SEG1[5]; unsigned int8 BUF7SEG2[5];
unsigned int8 BUF_MENU1[5]; unsigned int8 BUF_MENU2[5];
//--------------------0----1----2----3----4----5----6----7----8----9---------C---------_----P---- ---- unsigned int8 BUFHEX [16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x01,0x39,0x08,0x40,0x73,0x00}; // HEX CODE 7 SEGMENT
/************************************************************/ void put_data_seg (unsigned int16 data) // Êè¢éÍÁÙ, 7SEG ¤ÃÑéÅÐ 1 byte { output_c(data); } /************************************************************/ void clear_scan (void) { output_high(SCAN_A); // à¤ÃÕ ºÔµ áÊ¡¹ output_high(SCAN_B); output_high(SCAN_C); output_high(SCAN_D); } /************************************************************/ void show_7seg (unsigned int8 digit1,digit2,digit3,digit4,digit5,digit6,digit7,digit8) { unsigned int16 i; // clear_scan(); put_data_seg (BUFHEX [ digit1 & 0x0f ] ); //Êè data 8 bit ËÅÑ¡·Õè 1 output_low(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 1 output_low(SCAN_B); output_low(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit2 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 2 output_high(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 2 output_low(SCAN_B); output_low(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit3 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 3 output_low(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 3 output_high(SCAN_B); output_low(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit4 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 4 output_high(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 4 output_high(SCAN_B); output_low(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit5 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 5 output_low(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 5 output_low(SCAN_B); output_high(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit6 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 6 output_high(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 6 output_low(SCAN_B); output_high(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit7 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 7 output_low(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 7 output_high(SCAN_B); output_high(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å // clear_scan(); put_data_seg ( BUFHEX [ digit8 & 0x0f] ); //Êè data 8 bit ËÅÑ¡·Õè 8 output_high(SCAN_A); // áÊ¡¹ ËÅÑ¡·Õè 8 output_high(SCAN_B); output_high(SCAN_C); output_low(SCAN_D); i=500; while(i--); // ˹èÇàÇÅÒáÊ´¼Å clear_scan(); } /************************************************************/ void servo_0 (int8 i) { if(i==1) { output_high(SERVO); delay_us(2400); output_low(SERVO); delay_us(1760); } if(i==2) { output_high(SERVO); delay_us(1499); output_low(SERVO); delay_us(1760); } if(i==3) { output_high(SERVO); delay_us(900); output_low(SERVO); delay_us(1790); } } /************************************************************/ void menu2 (void) { unsigned int8 a,y; unsigned int16 i;
lcd_xy(2,1); printf(putl,"CONTAINER =0000"); a = 0; y = 12; BUF_MENU2[0] = '0'; BUF_MENU2[1] = '0'; BUF_MENU2[2] = '0'; BUF_MENU2[3] = '0'; while(true) { a = key(); if(a==0); else if(a=='*'){ lcd_xy(2,y); putl(BUF_MENU2[y-12]); if(y==12); else y--; i=1; } else if(a=='#') { lcd_xy(2,y); putl(BUF_MENU2[y-12]); count_set2 = (BUF_MENU2[0] & 0x0f) * 1000; count_set2 += (BUF_MENU2[1] & 0x0f) * 100; count_set2 += (BUF_MENU2[2] & 0x0f) * 10; count_set2 += (BUF_MENU2[3] & 0x0f) * 1; delay_ms(500); return; } else { BUF_MENU2[y-12] = a; lcd_xy(2,y); putl(BUF_MENU2[y-12]); if(y==15); else y++; i=1; } // if(i==1){lcd_xy(2,y); putl(BUF_MENU2[y-12]); } else if(i==600){ lcd_xy(2,y); putl('_'); } else if(i==1200) i=0; i++; delay_us(200); } } /************************************************************/ void menu1 (void) { unsigned int8 a,y; unsigned int16 i; lcd_clear(); lcd_xy(1,1); printf(putl,"ENTER PILL=0000"); a = 0; y = 12; BUF7SEG1[0] = 0; BUF7SEG1[1] = 0; BUF7SEG1[2] = 0; BUF7SEG1[3] = 0; BUF7SEG2[0] = 0; BUF7SEG2[1] = 0; BUF7SEG2[2] = 0; BUF7SEG2[3] = 0; BUF_MENU1[0] = '0'; BUF_MENU1[1] = '0'; BUF_MENU1[2] = '0'; BUF_MENU1[3] = '0'; while(true) { a = key(); if(a==0); else if(a=='*'){ lcd_xy(1,y); putl(BUF_MENU1[y-12]); if(y==12); else y--; i=1; } else if(a=='#') { lcd_xy(1,y); putl(BUF_MENU1[y-12]); count_set1 = (BUF_MENU1[0] & 0x0f) * 1000; count_set1 += (BUF_MENU1[1] & 0x0f) * 100; count_set1 += (BUF_MENU1[2] & 0x0f) * 10; count_set1 += (BUF_MENU1[3] & 0x0f) * 1; delay_ms(500); return; } else { BUF_MENU1[y-12] = a; lcd_xy(1,y); putl(BUF_MENU1[y-12]); if(y==15); else y++; i=1; } // if(i==1){lcd_xy(1,y); putl(BUF_MENU1[y-12]); } else if(i==600){ lcd_xy(1,y); putl('_'); } else if(i==1200) i=0; i++; delay_us(200); } } /************************************************************/ void start_count (void) { unsigned int8 s,a; count_sensor = 0; count_7seg1 = 0; count_7seg2 = 0; BUF7SEG1[0] = 0; BUF7SEG1[1] = 0; BUF7SEG1[2] = 0; BUF7SEG1[3] = 0; BUF7SEG2[0] = 0; BUF7SEG2[1] = 0; BUF7SEG2[2] = 0; BUF7SEG2[3] = 0;
s = 1; while(true) { if(count_7seg1 != count_sensor) { if(count_sensor == count_set1) { count_sensor = 0; count_7seg2++; if(s==1) s = 3; else s = 1; } count_7seg1 = count_sensor; sprintf(BUF7SEG1,"%04ld",count_7seg1); sprintf(BUF7SEG2,"%04ld",count_7seg2);
if(count_7seg2 == count_set2) { while(true) { a = key(); if(a=='#')return; servo_0(2); } } } servo_0(s); } } /************************************************************/ #int_EXT void EXT_isr(void) { count_sensor ++; } /************************************************************/ #INT_TIMER0 void isr_timer0() { show_7seg (BUF7SEG1[0],BUF7SEG1[1],BUF7SEG1[2],BUF7SEG1[3], BUF7SEG2[0],BUF7SEG2[1],BUF7SEG2[2],BUF7SEG2[3]); } /************************************************************/ void main (void) {
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); set_timer0(207); enable_interrupts(int_timer0); enable_interrupts(INT_EXT); enable_interrupts(GLOBAL);
key_start(); lcd_init(); lcd_xy(1,1); printf(putl," START"); delay_ms(500); count_sensor = 0; while(true) { menu1(); menu2(); start_count(); } }
|