zzssww8936
New Member
this two is my circuit and my code for mikroc, and it can not be displayed on my lcd, and i dont know the problem, i really hope who can point out my mistakes.
thanks for ur time.
thanks for ur time.
Code:
// LCD module connections-------------------------------------------------------------------------------------
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections---------------------------------------------------------------------------------
//------------------------------Declare Variables section--------------------------------------------------------
unsigned short read_ds1307(unsigned short address );
void write_ds1307(unsigned short address,unsigned short w_data);
unsigned short sec;
unsigned short minute;
unsigned short hour;
unsigned short day;
unsigned short date;
unsigned short month;
unsigned short year;
unsigned short dataa;
unsigned short a=0;
unsigned short b=0;
unsigned short yr=0;
unsigned short hour1;
char ddate[11];
char time[15];
char time1[15];
char ampm;
unsigned char dday;
unsigned char dhour;
unsigned char BCD2UpperCh(unsigned char bcd);
unsigned char BCD2LowerCh(unsigned char bcd);
//------------------------------End of Declare Variables section--------------------------------------------------------
void main(){
//------------------------------Start of Initialization Section-------------------------------------------------------------
I2C1_Init(100000); //DS1307 I2C is running at 100KHz
TRISB = 0x00; // Configure PORTB as output
TRISC = 0xFF;
TRISD = 0xFF;
PORTD=0x00;
Lcd_Init();
Lcd_Cmd(_Lcd_CLEAR); // Clear display
Lcd_Cmd(_Lcd_CURSOR_OFF); // Turn cursor off
//Set Time
write_ds1307(0,0x80); //Reset second to 0 sec. and stop Oscillator
write_ds1307(1,0x59); //write min 59
write_ds1307(2,0x11); //write hour 11 PM
write_ds1307(3,0x04); //write day
write_ds1307(4,0x15); // write date 15
write_ds1307(5,0x06); // write month June
write_ds1307(6,0x11); // write year 11 --> 2011
write_ds1307(7,0x10); //SQWE output at 1 Hz
write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
//------------------------------End of Initialization Section---------------------------------------------------------
while(1)
{
sec=read_ds1307(0); // read second
minute=read_ds1307(1); // read minute
hour=read_ds1307(2); // read hour
day=read_ds1307(3); // read day
date=read_ds1307(4); // read date
month=read_ds1307(5); // read month
year=read_ds1307(6); // read year
a=BCD2UpperCh(year);
b=BCD2LowerCh(year);
yr=a*10+b;
hour1 =hour;
//------------------------------Start of Setting Clock Time Section-------------------------------------------------------
if (PORTD.F0==1 && PORTD.F6==0)
{
if (PORTD.F1==1){
minute=minute+1;
if (minute==0x0A||minute==0x1A||minute==0x2A||minute==0x3A||minute==0x4A){
minute=minute+6;}
else if (minute==0x5A){
minute=0x00;} }
else if (PORTD.F2==1)
{
hour=hour+1;
if (hour==0x0A||hour==0x1A||hour==0x2A){
hour=hour+6;}
else if (hour==0x24){
hour=0x00;} }
else if (PORTD.F3==1) {
date=date+1;
day=day+1;
if (month==0x01||month==0x03||month==0x05||month==0x07||month==0x08||month==0x10||month==0x12){
if (date==0x0A||date==0x1A||date==0x2A){
date=date+6;}
else if (date==0x32){
date=0x01;}
if(day==8)
day=1;}
else if (month==0x04||month==0x06||month==0x09||month==0x11){
if (date==0x0A||date==0x1A||date==0x2A){
date=date+6;}
else if (date==0x31){
date=0x01;}
if(day==8)
day=1;}
else if (month==0x02){
if (yr%4==0){
if (date==0x0A||date==0x1A){
date=date+6;}
else if (date==0x2A){
date=0x01;}
if(day==8)
day=1;}
else if (yr%4==3||yr%4==0||yr%4==1){
if (date==0x0A||date==0x1A){
date=date+6;}
else if (date==0x29){
date=0x01;}
if(day==8)
day=1;}
}
}
else if (PORTD.F4==1) {
month=month+1;
if (month==0x0A||month==0x1A){
month=month+6;}
else if (month==0x13){
month=0x01;}
}
else if (PORTD.F5==1) {
year=year+1;
if (year==0x0A||year==0x1A||year==0x2A||year==0x3A||year==0x4A||year==0x5A||year==0x6A||year==0x7A||year==0x8A){
year=year+6;}
else if (year==0x9A){
year=0x11;}
}
else if (PORTD.F7 = 1){
day = day + 1 ;
if (day==0x8){
year=0x01;}
}
Delay_ms(100);
write_ds1307(0,0x80);
write_ds1307(1, minute);
write_ds1307(2, hour);
write_ds1307(3, day);
write_ds1307(4, date);
write_ds1307(5, month);
write_ds1307(6, year);
write_ds1307(0,0x00);
dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,1,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,1,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,1,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,1,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,1,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,1,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,1,"Sat");
}
ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';
time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = ' ';
time[9] = ' ';
time[10] = ' ';
time[11] = ' ';
time[12] = '\0';
Lcd_Out(1,5,ddate);
Lcd_Out(2,1,time);
Delay_ms(50);
}
else if (PORTD.F0==0 && PORTD.F6==0)
{
dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,1,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,1,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,1,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,1,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,1,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,1,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,1,"Sat");
}
ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';
time[0] = BCD2UpperCh(hour);
time[1] = BCD2LowerCh(hour);
time[2] = ':';
time[3] = BCD2UpperCh(minute);
time[4] = BCD2LowerCh(minute);
time[5] = ':';
time[6] = BCD2UpperCh(sec);
time[7] = BCD2LowerCh(sec);
time[8] = ' ';
time[9] = ' ';
time[10] = ' ';
time[11] = ' ';
time[12] = '\0';
Lcd_Out(1,5,ddate);
Lcd_Out(2,1,time);
Delay_ms(50);
}
//------------------------------End of Setting Clock Time Section-------------------------------------------------------
//------------------------------Start of Display Time in 12 hr system Section------------------------------------------
else if (PORTD.F0==0 && PORTD.F6==1)
{
dday = BCD2LowerCh(day);
if (dday == '1') {
Lcd_Out(1,1,"Sun");
}
else if (dday == '2') {
Lcd_Out(1,1,"Mon");
}
else if (dday == '3') {
Lcd_Out(1,1,"Tue");
}
else if (dday == '4') {
Lcd_Out(1,1,"Wed");
}
else if (dday == '5') {
Lcd_Out(1,1,"Thu");
}
else if (dday == '6') {
Lcd_Out(1,1,"Fri");
}
else if (dday == '7') {
Lcd_Out(1,1,"Sat");
}
ddate[0] = BCD2UpperCh(date);
ddate[1] = BCD2LowerCh(date);
ddate[2] = '/';
ddate[3] = BCD2UpperCh(month);
ddate[4] = BCD2LowerCh(month);
ddate[5] = '/';
ddate[6] = '2';
ddate[7] = '0';
ddate[8] = BCD2UpperCh(year);
ddate[9] = BCD2LowerCh(year);
ddate[10] = '\0';
if (hour1<0x12){
if (hour1==0){
hour1=0x12;
ampm= 'A';
}
else
ampm= 'A';
}
else if (hour1 == 0x12){
ampm= 'P';
}
else if (hour1>0x12 && hour1<0x20){
hour1=hour1-0x12;
ampm= 'P';
}
else if (hour1==0x20){
hour1=0x08;
ampm= 'P';
}
else if (hour1==0x21){
hour1=0x09;
ampm= 'P';
}
else if (hour1==0x22){
ampm= 'P';
hour1=0x10;
}
else if (hour1==0x23){
ampm= 'P';
hour1=0x11;
}
time1[0] = BCD2UpperCh(hour1);
time1[1] = BCD2LowerCh(hour1);
time1[2] = ':';
time1[3] = BCD2UpperCh(minute);
time1[4] = BCD2LowerCh(minute);
time1[5] = ':';
time1[6] = BCD2UpperCh(sec);
time1[7] = BCD2LowerCh(sec);
time1[8] = ' ';
time1[9] = ampm;
time1[10] = 'M';
time1[11] = '\0';
Lcd_Out(1,5,ddate);
Lcd_Out(2,1,time1);
Delay_ms(50);
hour1=hour;
}
}
//------------------------------Start of Display Time in 12 hr system Section------------------------------------------
}
unsigned short read_ds1307(unsigned short address)
{
I2C1_Start();
I2C1_Wr(0xd0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(address);
I2C1_Repeated_Start();
I2C1_Wr(0xd1); //0x68 followed by 1 --> 0xD1
dataa=I2C1_Rd(0);
I2C1_Stop();
return(dataa);
}
unsigned char BCD2UpperCh(unsigned char bcd)
{
return ((bcd >> 4) + '0');
}
unsigned char BCD2LowerCh(unsigned char bcd)
{
return ((bcd & 0x0F) + '0');
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
I2C1_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(0xD0); // send byte via I2C (device address + W)
I2C1_Wr(address); // send byte (address of DS1307 location)
I2C1_Wr(w_data); // send data (data to be written)
I2C1_Stop(); // issue I2C stop signal
}[ATTACH=CONFIG]68374[/ATTACH]