hardik.patel
Member level 5

I want to convert 24Hr-to-12 mode in RTC DS1307 with atmega16(11.0592Mhz).
Its ok with 24Hr mode.
For its solution i had referred its datasheet....and read about 24/12 hour bit but in my code i cant find that where i have to make that change so that i can convert that time in 12HR.
Compiler AVR GCC.
Its ok with 24Hr mode.
For its solution i had referred its datasheet....and read about 24/12 hour bit but in my code i cant find that where i have to make that change so that i can convert that time in 12HR.
Compiler AVR GCC.
Code:
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#define MT_SLA_ACK1 0x18 //twi interface commands for ds1307
#define MT_DATA_ACK1 0x28
#define START 0x08
#define MT_SLA_ACK 0x40
#define MT_DATA_ACK 0x58
#define SLA_R 0b11010001 //address for ds1307
#define SLA_W 0b11010000
#define SEG_ALL 0x7F
#define SEG_A 1
#define SEG_B 2
#define SEG_C 4
#define SEG_D 8
#define SEG_E 16
#define SEG_F 32
#define SEG_G 64
#define sec_register 0x00
#define min_register 0x01
#define hour_register 0x02
#define weekday_weekend_register 0x03
#define day_register 0x04
#define month_register 0x05
#define year_register 0x06
uint8_t address;
voidinitialize_LCD(void);
char first_column_positions_for_LCD[4]={0,64,20,84};
void check_if_LCDisbusy(void); // checking LCD ready to process
void LCD_enabledisplay(void); //disply enable
void send_A_command(unsigned char command); // sending command
void send_A_character(unsigned char character); // sending character
void send_A_string(char *stringsofcharacter); // send string
void goto_location(uint8_t x, uint8_t y);
void send_string_and_location(uint8_t x,uint8_t y, char *stringOFcharacter);
void send_integer(uint8_t x,uint8_t y,int integertodisplay,char numberdigits);
void seven_seg();
uint8_t data_Read;
uint8_t H,M,S,WK,DY,MN,YR;
//uint8_t H1,M1,S1,WK1,DY1,MN1,YR1;
void display(uint8_t n) {
switch(n) {
case 0: PORTD = SEG_ALL-SEG_G; break;
case 1: PORTD = SEG_B+SEG_C; break;
case 2: PORTD = SEG_ALL-SEG_F-SEG_C; break;
case 3: PORTD = SEG_ALL-SEG_F-SEG_E; break;
case 4: PORTD = SEG_F+SEG_G+SEG_B+SEG_C; break;
case 5: PORTD = SEG_ALL-SEG_B-SEG_E; break;
case 6: PORTD = SEG_ALL-SEG_B; break;
case 7: PORTD = SEG_A+SEG_B+SEG_C; break;
case 8: PORTD = SEG_ALL; break;
case 9: PORTD = SEG_ALL-SEG_E; break;
default:PORTD = 0; break;
}
}
void seven_seg(void)
{
//read(hour_register);
//// hour_digits(data_Read);
////
//// read(min_register);
//// min_digits(data_Read);
//
int j,k,l,m;
//int count=1234;
int H9=12;
m=H9/10; //min1
PORTB=0x01;
display(m);
_delay_ms(1000);
l=H9%10; //min2
PORTB=0x02;
display(l);
_delay_ms(1000);
//
//k=(count/10)%10; //hour1
//PORTB=0x04;
//display(k);
//_delay_ms(1000);
//
//j=count%10; //hour1
//PORTB=0x08;
//display(j);
//_delay_ms(1000);
}
uint8_t BCDToDecimal (uint8_t bcdByte)
{
return (((bcdByte& 0xF0) >> 4) * 10) + (bcdByte& 0x0F);
}
uint8_t DecimalToBCD (uint8_t decimalByte)
{
return (((decimalByte / 10) << 4) | (decimalByte % 10));
}
void TWI_bit_rate_set(void)
{
TWBR=8; // set SCL frequency to 400kHz
TWCR|=1<<TWEN;
}
int TWI_start()
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // set start
while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the START condition has been transmitted
if ((TWSR & 0xF8) != START) //Check value of TWI Status Register. Mask prescaler bits. If status different from START go to ERROR
return 0;
}
int TWI_send_address_read() // READ..........send slave address in read mode
{
TWDR = SLA_R;
TWCR = (1<<TWINT) | (1<<TWEN); // Load SLA_W into TWDR Register. Clear TWINT bit in TWCR to start transmission of address
while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received
if ((TWSR & 0xF8) != MT_SLA_ACK) // Check value of TWI Status Register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
return 0;
}
int TWI_send_addr_data(uint8_t data) // WRITE.........send the address of data
{
TWDR=data;
TWCR = (1<<TWINT) | (1<<TWEN); //Load DATA into TWDR Register. Clear TWINT bit in TWCR to start transmission of data
while (!(TWCR & (1<<TWINT))); // Wait for TWINT Flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received
if ((TWSR & 0xF8) != MT_DATA_ACK1) // Check value of TWI Status Register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
return 0;
}
int TWI_get_data() // READ.....get the address of the data required
{
TWCR = (1<<TWINT) | (1<<TWEN); //Load DATA into TWDR Register. Clear TWINT bit in TWCR to start transmission of data
while (!(TWCR & (1<<TWINT))); // Wait for TWINT Flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received
data_Read=TWDR;
TWCR = (1<<TWINT) | (1<<TWEN); //Load DATA into TWDR Register. Clear TWINT bit in TWCR to start transmission of data
while (!(TWCR & (1<<TWINT))); // Wait for TWINT Flag set. This indicates that the DATA has been transmitted, and ACK/NACK has been received
if ((TWSR & 0xF8) != MT_DATA_ACK) // Check value of TWI Status Register. Mask prescaler bits. If status different from MT_DATA_ACK go to ERROR
return 0;
}
int TWI_send_address_write() // WRITE........send slave address in write mode
{
TWDR = SLA_W;
TWCR = (1<<TWINT) | (1<<TWEN); // Load SLA_W into TWDR Register. Clear TWINT bit in TWCR to start transmission of address
while (!(TWCR & (1<<TWINT))); //Wait for TWINT Flag set. This indicates that the SLA+W has been transmitted, and ACK/NACK has been received
if ((TWSR & 0xF8) != MT_SLA_ACK1) // Check value of TWI Status Register. Mask prescaler bits. If status different from MT_SLA_ACK go to ERROR
return 0;
}
void TWI_stop() //................................stop................................................//
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // Transmit STOP condition
}
int write(uint8_t pg_addr,uint8_t data1)
{
TWI_bit_rate_set();
TWI_start();
TWI_send_address_write();
TWI_send_addr_data(pg_addr); // send address of page
TWI_send_addr_data(data1); // send data to be written to the address
TWI_stop();
}
int read(uint8_t pg_addr)
{
TWI_bit_rate_set();
TWI_start();
TWI_send_address_write();
TWI_send_addr_data(pg_addr);
TWI_start();
TWI_send_address_read();
TWI_get_data();
TWI_stop();
}
void display_num(uint8_t DIG,uint8_t loc)
{
if(DIG<=9)
{send_integer(loc,1,0,3);
send_integer((loc+1),1,DIG,3);
}
else
send_integer(loc,1,DIG,3);
}
void display_ALP(uint8_t DIG,uint8_t loc)
{
if(DIG<=9)
{send_integer(loc,2,0,3);
send_integer((loc+1),2,DIG,3);
}
else
send_integer(loc,2,DIG,3);
}
void display_weekday(uint8_t WK)
{
if(WK==1){send_string_and_location(1,2,"<SUN>");}
else if(WK==2){send_string_and_location(1,2,"<MON>");}
else if(WK==3){send_string_and_location(1,2,"<TUE>");}
else if(WK==4){send_string_and_location(1,2,"<WED>");}
else if(WK==5){send_string_and_location(1,2,"<THU>");}
else if(WK==6){send_string_and_location(1,2,"<FRI>");}
else if(WK==7){send_string_and_location(1,2,"<SAT>");}
}
void hour_digits(uint8_t data)
{H=BCDToDecimal(data);}
void sec_digits(uint8_t data)
{S=BCDToDecimal(data);}
void min_digits(uint8_t data)
{M=BCDToDecimal(data);}
void weekday_weekend_digits(uint8_t data)
{WK=BCDToDecimal(data);}
void day_digits(uint8_t data)
{DY=BCDToDecimal(data);}
void month_digits(uint8_t data)
{MN=BCDToDecimal(data);}
void year_digits(uint8_t data)
{YR=BCDToDecimal(data);}
void initialize_LCD()
{
DDRC|=(1<<5)|(1<<6)|(1<<7);
_delay_ms(2);
send_A_character(0x01);
_delay_ms(2);
send_A_command(0x38);
_delay_us(10);
send_A_command(0b00001110);
_delay_us(10);
}
void check_if_LCDisbusy()
{
DDRA=0;
PORTC|=1<<6;
PORTC&=~1<<6;
while(PORTA>=0x80)
{
LCD_enabledisplay();
}
DDRA=0xFF;
}
void LCD_enabledisplay()
{
PORTC|=1<<5; // enable on , bit of delay then off
_delay_ms(5);
PORTC&=~1<<5;
}
void send_A_command(unsigned char command)
{
check_if_LCDisbusy();
PORTA=command;
PORTC&=~((1<<6)|(1<<7)); // readwrite =0 and register select also =0 for sending a command
LCD_enabledisplay();
PORTA=0;
}
void send_A_character(unsigned char character)
{
check_if_LCDisbusy(); // readwrite =0 and register select =1 for sending a command
PORTA=character;
PORTC&=~(1<<6);
PORTC|=1<<7;
LCD_enabledisplay();
DDRA=0;
}
void send_A_string(char *stringsofcharacter)
{
while(*stringsofcharacter>0)
{
send_A_character(*stringsofcharacter++);
}
}
void goto_location(uint8_t x, uint8_t y) // takes cursor to the desired position as on x and y value.
{
send_A_command(0x80 + first_column_positions_for_LCD[y-1] + (x-1));
}
void send_string_and_location(uint8_t x,uint8_t y, char *stringofcharacters) // the string is taken to the desired location on lcd
{
goto_location(x,y);
send_A_string(stringofcharacters);
}
void send_integer(uint8_t x,uint8_t y,int integertodisplay,char numberdigits) // specify the no. of digits of the integer
{
char stringtodisplay[numberdigits];
itoa(integertodisplay,stringtodisplay,10); // 10 means decimal -user undersatandable
// converted the integer to string
int i;
for(i=0;i<4;i++) {send_A_string(" ");} //reserves 4 digits for our display and removes any garbage
send_string_and_location(x,y,stringtodisplay);
send_A_string(" "); // space provided to get rid of any garbage or 0 value
}
void wait(int a)
{ int i;
for(i=1;i<=a;i++)
{_delay_ms(1000);}
}
void RTC_write_data(void)
{
write(sec_register,DecimalToBCD(00));
write(min_register,DecimalToBCD(56));
write(hour_register,DecimalToBCD(18));
write(weekday_weekend_register,DecimalToBCD(1));
write(day_register,DecimalToBCD(15));
write(month_register,DecimalToBCD(03));
write(year_register,DecimalToBCD(15));
}
void RTC_read_data()
{
int j,k,l,m;
read(hour_register);
hour_digits(data_Read);
read(min_register);
min_digits(data_Read);
//H=H-12;
m=H/10; //min1
PORTB=0x01;
display(m);
_delay_ms(1000);
l=H%10; //min2
PORTB=0x02;
display(l);
_delay_ms(1000);
k=M/10; //hour1
PORTB=0x04;
display(k);
_delay_ms(1000);
j=M%10; //hour1
PORTB=0x08;
display(j);
_delay_ms(1000);
}
int main(void)
{
DDRD=0XFF;
//initialize_LCD();
//send_A_command(0x01);
//RTC_write_data();
// first program with write statement then comment write statement and reprogram
while(1)
{
//seven_seg();
RTC_read_data();
}
}