#include <reg52.h>
#include <intrins.h>
#include <string.h>
// LCD1602????
sbit LCD_RS = P1^0;
sbit LCD_RW = P1^1;
sbit LCD_EN = P1^2;
#define LCD_DATA P2
// DS1302????
sbit DS1302_RST = P3^5;
sbit DS1302_IO = P3^6;
sbit DS1302_SCLK = P3^7;
// DS18B20????
sbit DS18B20 = P1^3;
// ????
sbit KEY_SET = P3^0;
sbit KEY_UP = P3^1;
sbit KEY_DOWN = P3^2;
sbit KEY_MODE = P3^3;
// ????????
sbit MOTOR_A = P0^0;
sbit MOTOR_B = P0^1;
sbit MOTOR_C = P0^2;
sbit MOTOR_D = P0^3;
// ????
unsigned char time_data[7]; // ?,?,?,?,?,?,?
unsigned char alarm_hour = 7, alarm_min = 0; // ????
unsigned int stopwatch_ms = 0; // ?????
unsigned int timer_sec = 0; // ?????
unsigned int temperature; // ???
unsigned char display_mode = 0; // 0:???? 1:???? 2:?? 3:????
bit alarm_enabled = 0; // ??????
bit alarm_triggered = 0; // ??????
/******************** LCD1602???? ********************/
void LCD_Delay(unsigned int t) {
while(t--);
}
void LCD_Write_Cmd(unsigned char cmd) {
LCD_RS = 0;
LCD_RW = 0;
LCD_DATA = cmd;
LCD_EN = 1;
LCD_Delay(5);
LCD_EN = 0;
}
void LCD_Write_Data(unsigned char dat) {
LCD_RS = 1;
LCD_RW = 0;
LCD_DATA = dat;
LCD_EN = 1;
LCD_Delay(5);
LCD_EN = 0;
}
void LCD_Init() {
LCD_Write_Cmd(0x38); // 8?????,2???,5x8??
LCD_Write_Cmd(0x0C); // ???,???
LCD_Write_Cmd(0x06); // ???????
LCD_Write_Cmd(0x01); // ??
}
void LCD_Set_Cursor(unsigned char x, unsigned char y) {
unsigned char addr;
if (y == 0) addr = 0x80 + x;
else addr = 0xC0 + x;
LCD_Write_Cmd(addr);
}
void LCD_Write_String(unsigned char x, unsigned char y, char *str) {
LCD_Set_Cursor(x, y);
while (*str) {
LCD_Write_Data(*str++);
}
}
/******************** DS1302???? ********************/
void DS1302_Write_Byte(unsigned char dat) {
unsigned char i;
for(i=0; i<8; i++) {
DS1302_IO = dat & 0x01;
DS1302_SCLK = 1;
_nop_();
DS1302_SCLK = 0;
dat >>= 1;
}
}
unsigned char DS1302_Read_Byte() {
unsigned char i, dat = 0;
for(i=0; i<8; i++) {
dat >>= 1;
if(DS1302_IO) dat |= 0x80;
DS1302_SCLK = 1;
_nop_();
DS1302_SCLK = 0;
}
return dat;
}
void DS1302_Write(unsigned char addr, unsigned char dat) {
DS1302_RST = 0;
_nop_();
DS1302_SCLK = 0;
_nop_();
DS1302_RST = 1;
_nop_();
DS1302_Write_Byte(addr);
DS1302_Write_Byte(dat);
DS1302_SCLK = 1;
DS1302_RST = 0;
}
unsigned char DS1302_Read(unsigned char addr) {
unsigned char dat;
DS1302_RST = 0;
_nop_();
DS1302_SCLK = 0;
_nop_();
DS1302_RST = 1;
_nop_();
DS1302_Write_Byte(addr | 0x01);
dat = DS1302_Read_Byte();
DS1302_SCLK = 1;
DS1302_RST = 0;
return dat;
}
void DS1302_Init() {
DS1302_Write(0x8E, 0x00); // ?????
DS1302_Write(0x80, 0x00); // ????
DS1302_Write(0x82, 0x30); // ????
DS1302_Write(0x84, 0x12); // ????
DS1302_Write(0x8E, 0x80); // ?????
}
void DS1302_Read_Time() {
time_data[0] = DS1302_Read(0x81); // ?
time_data[1] = DS1302_Read(0x83); // ?
time_data[2] = DS1302_Read(0x85); // ?
time_data[3] = DS1302_Read(0x87); // ?
time_data[4] = DS1302_Read(0x89); // ?
time_data[5] = DS1302_Read(0x8B); // ?
time_data[6] = DS1302_Read(0x8D); // ?
}
/******************** DS18B20???? ********************/
void DS18B20_Delay(unsigned int t) {
while(t--);
}
bit DS18B20_Init() {
bit flag;
DS18B20 = 1;
DS18B20_Delay(8);
DS18B20 = 0;
DS18B20_Delay(80);
DS18B20 = 1;
DS18B20_Delay(14);
flag = DS18B20;
DS18B20_Delay(20);
return flag;
}
void DS18B20_Write_Byte(unsigned char dat) {
unsigned char i;
for(i=0; i<8; i++) {
DS18B20 = 0;
DS18B20_Delay(2);
DS18B20 = dat & 0x01;
DS18B20_Delay(30);
DS18B20 = 1;
dat >>= 1;
}
}
unsigned char DS18B20_Read_Byte() {
unsigned char i, dat = 0;
for(i=0; i<8; i++) {
dat >>= 1;
DS18B20 = 0;
DS18B20_Delay(2);
DS18B20 = 1;
DS18B20_Delay(4);
if(DS18B20) dat |= 0x80;
DS18B20_Delay(30);
}
return dat;
}
void DS18B20_Read_Temp() {
if(DS18B20_Init()) return;
DS18B20_Write_Byte(0xCC); // Skip ROM
DS18B20_Write_Byte(0x44); // Convert T
while(!DS18B20);
if(DS18B20_Init()) return;
DS18B20_Write_Byte(0xCC); // Skip ROM
DS18B20_Write_Byte(0xBE); // Read Scratchpad
unsigned char LSB = DS18B20_Read_Byte();
unsigned char MSB = DS18B20_Read_Byte();
temperature = (MSB << 8) | LSB;
temperature = (temperature * 0.0625) * 10; // ??????????
}
/******************** ???????? ********************/
void Motor_Step(unsigned char step) {
switch(step % 4) {
case 0: MOTOR_A=1; MOTOR_B=0; MOTOR_C=0; MOTOR_D=0; break;
case 1: MOTOR_A=0; MOTOR_B=1; MOTOR_C=0; MOTOR_D=0; break;
case 2: MOTOR_A=0; MOTOR_B=0; MOTOR_C=1; MOTOR_D=0; break;
case 3: MOTOR_A=0; MOTOR_B=0; MOTOR_C=0; MOTOR_D=1; break;
}
}
void Motor_Alarm() {
unsigned char i;
for(i=0; i<100; i++) {
Motor_Step(i);
DS18B20_Delay(5000);
}
}
/******************** ?????? ********************/
unsigned char Key_Scan() {
if(KEY_SET == 0) { DS18B20_Delay(1000); return 1; } // ???
if(KEY_UP == 0) { DS18B20_Delay(1000); return 2; } // ???
if(KEY_DOWN == 0) { DS18B20_Delay(1000); return 3; } // ???
if(KEY_MODE == 0) { DS18B20_Delay(1000); return 4; } // ???
return 0;
}
/******************** ?????? ********************/
bit Is_Leap_Year(unsigned char year) {
unsigned int full_year = 2000 + year;
return ((full_year % 4 == 0) && (full_year % 100 != 0)) || (full_year % 400 == 0);
}
/******************** ???? ********************/
void Display_DateTime() {
char buf[16];
// ????
sprintf(buf, "20%02d-%02d-%02d", time_data[6], time_data[4], time_data[3]);
LCD_Write_String(0, 0, buf);
// ????
sprintf(buf, "%02d:%02d:%02d", time_data[2] & 0x3F, time_data[1] & 0x7F, time_data[0] & 0x7F);
LCD_Write_String(0, 1, buf);
// ??????
if(alarm_enabled) LCD_Write_String(13, 1, "A");
}
void Display_Timer() {
char buf[16];
unsigned int hours = timer_sec / 3600;
unsigned int minutes = (timer_sec % 3600) / 60;
unsigned int seconds = timer_sec % 60;
sprintf(buf, "Timer:%02d:%02d:%02d", hours, minutes, seconds);
LCD_Write_String(0, 0, buf);
LCD_Write_String(0, 1, "Press SET to start");
}
void Display_Stopwatch() {
char buf[16];
unsigned int seconds = stopwatch_ms / 1000;
unsigned int ms = (stopwatch_ms % 1000) / 100;
sprintf(buf, "Stopwatch:%02d.%1d", seconds, ms);
LCD_Write_String(0, 0, buf);
LCD_Write_String(0, 1, "Press SET to reset");
}
void Display_All() {
char buf[16];
// ???: ?????
sprintf(buf, "20%02d-%02d-%02d %02d:%02d",
time_data[6], time_data[4], time_data[3],
time_data[2] & 0x3F, time_data[1] & 0x7F);
LCD_Write_String(0, 0, buf);
// ???: ????????
sprintf(buf, "%2d.%1dC %c %02d.%1d",
temperature / 10, temperature % 10,
alarm_enabled ? 'A' : ' ',
stopwatch_ms / 1000, (stopwatch_ms % 1000) / 100);
LCD_Write_String(0, 1, buf);
}
void Display_Mode() {
LCD_Write_Cmd(0x01); // ??
switch(display_mode) {
case 0: Display_DateTime(); break;
case 1: Display_Timer(); break;
case 2: Display_Stopwatch(); break;
case 3: Display_All(); break;
}
}
/******************** ??????? ********************/
void Timer0_Init() {
TMOD = 0x01; // ???0??1
TH0 = 0xFC; // 1ms??
TL0 = 0x18;
ET0 = 1; // ????0??
EA = 1; // ????
TR0 = 1; // ?????0
}
void main() {
LCD_Init();
DS1302_Init();
Timer0_Init();
while(1) {
unsigned char key = Key_Scan();
// ????
switch(key) {
case 1: // SET?
if(display_mode == 1) {
timer_sec = 3600; // ??????1??
} else if(display_mode == 2) {
stopwatch_ms = 0; // ????
}
break;
case 2: // UP?
if(display_mode == 0) alarm_min = (alarm_min + 1) % 60;
else if(display_mode == 1) timer_sec += 60;
break;
case 3: // DOWN?
if(display_mode == 0) alarm_min = (alarm_min > 0) ? alarm_min - 1 : 59;
else if(display_mode == 1) timer_sec = (timer_sec > 60) ? timer_sec - 60 : 0;
break;
case 4: // MODE?
display_mode = (display_mode + 1) % 4;
break;
}
// ????
if(alarm_enabled && !alarm_triggered) {
if((time_data[2] & 0x3F) == alarm_hour &&
(time_data[1] & 0x7F) == alarm_min) {
alarm_triggered = 1;
Motor_Alarm(); // ????
}
}
// ???10?????
static unsigned int temp_counter = 0;
if(++temp_counter >= 10000) {
temp_counter = 0;
DS18B20_Read_Temp();
}
Display_Mode(); // ????
}
}
void Timer0_ISR() interrupt 1 {
TH0 = 0xFC;
TL0 = 0x18;
static unsigned int ms_counter = 0;
static unsigned int sec_counter = 0;
// ????
if(display_mode == 2) {
stopwatch_ms += 10; // ?10ms????
if(stopwatch_ms >= 60000) stopwatch_ms = 0; // 60???
}
// ??????
if(display_mode == 1 && timer_sec > 0) {
if(++ms_counter >= 100) {
ms_counter = 0;
timer_sec--;
if(timer_sec == 0) Motor_Alarm(); // ??????
}
}
// ????????
if(++sec_counter >= 1000) {
sec_counter = 0;
DS1302_Read_Time();
}
}
原始代码如上,请根据我在前面向你提问的错误提示,帮我修改一下这份原始代码并将修改完的完整代码发给我