Bác nào xem giúp em đoạn code sai ở chổ nào ạ? Mạch mô phỏng không chạy được.
Mạch đồng hồ sử dụng PIC 16F877A + DS1307 + LCD 16x2
code::
//************************************************** ********************************
// Khoi chuong trinh giao tiep I2C
// Xoa co ngat
void I2C_wait()
{
while(0 == SSPIF);
SSPIF = 0;
}
// Ham tao dieu kien START
void I2C_start()
{
SEN = 1;
__delay_us(2);
I2C_wait();
}
// Ham tao dieu kien STOP
void I2C_stop()
{
PEN = 1;
__delay_us(2);
I2C_wait();
}
// HAM KHOI TAO I2C
void I2C_init()
{
SDA = 1;
SCL = 1; // dat cac chan SDA va SCL len cao
TRISC3 = 1;
TRISC4 = 1; // CHE DOloi vao VDK
SSPCON = 0x28;
SSPSTAT = 0x3f;
SSPADD = 0x31; // f= F0sc/(4*(sspadd+1)) nen =49 dk 100kHz
}
void RESTARTBit(void) {
RSEN = 1; //gui bit restart
__delay_us(2);
I2C_wait();
}
// Ham doc 1 byte data
unsigned char I2C_read(unsigned char ack)
{
unsigned char bcd, bcd1, bcd2;
while(0 == R);
R = 0;
RCEN =1;
I2C_wait();
ACKDT = ack;
I2C_wait();
ACKEN = 1;
// doi tu BCD sang Decema
bcd = SSPBUF;
bcd1 = (bcd/16)*10;
bcd2 = bcd &0x0f;
return (bcd1+bcd2);
}
// Ham ghi 1 byte data
unsigned char I2C_write(unsigned char dat)
{
unsigned char temp;
//HEX to BCD
temp = dat ;
dat = (((dat/10)*16)|(temp %10));
SSPBUF = dat;
while(BF = 1);
I2C_wait();
}
// HAM doc nhieu byte len DS1307
unsigned char DS1307_read()
{
RCEN =1;
I2C_start();
I2C_write(0xd0); // d0 = dia chi cua ds + bit write , connect voi ds
I2C_write(0x00);
RESTARTBit();
I2C_write(0xd1); // cho phep doc data
second = I2C_read(0);
min = I2C_read(0);
hour = I2C_read(0);
day = I2C_read(0);
date = I2C_read(0);
month = I2C_read(0);
year = I2C_read(1);
I2C_stop();
}
// Ham ghi nhieu byte vao ds1307
void DS1307_write(unsigned char addr, unsigned char dat)
{
I2C_start();
I2C_write(0xd0); // ket noi voi ds1307
I2C_write(addr);
I2C_write(dat);
I2C_stop();
}
//************************************************** *****************
// Ham Main
void main()
{
unsigned char CH, str[18], kieu_gio;
khoitao();
I2C_init();
ghi_lenh(0xc0);
ghi_lenh(0x80);
// thiet lap che do 24h tu 00:23
kieu_gio = hour;
DS1307_write(0x02, kieu_gio & 0x3f);
DS1307_write(0x01, min);
// kiem tra bit CH
CH = second;
while((CH & 0x10)==1)
{
DS1307_write(0x00, CH & 0x7f);
}
DS1307_write(0x03, day);
DS1307_write(0x04, date);
DS1307_write(0x05, month);
DS1307_write(0x06, year);
while(1)
{
ghi_lenh(0x80);
sprintf(str, " %d:%d:%d", hour, min, second);
chuoi(str);
ghi_lenh(0xc0);
sprintf(str, "%d:%d-%d-%d", day, date, month, year);
chuoi(str);
__delay_us(10);
}
}
Mạch đồng hồ sử dụng PIC 16F877A + DS1307 + LCD 16x2
code::
//************************************************** ********************************
// Khoi chuong trinh giao tiep I2C
// Xoa co ngat
void I2C_wait()
{
while(0 == SSPIF);
SSPIF = 0;
}
// Ham tao dieu kien START
void I2C_start()
{
SEN = 1;
__delay_us(2);
I2C_wait();
}
// Ham tao dieu kien STOP
void I2C_stop()
{
PEN = 1;
__delay_us(2);
I2C_wait();
}
// HAM KHOI TAO I2C
void I2C_init()
{
SDA = 1;
SCL = 1; // dat cac chan SDA va SCL len cao
TRISC3 = 1;
TRISC4 = 1; // CHE DOloi vao VDK
SSPCON = 0x28;
SSPSTAT = 0x3f;
SSPADD = 0x31; // f= F0sc/(4*(sspadd+1)) nen =49 dk 100kHz
}
void RESTARTBit(void) {
RSEN = 1; //gui bit restart
__delay_us(2);
I2C_wait();
}
// Ham doc 1 byte data
unsigned char I2C_read(unsigned char ack)
{
unsigned char bcd, bcd1, bcd2;
while(0 == R);
R = 0;
RCEN =1;
I2C_wait();
ACKDT = ack;
I2C_wait();
ACKEN = 1;
// doi tu BCD sang Decema
bcd = SSPBUF;
bcd1 = (bcd/16)*10;
bcd2 = bcd &0x0f;
return (bcd1+bcd2);
}
// Ham ghi 1 byte data
unsigned char I2C_write(unsigned char dat)
{
unsigned char temp;
//HEX to BCD
temp = dat ;
dat = (((dat/10)*16)|(temp %10));
SSPBUF = dat;
while(BF = 1);
I2C_wait();
}
// HAM doc nhieu byte len DS1307
unsigned char DS1307_read()
{
RCEN =1;
I2C_start();
I2C_write(0xd0); // d0 = dia chi cua ds + bit write , connect voi ds
I2C_write(0x00);
RESTARTBit();
I2C_write(0xd1); // cho phep doc data
second = I2C_read(0);
min = I2C_read(0);
hour = I2C_read(0);
day = I2C_read(0);
date = I2C_read(0);
month = I2C_read(0);
year = I2C_read(1);
I2C_stop();
}
// Ham ghi nhieu byte vao ds1307
void DS1307_write(unsigned char addr, unsigned char dat)
{
I2C_start();
I2C_write(0xd0); // ket noi voi ds1307
I2C_write(addr);
I2C_write(dat);
I2C_stop();
}
//************************************************** *****************
// Ham Main
void main()
{
unsigned char CH, str[18], kieu_gio;
khoitao();
I2C_init();
ghi_lenh(0xc0);
ghi_lenh(0x80);
// thiet lap che do 24h tu 00:23
kieu_gio = hour;
DS1307_write(0x02, kieu_gio & 0x3f);
DS1307_write(0x01, min);
// kiem tra bit CH
CH = second;
while((CH & 0x10)==1)
{
DS1307_write(0x00, CH & 0x7f);
}
DS1307_write(0x03, day);
DS1307_write(0x04, date);
DS1307_write(0x05, month);
DS1307_write(0x06, year);
while(1)
{
ghi_lenh(0x80);
sprintf(str, " %d:%d:%d", hour, min, second);
chuoi(str);
ghi_lenh(0xc0);
sprintf(str, "%d:%d-%d-%d", day, date, month, year);
chuoi(str);
__delay_us(10);
}
}
Comment