最近李老師把我們上次焊的model還給我們操作
想著想著就不是很想照著他的code操作
所以就把上次的 code拿過來貼貼
沒想到貼完就是某大學三個大學生的畢業專題 = =
而且我的還是用4bit去實作的...
只是差一個鬧鐘 = =
有點好笑...
------------------------------------------------------
#include <REG51F.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS = P0^0;
sbit EN = P0^1;
sfr DataBus = 0x80;
int pc_count = 0;
int int0 = 0;
int secclk = 0;
int time[6] = {0,0,0,0,0,0}; //time[3] = min, time[2] = Min,time[1] = hour, time[0] = Hour
void Init_Lcd(void);
void Delay(uint del);
void DLY(uchar dly);
void LCD_CmdI(uchar comm);
void LCD_Cmd(uchar comm);
void Send_Data(uchar Data);
void init_rs232(void);
void time0_int(void) interrupt 1
{
TH0 = (65536-50000)/256;
TL0 = (65536-50000)%256;
int0++; //50ms; = 0.05sec;
if (int0 >= 20) //1sec;
{
int0 = 0;
time[5]++;
if (time[5] >= 10){
time[5] = 0;
time[4]++;
}
}
if (time[4] >= 6 && time[5] >= 0) //60sec = 1 min
{
time[4] = 0;
time[5] = 0;
time[3]++;
if (time[3] >= 10) //min = 10, Min++
{
time[3] = 0;
time[2]++;
if (time[2] >= 6 && time[3] >= 0) //Min = 6, min = 10,hour++;
{
time[2] = 0;
time[3] = 0;
time[1]++;
if (time[1] >= 10) //hour = 10 , Hour ++
{
time[1] = 0;
time[0]++;
if (time[0] >= 2 && time[1] >= 4) //hour = 4,Hour = 2,
{
time[0] = 0;
time[1] = 0;
}
}
}
}
}
}
void main(void){
uchar i;EN = 0;
Init_Lcd();
init_rs232();
LCD_Cmd(0x80);
while (1){
for (i = 0;i < 4;i++)
Send_Data(' ');
for (i = 0;i < 6;i++){
if (i == 2 | i == 4)
Send_Data(':');
Send_Data(time[i] + 0x30);
}
LCD_Cmd(0x80);
}
}
void init_rs232(void)
{
TMOD = 0x01; //time1 mod 2, time0 mod 1
SCON = 0x50; // SM1 = 1, mod 1, REN = 1
ET0 = 1; //interrupt time start
EA = 1; //inetrrupt start
TI = 1;
TI = 0;
TH0 = (65536-50000)/256; //time0 0.05s
TL0 = (65536-50000)%256;
TR0 = 1; //time0 start
}
void Init_Lcd(void){
Delay(65000);
LCD_CmdI(0x30);
Delay(50000);
LCD_CmdI(0x30);
Delay(200);
LCD_CmdI(0x30);
LCD_CmdI(0x20);
LCD_Cmd(0x28);
LCD_Cmd(0x01);
Delay(5000);
LCD_Cmd(0x0c);
LCD_Cmd(0x06);
}
void Send_Data(uchar Data){
DataBus = (DataBus & 0x0c) | (Data & 0xf3);
RS = 1;
EN = 1;DLY(10);
EN = 0;DLY(10);
DataBus = (DataBus & 0x0c) | ((Data << 4) & 0xf3);
RS = 1;
EN = 1;DLY(10);
EN = 0;DLY(10);
}
void LCD_Cmd(uchar comm){
DataBus = (DataBus & 0x0c) | (comm & 0xf3);
RS = 0;
EN = 1;DLY(10);
EN = 0;DLY(10);
DataBus = (DataBus & 0x0c) | ((comm << 4) & 0xf3);
RS = 0;
EN = 1;DLY(10);
EN = 0;DLY(10);
}
void LCD_CmdI(uchar comm){
DataBus = (DataBus & 0x0c) | (comm & 0xf3);
EN = 1;DLY(10);
EN = 0;DLY(10);
}
void Delay(uint del){
wait:
del--;
if (del != 0) goto wait;
}
void DLY(uchar dly){
next:
dly--;
if (dly != 0) goto next;
}