上班摸鱼中,只能用Proteus 仿真一下
Proteus 的AT89没有xdata, 用idata代替一下,编译成功
main.c
- #include <STC89C5xRC.H>//Code:8k Ram:512 xdata:256 EEProm:5k Addr:0x2000
- #include "struct_Test.h"
- typedef unsigned char u8; //0 to 255
- typedef unsigned int u16; //0 to 65535
- typedef unsigned long u32; //0 to 4294967295
- DateTime idata PLC_Clock = {2022, 2, 9, 15, 12, 0};
- u8 TMR_Clock_ACC;
- u8 TMR_Disp_ACC;
- u8 Disp_Buff[8];
- u8 code smgduan[] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x40}; //共阴数码管
- //0*****1*****2*****3*****4*****5*****6*****7*****8*****9*****-**
- void Timer0Init(void)
- {
- TMOD &= 0xF0; //设置定时器模式
- TMOD |= 0x01; //设置定时器模式
- TL0 = 0x18; //设置定时初始值1ms@12.000MHz 12T
- TH0 = 0xFC; //设置定时初始值1ms@12.000MHz 12T
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- }
- void timer0_int (void) interrupt 1
- {
- static u8 Sys10ms;
- TL0 = 0x18; //设置定时初始值1ms@12.000MHz 12T
- TH0 = 0xFC; //设置定时初始值1ms@12.000MHz 12T
- if(TMR_Disp_ACC)TMR_Disp_ACC--;
- if(++Sys10ms == 10)
- {
- Sys10ms = 0;
- //Timer 10ms
- if(TMR_Clock_ACC)TMR_Clock_ACC--;
- }
- }
- void Sys_Init(void)
- {
- ET0 = 1; //Timer0 interrupt Enable
- EA = 1; //All interrupt Enable
- }
- void main()
- {
- u8 Sta = 0;
- Timer0Init();
- Sys_Init();
- while (1)
- {
- if(!TMR_Clock_ACC)
- { TMR_Clock_ACC = 100;
- Clock_Calc();
- Disp_Buff[0] = PLC_Clock.SEC % 10;
- Disp_Buff[1] = PLC_Clock.SEC / 10;
- Disp_Buff[3] = PLC_Clock.MIN % 10;
- Disp_Buff[4] = PLC_Clock.MIN / 10;
- Disp_Buff[6] = PLC_Clock.HR % 10;
- Disp_Buff[7] = PLC_Clock.HR / 10;
- }
- if(!TMR_Disp_ACC)
- { static u8 i = 0;
- TMR_Disp_ACC = 2;
- P2 = 0xff;
- if(i == 2 || i == 5)
- P0 = 0x40;
- else
- P0 = smgduan[Disp_Buff[i]];
- P2 = ~(0x01 << i);
- i = ++i % 8;
- }
- }
- }
复制代码
struct_Test.h
- #ifndef _struct_Test_H_
- #define _struct_Test_H_
- typedef struct {
- unsigned int YY;
- unsigned char MM;
- unsigned char DD;
- unsigned char HR;
- unsigned char MIN;
- unsigned char SEC;
- } DateTime;
- extern DateTime idata PLC_Clock;
- void Clock_Calc();
- #endif
复制代码
struct_Test.c
- #ifndef _struct_Test_H_
- #include "struct_Test.h"
- #endif
- void Clock_Calc()
- {
- if(++PLC_Clock.SEC >= 60)
- {
- PLC_Clock.SEC = 0;
- if(++PLC_Clock.MIN >= 60)
- {
- PLC_Clock.MIN = 0;
- if(++PLC_Clock.HR >= 24)
- {
- PLC_Clock.HR = 0;
- if(++PLC_Clock.DD >= 30)
- {
- PLC_Clock.DD = 0;
- if(++PLC_Clock.MM >= 13)
- {
- PLC_Clock.MM = 1;
- PLC_Clock.YY++;
- }
- }
- }
- }
- }
- }
复制代码
|