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.

Time to display am or pm in PIC16F877A

Status
Not open for further replies.

kvns.kapil

Newbie level 4
Joined
Oct 12, 2010
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,461
Hi,
I am new to Embedded programming, My project is about moving message display in which i am using PIC 16F877A, 5x7 dot matrix display, DS1307 RTC.
Already the program is written for this project, i wanted to change time from 24 hour format to 12 hour format. I am sending that code in attached file, please verify this one and suggest me the modifications.

#include<htc.h>
#include"font15x8.h"
#include<string.h>
#include<stdlib.h>
#include"inbuilt_pic_i2c.h"

#define _XTAL_FREQ 20000000
__CONFIG(HS&WDTDIS&PWRTDIS&UNPROTECT&BORDIS&LVPDIS);

#define ACK 1
#define NO_ACK 0
#define EEPROM_W 0xA0
#define EEPROM_R 0xA1
#define data_status 0x40
#define per_status 0x80
#define wght_status 0xC0
#define SECONDS rtc_register[0]
#define MINUTES rtc_register[1]
#define HOURS rtc_register[2]
#define DAY rtc_register[3]
#define DATE rtc_register[4]
#define MONTH rtc_register[5]
#define YEAR rtc_register[6]
#define DS1307_W 0xD0
#define DS1307_R 0xD1


#define row_data_drive PORTD

#define _74HC595_SDA RB7 // 30
#define _LATCH_ClK RB6 // 29
#define _SHIFT_CLK RB5 // 28


void scroll_char(unsigned char ); // Function used to prepare necessary information to scroll charater on LED MATRIX.
void Shift_row_buffer_left(unsigned int); // Function used to shift data from right to left on display.
void Scroll_String(char *); // Function used to pass string to Scroll_char() function.
void Transfer_Data();
void shiftOut(unsigned char);
//void Name_Write();
//void Name_read();
//void no_of_persons_write();
//void no_of_persons_read();
//void weight_write();
//void weight_read();
//void write_char(unsigned char);
unsigned char EEPROM_write(unsigned char,unsigned char,unsigned int);
unsigned char RTC_writeTime(void);
unsigned char RTC_updateTime(void);
void RTC_updateRegisters(void);
unsigned char RTC_getTime(void);
void RTC_getDate(void);
void RTC_read(void);
char * Int2Day(unsigned char);
void Init_ADC();
unsigned int ReadADC(unsigned char);

unsigned int delay = 0,i=0;
static int col_inc = 0;
unsigned int data_len = 0,per_len = 0,wght_len = 0;
static unsigned int row_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row1_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row2_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row3_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row4_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row5_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};
static unsigned int row6_buf[8]={0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000};

const unsigned char *DayStr[7] = {{"Sunday"},
{"Monday"},
{"Tuesday"},
{"Wednesday"},
{"Thursday"},
{"Friday"},
{"Saturday"}};

static unsigned char rtc_register[7]={0,0,0,0,0,0,0};
static unsigned char time[9];
static unsigned char date[11]; //xx/xx/xxxx;
static unsigned char day = 0,Data = 0;
static unsigned long int temp_value = 0,temp_value1 = 0;
/*static unsigned char str[38];
static unsigned char str1[13];
static unsigned char str2[10];*/
static unsigned char buf[30];
//static unsigned char buf1[65];


/*************** MAIN FUNCTION ***************/


void main()
{


for(delay=0;delay<30;delay++)
__delay_ms(30);

RBPU = 0;
PORTD = 0x00;
TRISD = 0;
PSPMODE = 0;
TRISB6 = 0;
PORTD = 0x00;
GIE = 0;
i2c_init();
__delay_ms(20);
Init_ADC();

__delay_ms(30);

_LATCH_ClK = 0;
shiftOut(0x00);
shiftOut(0x00);
shiftOut(0x00);
shiftOut(0x00);
shiftOut(0x00);
shiftOut(0x00);
shiftOut(0x00);
_LATCH_ClK = 1;

memset(row_buf,0,sizeof(row_buf));
memset(row1_buf,0,sizeof(row1_buf));
memset(row2_buf,0,sizeof(row2_buf));
memset(row3_buf,0,sizeof(row3_buf));
memset(row4_buf,0,sizeof(row4_buf));
memset(row5_buf,0,sizeof(row5_buf));
memset(row6_buf,0,sizeof(row6_buf));

memset(time,0,sizeof(time));
memset(date,0,sizeof(date));
memset(buf,0,sizeof(buf));
//memset(buf1,0,sizeof(buf1));

for(delay=0;delay<10;delay++)
__delay_ms(30);
delay = 0;



rtc_register[0] = (0x32 & 0x7F);
i2c_start();
i2c_write(DS1307_W);
i2c_write(0x00);
i2c_write(rtc_register[0]);
i2c_stop();


/* i2c_start();
i2c_write(DS1307_W);
i2c_write(0x07);
i2c_write(0x13);
i2c_stop();

rtc_register[0] = (rtc_register[0] & 0x7F); // enable oscillator (bit 7=0)

rtc_register[1] = 0x42; // minute = 59

rtc_register[2] = 0x12; // hour = 05 ,24-hour mode(bit 6=0)

rtc_register[3] = 0x02; // Day = 1 or sunday

rtc_register[4] = 0x06; // Date = 30

rtc_register[5] = 0x09; // month = March

rtc_register[6] = 0x10; // year = 10 or 2010

Data = RTC_writeTime();

*/
for(delay=0;delay< 5;delay++)
__delay_ms(30);
delay = 0;

/*data_len = 0;
per_len = 0;
wght_len = 0;

Name_read();
__delay_ms(5);

no_of_persons_read();
__delay_ms(5);

weight_read();
__delay_ms(5);

*/

while(1)
{

RTC_read();
RTC_getTime();
__delay_ms(3);
RTC_getDate();
__delay_ms(3);
col_inc = -2;
Scroll_String(time);
memset(row_buf,0,sizeof(row_buf));
memset(row1_buf,0,sizeof(row1_buf));
memset(row2_buf,0,sizeof(row2_buf));
memset(row3_buf,0,sizeof(row3_buf));
memset(row4_buf,0,sizeof(row4_buf));
memset(row5_buf,0,sizeof(row5_buf));
memset(row6_buf,0,sizeof(row6_buf));
col_inc = -2;
day = rtc_register[3];
Scroll_String((char*)DayStr[day-1]);
memset(row_buf,0,sizeof(row_buf));
memset(row1_buf,0,sizeof(row1_buf));
memset(row2_buf,0,sizeof(row2_buf));
memset(row3_buf,0,sizeof(row3_buf));
memset(row4_buf,0,sizeof(row4_buf));
memset(row5_buf,0,sizeof(row5_buf));
memset(row6_buf,0,sizeof(row6_buf));
col_inc = -2;
Scroll_String(date);
memset(row_buf,0,sizeof(row_buf));
memset(row1_buf,0,sizeof(row1_buf));
memset(row2_buf,0,sizeof(row2_buf));
memset(row3_buf,0,sizeof(row3_buf));
memset(row4_buf,0,sizeof(row4_buf));
memset(row5_buf,0,sizeof(row5_buf));
memset(row6_buf,0,sizeof(row6_buf));
memset(time,0,sizeof(time));
memset(date,0,sizeof(date));
col_inc = -2;
i = 0;
while(i != 20)
{
temp_value = ReadADC(1);
temp_value1 += temp_value;
i++;
if(i == 18)
{
temp_value1 = (temp_value1/18);
temp_value1 = ((temp_value1*500)/1023);
temp_value1 = (temp_value1-6);
__delay_ms(1);
utoa(buf,temp_value1,10);
temp_value1 = 0;
temp_value = 0;
}
}
i=0;
strcat(buf,(char*)" Degree Celcius");
__delay_ms(2);
Scroll_String(buf);
memset(row_buf,0,sizeof(row_buf));
memset(row1_buf,0,sizeof(row1_buf));
memset(row2_buf,0,sizeof(row2_buf));
memset(row3_buf,0,sizeof(row3_buf));
memset(row4_buf,0,sizeof(row4_buf));
memset(row5_buf,0,sizeof(row5_buf));
memset(row6_buf,0,sizeof(row6_buf));
memset(buf,0,sizeof(buf));

}

}


/*************** SHIFT COLUMN FROM RIGHT TO LEFT ***************/

void Shift_row_buffer_left(unsigned int columns)
{
unsigned int index;//,cnt;
//cnt = 0;
col_inc++;

for(index = 0;index < 8;index++)
{

row_buf[index] <<= columns;

if(col_inc > 7)
{
row1_buf[index] <<= columns;
row1_buf[index] |= ((row_buf[index]>>8) & 0xFFFF);
row_buf[index] &= 0x00FF;
}
if(col_inc > 15)
{
row2_buf[index] <<= columns;
row2_buf[index] |= ((row1_buf[index]>>8) & 0xFFFF);
row1_buf[index] &= 0x00FF;
}
if(col_inc > 23)
{
row3_buf[index] <<= columns;
row3_buf[index] |= ((row2_buf[index]>>8) & 0xFFFF);
row2_buf[index] &= 0x00FF;
}
if(col_inc > 31)
{
row4_buf[index] <<= columns;
row4_buf[index] |= ((row3_buf[index]>>8) & 0xFFFF);
row3_buf[index] &= 0x00FF;
}
//row4_buf[index] &= 0x00FF;

if(col_inc > 39)
{
row5_buf[index] <<= columns;
row5_buf[index] |= ((row4_buf[index]>>8) & 0xFFFF);
row4_buf[index] &= 0x00FF;
}
if(col_inc > 47)
{
row6_buf[index] <<= columns;
row6_buf[index] |= ((row5_buf[index]>>8) & 0xFFFF);
row5_buf[index] &= 0x00FF;
}
row6_buf[index] &= 0x00FF;

}

index = 0;
while(index++ < 8)// we have to rotate loop up to required iterations to get brightness.
{

Transfer_Data();// After the left shift operation we send data to the rows and columns of led display.
//index++;
//cnt++;

}
index = 0;
//cnt = 0;

}


/*************** SCROLL CHARACTER ON LED MATRIX ***************/

void scroll_char(unsigned char letter)
{

unsigned char Data,i,j;

letter -= 32; // Here we get location of character in font table with help of character coming from Scroll_String function.

Shift_row_buffer_left(1); // here before going to next data we have to shift one column left.

for(i=0;i<5;i++)
{

Shift_row_buffer_left(1); // Again we shift one column before going to next font size of next column data.

Data = (unsigned char)((int*)font5x8[(5*letter) + i]); // here we find location of character font size in font table.

for(j=0;j<8;j++) //here logical operation is done to display data on led display for proper output.
{
//write bits to appropriate row_buffer locaton

if(Data & (1<<j))
{
row_buf[j] |= 1;
}
else
{
row_buf[j] &= ~1;
}
}
}
}


/*************** FUNCTION TO PASS STRING FOR SCROLLING ***************/

void Scroll_String(char * myString) //Function that scrolls a given string onto the display
{

//unsigned int i;

while(*myString)//Repeat until a null character is encountered (signals end of a string)
{

scroll_char(*myString);//Scroll in the next character

++myString; //Increment the point which tracks where we are in the string


}
for(i = 0;i < 62;i++)
{
Shift_row_buffer_left(1);// After last string we have to shift columns from 0 to 40 to clear display.

}
i = 0;
}


/*************** TRANSFER DATA TO LED MATRIX FROM BUFFER ***************/



void Transfer_Data()
{

unsigned char row_shift; //,row_inc;

row_shift = 0x40;

for(delay = 0;delay < 7;delay++)
{
_LATCH_ClK = 0;

shiftOut(row6_buf[delay]);
shiftOut(row5_buf[delay]);
shiftOut(row4_buf[delay]);
shiftOut(row3_buf[delay]);
shiftOut(row2_buf[delay]);
shiftOut(row1_buf[delay]);
shiftOut(row_buf[delay]);

row_data_drive = row_shift;

_LATCH_ClK = 1;

row_shift = (row_shift>>1);
}
delay = 0;

}

void shiftOut(unsigned char myDataOut)
{

int i=0;

TRISB7 = 0;
TRISB5 = 0;
_74HC595_SDA = 0;
_SHIFT_CLK = 0;
__delay_us(2);

for(i=7; i>=0; i--)//for each bit in the byte myDataOut…
{
_SHIFT_CLK = 0;
if(myDataOut&(1<<i))
{
_74HC595_SDA = 1;
}
else
{
_74HC595_SDA = 0;
}
//register shifts bits on upstroke of clock pin
_SHIFT_CLK = 1;
//zero the data pin after shift to prevent bleed through
_74HC595_SDA = 0;
}

}


/*void Name_Write()
{
//unsigned char errorStatus;
i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x00);
for(i=0;i<data_len;i++)
{
i2c_write(str);
__delay_us(10);
}
i2c_stop();
i2c_idle();
__delay_ms(3);
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(data_status);
i2c_write(data_len);
i2c_stop();
return;
}

void Name_read()
{
i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(data_status);
i2c_repstart();
i2c_write(EEPROM_R);
data_len = i2c_read(NO_ACK);
i2c_stop();

i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x00);
i2c_repstart();
i2c_write(EEPROM_R);

for(i=0;i<data_len;i++)
{
if(i == (data_len-1))//no Acknowledge after receiving the last byte
str = i2c_read(NO_ACK);
else
str = i2c_read(ACK);
}

i2c_stop();

return;

}


void no_of_persons_write()
{
i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x61);
for(i=0;i<per_len;i++)
{
i2c_write(str1);
__delay_us(20);
}
i2c_stop();

i2c_idle();
__delay_ms(5);
i2c_start();
__delay_ms(2);
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x81);
i2c_write(per_len);
i2c_stop();
return;

}

void no_of_persons_read()
{
i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x81);
i2c_repstart();
i2c_write(EEPROM_R);
per_len = i2c_read(NO_ACK);
i2c_stop();

i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0x61);
i2c_repstart();
i2c_write(EEPROM_R);

for(i=0;i < per_len;i++)
{
if(i == (per_len-1))//no Acknowledge after receiving the last byte
str1 = i2c_read(NO_ACK);
else
str1 = i2c_read(ACK);
}

i2c_stop();

return;

}

void weight_write()
{

i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0xA0);
for(i=0;i < wght_len;i++)
{
i2c_write(str2);
__delay_us(10);
}
i2c_stop();
i2c_idle();
__delay_ms(3);
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(wght_status);
i2c_write(wght_len);
i2c_stop();
return;

}


void weight_read()
{
i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(wght_status);
i2c_repstart();
i2c_write(EEPROM_R);
wght_len = i2c_read(NO_ACK);
i2c_stop();

i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(0x00);
i2c_write(0xA0);
i2c_repstart();
i2c_write(EEPROM_R);

for(i=0;i < wght_len;i++)
{
if(i == (wght_len-1))//no Acknowledge after receiving the last byte
str2 = i2c_read(NO_ACK);
else
str2 = i2c_read(ACK);
__delay_us(3);
}

i2c_stop();

return;

}
*/
/*unsigned char EEPROM_write(unsigned char highAddress, unsigned char lowAddress,unsigned int no_of_bytes)
{
unsigned char errorStatus, data;

i2c_idle();
i2c_start();
i2c_write(EEPROM_W);
i2c_write(highAddress);
i2c_write(lowAddress);

for(i=0;i<no_of_bytes;i++)
{
errorStatus = i2c_write(0x00);

if(errorStatus == 1)
{
__delay_ms(35);

}
else
{

__delay_ms(35);

}

}
i2c_stop();
return(0);
}*/

/************* Writing Time to RTC **************/

unsigned char RTC_writeTime(void)
{
//unsigned char i;
i2c_start();
i2c_write(DS1307_W);
i2c_write(0x00);
for(i=0;i<7;i++)
{
i2c_write(rtc_register);
__delay_us(20);

}
i2c_stop();
return(0);
}

/************* Get Time From RTC *******************/

unsigned char RTC_getTime(void)
{


time[8] = 0;
time[7] = ((SECONDS & 0x0f) | 0x30); //seconds(1's)
time[6] = (((SECONDS & 0x70) >> 4) | 0x30); //seconds(10's)
time[5] = ':';
time[4] = ((MINUTES & 0x0f) | 0x30);
time[3] = (((MINUTES & 0x70) >> 4) | 0x30);
time[2] = ':';
time[1] = ((HOURS & 0x0f) | 0x30);
time[0] = (((HOURS & 0x30) >> 4) | 0x30);

__delay_ms(3);
return(0);

}

/********** Read Time From RTC *************/

void RTC_read(void)
{
//unsigned int i;
unsigned char data = 0;
i2c_start();
i2c_write(DS1307_W);
i2c_write(0x00);
i2c_repstart();
i2c_write(DS1307_R);

for(i=0;i<7;i++)
{
if(i == 6) //no Acknowledge after receiving the last byte
data = i2c_read(NO_ACK);
else
data = i2c_read(ACK);

rtc_register = data;
__delay_us(15);
}
i2c_stop();
rtc_register = '\0';

}


//******************************************************************
//Function to form date string for sending it to LCD & UART
//******************************************************************

void RTC_getDate(void)
{

//date[11] = 0x00;
//date[10] = '\0';
date[9] = ((YEAR & 0x0f) | 0x30);
date[8] = (((YEAR & 0xf0) >> 4) | 0x30);
date[7] = '0';
date[6] = '2';
date[5] = '/';
date[4] = ((MONTH & 0x0f) | 0x30);
date[3] = (((MONTH & 0x10) >> 4) | 0x30);
date[2] = '/';
date[1] = ((DATE & 0x0f) | 0x30);
date[0] = (((DATE & 0x30) >> 4) | 0x30);

}


//-------------------------------
// Convert date (BCD) to string of Day
// 1=Sunday
// 2=Monday
// And so on
//-------------------------------
char * Int2Day(unsigned char day)
{
Scroll_String((char*)DayStr[day-1]);
return(0);
}

void Init_ADC()
{

TRISA0 = 1;
ADCON0 = 0x81; // select Fosc/32
ADCON1 = 0x84; // Right justified... RA3,RA1,RA0 treat as analog pins........
return;

}

unsigned int ReadADC(unsigned char ADC_Channel)
{

volatile unsigned int ADC_Value;
ADON = 1;
ADC_Channel &= 0x07; // truncate channel to 3 bits.

ADCON0 &= 0xC5; // clear current select.

ADCON0 |= (ADC_Channel<<3); // apply the new channel select

ADIE = 0;

ADIF = 0;

ADRESL = 0;
ADRESH = 0;

ADGO = 1; // initiate conversion on the selected channel

while(ADGO); //wait until conversion completed i.e., ADGO = 0;

ADC_Value = ((ADRESH<<8)+(ADRESL));
ADON = 0;
return(ADC_Value);

}


/*void write_char(unsigned char letter)
{

unsigned char Data,i,j;

letter -= 32;

for(i=0;i<5;i++)
{
Data = (unsigned char)((int*)font5x8[(5*letter) + i]);

for(j=0;j<8;j++)
{
if(Data & (1<<j))
{
row_buf[j] |= (1<<(4-i));
//fontport = row_buf[7-j];
}
else
{
row_buf[j] &= ~(1<<(4-i));
//fontport = row_buf[7-j];
}

}
for(delay=0;delay<75;delay++)
Transfer_Data();
//Delay(1);
}


}*/


Please help me for this code...
thanks...
 

sir,

rtc_register[2] = 0x12; // hour = 05 ,24-hour mode(bit 6=0)
change your mode selection bit in DS1307 using PIC
 

sir,

rtc_register[2] = 0x12; // hour = 05 ,24-hour mode(bit 6=0)
change your mode selection bit in DS1307 using PIC

hello,
how to change the mode sir, please inform me how to change the mode selection bit in ds1307
 

Sir,

The DS1307 can be run in either 12-hour or 24-hour mode. Bit 6 of the hours register is defined as the
12- or 24-hour mode select bit. When high, the 12-hour mode is selected. In the 12-hour mode, bit 5 is
the AM/PM bit with logic high being PM. In the 24-hour mode, bit 5 is the second 10 hour bit (20-
23 hours.

for more clarifications just read datasheet ds1307,i attached it
 

Attachments

  • DS1307.pdf
    348.5 KB · Views: 113

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top