DS1307+LCD+KEYPAD ..please
Discussion in "PIC Microcontroller Discussion" started by Nongpa-nga May 14, 2009.
Thu May 14 2009, 01:29 am
i'm a student in university of thailand and about to complete my final project. One task in the project is that i need to set date and time pass the key pack and LCD, show it would show the data on the LCD Please would you be able to help me with the source code for using micro PIC16F877.
Thank you very much for your kindness
Thank you very much for your kindness
Thu May 14 2009, 05:30 am
I use program ccs complier to write program and Winpic complier to pic16F877.
Thu May 14 2009, 05:16 pm
well you have to start on your own, we will surely help you achieve what you want.
so start with coding.
so start with coding.
Thu May 14 2009, 08:48 pm
so..now
I can''t send data to i2c (DS1307) .
but I can to set date set time at hyperlink ... show date and time to lcd. but time is and date don't linear.:-s
but... i need to set date and time pass the key pack and LCD.
:-) Please would you be able to help me
thank you
I can''t send data to i2c (DS1307) .
but I can to set date set time at hyperlink ... show date and time to lcd. but time is and date don't linear.:-s
but... i need to set date and time pass the key pack and LCD.
:-) Please would you be able to help me
thank you
Thu May 14 2009, 08:57 pm
#include <16F877.h> //header file for 16F877 #include <stdlib.h> //library for input.c /*Setting configuration fuses*/ #fuses HS,NOWDT,NOPROTECT,NOLVP //Configuration word #use delay(clock=1000000) //Chang OSC here (default 20MHz) #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7) //Serial port #use I2C(master, sda=PIN_C4, scl=PIN_C3) //I2C Bus module #include "input.c" //library for gethex() #define use_portb_lcd #use fast_io(B) #use fast_io(D) #bit row1 = 0x08.0 #bit row2 = 0x08.1 #bit row3 = 0x08.2 #bit row4 = 0x08.3 #bit col1 = 0x08.4 #bit col2 = 0x08.5 #bit col3 = 0x08.6 #bit col4 = 0x08.7 /*Defines*/ #define ADDR_DS1307 0xD0 //Address DS1307 #define STRING_SIZE 20 #define msg0 " LCD 20x4 0x00" #define msg1 " Line2 adrs 0x40" #define msg2 " Line3 adrs 0x14" #define msg3 " Line4 adrs 0x54" void kbd_init(); /****************************/ /* Key Matrix 4*4 */ /****************************/ void kbd_init() { col1 = 1; col2 = 1; col3 = 1; col4 = 1; } int kbd_getc() { col1 = 0; if(row1 == 0) { while(row1==0); col1 = 1; return('7'); } if(row2 == 0) { while(row2==0); col1 = 1; return('4'); } if(row3 == 0) { while(row3==0); col1 = 1; return('1'); } if(row4 == 0) { while(row4==0); col1 = 1; return('*'); }//Clear col1 = 1; col2 = 0; if(row1 == 0) { while(row1==0); col2 = 1; return('8'); } if(row2 == 0) { while(row2==0); col2 = 1; return('5'); } if(row3 == 0) { while(row3==0); col2 = 1; return('2'); } if(row4 == 0) { while(row4==0); col2 = 1; return('0'); } col2 = 1; col3 = 0; if(row1 == 0) { while(row1==0); col3 = 1; return('9'); } if(row2 == 0) { while(row2==0); col3 = 1; return('6'); } if(row3 == 0) { while(row3==0); col3 = 1; return('3'); } if(row4 == 0) { while(row4==0); col3 = 1; return('#'); }//Help col3 = 1; col4 = 0; if(row1 == 0) { while(row1==0); col4 = 1; return('U'); }//UP if(row2 == 0) { while(row2==0); col4 = 1; return('G'); }//Down if(row3 == 0) { while(row3==0); col4 = 1; return('H'); }//Function if(row4 == 0) { while(row4==0); col4 = 1; return('I'); //Enter } col4 = 1; return(0); } /*Data types*/ typedef struct { BYTE sec; // seconds BYTE min; // Minutes BYTE hr; // Hours BYTE day; BYTE date; BYTE month; BYTE year; } DS1307_RTC; DS1307_RTC RTC; /* Prototypes*/ void DS1307_Write(unsigned char ctl,unsigned char dat); BYTE DS1307_Read(unsigned char ctl); void DS1307_WriteDate(void); void DS1307_WriteTime(void); void DS1307_ReadDate(void); void DS1307_ReadTime(void); /*Ds1307 Write*/ void DS1307_Write(unsigned char ctl,unsigned char dat) { i2c_start(); // Start condition i2c_write(ADDR_DS1307); //Device Address-Write i2c_write(ctl); //Control byte i2c_write(dat); // Write data i2c_stop(); //Stop condition } /*Ds1307 Read*/ BYTE DS1307_Read(unsigned char ctl) { BYTE dat; i2c_start(); // Start condition i2c_write(ADDR_DS1307); // Device Address - Write i2c_write(ctl); // Control byte i2c_start(); i2c_write(ADDR_DS1307+1); // Device Address - Read dat=i2c_read(0); // Read data , Not ACK i2c_stop(); // Stop condition Return (dat); // Return date } /*Ds1307 WriteDate*/ void DS1307_WriteDate( ) { DS1307_Write(0x04,RTC.date); DS1307_Write(0x05,RTC.month); DS1307_Write(0x06,RTC.year); } /*Ds1307 WriteTime*/ void DS1307_Writetime() { DS1307_Write(0x00,RTC.sec); DS1307_Write(0x01,RTC.min); DS1307_Write(0x02,RTC.hr); } /*Ds1307 ReadDate*/ void DS1307_ReadDate() { RTC.date =DS1307_Read(0x04); RTC.month=DS1307_Read(0x05); RTC.year =DS1307_Read(0x06); } /*Ds1307 ReadTime*/ void DS1307_ReadTime() { RTC.sec=DS1307_Read(0x00); RTC.min=DS1307_Read(0x01); RTC.hr =DS1307_Read(0x02); } /*DS1307 SetDate*/ void DS1307_Setdate() { char date; int a; int x; short int result; a=0; kbd_init(); lcd_send_byte(0,0x01);lcd_gotoxy(1,1);printf(lcd_putc,"\n\rDate : "); while (a<11) { lcd_send_byte(0,0x0F); date = kbd_getc(); if(date!=0) { for(x=15;x<16;x++); { lcd_gotoxy(x,1);lcd_putc(date); delay_ms(1000); } for(x=16;x<17;x++); { lcd_gotoxy(x,1);lcd_putc(date); delay_ms(1000); } } } } /*DS1307 SetTime*/ void DS1307_SetTime() { lcd_send_byte(0,0x01); lcd_gotoxy(1,1);printf(lcd_putc,"\n\rHour :"); RTC.hr=kbd_getc(); lcd_gotoxy(1,2);printf(lcd_putc,"\n\rMin :"); RTC.min=kbd_getc(); lcd_gotoxy(1,3);printf(lcd_putc,"\n\rSec :"); RTC.sec=kbd_getc(); } /*Main Function*/ void main() { char k; char dt ; //SET_TRIS_D(0x0F); lcd_init(); kbd_init(); lcd_gotoxy(1,1);printf(lcd_putc,"Set Real Time Clock "); lcd_gotoxy(1,2);printf(lcd_putc,"Set Time = 1 "); lcd_gotoxy(1,3);printf(lcd_putc,"Auto Time = 2 "); lcd_gotoxy(1,4);printf(lcd_putc,"Select = " ); while (1) { k = kbd_getc(); if(k!=0) { lcd_send_byte(0,0x0F); lcd_gotoxy(20,4);lcd_putc(k); delay_ms(1000); } switch (k) { case '1' :lcd_send_byte(0,0x01); lcd_gotoxy(1,1);printf(lcd_putc,"Set Date = 1 " ); lcd_gotoxy(1,2);printf(lcd_putc,"Set Time = 2 " ); lcd_gotoxy(1,3);printf(lcd_putc,"Select = " ); while (1) { dt = kbd_getc(); if(dt!=0) { lcd_send_byte(0,0x0F); lcd_gotoxy(20,3);lcd_putc(dt); delay_ms(1000); } switch (dt) { case '1' :DS1307_Setdate();DS1307_WriteDate();break; case '2' :DS1307_SetTime();DS1307_WriteTime();break; } } ;break; case '2' :lcd_send_byte(0,0x01);lcd_gotoxy(1,3);printf(lcd_putc,"%2X/%2X/%2X %2X:%2X:%2X",RTC.date,RTC.month,RTC.year,RTC.hr,RTC.min,RTC.sec);break; } } }
[ Edited Fri May 15 2009, 02:01 am ]
Mon May 18 2009, 04:38 pm
Nongpa, Really want to help but your code need a lot of time to understand first.. hope you are getting me..
One suggestion that will help you go forward is.. test your code in parts.
take the keypad routine only, write a program to send key pressed on serial terminal. test LCD routine separately this will make sure that your partial codes are working.
then test DS1307 with LCD, just read and display the DS1307 content.
one you are sure that every part of code is working, then you can easily find out what is the prob in main code. coz focus will not be on keypad or LCD or DS1307 but on the logic of main program. getting me?
So i think you better start testing now.
One suggestion that will help you go forward is.. test your code in parts.
take the keypad routine only, write a program to send key pressed on serial terminal. test LCD routine separately this will make sure that your partial codes are working.
then test DS1307 with LCD, just read and display the DS1307 content.
one you are sure that every part of code is working, then you can easily find out what is the prob in main code. coz focus will not be on keypad or LCD or DS1307 but on the logic of main program. getting me?
So i think you better start testing now.
Powered by e107 Forum System