| ??? 12/20/09 10:37 Modified: 12/20/09 10:38 Read: times |
#171791 - 89V51 and DS1307 |
Hello all,
I am trying to connect a 8051 with a DS1307 RTC. For the sake of testing the connection, I am fetching the unit value of the seconds and display it on a seven segement display. Unfortunately, i don't get the value right. Could you please let me know what i am doing wrong?
#include "Main.h"
#include "Hardware_Config.h"
#include "ds1307.h"
#include "Seven_Seg_Symbols.h"
#include <stdio.h>
//connect SDA and SCL to Port1.1 and Port1.0
#define SDA P1_1
#define SCL P1_0
void main()
{
//Start the clock
tByte second;
tByte FirstDigit;
P2 = 0x00;
tByte
DS1307_startTimer();
while(1)
{
second = DS1307_get(SEC);
FirstDigit = second % 10;
P2 = SevenSeg_DisplayNumber(FirstDigit);
}
}
/**********************************************
* File: DS1307.H
* Defines the basic function required to interact with the DS1307
* using the i2c.h header
***********************************************/
#ifndef _DS1307_H
#define _DS1307_H
#include "i2c.h" /* Need i2c bus */
#define DS1307_ID 0xD0
#define SEC 0x00
#define MIN 0x01
#define HOUR 0x02
#define DATE 0x04
#define MONTH 0x05
#define YEAR 0x06
unsigned char DS1307_get(unsigned char addr)
{
unsigned char ret;
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID); /* Connect to DS1307 */
I2C_write(addr); /* Request RAM address on DS1307 */
I2C_start(); /* Start i2c bus */
I2C_write(DS1307_ID+1); /* Connect to DS1307 for Read */
ret = I2C_read(); /* Receive data */
I2C_stop(); /* Stop i2c bus */
return ret;
}
void DS1307_settime(unsigned char hh, unsigned char mm, unsigned char ss)
{
I2C_start();
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0x00); /* Request RAM address at 00H */
I2C_write(ss); /* Write sec on RAM address 00H */
I2C_write(mm); /* Write min on RAM address 01H */
I2C_write(hh); /* Write hour on RAM address 02H */
I2C_stop(); /* Stop i2c bus */
}
void DS1307_setdate(unsigned char dd, unsigned char mm, unsigned char yy)
{
I2C_start();
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0x04); /* Request RAM address at 04H */
I2C_write(dd); /* Write date on RAM address 04H */
I2C_write(mm); /* Write month on RAM address 05H */
I2C_write(yy); /* Write year on RAM address 06H */
I2C_stop(); /* Stop i2c bus */
}
void DS1307_startTimer()
{
I2C_start();
I2C_write(DS1307_ID); /* connect to DS1307 */
I2C_write(0x00); /* Request RAM address at 00H */
I2C_write(0x80); /* Set CH bit ie. 7th bit og 0x00 to start timer*/
I2C_stop(); /* Stop i2c bus */
}
#endif
/**************************
* File name: i2c.h
* I2C drivers in C51
**************************/
#ifndef _I2C_H
#define _I2C_H
#define I2C_DELAY 0x0F /* For delay i2c bus */
void I2C_delay(void)
{
unsigned char i;
for(i=0; i<I2C_DELAY; i++);
}
void I2C_clock(void)
{
I2C_delay();
SCL = 1; /* Start clock */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_start(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 1; /* Set SDA */
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 0; /* Clear SCL */
}
void I2C_stop(void)
{
if(SCL)
SCL = 0; /* Clear SCL */
SDA = 0; /* Clear SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
SDA = 1; /* Set SDA */
}
bit I2C_write(unsigned char dat)
{
bit data_bit;
unsigned char i;
for(i=0;i<8;i++) /* For loop 8 time(send data 1 byte) */
{
data_bit = dat & 0x80; /* Filter MSB bit keep to data_bit */
SDA = data_bit; /* Send data_bit to SDA */
I2C_clock(); /* Call for send data to i2c bus */
dat = dat<<1;
}
SDA = 1; /* Set SDA */
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
data_bit = SDA; /* Check acknowledge */
SCL = 0; /* Clear SCL */
I2C_delay();
return data_bit; /* If send_bit = 0 i2c is valid */
}
unsigned char I2C_read(void)
{
bit rd_bit;
unsigned char i, dat;
dat = 0x00;
for(i=0;i<8;i++) /* For loop read data 1 byte */
{
I2C_delay();
SCL = 1; /* Set SCL */
I2C_delay();
rd_bit = SDA; /* Keep for check acknowledge */
dat = dat<<1;
dat = dat | rd_bit; /* Keep bit data in dat */
SCL = 0; /* Clear SCL */
}
return dat;
}
void I2C_ack()
{
SDA = 0; /* Clear SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SDA = 1; /* Set SDA */
}
void I2C_noack()
{
SDA = 1; /* Set SDA */
I2C_delay();
I2C_clock(); /* Call for send data to i2c bus */
SCL = 1; /* Set SCL */
}
#endif
/***************************
* File name: Seven_Seg_Symbols.h
* Has the numberic equivalents of
* possible 7 segement charecters
* Numbers in table: active high segments, (bit0,bit1..bit6,bit7) correspond to
* segments (a,b..g,h). Many fonts are defined as three horizontal bars to indicate
* that how to define them on 7-segment display is not known
***************************/
#ifndef _SEVEN_SEGEMENT_SYMBOLS_H
#define _SEVEN_SEGEMENT_SYMBOLS_H
//Numbers
#define NUMERIC_ZERO 0x3F
#define NUMERIC_ONE 0x06
#define NUMERIC_TWO 0x5B
#define NUMERIC_THREE 0x4F
#define NUMERIC_FOUR 0x66
#define NUMERIC_FIVE 0x6D
#define NUMERIC_SIX 0x7D
#define NUMERIC_SEVEN 0x07
#define NUMERIC_EIGHT 0x7F
#define NUMERIC_NINE 0x6F
//Aplhabets
//Capitals
//The value is left to 0x49 if not possible
#define CAPITAL_A 0x77
#define SMALL_a 0x5F
#define CAPITAL_B 0x49 //Not possible
#define SMALL_b 0x7C
#define CAPITAL_C 0x39
#define SMALL_c 0x58
#define CAPITAL_D 0x49 //Not possible
#define SMALL_d 0x5E
#define CAPITAL_E 0x79
#define SMALL_e 0x7B
#define CAPITAL_F 0x71
#define SMALL_f 0x71
#define CAPITAL_G 0x7D
#define SMALL_g 0x6F
#define CAPITAL_H 0x76
#define SMALL_h 0x74
#define CAPITAL_I 0x06
#define SMALL_i 0x04
#define CAPITAL_J 0x0E
#define SMALL_j 0x0E
#define CAPITAL_K 0x49 //Not possible
#define SMALL_k 0x49 //Not possible
#define CAPITAL_L 0x38
#define SMALL_l 0x06
#define CAPITAL_M 0x49 //Not possible
#define SMALL_m 0x49 //Not possible
#define CAPITAL_N 0x37
#define SMALL_n 0x54
#define CAPITAL_O 0x3F
#define SMALL_o 0x5C
#define CAPITAL_P 0x73
#define SMALL_p 0x73
#define CAPITAL_Q 0x49 //Not possible
#define SMALL_q 0x67
#define CAPITAL_R 0x49 //Not possible
#define SMALL_r 0x50
#define CAPITAL_S 0x6D
#define SMALL_s 0x6D
#define CAPITAL_T 0x49 //Not possible
#define SMALL_t 0x49 //Not possible
#define CAPITAL_U 0x3E
#define SMALL_u 0x1C
#define CAPITAL_V 0x49 //Not possible
#define SMALL_v 0x49 //Not possible
#define CAPITAL_W 0x49 //Not possible
#define SMALL_w 0x49 //Not possible
#define CAPITAL_X 0x49 //Not possible
#define SMALL_x 0x49 //Not possible
#define CAPITAL_Y 0x6E
#define SMALL_y 0x6E
#define CAPITAL_Z 0x5B
#define SMALL_z 0x5B
#define NUMERIC_NULL 0x49
unsigned char SevenSeg_DisplayNumber(unsigned char Value)
{
switch(Value)
{
case 0x00:
{
return NUMERIC_ZERO;
}
case 0x01:
{
return NUMERIC_ONE;
}
case 0x02:
{
return NUMERIC_TWO;
}
case 0x03:
{
return NUMERIC_THREE;
}
case 0x04:
{
return NUMERIC_FOUR;
}
case 0x05:
{
return NUMERIC_FIVE;
}
case 0x06:
{
return NUMERIC_SIX;
}
case 0x07:
{
return NUMERIC_SEVEN;
}
case 0x08:
{
return NUMERIC_EIGHT;
}
case 0x09:
{
return NUMERIC_NINE;
}
default:
{
return NUMERIC_NULL;
}
}
}
#endif
|
| Topic | Author | Date |
| 89V51 and DS1307 | 01/01/70 00:00 | |
| Convention is no code in .H files | 01/01/70 00:00 | |
| Timer not ticking | 01/01/70 00:00 | |
| Read the data sheet. | 01/01/70 00:00 | |
Nice decomposition. | 01/01/70 00:00 |



