Các pác ơi giúp em với.huhu.Em đang làm đồ án môn học, cụ thể là đồng hồ kèm đo nhiệt độ hiển thị bằng LCD nhưng em thử viết code thì không biết sao không chạy được. Bác nào giúp em với.hic.thứ 6 em phải nộp rồi.huhu
code của em như sau:
#include <AT89X52.H>
#include<stdio.h>
sbit adc_rd=P3^0;
sbit adc_wr=P3^1;
#define adc_data P1;
#define IT0 P3_2
void delay_us(unsigned char n);
void read_0804();
void convert_bcd(unsigned char n);
void display();
void SetTime(void);
void GetTime(void);
void SetDate(void);
void GetDate(void);
void DispTime(void);
void DispDate(void);
void Delay(unsigned int n);
void WaitBusy(void);
void SetupLCD(void);
void WriteCmd(unsigned char n);
void WriteNible(unsigned char n);
void WriteChar(unsigned char n);
void GoToXY(unsigned char row,unsigned char col);
void SetBgOn(void);
void SetBgOff(void);
void GoHome();
void WriteHex(unsigned char n);
void WriteNum(unsigned char n);
void Clearscreen();
extern unsigned char ReadBYTE(unsigned char Addr);
extern void WriteBYTE(unsigned char Addr,unsigned char Data);
extern unsigned char ReadRTC(unsigned char * buff);
extern void WriteRTC(unsigned char * buff);
extern char * Int2Day(unsigned char day);
extern char * Int2Month(unsigned char month);
extern unsigned char BCD2HEX(unsigned int bcd);
extern void loop( unsigned int n);
extern void Start(void);
extern void Stop(void);
extern void InitSerial(void);
unsigned char second,minute,hour,day,date,month,year;
unsigned char RTC_ARR[7]; // Buffer for second,minute,.....,year
unsigned char p;
unsigned char nTick0;
unsigned char temp,tram,chuc,donvi;
void main (void)
{
Delay(10);
Start();
SetupLCD();
nTick0 = 0;
Delay(2);
Clearscreen();
SetBgOn();
/*
second = 56;
minute = 59;
hour = 23;
SetTime();
day = 1;
date = 28;
month = 2;
year = 7;
SetDate();
*/
InitSerial(); // Initialize serial port
//printf("%s",&Int2Day(1));
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = 0x59; // minute = 59
RTC_ARR[2] = 0x23; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x04; // Day = 1 or sunday
RTC_ARR[4] = 0x31; // Date = 30
RTC_ARR[5] = 0x10; // month = August
RTC_ARR[6] = 0x05; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC
while(1)
{
ReadRTC(&RTC_ARR[0]);
putchar(0x0C); // clear Hyper terminal
printf("Day : %s\r\n",Int2Day(RTC_ARR[3]));
printf("Time : %02bX:%02bX:%02bX\r\n",RTC_ARR[2],RTC_ARR[1],RTC_ARR[0]);
printf("Data : %02bX-%s-20%02bX",RTC_ARR[4],Int2Month(RTC_ARR[5]),RTC_ARR[6]);
//
Delay(1000); // delay about 1 second
}
GetTime();
GetDate();
while (1)
{
if (IT0)
{
DispTime();
DispDate();
Delay(20000);
}
if (IT0=!IT0)
{
unsigned int count;
P1=0xFF;
count=0;
while (1)
{
if (count==255)
{
read_0804();
count=0;
}
count++;
display();
}
}
}
}
void SetTime(void)
{
WriteRTC(0x00);
WriteRTC(0x01);
WriteRTC(0x02);
}
void GetTime(void)
{
second = ReadRTC(0x00);
minute = ReadRTC(0x01);
hour = ReadRTC(0x02);
}
void DispTime(void)
{
GoToXY(1,0);
WriteNum(hour);
WriteChar(':');
WriteNum(minute);
WriteChar(':');
WriteNum(second);
}
void SetDate(void)
{
WriteRTC(0x03);
WriteRTC(0x04);
WriteRTC(0x05);
WriteRTC(0x06);
}
void GetDate(void)
{
day = ReadRTC(0x03);
date = ReadRTC(0x04);
month = ReadRTC(0x05);
year = ReadRTC(0x06);
}
void DispDate(void)
{
GoToXY(0,0);
WriteNum(year);
WriteChar('/');
WriteNum(month);
WriteChar('/');
WriteNum(date);
}
//lcd.c
#include <AT89X52.H>
//#define FUNCTION 0x38
#define FUNCTION0 0x20
#define FUNCTION1 0x20
#define FUNCTION2 0x80
//#define CONTROL 0x0c
#define CONTROL0 0x00
#define CONTROL1 0xC0
//#define CLRSCR 0x01
#define CLRSCR0 0x00
#define CLRSCR1 0x10
//#define ENTRYMODE 0x06
#define ENTRYMODE0 0x00
#define ENTRYMODE1 0x60
//#define RETHOME 0x03
#define RETHOME0 0x00
#define RETHOME1 0x30
#define PortData P2
#define BG P1_0
#define RS P1_1
#define RW P1_2
#define E P1_3
void Delay(unsigned int n)
{
unsigned int j;
for(j=0;j<n;j++);
}
void WaitBusy(void)
{
unsigned char i,nLcdBusy;
PortData = (PortData | 0x80);
RS = 0;
RW = 1;
for(i=0;i<50;i++)
{
while(1)
{
E = 1;
Delay(0);
nLcdBusy = (PortData & 0x80);
E = 0;
Delay(0);
if(nLcdBusy == 0)
break;
}
}
}
void WriteCmd(unsigned char n)
{
unsigned char tmp;
WaitBusy();
RS = 0;
RW = 0;
tmp = PortData & 0x0f;
tmp = tmp | (n & 0xf0);
PortData = tmp;
E = 1;
Delay(0);
E = 0;
}
void SetupLCD(void)
{
Delay(150);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
E = 1;
Delay(0);
E = 0;
WaitBusy();
WriteCmd(FUNCTION0);
WriteCmd(FUNCTION1);
WriteCmd(FUNCTION2);
Delay(0);
WriteCmd(CONTROL0);
WriteCmd(CONTROL1);
Delay(0);
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(0);
WriteCmd(ENTRYMODE0);
WriteCmd(ENTRYMODE1);
}
void WriteChar(unsigned char n)
{
unsigned char tmp;
WaitBusy();
RS = 1;
RW = 0;
Delay(0);
tmp = PortData & 0x0f; //store low nible
tmp = tmp | (n & 0xf0); //write high nible
PortData = tmp;
E = 1;
Delay(0);
E = 0;
tmp = PortData & 0x0f; //store low nible
tmp = (tmp | (n & 0x0f) << 4); //write low nible
PortData = tmp;
E = 1;
Delay(0);
E = 0;
}
void WriteNum(unsigned char n)
{
unsigned char digit;
digit = n/100;
WriteChar(digit + '0'); //write tram
digit = n/10;
digit = digit % 10;
WriteChar(digit + '0'); //write chuc
digit = n % 100;
digit = digit % 10;
WriteChar(digit + '0'); //write dvi
}
void WriteNible(unsigned char n)
{
if(n < 10)
{
n = n + 48;
WriteChar(n);
}
else
{
n= n + 55;
WriteChar(n);
}
}
void WriteHex(unsigned char n)
{
unsigned char tbyte;
tbyte = (n & 0xf0) >> 4;
WriteNible(tbyte); //hi nible
tbyte = n & 0x0f;
WriteNible(tbyte); //low nible
}
void ClearScreen()
{
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(100);
}
void GoToXY(unsigned char row,unsigned char col)
{
unsigned char ac,tmp;
ac = 0x80;
ac = ac | ((row & 1) << 6);
ac = ac | (col & 15);
tmp = PortData & 0x0f; //protect low nible
tmp = tmp | (ac & 0xf0);
WriteCmd(tmp); //write high nible
tmp = PortData & 0x0f; //protect low nible
tmp = tmp | ((ac & 0x0f) << 4);
WriteCmd(tmp); //write low nible
}
void GoHome()
{
WriteCmd(RETHOME0);
WriteCmd(RETHOME1);
Delay(100);
}
void SetBgOn(void)
{
BG = 1;
}
void SetBgOff(void)
{
BG = 0;
}
// ds1307.c
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
unsigned char i;
const unsigned char * DayStr[7] = {{"Sun"},
{"Mon"},
{"Tue"},
{"Wed"},
{"Thu"},
{"Fri"},
{"Sat"}};
const unsigned char * MonthStr[12] ={ {"Jan"},
{"Feb"},
{"Mar"},
{"Apr"},
{"May"},
{"Jun"},
{"Jul"},
{"Aug"},
{"Sep"},
{"Oct"},
{"Nov"},
{"Dec"}};
sbit SDA = P2^1; // connect to SDA pin (Data)
sbit SCL = P2^2; // connect to SCL pin (Clock)
void loop( unsigned int n)
{
while(n--);
}
//-------------------------------
// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
tempbcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}
//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
SDA = 1;
SCL = 1;
loop(4);
SDA = 0;
loop(4);
SCL = 0;
loop(4);
}
//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
SDA = 0;
loop(4);
SCL = 1;
loop(4);
SDA = 1;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
loop(4);
SCL = 0;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{
unsigned char Data=0;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
loop(2);
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
loop(4);
SCL = 1;
loop(4);
SCL = 0;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned char Addr)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
Start();
WriteI2C(0xD1);
Data = ReadI2C(NO_ACK);
Stop();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned char Addr,unsigned char Data)
{
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
WriteI2C(Data);
Stop();
}
//-------------------------------
// Read RTC (all real time)
//-------------------------------
unsigned char ReadRTC(unsigned char * buff)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
Stop();
return(Data);
}
//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
Stop();
}
//-------------------------------
// Convert date (BCD) to string of Day
// 1=Sanday
// 2=Monday
// And so on
//-------------------------------
char * Int2Day(unsigned char day)
{
return DayStr[day-1];
}
//-------------------------------
// Convert month (BCD) to string of Month
// 0x01=January
// 0x02=February
// ...........
// 0x12 = December
// And so on
//-------------------------------
char * Int2Month(unsigned char month)
{
return MonthStr[BCD2HEX(month)-1];
}
void InitSerial(void)
{
SCON = 0x52; // setup serial port control
TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ)
TH1 = 0xFD; // TH1
TR1 = 1; // Timer 1 on
}
//adc0804.c
void read_0804()
{
unsigned char temp;
adc_wr=0;
delay_us(2);
adc_wr=1;
delay_us(120);
adc_rd=0;
temp=adc_data;
convert_bcd(temp);
}
void convert_bcd(unsigned char n)
{
temp=n;
tram= temp/100;
temp=temp%100;
chuc=temp/10;
donvi=temp%10;
}
void display()
{
GoToXY(0,0);
WriteNum(tram);
WriteNum(chuc);
WriteNum(donvi);
WriteChar('D');
WriteChar('o');
}
void delay_us(unsigned char n)
{
unsigned char j;
for (j=0;j<n;j++)
{
}
}
Em đính kèm mạch nguyên lý của em.
code của em như sau:
#include <AT89X52.H>
#include<stdio.h>
sbit adc_rd=P3^0;
sbit adc_wr=P3^1;
#define adc_data P1;
#define IT0 P3_2
void delay_us(unsigned char n);
void read_0804();
void convert_bcd(unsigned char n);
void display();
void SetTime(void);
void GetTime(void);
void SetDate(void);
void GetDate(void);
void DispTime(void);
void DispDate(void);
void Delay(unsigned int n);
void WaitBusy(void);
void SetupLCD(void);
void WriteCmd(unsigned char n);
void WriteNible(unsigned char n);
void WriteChar(unsigned char n);
void GoToXY(unsigned char row,unsigned char col);
void SetBgOn(void);
void SetBgOff(void);
void GoHome();
void WriteHex(unsigned char n);
void WriteNum(unsigned char n);
void Clearscreen();
extern unsigned char ReadBYTE(unsigned char Addr);
extern void WriteBYTE(unsigned char Addr,unsigned char Data);
extern unsigned char ReadRTC(unsigned char * buff);
extern void WriteRTC(unsigned char * buff);
extern char * Int2Day(unsigned char day);
extern char * Int2Month(unsigned char month);
extern unsigned char BCD2HEX(unsigned int bcd);
extern void loop( unsigned int n);
extern void Start(void);
extern void Stop(void);
extern void InitSerial(void);
unsigned char second,minute,hour,day,date,month,year;
unsigned char RTC_ARR[7]; // Buffer for second,minute,.....,year
unsigned char p;
unsigned char nTick0;
unsigned char temp,tram,chuc,donvi;
void main (void)
{
Delay(10);
Start();
SetupLCD();
nTick0 = 0;
Delay(2);
Clearscreen();
SetBgOn();
/*
second = 56;
minute = 59;
hour = 23;
SetTime();
day = 1;
date = 28;
month = 2;
year = 7;
SetDate();
*/
InitSerial(); // Initialize serial port
//printf("%s",&Int2Day(1));
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = 0x59; // minute = 59
RTC_ARR[2] = 0x23; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x04; // Day = 1 or sunday
RTC_ARR[4] = 0x31; // Date = 30
RTC_ARR[5] = 0x10; // month = August
RTC_ARR[6] = 0x05; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC
while(1)
{
ReadRTC(&RTC_ARR[0]);
putchar(0x0C); // clear Hyper terminal
printf("Day : %s\r\n",Int2Day(RTC_ARR[3]));
printf("Time : %02bX:%02bX:%02bX\r\n",RTC_ARR[2],RTC_ARR[1],RTC_ARR[0]);
printf("Data : %02bX-%s-20%02bX",RTC_ARR[4],Int2Month(RTC_ARR[5]),RTC_ARR[6]);
//
Delay(1000); // delay about 1 second
}
GetTime();
GetDate();
while (1)
{
if (IT0)
{
DispTime();
DispDate();
Delay(20000);
}
if (IT0=!IT0)
{
unsigned int count;
P1=0xFF;
count=0;
while (1)
{
if (count==255)
{
read_0804();
count=0;
}
count++;
display();
}
}
}
}
void SetTime(void)
{
WriteRTC(0x00);
WriteRTC(0x01);
WriteRTC(0x02);
}
void GetTime(void)
{
second = ReadRTC(0x00);
minute = ReadRTC(0x01);
hour = ReadRTC(0x02);
}
void DispTime(void)
{
GoToXY(1,0);
WriteNum(hour);
WriteChar(':');
WriteNum(minute);
WriteChar(':');
WriteNum(second);
}
void SetDate(void)
{
WriteRTC(0x03);
WriteRTC(0x04);
WriteRTC(0x05);
WriteRTC(0x06);
}
void GetDate(void)
{
day = ReadRTC(0x03);
date = ReadRTC(0x04);
month = ReadRTC(0x05);
year = ReadRTC(0x06);
}
void DispDate(void)
{
GoToXY(0,0);
WriteNum(year);
WriteChar('/');
WriteNum(month);
WriteChar('/');
WriteNum(date);
}
//lcd.c
#include <AT89X52.H>
//#define FUNCTION 0x38
#define FUNCTION0 0x20
#define FUNCTION1 0x20
#define FUNCTION2 0x80
//#define CONTROL 0x0c
#define CONTROL0 0x00
#define CONTROL1 0xC0
//#define CLRSCR 0x01
#define CLRSCR0 0x00
#define CLRSCR1 0x10
//#define ENTRYMODE 0x06
#define ENTRYMODE0 0x00
#define ENTRYMODE1 0x60
//#define RETHOME 0x03
#define RETHOME0 0x00
#define RETHOME1 0x30
#define PortData P2
#define BG P1_0
#define RS P1_1
#define RW P1_2
#define E P1_3
void Delay(unsigned int n)
{
unsigned int j;
for(j=0;j<n;j++);
}
void WaitBusy(void)
{
unsigned char i,nLcdBusy;
PortData = (PortData | 0x80);
RS = 0;
RW = 1;
for(i=0;i<50;i++)
{
while(1)
{
E = 1;
Delay(0);
nLcdBusy = (PortData & 0x80);
E = 0;
Delay(0);
if(nLcdBusy == 0)
break;
}
}
}
void WriteCmd(unsigned char n)
{
unsigned char tmp;
WaitBusy();
RS = 0;
RW = 0;
tmp = PortData & 0x0f;
tmp = tmp | (n & 0xf0);
PortData = tmp;
E = 1;
Delay(0);
E = 0;
}
void SetupLCD(void)
{
Delay(150);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
WriteCmd(0x10);
WriteCmd(0xe0);
Delay(5);
E = 1;
Delay(0);
E = 0;
WaitBusy();
WriteCmd(FUNCTION0);
WriteCmd(FUNCTION1);
WriteCmd(FUNCTION2);
Delay(0);
WriteCmd(CONTROL0);
WriteCmd(CONTROL1);
Delay(0);
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(0);
WriteCmd(ENTRYMODE0);
WriteCmd(ENTRYMODE1);
}
void WriteChar(unsigned char n)
{
unsigned char tmp;
WaitBusy();
RS = 1;
RW = 0;
Delay(0);
tmp = PortData & 0x0f; //store low nible
tmp = tmp | (n & 0xf0); //write high nible
PortData = tmp;
E = 1;
Delay(0);
E = 0;
tmp = PortData & 0x0f; //store low nible
tmp = (tmp | (n & 0x0f) << 4); //write low nible
PortData = tmp;
E = 1;
Delay(0);
E = 0;
}
void WriteNum(unsigned char n)
{
unsigned char digit;
digit = n/100;
WriteChar(digit + '0'); //write tram
digit = n/10;
digit = digit % 10;
WriteChar(digit + '0'); //write chuc
digit = n % 100;
digit = digit % 10;
WriteChar(digit + '0'); //write dvi
}
void WriteNible(unsigned char n)
{
if(n < 10)
{
n = n + 48;
WriteChar(n);
}
else
{
n= n + 55;
WriteChar(n);
}
}
void WriteHex(unsigned char n)
{
unsigned char tbyte;
tbyte = (n & 0xf0) >> 4;
WriteNible(tbyte); //hi nible
tbyte = n & 0x0f;
WriteNible(tbyte); //low nible
}
void ClearScreen()
{
WriteCmd(CLRSCR0);
WriteCmd(CLRSCR1);
Delay(100);
}
void GoToXY(unsigned char row,unsigned char col)
{
unsigned char ac,tmp;
ac = 0x80;
ac = ac | ((row & 1) << 6);
ac = ac | (col & 15);
tmp = PortData & 0x0f; //protect low nible
tmp = tmp | (ac & 0xf0);
WriteCmd(tmp); //write high nible
tmp = PortData & 0x0f; //protect low nible
tmp = tmp | ((ac & 0x0f) << 4);
WriteCmd(tmp); //write low nible
}
void GoHome()
{
WriteCmd(RETHOME0);
WriteCmd(RETHOME1);
Delay(100);
}
void SetBgOn(void)
{
BG = 1;
}
void SetBgOff(void)
{
BG = 0;
}
// ds1307.c
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
unsigned char i;
const unsigned char * DayStr[7] = {{"Sun"},
{"Mon"},
{"Tue"},
{"Wed"},
{"Thu"},
{"Fri"},
{"Sat"}};
const unsigned char * MonthStr[12] ={ {"Jan"},
{"Feb"},
{"Mar"},
{"Apr"},
{"May"},
{"Jun"},
{"Jul"},
{"Aug"},
{"Sep"},
{"Oct"},
{"Nov"},
{"Dec"}};
sbit SDA = P2^1; // connect to SDA pin (Data)
sbit SCL = P2^2; // connect to SCL pin (Clock)
void loop( unsigned int n)
{
while(n--);
}
//-------------------------------
// Convert BCD 1 byte to HEX 1 byte
//-------------------------------
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
tempbcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}
//-------------------------------
// start I2C
//-------------------------------
void Start(void)
{
SDA = 1;
SCL = 1;
loop(4);
SDA = 0;
loop(4);
SCL = 0;
loop(4);
}
//-------------------------------
// stop I2C
//-------------------------------
void Stop(void)
{
SDA = 0;
loop(4);
SCL = 1;
loop(4);
SDA = 1;
}
//-------------------------------
// Write I2C
//-------------------------------
void WriteI2C(unsigned char Data)
{
for (i=0;i<8;i++)
{
SDA = (Data & 0x80) ? 1:0;
SCL=1;SCL=0;
Data<<=1;
}
SCL = 1;
loop(4);
SCL = 0;
}
//-------------------------------
// Read I2C
//-------------------------------
unsigned char ReadI2C(bit ACK_Bit)
{
unsigned char Data=0;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
loop(2);
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
loop(4);
SCL = 1;
loop(4);
SCL = 0;
return Data;
}
//-------------------------------
// Read 1 byte form I2C
//-------------------------------
unsigned char ReadBYTE(unsigned char Addr)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
Start();
WriteI2C(0xD1);
Data = ReadI2C(NO_ACK);
Stop();
return(Data);
}
//-------------------------------
// Write 1 byte to I2C
//-------------------------------
void WriteBYTE(unsigned char Addr,unsigned char Data)
{
Start();
WriteI2C(0xD0);
WriteI2C(Addr);
WriteI2C(Data);
Stop();
}
//-------------------------------
// Read RTC (all real time)
//-------------------------------
unsigned char ReadRTC(unsigned char * buff)
{
unsigned char Data;
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
Start();
WriteI2C(0xD1);
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
Stop();
return(Data);
}
//-------------------------------
// Write RTC
//-------------------------------
void WriteRTC(unsigned char *buff)
{
Start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
Stop();
}
//-------------------------------
// Convert date (BCD) to string of Day
// 1=Sanday
// 2=Monday
// And so on
//-------------------------------
char * Int2Day(unsigned char day)
{
return DayStr[day-1];
}
//-------------------------------
// Convert month (BCD) to string of Month
// 0x01=January
// 0x02=February
// ...........
// 0x12 = December
// And so on
//-------------------------------
char * Int2Month(unsigned char month)
{
return MonthStr[BCD2HEX(month)-1];
}
void InitSerial(void)
{
SCON = 0x52; // setup serial port control
TMOD = 0x20; // hardware (9600 BAUD @11.05592MHZ)
TH1 = 0xFD; // TH1
TR1 = 1; // Timer 1 on
}
//adc0804.c
void read_0804()
{
unsigned char temp;
adc_wr=0;
delay_us(2);
adc_wr=1;
delay_us(120);
adc_rd=0;
temp=adc_data;
convert_bcd(temp);
}
void convert_bcd(unsigned char n)
{
temp=n;
tram= temp/100;
temp=temp%100;
chuc=temp/10;
donvi=temp%10;
}
void display()
{
GoToXY(0,0);
WriteNum(tram);
WriteNum(chuc);
WriteNum(donvi);
WriteChar('D');
WriteChar('o');
}
void delay_us(unsigned char n)
{
unsigned char j;
for (j=0;j<n;j++)
{
}
}
Em đính kèm mạch nguyên lý của em.
Comment