2、基于C8051F410定时器1以工作方式2设计一个电子钟表应用程序,并将钟表当前的分秒信息显示在4位数码管上。 部分参考代码: // Peripheral specific…
- User
2、基于C8051F410定时器1以工作方式2设计一个电子钟表应用程序,并将钟表当前的分秒信息显示在4位数码管上。
部分参考代码: // Peripheral specific initialization functions, // Called from the Init_Device() function void PCA_Init() { PCA0MD &= ~0x40; PCA0MD = 0x00; }
void Timer_Init() { TCON = 0x40; TMOD = 0x10; // TH1=0xFF; //10us // TL1=0xEC; TH1=0xFF; //20us TL1=0xD8; }
void Port_IO_Init() { P0MDOUT = 0x01; P1MDOUT = 0x80; XBR1 = 0x40; }
void Oscillator_Init() { OSCICN = 0x87; }
// Initialization function for device, // Call Init_Device() from your main program void Init_Device(void) { PCA_Init(); Timer_Init(); Port_IO_Init(); Oscillator_Init();
} 请补全代码
- Assistant
#include <C8051F410.h>
// 定义数码管显示数字的对应编码 unsigned char code Char2Seg[] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// 定义变量储存电子钟表的小时、分钟、秒钟 unsigned char Hour, Minute, Second;
// 用于数码管显示的数据 unsigned char SegData[4];
// 显示一个数字到指定的数码管上 void DisplayDigit(unsigned char DigitIndex, unsigned char DigitData) { SegData[DigitIndex] = Char2Seg[DigitData]; }
// 更新电子钟表的数据 void UpdateClock() { Second++; // 秒钟加1
codeif (Second >= 60) { Second = 0; Minute++; // 分钟加1 if (Minute >= 60) { Minute = 0; Hour++; // 小时加1 if (Hour >= 24) { Hour = 0; // 小时归零 } } }