asking
Full Member level 5
- Joined
- Sep 21, 2010
- Messages
- 279
- Helped
- 6
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 1,298
- Activity points
- 3,377
unsigned short i, DD0, DD1, DD2, DD3;
unsigned int Count, hour, minute;
//------ Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
// Software I2C connections
sbit Soft_I2C_Scl at RA6_bit;
sbit Soft_I2C_Sda at RA7_bit;
sbit Soft_I2C_Scl_Direction at TRISA6_bit;
sbit Soft_I2C_Sda_Direction at TRISA7_bit;
// End Software I2C connections
// this is my programming part 1
unsigned short read_ds1307(unsigned short address)
{
unsigned short r_data;
Soft_I2C_Start();
Soft_I2C_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xD1); //0x68 followed by 1 --> 0xD1
r_data=Soft_I2C_Read(0);
Soft_I2C_Stop();
return(r_data);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
Soft_I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
Soft_I2C_Write(address); // send byte (address of DS1307 location)
Soft_I2C_Write(w_data); // send data (data to be written)
Soft_I2C_Stop(); // issue I2C stop signal
}
void main()
{
Soft_I2C_Init();
minute = read_ds1307(1);
hour = read_ds1307(2);
CMCON |= 7; // Disable Comparators
TRISB = 0xff; // Set PORTB7 input all other direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA = 0b11100000; // RA5-6-7 is input only
do {
DD0 = Count%10; // Extract Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // Extract Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // Extract Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // Extract Thousands Digit
DD3 = mask(DD3);
for (i = 0; i<=50; i++) {
RA3_bit = 1; //disable RA3
PORTB = DD0;
RA0_bit = 0; // Select Ones Digit
RA1_bit = 1;
RA2_bit = 1;
delay_ms(5);
RA0_bit = 1; //disable RA0
PORTB = DD1;
RA1_bit = 0; // Select Tens Digit
RA2_bit = 1;
RA3_bit = 1;
delay_ms(5);
RA1_bit = 1; //disable RA1
PORTB = DD2;
RA0_bit = 1;
RA2_bit = 0; // Select Hundreds Digit
RA3_bit = 1;
delay_ms(5);
RA2_bit = 1; //disable RA2
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 1;
RA3_bit = 0; // Select Thousands Digit
delay_ms(5);
}
Count = hour*100 + minute;
} while(1);
}
// endless loop
DD0 = Count%10; // Extract Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // Extract Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // Extract Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // Extract Thousands Digit
DD3 = mask(DD3);
int bcd2int(int BCD){return(BCD>>4)*10+(BCD&0x0F);}
Do{
minute=(minute>>4)*10+(minute&0x0F);
hour=((hour>>4)&0x01)*10+(hour&0x0F);//if you use 12 hour format
hour=((hour>>4)&0x03)*10+(hour&0x0F);//if you use 24 hour format
count=hour*100+minute;
DD0 = Count%10; // Extract Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // Extract Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // Extract Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // Extract Thousands Digit
DD3 = mask(DD3);
read the datasheet of ds1307. you will see that at it holds minutes at 0x01h address. the lower 4 bits for minutes and the other 3 bits for 10 minutes. and the last bit is empty.
after you read minute data from ds1307, you must reformat it.
Do{
minute=(minute>>4)*10+(minute&0x0F);
hour=((hour>>4)&0x01)*10+(hour&0x0F);//if you use 12 hour format
hour=((hour>>4)&0x03)*10+(hour&0x0F);//if you use 24 hour format
count=hour*100+minute;
DD0 = Count%10; // Extract Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // Extract Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // Extract Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // Extract Thousands Digit
DD3 = mask(DD3);
Thanks Bigdogguru,
for the link. But i want to use soft i2c connection and that is where i m stucked. If i define TRISA = 0 (means all pins are set for output) then how can i define
sbit Soft_I2C_Scl at RA6_bit;
sbit Soft_I2C_Sda at RA7_bit;
because i think in serial i2c is bidirectional...so m confused...because 16F628A has no hardware i2c. so if any one can help to clear this.....please do let me know.
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
[COLOR="#FF0000"]sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;[/COLOR]
// End Software I2C connections
// Software I2C connections
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
// End Software I2C connections
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
char seconds, minutes, hours, day, month, year; // Global date/time variables
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xA1); // Address PCF8583 for reading R/W=1
seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal
}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (day / 10) + 48); // Print tens digit of day variable
Lcd_Chr(1, 7, (day % 10) + 48); // Print oness digit of day variable
Lcd_Chr(1, 9, (month / 10) + 48);
Lcd_Chr(1,10, (month % 10) + 48);
Lcd_Chr(1,15, year + 48); // Print year variable (start from year 2010)
Lcd_Chr(2, 6, (hours / 10) + 48);
Lcd_Chr(2, 7, (hours % 10) + 48);
Lcd_Chr(2, 9, (minutes / 10) + 48);
Lcd_Chr(2,10, (minutes % 10) + 48);
Lcd_Chr(2,12, (seconds / 10) + 48);
Lcd_Chr(2,13, (seconds % 10) + 48);
}
//------------------ Performs project-wide init
void Init_Main() {
TRISB = 0;
PORTB = 0xFF;
TRISB = 0xff;
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
Soft_I2C_Init(); // Initialize Soft I2C communication
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear LCD display
Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Date:"); // Prepare and output static text on LCD
Lcd_Chr(1,8,'.');
Lcd_Chr(1,11,'.');
Lcd_Chr(1,16,'.');
Lcd_Out(2,1,"Time:");
Lcd_Chr(2,8,':');
Lcd_Chr(2,11,':');
Lcd_Out(1,12,"201"); // start from year 2010
}
//----------------- Main procedure
void main() {
Delay_ms(500);
Init_Main(); // Perform initialization
while (1) { // Endless loop
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}
}
// Software I2C connections
sbit Soft_I2C_Scl at RA2_bit;
sbit Soft_I2C_Sda at RA3_bit;
sbit Soft_I2C_Scl_Direction at TRISA2_bit;
sbit Soft_I2C_Sda_Direction at TRISA3_bit;
// End Software I2C connections
// this is my progRAmming part 1
unsigned short read_ds1307(unsigned short address)
{
unsigned short r_data;
Soft_I2C_Start();
Soft_I2C_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xD1); //0x68 followed by 1 --> 0xD1
r_data=Soft_I2C_Read(0);
Soft_I2C_Stop();
return(r_data);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
Soft_I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
Soft_I2C_Write(address); // send byte (address of DS1307 location)
Soft_I2C_Write(w_data); // send data (data to be written)
Soft_I2C_Stop(); // issue I2C stop signal
}
unsigned short i, DD0, DD1, DD2, DD3;
unsigned int Count, hour, minute;
//------ Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{
CMCON |= 7; // Disable CompaRAtors
TRISB = 0x00; // Set PORTB7 input all other direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA = 0b00100000; // RA5-6-7 is input only
Soft_I2C_Init();
minute = read_ds1307(1);
hour = read_ds1307(2);
do {
DD0 = Count%10; // ExtRAct Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // ExtRAct Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // ExtRAct Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // ExtRAct Thousands Digit
DD3 = mask(DD3);
for (i = 0; i<=50; i++) {
RA7_bit = 1; //disable RA3
PORTB = DD0;
RA0_bit = 0; // Select Ones Digit
RA1_bit = 1;
RA6_bit = 1;
delay_ms(5);
RA0_bit = 1; //disable RA0
PORTB = DD1;
RA1_bit = 0; // Select Tens Digit
RA6_bit = 1;
RA7_bit = 1;
delay_ms(5);
RA1_bit = 1; //disable RA1
PORTB = DD2;
RA0_bit = 1;
RA6_bit = 0; // Select Hundreds Digit
RA7_bit = 1;
delay_ms(5);
RA6_bit = 1; //disable RA2
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 1;
RA7_bit = 0; // Select Thousands Digit
delay_ms(5);
}
minute=(minute>>4)*10+(minute&0x0F);
hour=((hour>>4)&0x01)*10+(hour&0x0F);
count=hour*100+minute;
} while(1);
}
// endless loop
// Software I2C connections
sbit Soft_I2C_Scl at RA3_bit;
sbit Soft_I2C_Sda at RA4_bit;
sbit Soft_I2C_Scl_Direction at TRISA3_bit;
sbit Soft_I2C_Sda_Direction at TRISA4_bit;
// End Software I2C connections
// this is my progRAmming part 1
unsigned short read_ds1307(unsigned short address)
{
unsigned short r_data;
Soft_I2C_Start();
Soft_I2C_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xD1); //0x68 followed by 1 --> 0xD1
r_data=Soft_I2C_Read(0);
Soft_I2C_Stop();
return(r_data);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
Soft_I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
Soft_I2C_Write(address); // send byte (address of DS1307 location)
Soft_I2C_Write(w_data); // send data (data to be written)
Soft_I2C_Stop(); // issue I2C stop signal
}
unsigned short i, DD0, DD1, DD2, DD3;
unsigned short Count, hour, minute, hour1, minute1;
//------ Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{
Soft_I2C_Init();
CMCON |= 7; // Disable Comparators
TRISB = 0x00; // Set PORTB7 input all other direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA = 0b00000000; // RA5-6-7 is input only
//
minute = read_ds1307(1);
hour = read_ds1307(2);
do {
DD0 = Count%10; // ExtRAct Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // ExtRAct Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // ExtRAct Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // ExtRAct Thousands Digit
DD3 = mask(DD3);
for (i = 0; i<=50; i++) {
RA7_bit = 1; //disable RA3
PORTB = DD0;
RA0_bit = 0; // Select Ones Digit
RA1_bit = 1;
RA6_bit = 1;
delay_ms(5);
RA0_bit = 1; //disable RA0
PORTB = DD1;
RA1_bit = 0; // Select Tens Digit
RA6_bit = 1;
RA7_bit = 1;
delay_ms(5);
RA1_bit = 1; //disable RA1
PORTB = DD2;
RA0_bit = 1;
RA6_bit = 0; // Select Hundreds Digit
RA7_bit = 1;
delay_ms(5);
RA6_bit = 1; //disable RA2
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 1;
RA7_bit = 0; // Select Thousands Digit
delay_ms(5);
}
minute1=(minute>>4)*10+(minute&0x0F);
hour1=((hour>>4)&0x02)*10+(hour&0x0F);
Count = hour1*100+minute1;
} while(1);
}
// endless loop
// Software I2C connections
sbit Soft_I2C_Scl at RA3_bit;
sbit Soft_I2C_Sda at RA4_bit;
sbit Soft_I2C_Scl_Direction at TRISA3_bit;
sbit Soft_I2C_Sda_Direction at TRISA4_bit;
// End Software I2C connections
// this is my progRAmming part 1
unsigned short read_ds1307(unsigned short address)
{
unsigned short r_data;
Soft_I2C_Start();
Soft_I2C_Write(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(address);
Soft_I2C_Start();
Soft_I2C_Write(0xD1); //0x68 followed by 1 --> 0xD1
r_data=Soft_I2C_Read(0);
Soft_I2C_Stop();
return(r_data);
}
void write_ds1307(unsigned short address,unsigned short w_data)
{
Soft_I2C_Start(); // issue I2C start signal
//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
Soft_I2C_Write(0xD0); // send byte via I2C (device address + W)
Soft_I2C_Write(address); // send byte (address of DS1307 location)
Soft_I2C_Write(w_data); // send data (data to be written)
Soft_I2C_Stop(); // issue I2C stop signal
}
unsigned short i, DD0, DD1, DD2, DD3;
unsigned short Count, hour, minute, hour1, minute1;
//------ Function to Return mask for common anode 7-seg. display
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
} //case end
}
void main()
{
void Soft_I2C_Init();
CMCON |= 7; // Disable CompaRAtors
TRISB = 0x00; // Set PORTB7 input all other direction to be output
PORTB = 0xff; // Turn OFF LEDs on PORTB
TRISA = 0b00000000; // RA5-6-7 is input only
//
do {
DD0 = Count%10; // ExtRAct Ones Digit
DD0 = mask(DD0);
DD1 = (Count/10)%10; // ExtRAct Tens Digit
DD1 = mask(DD1);
DD2 = (Count/100)%10; // ExtRAct Hundreds Digit
DD2 = mask(DD2);
DD3 = (Count/1000); // ExtRAct Thousands Digit
DD3 = mask(DD3);
for (i = 0; i<=50; i++) {
RA7_bit = 1; //disable RA3
PORTB = DD0;
RA0_bit = 0; // Select Ones Digit
RA1_bit = 1;
RA6_bit = 1;
delay_ms(5);
RA0_bit = 1; //disable RA0
PORTB = DD1;
RA1_bit = 0; // Select Tens Digit
RA6_bit = 1;
RA7_bit = 1;
delay_ms(5);
RA1_bit = 1; //disable RA1
PORTB = DD2;
RA0_bit = 1;
RA6_bit = 0; // Select Hundreds Digit
RA7_bit = 1;
delay_ms(5);
RA6_bit = 1; //disable RA2
PORTB = DD3;
RA0_bit = 1;
RA1_bit = 1;
RA7_bit = 0; // Select Thousands Digit
delay_ms(5);
}
minute = read_ds1307(1);
hour = read_ds1307(2);
minute1=(minute>>4)*10+(minute&0x0F);
hour1=((hour>>4)&0x02)*10+(hour&0x0F);
Count = hour1*100+minute1;
} while(1);
}
// endless loop
CLOCK AND CALENDAR
The time and calendar information is obtained by reading the appropriate register bytes. Table 2 shows the RTC registers. The time and calendar are set or initialized by writing the appropriate register bytes. The contents of the time and calendar registers are in the BCD format. The day-of-week register increments at midnight. Values that correspond to the day of week are user-defined but must be sequential (i.e., if 1 equals Sunday, then 2 equals Monday, and so on.) Illogical time and date entries result in undefined operation. Bit 7 of Register 0 is the clock halt (CH) bit. When this bit is set to 1, the oscillator is disabled. When cleared to 0, the oscillator is enabled. On first application of power to the device the time and date registers are typically reset to 01/01/00 01 00:00:00 (MM/DD/YY DOW HH:MM:SS). The CH bit in the seconds register will be set to a 1. The clock can be halted whenever the timekeeping functions are not required, which minimizes current (IBATDR).
Have you clear the Clock Halt (CH) bit?
BigDog
do {
set = 0;
if(PORTA.F5 == 0)
{
Delay_ms(100);
if(PORTA.F5 == 0)
{
set_count++;
if(set_count >= 2)
{
set_count = 0;
}
}
}
if(set_count)
{
if(PORTA.F2 == 0)
{
Delay_ms(100);
if(PORTA.F2 == 0)
set = 1;
}
if(set_count && set)
{
switch(set_count)
{
case 1:
hour = BCD2Binary(hour);
hour = hour + set;
hour = Binary2BCD(hour);
if((hour & 0x1F) >= 0x13)
{
hour = hour & 0b11100001;
hour = hour ^ 0x20;
}
else if((hour & 0x1F) <= 0x00)
{
hour = hour | 0b00010010;
hour = hour ^ 0x20;
}
write_ds1307(2, hour); //write hour
break;
case 2:
minute = BCD2Binary(minute);
minute = minute + set;
if(minute >= 60)
minute = 0;
if(minute < 0)
minute = 59;
minute = Binary2BCD(minute);
write_ds1307(1, minute); //write min
break;
}
}
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?