//main.c#include <reg52.h>#include "1602.h"#include "i2c.h"#include "key.h"#include "delay.h"sbit SPK = P2^2;char hour;char min;char sec;char msec;char ckhour;char ckmin;char cksec; bit spk_flag_h;//定义整点报时标志位bit spk_flag_ck;//定义闹铃报时标志位bit KeyPRessFlag;//定义按键标志位bit changeflag;//显示切换标志位unsigned char temp[6];//hour,min,sec,ckhour,ckmin,cksecunsigned char wei[3] = {'s','m','h'};unsigned char weima;//0,1,2表示sec,min,hourvoid key_fun(void);//按键功能函数void main(void){ IRcvStr(0xae,0,temp,6); hour = temp[0]; min = temp[1] ; sec = temp[2]; ckhour = temp[3]; ckmin = temp[4]; cksec = temp[5]; LCD_Init(); LCD_Clear(); TMOD = 0X01; TH0 = 0XFC; TL0 = 0X18; EA = 1; ET0 = 1; TR0 = 1; EX0 = 1; IT0 = 1; while (1) { if(!changeflag) { LCD_Write_String(0,0,"Time: "); LCD_Write_String(11,0,"Gps:"); LCD_Write_Char(15,0,wei[weima]); LCD_Write_Char(0,1,0x30+temp[0]/10); LCD_Write_Char(1,1,0x30+temp[0]%10); LCD_Write_Char(2,1,':'); LCD_Write_Char(3,1,0x30+temp[1]/10); LCD_Write_Char(4,1,0x30+temp[1]%10); LCD_Write_Char(5,1,':'); LCD_Write_Char(6,1,0x30+temp[2]/10); LCD_Write_Char(7,1,0x30+temp[2]%10); } else { LCD_Write_String(0,0,"Clock:"); LCD_Write_String(11,0,"Gps:"); LCD_Write_Char(15,0,wei[weima]); LCD_Write_Char(0,1,0x30+ckhour/10); LCD_Write_Char(1,1,0x30+ckhour%10); LCD_Write_Char(2,1,':'); LCD_Write_Char(3,1,0x30+ckmin/10); LCD_Write_Char(4,1,0x30+ckmin%10); LCD_Write_Char(5,1,':'); LCD_Write_Char(6,1,0x30+cksec/10); LCD_Write_Char(7,1,0x30+cksec%10); } key_fun(); ISendStr(0xae,0,temp,6); }}void exit0_isr(void) interrupt 0{ EX0 = 0; KeyPressFlag = 1; EX0 = 1;}void time0_isr(void) interrupt 1{ static unsigned char i = 0; static unsigned char count = 0; ET0 = 0; TH0 = 0XFC; TL0 = 0X18; i++; if(spk_flag_h || spk_flag_ck) { SPK = ~SPK; } if(i == 10) { i = 0; msec++; if(msec == 100) { if(spk_flag_h) { spk_flag_h = 0; } if(spk_flag_ck) { count++; if(count == 5) { count = 0; spk_flag_ck = 0; } } msec = 0; sec++; if(sec == 60) { sec = 0; min++; if(min == 60) { min = 0; hour++; spk_flag_h = 1; if(hour == 24) hour = 0; } } } } if(hour == ckhour && min == ckmin && sec == cksec) { spk_flag_ck = 1; } temp[0] = hour; temp[1] = min; temp[2] = sec; ET0 = 1; }void key_fun(void){ unsigned char num; KeyPort=0xf0;// 赋值用于中断检测 if(KeyPressFlag == 1) { KeyPressFlag = 0; //按键标志清零,以便下次检测 num = KeyPro(); if(num != 0xff) { if(num == 12)//开启,暂停 { ET0 = ~ET0; } else if(num == 15) { weima++; if(weima == 3) weima = 0; } else if(num == 9) { weima--; if(weima > 2) weima = 2; } else if(num == 13) { if(!changeflag) { if(weima == 0) { sec++; if(sec == 60) sec = 0; } else if(weima == 1) { min++; if(min == 60) min = 0; } else if(weima == 2) { hour++; if(hour == 24) hour = 0; } temp[0] = hour; temp[1] = min; temp[2] = sec; } else { if(weima == 0) { cksec++; if(cksec == 60) cksec = 0; } else if(weima == 1) { ckmin++; if(ckmin == 60) ckmin = 0; } else if(weima == 2) { ckhour++; if(ckhour == 24) ckhour = 0; } temp[3] = ckhour; temp[4] = ckmin; temp[5] = cksec; } } else if(num == 11) { if(!changeflag) { if(weima == 0) { sec--; if(sec < 0) sec = 59; } else if(weima == 1) { min--; if(min < 0) min = 59; } else if(weima == 2) { hour--; if(hour < 0) hour = 23; } temp[0] = hour; temp[1] = min; temp[2] = sec; } else { if(weima == 0) { cksec--; if(cksec < 0) cksec = 59; } else if(weima == 1) { ckmin--; if(ckmin < 0) ckmin = 59; } else if(weima == 2) { ckhour--; if(ckhour < 0) ckhour = 23; } temp[3] = ckhour; temp[4] = ckmin; temp[5] = cksec; } } else if(num == 16)//显示切换 { changeflag = ~changeflag; } } } }//1602.c#include "1602.h"#include "delay.h"sbit RS = P2^4; //定义端口 sbit RW = P2^5;sbit EN = P2^6;#define RS_CLR RS=0 #define RS_SET RS=1#define RW_CLR RW=0 #define RW_SET RW=1 #define EN_CLR EN=0#define EN_SET EN=1#define DataPort P0/*------------------------------------------------ 判忙函数------------------------------------------------*/ /*bit LCD_Check_Busy(void) { DataPort= 0xFF; RS_CLR; RW_SET; EN_CLR; _nop_(); EN_SET; return (bit)(DataPort & 0x80); }*//*------------------------------------------------ 写入命令函数------------------------------------------------*/void LCD_Write_Com(unsigned char com) { // while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_CLR; RW_CLR; EN_SET; DataPort= com; _nop_(); EN_CLR;}/*------------------------------------------------ 写入数据函数------------------------------------------------*/void LCD_Write_Data(unsigned char Data) { //while(LCD_Check_Busy()); //忙则等待 DelayMs(5); RS_SET; RW_CLR; EN_SET; DataPort= Data; _nop_(); EN_CLR;}/*------------------------------------------------ 清屏函数------------------------------------------------*/ void LCD_Clear(void) { LCD_Write_Com(0x01); DelayMs(5); }/*------------------------------------------------ 写入字符串函数------------------------------------------------*/ void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { if (y == 0) { LCD_Write_Com(0x80 + x); //表示第一行 } else { LCD_Write_Com(0xC0 + x); //表示第二行 } while (*s) { LCD_Write_Data( *s); s ++; } }/*------------------------------------------------ 写入字符函数------------------------------------------------*/ void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data) { if (y == 0) { LCD_Write_Com(0x80 + x); } else { LCD_Write_Com(0xC0 + x); } LCD_Write_Data( Data); }/*------------------------------------------------ 初始化函数------------------------------------------------*/ void LCD_Init(void) { LCD_Write_Com(0x38); /*显示模式设置*/ DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); DelayMs(5); LCD_Write_Com(0x38); LCD_Write_Com(0x08); /*显示关闭*/ LCD_Write_Com(0x01); /*显示清屏*/ LCD_Write_Com(0x06); /*显示光标移动设置*/ DelayMs(5); LCD_Write_Com(0x0C); /*显示开及光标设置*/ }//i2c.c#include "i2c.h"#include "delay.h"#define _Nop() _nop_() //定义空指令 bit ack; //应答标志位sbit SDA=P2^1;sbit SCL=P2^0;/*------------------------------------------------ 启动总线------------------------------------------------*/void Start_I2c(){ SDA=1; //发送起始条件的数据信号 _Nop(); SCL=1; _Nop(); //起始条件建立时间大于4.7us,延时 _Nop(); _Nop(); _Nop(); _Nop(); SDA=0; //发送起始信号 _Nop(); //起始条件锁定时间大于4μ _Nop(); _Nop(); _Nop(); _Nop(); SCL=0; //钳住I2C总线,准备发送或接收数据 _Nop(); _Nop();}/*------------------------------------------------ 结束总线------------------------------------------------*/void Stop_I2c(){ SDA=0; //发送结束条件的数据信号 _Nop(); //发送结束条件的时钟信号 SCL=1; //结束条件建立时间大于4μ _Nop(); _Nop(); _Nop(); _Nop(); _Nop(); SDA=1; //发送I2C总线结束信号 _Nop(); _Nop(); _Nop(); _Nop();}/*---------------------------------------------------------------- 字节数据传送函数 函数原型: void SendByte(unsigned char c);功能: 将数据c发送出去,可以是地址,也可以是数据,发完后等待应答,并对 此状态位进行操作.(不应答或非应答都使ack=0 假) 发送数据正常,ack=1; ack=0表示被控器无应答或损坏。------------------------------------------------------------------*/void SendByte(unsigned char c){ unsigned char BitCnt; for(BitCnt=0;BitCnt<8;BitCnt++) //要传送的数据长度为8位 { if((c<<BitCnt)&0x80)SDA=1; //判断发送位 else SDA=0; _Nop(); SCL=1; //置时钟线为高,通知被控器开始接收数据位 _Nop(); _Nop(); //保证时钟高电平周期大于4μ _Nop(); _Nop(); _Nop(); SCL=0; } _Nop(); _Nop(); SDA=1; //8位发送完后释放数据线,准备接收应答位 _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); _Nop(); if(SDA==1)ack=0; else ack=1; //判断是否接收到应答信号 SCL=0; _Nop(); _Nop();}/*---------------------------------------------------------------- 字节数据传送函数 函数原型: unsigned char RcvByte();功能: 用来接收从器件传来的数据,并判断总线错误(不发应答信号), 发完后请用应答函数。 ------------------------------------------------------------------*/ unsigned char RcvByte(){ unsigned char retc; unsigned char BitCnt; retc=0; SDA=1; //置数据线为输入方式 for(BitCnt=0;BitCnt<8;BitCnt++) { _Nop(); SCL=0; //置时钟线为低,准备接收数据位 _Nop(); _Nop(); //时钟低电平周期大于4.7us _Nop(); _Nop(); _Nop(); SCL=1; //置时钟线为高使数据线上数据有效 _Nop(); _Nop(); retc=retc<<1; if(SDA==1)retc=retc+1; //读数据位,接收的数据位放入retc中 _Nop(); _Nop(); } SCL=0; _Nop(); _Nop(); return(retc);}/*---------------------------------------------------------------- 应答子函数原型: void Ack_I2c(void); ----------------------------------------------------------------*/void Ack_I2c(void){ SDA=0; _Nop(); _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); //时钟低电平周期大于4μ _Nop(); _Nop(); _Nop(); SCL=0; //清时钟线,钳住I2C总线以便继续接收 _Nop(); _Nop(); }/*---------------------------------------------------------------- 非应答子函数原型: void NoAck_I2c(void); ----------------------------------------------------------------*/void NoAck_I2c(void){ SDA=1; _Nop(); _Nop(); _Nop(); SCL=1; _Nop(); _Nop(); //时钟低电平周期大于4μ _Nop(); _Nop(); _Nop(); SCL=0; //清时钟线,钳住I2C总线以便继续接收 _Nop(); _Nop(); }/*---------------------------------------------------------------- 向无子地址器件发送字节数据函数 函数原型: bit ISendByte(unsigned char sla,ucahr c); 功能: 从启动总线到发送地址,数据,结束总线的全过程,从器件地址sla. 如果返回1表示操作成功,否则操作有误。注意: 使用前必须已结束总线。----------------------------------------------------------------*//*bit ISendByte(unsigned char sla,unsigned char c){ Start_I2c(); //启动总线 SendByte(sla); //发送器件地址 if(ack==0)return(0); SendByte(c); //发送数据 if(ack==0)return(0); Stop_I2c(); //结束总线 return(1);}*//*---------------------------------------------------------------- 向有子地址器件发送多字节数据函数 函数原型: bit ISendStr(unsigned char sla,unsigned char suba,ucahr *s,unsigned char no); 功能: 从启动总线到发送地址,子地址,数据,结束总线的全过程,从器件 地址sla,子地址suba,发送内容是s指向的内容,发送no个字节。 如果返回1表示操作成功,否则操作有误。注意: 使用前必须已结束总线。----------------------------------------------------------------*/bit ISendStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no){ unsigned char i; for(i=0;i<no;i++) { Start_I2c(); //启动总线 SendByte(sla); //发送器件地址 if(ack==0)return(0); SendByte(suba); //发送器件子地址 if(ack==0)return(0); SendByte(*s); //发送数据 if(ack==0)return(0); Stop_I2c(); //结束总线 DelayMs(1); //必须延时等待芯片内部自动处理数据完毕 s++; suba++; } return(1);}/*---------------------------------------------------------------- 向无子地址器件读字节数据函数 函数原型: bit IRcvByte(unsigned char sla,ucahr *c); 功能: 从启动总线到发送地址,读数据,结束总线的全过程,从器件地 址sla,返回值在c. 如果返回1表示操作成功,否则操作有误。注意: 使用前必须已结束总线。----------------------------------------------------------------*//*bit IRcvByte(unsigned char sla,unsigned char *c){ Start_I2c(); //启动总线 SendByte(sla+1); //发送器件地址 if(ack==0)return(0); *c=RcvByte(); //读取数据 NoAck_I2c(); //发送非就答位 Stop_I2c(); //结束总线 return(1);}*//*---------------------------------------------------------------- 向有子地址器件读取多字节数据函数 函数原型: bit ISendStr(unsigned char sla,unsigned char suba,ucahr *s,unsigned char no); 功能: 从启动总线到发送地址,子地址,读数据,结束总线的全过程,从器件 地址sla,子地址suba,读出的内容放入s指向的存储区,读no个字节。 如果返回1表示操作成功,否则操作有误。注意: 使用前必须已结束总线。----------------------------------------------------------------*/bit IRcvStr(unsigned char sla,unsigned char suba,unsigned char *s,unsigned char no){ unsigned char i; Start_I2c(); //启动总线 SendByte(sla); //发送器件地址 if(ack==0)return(0); SendByte(suba); //发送器件子地址 if(ack==0)return(0); Start_I2c(); SendByte(sla+1); if(ack==0)return(0); for(i=0;i<no-1;i++) { *s=RcvByte(); //发送数据 Ack_I2c(); //发送就答位 s++; } *s=RcvByte(); NoAck_I2c(); //发送非应位 Stop_I2c(); //结束总线 return(1);}//key.c#include <reg52.h>#include "delay.h"#define KeyPort P1/*------------------------------------------------按键扫描函数,返回扫描键值------------------------------------------------*/unsigned char KeyScan(void) //键盘扫描函数,使用行列逐级扫描法{ unsigned char Val; KeyPort=0xf0;//高四位置高,低四位拉低 if(KeyPort!=0xf0)//表示有按键按下 { DelayMs(10); //去抖 if(KeyPort!=0xf0) { //表示有按键按下 KeyPort=0xfe; //检测第一行 if(KeyPort!=0xfe) { Val=KeyPort&0xf0; Val+=0x0e; while(KeyPort!=0xfe); DelayMs(10); //去抖 while(KeyPort!=0xfe); return Val; } KeyPort=0xfd; //检测第二行 if(KeyPort!=0xfd) { Val=KeyPort&0xf0; Val+=0x0d; while(KeyPort!=0xfd); DelayMs(10); //去抖 while(KeyPort!=0xfd); return Val; } KeyPort=0xfb; //检测第三行 if(KeyPort!=0xfb) { Val=KeyPort&0xf0; Val+=0x0b; while(KeyPort!=0xfb); DelayMs(10); //去抖 while(KeyPort!=0xfb); return Val; } KeyPort=0xf7; //检测第四行 if(KeyPort!=0xf7) { Val=KeyPort&0xf0; Val+=0x07; while(KeyPort!=0xf7); DelayMs(10); //去抖 while(KeyPort!=0xf7); return Val; } } } return 0xff;}/*------------------------------------------------按键值处理函数,返回扫键值------------------------------------------------*/unsigned char KeyPro(void){ switch(KeyScan()) { case 0x7e:return 1;break; case 0x7d:return 5;break; case 0x7b:return 6;break; case 0x77:return 7;break; case 0xbe:return 2;break; case 0xbd:return 8;break; case 0xbb:return 9;break; case 0xb7:return 10;break; case 0xde:return 3;break; case 0xdd:return 11;break; case 0xdb:return 12;break; case 0xd7:return 13;break; case 0xee:return 4;break; case 0xed:return 14;break; case 0xeb:return 15;break; case 0xe7:return 16;break; default:return 0xff;break; }}//delay.c#include "delay.h"/*------------------------------------------------ uS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时 长度如下 T=tx2+5 uS ------------------------------------------------*/void DelayUs2x(unsigned char t){ while(--t);}/*------------------------------------------------ mS延时函数,含有输入参数 unsigned char t,无返回值 unsigned char 是定义无符号字符变量,其值的范围是 0~255 这里使用晶振12M,精确延时请使用汇编------------------------------------------------*/void DelayMs(unsigned char t){ while(t--) { //大致延时1mS DelayUs2x(245); DelayUs2x(245); }}
新闻热点
疑难解答