Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Help with RTC date on USART

Status
Not open for further replies.

PA3040

Advanced Member level 3
Advanced Member level 3
Joined
Aug 1, 2011
Messages
883
Helped
43
Reputation
88
Reaction score
43
Trophy points
1,308
Visit site
Activity points
6,936
Dear All,
This is related to PIC16f877a MCU and Ds1307 RTC

I need to display DAY using following array
My I2C reading program is working well
I2C reading verging Day 0 to 7

char* weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

I need to send to USART data on array weekdayname[]

Code:
TXREG = weekdayname[i]  //  i hear veriable

Please advice to display on serial port

Thanks in advance
 


Code C - [expand]
1
2
3
4
while(*weekdayname[i]){
    while(!TRMT);
    TXREG = *weekdayname[i]++;
}



i = 0, loops 6 times till all characters of weekdayname[0] is printed.
 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
I don't understand what you want. You want to print all weekday names?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
void printWeekDayNames(){
 
    unsigned char i = 0, j = 0;
 
    for(i = 0; i < 7; i++){
        j = i;
        while(*weekdayname[j]){
                while(!TRMT);
                    TXREG = *weekdayname[j]++;
        }
    
    }
 
}




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void printWeekDayNames(){
 
    unsigned char i = 0;
 
    while(*weekdayname[i]){
              while(!TRMT);
                    TXREG = *weekdayname[i]++;
        }   
 
}

 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Jatanth thanks for the reply

I did like this for testing

Code:
[COLOR=#333333][FONT=Verdana]char* weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};[/FONT][/COLOR]
Code:
    while(*weekdayname[0]){
    while(!TRMT);
    __delay_ms(1);
    TXREG = *weekdayname[0]++;
    __delay_ms(1);

I think this method should print Sunday continuously,but it dose only one time

Please advice
 

Post full code.

Code:
#include <htc.h>
#define _XTAL_FREQ 4000000 // 4 MHz clock 

__CONFIG(0X3F39);
#define LCD_EN RB3
#define	LCD_RS RB5
#define	LCD_RW RB4
#define LCD_DATA	PORTD
#define	LCD_STROBE()	((LCD_EN = 1),(LCD_EN=0))
unsigned char date [5] = {'D','a','t','e',':'};
unsigned char time [5] = {'T','i','m','e',':'};

unsigned char I2CData[]= {0x00,0x01,0x40,0x03,0x04,0x05,0x06,0x07};
char* weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
//unsigned I2CInitval  []= {0x55,0x59,0x23,0x31,0x29,0x20,0x12,0x20};
unsigned char tmp;
unsigned char i;
unsigned char char_loader;
unsigned char x =26;
void I2CInit(void);
void I2CStart(void);
void I2CStop(void);
void I2CStart(void);
void I2CRestart(void);
void I2CAck();
void I2CNak();
void I2CAck_status();
void I2CSend(unsigned char dat);
unsigned char I2CRead(void);
void rtc_tx_init ();
void rtc_to_rs232();
void printWeekDayNames(void);

void I2CWait(){
        while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) );		/* wait for any pending transfer */

}

void I2CInit(void){
        TRISC3 = 1;      	/* SDA and SCL as input pin */
        TRISC4 = 1;      	/* these pins can be configured either i/p or o/p */
        SSPSTAT |= 0x80; 	/* Slew rate disabled */
        SSPCON = 0x28;   	/* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
        SSPADD = 0x28;    	/* 100Khz @ 4Mhz Fosc */
}

void I2CStart(){
		I2CWait(); 
        SEN = 1;         	
        while(SEN);      	
                    	
}

void I2CStop(){
        PEN = 1;         	/* Stop condition enabled */
        while(PEN);      	/* Wait for stop condition to finish */
                     		/* PEN automatically cleared by hardware */
}

void I2CRestart(){
        RSEN = 1;        	/* Repeated start enabled */
        while(RSEN);     	/* wait for condition to finish */
}

void I2CAck(){
        ACKDT = 0;       	/* Acknowledge data bit, 0 = ACK */
        ACKEN = 1;       	/* Ack data enabled */
        while(ACKEN);    	/* wait for ack data to send on bus */
}

void I2CNak(){
        ACKDT = 1;       	/* Not acknowledge data bit, 1 = NAK */
        ACKEN = 1;       	/* Not Ack data enabled */
        while(ACKEN);    	/* wait for Not ack data to send on bus */
}
void I2CAck_status(){
		ACKSTAT = 1;
        while(ACKSTAT);    	/* wait for Not ack data to send on bus */
}


void I2CSend(unsigned char dat){
	
        SSPBUF = dat;    		/* Move data to SSPBUF */
        while(BF);       		/* wait till complete data is sent from buffer */
        I2CWait();       		/* wait for any pending transfer */
}

unsigned char I2CRead(void){
        unsigned char temp;
		I2CWait(); 				/* Reception works if transfer is initiated in read mode */
        RCEN = 1;        		/* Enable data reception */
        while(!BF);      		/* wait for buffer full */
        temp = SSPBUF;   		/* Read serial buffer and store in temp register */
        	     				/* wait to check any pending transfer */	     
}

void lcddata(unsigned char value)
	{
LCD_DATA = value;
LCD_RS = 1; 
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;

	}

void lcdcmd(unsigned char value)
	{
LCD_DATA = value;
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 1;
__delay_ms (1);
LCD_EN = 0;
	}
void bcdToascii(unsigned char value){

	tmp = value;
	tmp = tmp & 0xf0;
	tmp = tmp >> 4;
	tmp = tmp | 0x30;
	lcddata(tmp);
	tmp = value;
	tmp = tmp & 0x0f;
	tmp = tmp | 0x30;
	lcddata(tmp);

}

void display (){
lcdcmd(0x80);
for (char i=0;i<5;i++)
			{
		lcddata(time[i]);
			}
	bcdToascii(I2CData[2]);
	lcddata(':');
	bcdToascii(I2CData[1]);
	lcddata(':');
	bcdToascii(I2CData[0]);
	
	lcdcmd(0xc0);
for (char j=0;j<5;j++)
			{
		lcddata(date[j]);
			}
	bcdToascii(I2CData[4]);
	lcddata('/');
	bcdToascii(I2CData[5]);
	lcddata('/');	
	lcddata('2');
	lcddata('0');
	bcdToascii(I2CData[6]);
	x--;
	
}
void lcd_init(){

TRISD	= 0;
TRISB	= 0;
TRISC7  = 0;
TRISC6  = 0;
LCD_EN =0;
__delay_ms(175);
lcdcmd(0x38);
__delay_ms(175);
lcdcmd(0x0e);
__delay_ms(15);
lcdcmd(0x01);
__delay_ms(10);
lcdcmd(0x06);
__delay_ms(10);
lcdcmd(0x80);
__delay_ms(10);
lcdcmd(0x0c);
__delay_ms(10);
				}


void I2Cwrite(){
										
 		I2CStart();							/* Send Start condition */
        I2CSend(0xD0);						/* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);						/*Send subaddress 0x00,we are writing to this location */
        I2CAck_status();
        I2CSend(0x80);		//write $80 to REG0. (pause counter + 0 sec)
        I2CAck_status();
        I2CSend(0x59);
        I2CAck_status();
        I2CSend(0x23);
        I2CAck_status();
       	I2CSend(0x00);		//write day of week 2:Monday
        I2CAck_status();
        I2CSend(0x31);
        I2CAck_status();	// write date 31
        I2CSend(0x12);		// write month dec
        I2CAck_status();
        I2CSend(0x12);
        I2CAck_status();
        I2CRestart();	
        I2CSend(0xD0);				/* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);
        I2CSend(0x55);	
        I2CAck_status();
        I2CStop();
}

void I2Cread(){
		
       I2CStart();
		I2CSend(0xD0);				/* Send slave address with write */ 					       						       
		I2CAck_status();
        I2CSend(0x00);				/* Send a repeated start, after a dummy write to start reading */
        I2CAck_status();
        I2CRestart();
        I2CSend(0xD1);				/* send slave address with read bit set */
        I2CAck_status();
  		I2CData[0] = I2CRead();	  
      	I2CAck();
      	I2CData[1] = I2CRead();
      	I2CAck();
      	I2CData[2] = I2CRead();
      	I2CAck();
      	I2CData[3] = I2CRead();
      	I2CAck();
      	I2CData[4] = I2CRead();
      	I2CAck();
      	I2CData[5] = I2CRead(); 
      	I2CAck();
      	I2CData[6] = I2CRead();
      	//I2CAck();
      	//I2CData[7] = I2CRead();
      	I2CNak();
      	I2CStop();						/* Send stop */
}

 
void main(){        
        I2CInit();
        lcd_init();       
		I2Cwrite();
		rtc_tx_init ();
		
while(1){
		while(x){
        I2Cread();			
		display();
	}
		rtc_to_rs232();
		x=26;

}
}

void rtc_tx_init (){

	TRISC7 = 1;
	SPBRG = 0X19;			//Set Baud rate 9600bps
	TXSTA = 0b00100100;
	RCSTA = 0b10010000;

}

void rtc_to_rs232(){
	
	for(i=0;i<3;i++){
	TXREG =(( I2CData[i]>>4) + '0');
	__delay_ms(1);
	TXREG =((I2CData[i]&0x0F)+ '0');
	__delay_ms(1);
	TXREG = (':');
	__delay_ms(1);
	
}
	TXREG = ('-');
	__delay_ms(1);
	for(i=4;i<6;i++){
	TXREG =(( I2CData[i]>>4) + '0');
	__delay_ms(1);
	TXREG =((I2CData[i]&0x0F)+ '0');
	__delay_ms(1);
	TXREG = ('/');
	__delay_ms(1);
	
}
	TXREG = ('2');
	__delay_ms(1);
	TXREG = ('0');
	__delay_ms(1);
	TXREG =(( I2CData[6]>>4) + '0');
	__delay_ms(1);
	TXREG =((I2CData[6]&0x0F)+ '0');
	__delay_ms(1);
	TXREG = ('-');
	__delay_ms(1);
//	TXREG =(( I2CData[3]>>4) + '0');
//	__delay_ms(1);
//	TXREG =((I2CData[3]&0x0F)+ '0');
	//__delay_ms(1);
	printWeekDayNames();
 		
	
	__delay_ms(5);
	TXREG = 0x0a;
	__delay_ms(5);
	TXREG = 0X0d;
	__delay_ms(5);


}

void printWeekDayNames(){
 
    unsigned char i = 0;
 
    while(*weekdayname[i]){
              while(!TRMT);
                    TXREG = *weekdayname[i]++;
                    __delay_ms(5);
        }  
  }
 

Dear jayanth,

Thanks for the reply

I would be much appreciated if you can give me a solution

Thanks in advance
 

See if making the string pointers constant helps.


Code C - [expand]
1
const char  *weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear jayanth

Still problem remaining unchanged

Code:
const char* weekdayname[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

Code:
void printWeekDayNames(){
 
    unsigned char i = 0;
 
    while(*weekdayname[i]){
              while(!TRMT);
              __delay_ms(1);
                    TXREG = *weekdayname[i]++;
                    __delay_ms(1);
        }   
 
}

Please one more advice
Thanks in advnace
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#pragma hdrstop
#pragma argsused
 
#include <stdio.h>
#include <tchar.h>
#include <string.h>
 
char weekdayname[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void printWeekDayNames(unsigned char dummy[7][10]);
 
void printWeekDayNames(unsigned char dummy[7][10]){
 
    unsigned char i = 0, j = 0;
 
    for(i = 0; i < 7; i++){
        j = 0;
        while(dummy[i][j])
               printf("%c",  dummy[i][j++]);
        printf("%s", "\n----------------------\n");
 
    }
 
}
 
int _tmain(int argc, _TCHAR* argv[])
{
    unsigned int k = 0;
 
    for(k = 0; k < 10; k++){
 
           printWeekDayNames(weekdayname);
    }
 
    scanf("Input", &k);
 
    return 0;
}




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char weekdayname[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void printWeekDayNames(unsigned char dummy[7][10]);
 
void printWeekDayNames(unsigned char dummy[7][10]){
 
    unsigned char i = 0, j = 0;
 
    for(i = 0; i < 7; i++){
        j = 0;
        while(dummy[i][j]){
        while(!TRMT);
                 TXREG = dummy[i][j++]);
        }
 
    }
 
}




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
char weekdayname[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
 
void printWeekDayNames(unsigned char dummy[7][10]);
 
void printWeekDayNames(unsigned char dummy[7][10]){
 
    unsigned char j = 0;
    //i is a global variable or static
    
    while(dummy[i][j]){
        while(!TRMT);
                 TXREG = dummy[i][j++]);
    }
 
    
 
}

 
Last edited:
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear jayanth
Thank for the reply

I will let you know the result tomorrow evening

I would much appreciated your help and voluble time

Thanks again and again
 

Dear Jayanth

Thanks for your help
now it is working

Code:
char weekdayname[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday",};

Code:
void printWeekDayNames(unsigned char valv){
 
    unsigned char j =0;
   	unsigned char k =0;
   	k = valv;
   	
    while(weekdayname[k][j]){
        while(!TRMT);
        TXREG = (weekdayname[k][j++]);
    }
}

Can you please help me to change 24 hour to change 12 Hour mode
This is my I2C initialization function
can you please modify this function to change 12 Hour mode

Code:
void I2Cwrite(){
										
 		I2CStart();			/* Send Start condition */
        I2CSend(0xD0);		/* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);		/*Send subaddress 0x00,we are writing to this location */
        I2CAck_status();
        I2CSend(0x80);		//write $80 to REG0. (pause counter + 0 sec)
        I2CAck_status();
        I2CSend(0x59);
        I2CAck_status();
        I2CSend(0x23);
        I2CAck_status();
       	I2CSend(0x00);		//write day of week
        I2CAck_status();
        I2CSend(0x31);		// write date 31
        I2CAck_status();	
        I2CSend(0x12);		// write month dec
        I2CAck_status();
        I2CSend(0x12);
        I2CAck_status();
        I2CRestart();	
        I2CSend(0xD0);		/* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);
        I2CSend(0x55);	
        I2CAck_status();
        I2CStop();
}

Please advice
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void I2Cwrite(){
                                        
    I2CStart();     /* Send Start condition */
        I2CSend(0xD0);      /* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);      /*Send subaddress 0x00,we are writing to this location */
        I2CAck_status();
        I2CSend(0x80);      //write $80 to REG0. (pause counter + 0 sec)
        I2CAck_status();
        I2CSend(0x59);
        I2CAck_status();
        I2CSend(0x7B);      //12 hour mode 11 PM
        I2CAck_status();
        I2CSend(0x00);      //write day of week
        I2CAck_status();
        I2CSend(0x31);      // write date 31
        I2CAck_status();    
        I2CSend(0x12);      // write month dec
        I2CAck_status();
        I2CSend(0x12);
        I2CAck_status();
        I2CRestart();   
        I2CSend(0xD0);      /* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);
        I2CSend(0x55);  
        I2CAck_status();
        I2CStop();
}

 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Jayanth
I changed as per your advive
It does not working

please advice

it is printing in Hour 74 lcdrtc.JPG

Please advice
 
Last edited:

Read the thread in the link provided in my previous post.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void I2Cwrite(){
                                        
        I2CStart();     /* Send Start condition */
        I2CSend(0xD0);      /* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);      /*Send subaddress 0x00,we are writing to this location */
        I2CAck_status();
        I2CSend(0x80);      //write $80 to REG0. (pause counter + 0 sec)
        I2CAck_status();
        I2CSend(0x40);
        I2CAck_status();
        I2CSend(0x71);      //12 hour mode 11 PM
        I2CAck_status();
        I2CSend(0x00);      //write day of week
        I2CAck_status();
        I2CSend(0x31);      // write date 31
        I2CAck_status();    
        I2CSend(0x12);      // write month dec
        I2CAck_status();
        I2CSend(0x12);
        I2CAck_status();
        I2CRestart();   
        I2CSend(0xD0);      /* Send DS1307 slave address with write operation */
        I2CAck_status();
        I2CSend(0x00);
        I2CSend(0x55);  
        I2CAck_status();
        I2CStop();
}

 
  • Like
Reactions: PA3040

    PA3040

    Points: 2
    Helpful Answer Positive Rating
Dear Jayanth

let's we suppose write operation done like your advice

Do we have to check the bit 5 of 0x02 register for 10 hours in the read operation in 12 Hour mode
or 0x02 register content only 0-12 hours only

Please advice
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top