Chào mọi người. Mình đang làm đồ án về đồng hồ thời gian thực (Đồng hồ vạn niên có cảm biến nhiệt độ.). Nói thực là mình nhờ bạn mình lập trình cho. giờ mạch bị hỏng con VĐK nên mất hết không có file HEX. Còn file .C giờ không thể dịch được. Ai có thể giúp mình được không. Mình xin hậu tạ.
code
code 2
code
Code:
#include <at89x52.h> #include <stdio.h> #include <INTRINS.H> #include <ds1307.c> sbit SET = P3^3; sbit DOWN = P3^2; sbit UP = P3^1; sbit MODE = P3^0; sbit WRADC = P0^7; // 0 1 2 3 4 5 6 7 8 9 - off unsigned char code ma_bcd[14] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF,0xFF}; // Common Anode unsigned char hour, min, sec, day, date, mon, year, adate, amon; unsigned char temp,i,j,chedo, mode, et1,x,dem; unsigned int tempx; bit bit_nhay; unsigned char ledData[8]; unsigned char code ledPort[8] = {0xFE,0xFD,0xFB,0xF7,0x7F,0xBF,0xDF,0xEF}; unsigned char code dayOfMon[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; unsigned char code numMonAl[37] = {0x01,0x12,0x03,0x14,0x05,0x06,0x17,0x08,0x19,0x0A,0x1B,0x0C,0x11,0x02,0x13,0x04,0x15,0x06,0x17,0x08,0x09,0x19,0x0A,0x1B,0x0C,0x11,0x02,0x13,0x14,0x05,0x16,0x07,0x08,0x09,0x1A,0x0B,0x1C}; void delay(unsigned int d); void quetLed(void); void scanKey(void); void chonCheDo(void); // tao tre void delay(unsigned int d) { while(d--); } unsigned char readAdc(void) { WRADC = 0; P2 = 0xFF; delay(100); return(P2); } // Quet LED void quetLed(void) { for(j=0; j<20; j++) { for(i=0;i<8;i++) { P1 = ledPort[i]; // Cho phep led1 P0 = ledData[i]; // Hien thi chuc gio delay(10); // Tre 10 chu ki P1 = 0xFF; // tat Led P0 = 0XFF; // tat led } } } unsigned int soNgayDl(unsigned char dd,unsigned char mm,unsigned char yy) { unsigned int ddate=0; for(i=13;i<yy;i++) { ddate+=365; if(i%4==0)ddate++; } for(j=1;j<mm;j++) { ddate += (unsigned int) (dayOfMon[j-1]); if((i%4==0)&(j==2))ddate++; // Nam nhuan } ddate += dd; return (ddate); } // Tinh am lich void amLich(void) { unsigned int numDateDl,sttAmon,dateAl; unsigned char i; numDateDl = soNgayDl(date,mon,year)-40; sttAmon = (numDateDl*2)/59; amon = numMonAl[sttAmon]&0x0F; // Thang Al dateAl = numDateDl; for(i=0;i<sttAmon;i++) { if((numMonAl[i]&0x10)==0x10) dateAl -= 30; else dateAl -= 29; } adate = numDateDl - ((sttAmon*59)/2); // Ngay Al } // chon che do -- dua du lieu ra bo dem hien thi void chonCheDo(void) { if(mode == 0) { rtcReadTime(&hour,&min,&sec); rtcReadDate(&date,&mon,&year); if((hour==0)&(min==0)&(sec<3))amLich(); } if(chedo==1) { ledData[0] = ma_bcd[hour/10]; ledData[1] = ma_bcd[hour%10]; ledData[2] = ma_bcd[min/10]; ledData[3] = ma_bcd[min%10]; ledData[4] = ma_bcd[sec/10]; ledData[5] = ma_bcd[sec%10]; ledData[6] = 0xFF; ledData[7] = 0xFF; } if(chedo==2) { ledData[0] = ma_bcd[date/10]; ledData[1] = ma_bcd[date%10]; ledData[2] = ma_bcd[mon/10]; ledData[3] = ma_bcd[mon%10]; ledData[4] = ma_bcd[2]; ledData[5] = ma_bcd[0]; ledData[6] = ma_bcd[year/10]; ledData[7] = ma_bcd[year%10]; } if(chedo==3) { tempx = readAdc()/2; ledData[0] = ma_bcd[adate/10]; ledData[1] = ma_bcd[adate%10]; ledData[2] = ma_bcd[amon/10]; ledData[3] = ma_bcd[amon%10]; ledData[4] = ma_bcd[temp/10]; ledData[5] = ma_bcd[temp%10]; ledData[6] = 0x9c; // o ledData[7] = 0xC6; // C } if (bit_nhay&(mode>0)) { switch (mode) { case 1: ledData[0] = ledData[1] = 0xFF; break; case 2: ledData[2] = ledData[3] = 0xFF; break; case 3: ledData[4] = ledData[5] = 0xFF; break; case 4: ledData[0] = ledData[1] = 0xFF; break; case 5: ledData[2] = ledData[3] = 0xFF; break; case 6: ledData[6] = ledData[7] = 0xFF; break; } } } // quet phim void scanKey(void) { if (MODE == 0) // Nhan phim MODE { while(!MODE)quetLed(); // Cho nha phim mode++; chedo = 1; if(mode>3)chedo = 2; if (mode > 6) { mode = 0; chedo = 1; rtcWriteTime(hour,min,sec); rtcWriteDate(date,mon,year); } } if (mode != 0) { if(UP == 0) // Nhan phim UP { dem=0; while(!UP)quetLed();// Cho nha phim switch (mode) { case 1: hour++; if (hour > 23)hour = 0; break; case 2: min++; if (min > 59)min = 0; break; case 3: sec++; if (sec > 59)sec = 0; break; case 4: date++; if (date > 31)date = 1; break; case 5: mon++; if (mon > 12)mon = 1; break; case 6: year++; if (year > 99)year = 0; break; } } if(DOWN == 0) // Nhan phim Down { dem=0; while(!DOWN)quetLed(); // Cho nha phim switch (mode) { case 1: if (hour == 0)hour = 23; else hour --; break; case 2: if (min == 0)min = 59; else min --; break; case 3: if (sec == 0)sec = 59; else sec--; break; case 4: if (date == 1)date = 31; else date --; break; case 5: if (mon == 1)mon = 12; else mon --; break; case 6: if (year == 0)year = 99; else year--; break; } } } if (SET == 0) { // Nhan phim set while(!SET)quetLed(); // Cho nha phim mode = 0; // Thoat che do dat thoi gian rtcWriteTime(hour,min,sec); rtcWriteDate(date,mon,year); } } void Ngat_T1(void) interrupt 3 { TR1 = 0; TF1 = 0; TH1 = -50000/256; TL1 = -50000%256; et1 ++; if ((et1 >= 2)& (mode != 0)) { et1 = 0; bit_nhay = ~bit_nhay; } else if(et>5) { if(chedo<3)chedo++; else chedo = 1; } TR1 = 1; } // Chuong trinh chinh void main() { TMOD = 0x11; // Dat Timer1 che do 1 ET1 = 1; // Cho phep ngat Timer1 EA = 1; // Cho phep ngat toan bo mode = 0; // Cam che do dieu khien rtcInit(); chedo = 1; // Chon che do hien thi gio rtcReadTime(&hour,&min,&sec); rtcReadDate(&date,&mon,&year); amLich(); TR1 = 1; // Cho phep Timer1 hoat dong while(1) { if(mode==0)chonCheDo(); scanKey(); quetLed(); } }
Code:
#include <intrins.h> // Dinh nghia cong cho I2C #define SDA P3_6 #define SCL P3_7 #define CLK {SCL=1;_nop_();SCL=0;_nop_();} #define ACK {SDA=0;CLK;} #define NO_ACK {SDA=1;CLK;} #define I2C_START {SDA=1;SCL=1;SDA=0;SCL=0;SDA = 1;} // Marco #define I2C_STOP {SDA=0;SCL=1;SDA=1;} // Marco void I2C_WR(unsigned char c) { char i; for (i=7;i>=0;i--) { SDA= (c>>i) & 0x01; CLK; } CLK; } unsigned char I2C_RD(bit ack) { unsigned char c=0; bit a; char i; for (i=7;i>=0;i--) { SDA=1; SCL=1; c=c<<1; if (SDA) c++; SCL=0; } SDA=ack; CLK; return (c); } // Thu - Ngay Thang Nam - Gio Phut Giay void rtcInit() { I2C_START; I2C_WR(0xd0); I2C_WR(0x07); //vi tri con tro RTC I2C_WR(0x90); I2C_STOP; } int bin2bcd (int x) { return (x%10) | ((x/10) << 4); } int bcd2bin (int x) { return (x >> 4) * 10 + (x & 0x0f); } void rtcWriteTime(char hour,char min,char sec) { I2C_START; I2C_WR(0xd0); I2C_WR(0x00); //vi tri con tro RTC I2C_WR(bin2bcd(sec)); //giay I2C_WR(bin2bcd(min)); I2C_WR(bin2bcd(hour)); I2C_STOP; } void rtcWriteDate(char date, char mon, char year) { I2C_START; I2C_WR(0xd0); I2C_WR(0x04); //vi tri con tro RTC I2C_WR(bin2bcd(date)); I2C_WR(bin2bcd(mon)); I2C_WR(bin2bcd(year)); I2C_STOP; } void rtcReadTime(char *hour,char *min,char *sec) { I2C_START; I2C_WR(0xd0); I2C_WR(0x00); //vi tri con tro RTC I2C_START; I2C_WR(0xd1); *sec=bcd2bin(I2C_RD(0)); *min=bcd2bin(I2C_RD(0)); *hour=bcd2bin(I2C_RD(1)); I2C_STOP; } void rtcReadDate(char *date,char *mon,char *year) { I2C_START; I2C_WR(0xd0); I2C_WR(0x04); //vi tri con tro RTC I2C_START; I2C_WR(0xd1); *date=bcd2bin(I2C_RD(0)); *mon=bcd2bin(I2C_RD(0)); *year=bcd2bin(I2C_RD(1)); I2C_STOP; }
Comment