声控电子钟初具有普通钟的功能外,还具有15秒钟休眠功能,此时当接收到触发端触发后,点亮显示屏,显示当前时间并在15秒后关闭。以节省电能。亦可晚上睡眠时使用。
#include
#include
unsigned char counter=0;
unsigned char second,minute,hour;
unsigned char second0,second1;
unsigned char minute0,minute1;
unsigned char hour0,hour1;
unsigned char gleaming,task;
unsigned char hs;
#define TskSetHour 0
#define TskSetMinute 1
#define TskSetSecond 2
#define TskPrint 3
#define TskSleep 4
void delay(int);
void ScreenOpen(void);
void IncreaseTime(void);
void Translate(void);
void send_command(unsigned char);
void send_single_command(unsigned char);
void send_double_command(unsigned char);
void PrintToZLG7289(void);
void GleamingHour(void);
void GleamingMinute(void);
void GleamingSecond(void);
void GleamingNone(void);
void ChangeStatus(void);
void Increase(void);
void Decrease(void);
void boot(void);
void boot(void)
{/*PLL初始化*/
PCTL_BCS=0;
PCTL=0x00; //禁止PLL
PBWC=0x80; //自动控制方式
PMSH=0x02; //反馈分频器因子高位N=02H
PMSL=0x58; //反馈分频器因子低位N=58H
PMRS=0x80; //因子L=80H
PCTL=0x22; //P=0,E=2,启动PLL
PCTL_PLLON=1; //启动压控振荡器时钟
PBWC_LOCK=0; //LOCK位为0,频率未稳定,等待
PCTL_BCS=1; //频率已锁定,选PLL作为时钟源
}
void main(void) {
CONFIG2=0x01; //stop模式下禁止振荡器工作;内部总线时钟用作SCI的时钟
CONFIG1=0x3d; //COP溢出范围在2^18-2^4个CGMXCLK时钟周期;禁止COP模块
DisableInterrupts;
boot();
T1SC=0b01000110;
T1MODH=0x96;
T1MODL=0x00;
DDRA=0x00;
DDRC=0x0f;
DDRB=0xff;
DDRD=0xff;
DDRE=0xff;
INTKBSCR_MODEK = 1; //选择触发方式为下降沿和低电平
INTKBSCR_IMASKK = 1; //禁止键盘中断
INTKBIER = 0xff; //A口都作为键盘输入
INTKBSCR_ACKK = 1; //键盘响应置1,用于清除IREQ
INTKBSCR_IMASKK = 0; //允许键盘中断
hour=16;
minute=27;
second=55;
hs=0;
gleaming=0x0a;
task=TskPrint;
PTC_PTC0=1;
GleamingNone();
EnableInterrupts;
for(;;);
}
void delay(int k){
int i;
for (i=0;i<=k;i++);
}
void ScreenOpen(void){
char i;
if (task==TskSleep) counter++;
if (counter==15) {
for(i=0;i<=8;i++){
delay(30000);
delay(30000);
send_single_command(0xa1);
counter=0;
}
PTC_PTC0=0;
}
}
void IncreaseTime(void){
second++;
if (second==60){
second=0;
minute++;
if (minute==60){
minute=0;
hour++;
if (hour==24){
hour=0;
}
}
}
}
void Translate(void){
second1=second/10;
second0=second-second1*10;
minute1=minute/10;
minute0=minute-minute1*10;
hour1=hour/10;
hour0=hour-hour1*10;
}
void send_command(unsigned char i_command)
{
unsigned char i;
for(i=0; i<8; i++)
{
PTC_PTC1=((i_command&0x80)==0x80?1:0);
delay(0);
PTC_PTC3=1; // clk=1;
delay(0);
PTC_PTC3=0; // clk=0;
i_command=i_command<<1;
}
}
void send_single_command(unsigned char command){
PTC_PTC2=0; //enable=0
delay(0);
send_command(command);
PTC_PTC2=1; //enable=1
delay(0);
}
void send_double_command(unsigned char command1,unsigned char command2){
PTC_PTC2=0; //enable=0
delay(0);
send_command(command1);
send_command(command2);
PTC_PTC2=1; //enable=1
delay(0);
}
void PrintToZLG7289(void){
if (gleaming==0x0f) gleaming=0x0a;
else gleaming=0x0f;
send_double_command(0x80,second0);
send_double_command(0x81,second1);
send_double_command(0x82,gleaming);
send_double_command(0x83,minute0);
send_double_command(0x84,minute1);
send_double_command(0x85,gleaming);
send_double_command(0x86,hour0);
send_double_command(0x87,hour1);
}
void GleamingHour(void){
send_double_command(0x88,0b00111111);
}
void GleamingMinute(void){
send_double_command(0x88,0b11100111);
}
void GleamingSecond(void){
send_double_command(0x88,0b11111100);
}
void GleamingNone(void){
send_double_command(0x88,0b11111111);
}
void ChangeStatus(void){
if (task==TskPrint) {
T1SC_TOIE=0;
GleamingHour();
task=TskSetHour;
}
else if (task==TskSetHour) {
task=TskSetMinute;
GleamingMinute();
}
else if (task==TskSetMinute) {
task=TskSetSecond;
GleamingSecond();
}
else if (task==TskSetSecond) {
task=TskSleep;
counter=14;
ScreenOpen();
GleamingNone();
T1SC_TOIE=1;
}
else if (task==TskSleep){
task=TskPrint;
PTC_PTC0=1;
GleamingNone();
counter=0;
}
}
void Increase(void){
if (task==TskSetHour) {
hour++;
if (hour==24) hour=0;
Translate();
PrintToZLG7289();
}
if (task==TskSetMinute) {
minute++;
if (minute==60) minute=0;
Translate();
PrintToZLG7289();
}
if (task==TskSetSecond) {
second++;
if (second==60) second=0;
Translate();
PrintToZLG7289();
}
}
void Decrease(void){
if (task==TskSetHour) {
if (hour==0) hour=24;
hour--;
Translate();
PrintToZLG7289();
}
if (task==TskSetMinute) {
if (minute==0) minute=60;
minute--;
Translate();
PrintToZLG7289();
}
if (task==TskSetSecond) {
if (second==0) second=60;
second--;
Translate();
PrintToZLG7289();
}
}
interrupt 6 void OverFlow(){
T1SC_TOIE=0;
T1SC_TOF=0;
if (hs==0){
hs=1;
}
else {
hs=0;
ScreenOpen();
IncreaseTime();
Translate();
PrintToZLG7289();
}
T1SC_TOIE=1;
}
interrupt 15 void KBI_ISR(void)
{
unsigned char i;
INTKBSCR_IMASKK = 1; //屏蔽键盘中断
delay(30000);
if (task!=TskPrint && task!=TskSleep){
if (PTA_PTA2==0)
Decrease();
if (PTA_PTA3==0)
Increase();
}
if (PTA_PTA0==0)
ChangeStatus();
if (PTA_PTA4==0) {
PTC_PTC0=1;
counter=0;
}
INTKBSCR_ACKK = 1; //清除中断确认
INTKBSCR_IMASKK = 0; //开启键盘中断
}
上一篇:电路中的"地"
下一篇:单片机笔记——显示驱动
推荐阅读最新更新时间:2024-03-16 13:49