|
Tài trợ cho PIC Vietnam |
Cơ bản về vi điều khiển và PIC Những bài hướng dẫn cơ bản nhất để làm quen với vi điều khiển PIC |
|
Ðiều Chỉnh | Xếp Bài |
03-09-2011, 01:25 PM | #1 |
Đệ tử 1 túi
Tham gia ngày: Apr 2010
Bài gửi: 12
: |
Anh chị cho em hỏi cách ghi data vào eprom của Pic16f877a
Chào anh chị!
Số là em đang viết code đồng hồ thời gian thực dùng ds1307+pic16f887a+lcd 1602. em viết nó chạy rồi mà giờ em muốn làm thêm phần hẹn giờ. Nhưng e ko biết ghi vào eprom để khi mất điện nó còn còn giờ đã hẹn: Đây là code của em: - main.c #include <main.h> #include <def_877a.h> #include <ds1307.h> #include <ds1307.c> #include <lcd_lib_4bit.c> #use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3) #define seconds gca_ds1307_regs[0] #define munites gca_ds1307_regs[1] int8 hours=(gca_ds1307_regs[2]&0x1f); #define hours_mode gca_ds1307_regs[2] #define days gca_ds1307_regs[3] #define dates gca_ds1307_regs[4] #define months gca_ds1307_regs[5] #define years gca_ds1307_regs[6] #define radc read_adc() void clock_menu(int8 chon); #define k1 rb4 #define k2 rb5 #define k3 rb6 #define k4 rb7 INT16 k,adc; char do_letter[8]={2,5,2,0,0,0,0,0}; char cusor[8]={8,12,14,15,14,12,8,0}; void change_days(int8 x); void change_dates(int8 x); //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void info() { lcd_gotoxy(1,1); lcd_putchar("Real Time Clock"); lcd_gotoxy(2,0); lcd_putchar("Designed:LeChang"); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void i2c_dislay() { lcd_gotoxy(1,2); switch(days) { case 1: Printf(LCD_putchar,"Sun"); break; case 2: Printf(LCD_putchar,"Mon"); break; case 3: Printf(LCD_putchar,"Tue"); break; case 4: Printf(LCD_putchar,"Wen"); break; case 5: Printf(LCD_putchar,"Thu"); break; case 6: Printf(LCD_putchar,"Fri"); break; case 7: Printf(LCD_putchar,"Sat"); break; } lcd_putchar(' '); lcd_putchar(48+dates/10); lcd_putchar(48+dates%10); lcd_putchar('/'); lcd_putchar(48+months/10); lcd_putchar(48+months%10); lcd_putchar('/'); lcd_putchar("20"); lcd_putchar(48+years/10); lcd_putchar(48+years%10); lcd_gotoxy(2,1); if(bit_test(hours_mode,5)) lcd_putchar("PM"); else lcd_putchar("AM"); lcd_gotoxy(2,4); lcd_putchar(48+hours/10); lcd_putchar(48+hours%10); lcd_putchar(':'); lcd_putchar(48+munites/10); lcd_putchar(48+munites%10); lcd_putchar(':'); lcd_putchar(48+seconds/10); lcd_putchar(48+seconds%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_days() { if(k2==0) { delay_ms(200); days++; if(days>7) days=1; } if(k3==0) { delay_ms(200); days--; if(days<1||dates==255) days=7; } lcd_gotoxy(1,1); lcd_putchar("Change Day:"); lcd_gotoxy(1,13); switch(days) { case 1: Printf(LCD_putchar,"Sun"); break; case 2: Printf(LCD_putchar,"Mon"); break; case 3: Printf(LCD_putchar,"Tue"); break; case 4: Printf(LCD_putchar,"Wen"); break; case 5: Printf(LCD_putchar,"Thu"); break; case 6: Printf(LCD_putchar,"Fri"); break; case 7: Printf(LCD_putchar,"Sat"); break; } } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_dates() // cai dat ngay { if(k2==0) { delay_ms(200); dates++; if(months==1||months==3||months==5||months==7|| months==8|| months==10||months==12) { if(dates>31) dates=1; } else if(months==2) { if(dates>28) dates=1; } else if(dates>30) dates=1; } if(k3==0) { delay_ms(200); dates--; if(dates<1||dates==255) { if(months==1||months==3||months==5||months==7|| months==8|| months==10||months==12) dates=31; else if(months==2) dates=28; else dates=30; } } lcd_gotoxy(1,1); lcd_putchar("Change Date:"); lcd_gotoxy(1,14); lcd_putchar(48+dates/10); lcd_putchar(48+dates%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_months() { if(k2==0) { delay_ms(200); months++; if(months>12) months=1; } if(k3==0) { delay_ms(200); months--; if(months<1||months==255) months=12; } lcd_gotoxy(1,1); lcd_putchar("Change Month:"); lcd_gotoxy(1,15); lcd_putchar(48+months/10); lcd_putchar(48+months%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_years() { if(k2==0) { delay_ms(200); years++; if(years>99) years=0; } if(k3==0) { delay_ms(200); years--; if(years==255) years=99; } lcd_gotoxy(1,1); lcd_putchar("Change Year:"); lcd_gotoxy(1,13); lcd_putchar("20"); lcd_putchar(48+years/10); lcd_putchar(48+years%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_hours() { if(k2==0) { delay_ms(200); hours++; if(hours>12) {hours=1;bit=!bit;} } if(k3==0) { delay_ms(200); hours--; if(hours<1||hours==255) {hours=12;bit=!bit;} } lcd_gotoxy(1,1); lcd_putchar("Change Hour:"); lcd_gotoxy(1,13); lcd_putchar(48+hours/10); lcd_putchar(48+hours%10); if(bit) lcd_putchar("PM"); else lcd_putchar("AM"); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_munites() { if(k2==0) { delay_ms(200); munites++; if(munites>59) munites=0; } if(k3==0) { delay_ms(200); munites--; if(munites==255) munites=59; } lcd_gotoxy(1,1); lcd_putchar("Change minute:"); lcd_gotoxy(1,15); lcd_putchar(48+munites/10); lcd_putchar(48+munites%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void set_seconds() { seconds=0; lcd_gotoxy(1,1); lcd_putchar("Change Second:"); lcd_gotoxy(1,15); lcd_putchar(48+seconds/10); lcd_putchar(48+seconds%10); } //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ /*void update_date() { trisb=0xff; set_days(); set_dates(); set_months(); set_years(); set_hours(); set_munites(); set_seconds(); if(k1==0) { delay_ms(200); lcd_clear(); ds1307_set_date_time(); } }*/ //++++++++++++++++++++++++++++++++++++++++++++++++++ ++++++++++++++++++++++++++++ void update_date() { if(k1==0) { delay_ms(100); while(k1==0); goto loop; while(true) { while(k1==1); if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); loop: lcd_clear(); while(k1==1) { lcd_gotoxy(1,1); lcd_putchar(1); clock_menu(1); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_years(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(1,8); lcd_putchar(1); clock_menu(1); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_months(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(2,1); lcd_putchar(1); clock_menu(1); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_dates(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(2,8); lcd_putchar(1); clock_menu(1); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_days(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(1,1); lcd_putchar(1); clock_menu(2); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); bit=bit_test(hours_mode,5); while(k4==1) { set_hours(); } hours_mode=hours; if(bit==1) bit_set(hours_mode,5); //else bit_clear(hours_mode,5); } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); lcd_clear(); while(k1==0); while(k1==1) { lcd_gotoxy(1,8); lcd_putchar(1); clock_menu(2); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_munites(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(2,1); lcd_putchar(1); clock_menu(2); if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); while(k4==1) { set_seconds(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(2,8); lcd_putchar(1); clock_menu(2); if(k4==0) { delay_ms(100); lcd_clear(); while(k4==0); while(k4==1) { info(); } } if(k4==0) { delay_ms(100); while(k4==0); lcd_clear(); } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(1,1); lcd_putchar(1); clock_menu(3); if(k4==0) { goto exit1; } if(k4==0) { delay_ms(100); lcd_clear(); while(k4==0); //break; } } } if(k1==0)////////////////////////////////////////////////////////////// { delay_ms(100); while(k1==0); lcd_clear(); while(k1==1) { lcd_gotoxy(2,1); lcd_putchar(1); clock_menu(3); if(k4==0) { goto exit; } if(k4==0) { delay_ms(100); lcd_clear(); while(k4==0); //break; } } } } exit: delay_ms(200); ds1307_set_date_time(); exit1: lcd_clear(); break; } } void adc_init() { Setup_ADC(ADC_CLOCK_INTERNAL); Setup_ADC_ports(AN0); Set_ADC_channel(0); } void lm35_dislay() { //adc=(radc*5.00*100.00)/1023.00; //lcd_gotoxy(2,3); //lcd_putchar(48+adc/1000); lcd_gotoxy(2,13); lcd_putchar(48+(int8)adc/10); lcd_putchar(48+adc%10); lcd_putchar(0); lcd_putchar('C'); } void clock_menu(int8 chon) { if(chon==1) { lcd_gotoxy(1,2); lcd_putchar("1.Year"); lcd_gotoxy(1,9); lcd_putchar("2.Month"); lcd_gotoxy(2,2); lcd_putchar("3.Date"); lcd_gotoxy(2,9); lcd_putchar("4.Day"); } else if(chon==2) { lcd_gotoxy(1,2); lcd_putchar("5.Hour"); lcd_gotoxy(1,9); lcd_putchar("6.Min"); lcd_gotoxy(2,2); lcd_putchar("7.Sec"); lcd_gotoxy(2,9); lcd_putchar("8.About"); } else { lcd_gotoxy(1,2); lcd_putchar("9.Just Exit"); lcd_gotoxy(2,2); lcd_putchar("10.Set And Exit"); } } void main() { trisa = 0xFF; trisb = 0xff; //trisd = 0x00; rd1=0; adc_init(); LCD_init(); CreateCustomChar(0,do_letter); CreateCustomChar(1,cusor); info(); delay_ms(1000); lcd_clear(); WHILE(TRUE) { adc=(radc*5.00*100.00)/1023.00; for(k=0;k<20;k++) { update_date(); ds1307_read_date_time(); i2c_dislay(); lm35_dislay(); } } } -lcd_lib_4bit.c #include <stddef.h> #define LCD_RS PIN_D0 #define LCD_RW PIN_D1 #define LCD_EN PIN_D2 #define LCD_D4 PIN_D3 #define LCD_D5 PIN_D4 #define LCD_D6 PIN_D5 #define LCD_D7 PIN_D6 // misc display defines- #define Line_1 0x80 #define Line_2 0xC0 #define Clear_Scr 0x01 // prototype statements #separate void LCD_Init ( void );// ham khoi tao LCD #separate void LCD_SetPosition ( unsigned int cX );//Thiet lap vi tri con tro #separate void LCD_PutChar ( unsigned int cX );// Ham viet1kitu/1chuoi len LCD #separate void LCD_PutCmd ( unsigned int cX) ;// Ham gui lenh len LCD #separate void LCD_PulseEnable ( void );// Xung kich hoat #separate void LCD_SetData ( unsigned int cX );// Dat du lieu len chan Data #separate void LCD_gotoxy(unsigned char row, unsigned char col); // D/n Cong #use standard_io (C) #use standard_io (D) //khoi tao LCD********************************************** #separate void LCD_Init ( void ) { LCD_SetData ( 0x00 ); delay_ms(200); /* wait enough time after Vdd rise >> 15ms */ output_low ( LCD_RS );// che do gui lenh LCD_SetData ( 0x03 ); /* init with specific nibbles to start 4-bit mode */ LCD_PulseEnable(); LCD_PulseEnable(); LCD_PulseEnable(); LCD_SetData ( 0x02 ); /* set 4-bit interface */ LCD_PulseEnable(); /* send dual nibbles hereafter, MSN first */ LCD_PutCmd ( 0x2C ); /* function set (all lines, 5x7 characters) */ LCD_PutCmd ( 0x0C ); /* display ON, cursor off, no blink */ LCD_PutCmd ( 0x06 ); /* entry mode set, increment & scroll left */ LCD_PutCmd ( 0x01 ); /* clear display */ delay_ms(300); } #separate void LCD_SetPosition ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ LCD_SetData ( swap ( cX ) | 0x08 ); LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); LCD_PulseEnable(); } #separate void LCD_PutChar ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ output_high ( LCD_RS ); LCD_PutCmd( cX ); output_low ( LCD_RS ); } #separate void LCD_PutCmd ( unsigned int cX ) { /* this subroutine works specifically for 4-bit Port A */ LCD_SetData ( swap ( cX ) ); /* send high nibble */ LCD_PulseEnable(); LCD_SetData ( swap ( cX ) ); /* send low nibble */ LCD_PulseEnable(); } #separate void LCD_PulseEnable ( void ) { output_high ( LCD_EN ); delay_us ( 3 ); // was 10 output_low ( LCD_EN ); delay_ms ( 3 ); // was 5 } #separate void LCD_SetData ( unsigned int cX ) { output_bit ( LCD_D4, cX & 0x01 ); output_bit ( LCD_D5, cX & 0x02 ); output_bit ( LCD_D6, cX & 0x04 ); output_bit ( LCD_D7, cX & 0x08 ); } #separate void LCD_gotoxy(unsigned char hang, unsigned char cot) { if(hang==1) lcd_setposition(cot-1); if(hang==2) lcd_setposition(63+cot); } #separate void LCD_clear() { LCD_PUTCMD(0X01); } #separate void CreateCustomChar(char charIndex,char *charBitmap) { char CGRAMptr; char index; if (charIndex > 7) /* check for index validity */ return; CGRAMptr= 0x40 + charIndex * 8; /* calculate start address based on index */ for(index=0;index<8;index++) { lcd_putcmd(CGRAMptr++); lcd_putchar(charBitmap[index]); } } /* Example char CHUAW[]={0x0A,0x04,0x0E,0x01,0x0F,0x11,0x0F,0x00}, CHUAWHOI[]={0x06,0x0E,0x04,0x0E,0x01,0x0F,0x11,0x0F}, CHUEE[]={0x00,0x04,0x0A,0x0E,0x11,0x1E,0x0E,0x00}; CreateCustomChar(0,CHUAW); CreateCustomChar(1,CHUAWHOI); CreateCustomChar(2,CHUEE); lcd_gotoxy(1,1); Printf(LCD_putchar," L"); LCD_putchar(2); Printf(LCD_putchar," V"); LCD_putchar(0); LCD_putchar("n"); Printf(LCD_putchar," Ch"); LCD_putchar(1); Printf(LCD_putchar,"ng");*/ Cảm ơn! |
|
|