main.c- #include <STC15F2K60S2.h>
- #include "ds1302.h"
- #include "iic.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define Y4 P2=(P2&0x1f)|0x80;
- #define Y5 P2=(P2&0x1f)|0xa0;
- #define Y6 P2=(P2&0x1f)|0xc0;
- #define Y7 P2=(P2&0x1f)|0xe0;
- uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf};
- uchar yi,er,san,si,wu,liu,qi,ba;
- uchar num;
- uint shidu;
- bit mode; //初始,自动模式
- bit buzz_mode=1;
- bit open_mode;
- bit S6,S5,S4;
- bit flag;
- uchar size=50;
- uchar temp;
- extern uchar hour,min,s;
- void allinit(void);
- void display1(uchar yi,uchar er);
- void display2(uchar san,uchar si);
- void display3(uchar wu,uchar liu);
- void display4(uchar qi,uchar ba);
- void keyscan();
- void keyscan16();
- void delayms(int ms) //延时函数
- {
- int i,j;
- for(i=ms;i>0;i--)
- for(j=845;j>0;j--);
- }
- void main()
- {
- allinit();
- DS1302_init(0x08,0x30,0x50);
- while(1)
- {
- shidu= pcf8591(3);
- shidu=shidu*99/255;
- keyscan(); //独立按键
- //keyscan16(); //矩阵按键
- DS1302_read();
- display1(yi,er);
- display2(san,si);
- display3(wu,liu);
- display4(qi,ba);
- yi=hour/16;er=hour%16;san=11;si=min/16;
- wu=min%16;liu=10;qi=shidu/10;ba=shidu%10;
- if(mode==0) //自动模式
- {
- Y4;P0=0xfe;
- if(shidu < size)
- {
- Y5;P0=0x10;
- }
- if(shidu > size)
- {
- Y5;P0=0x00;
- }
- if(S6==1)
- {
- yi=11;er=11;san=10;si=10;
- wu=temp/10;liu=temp%10;qi=size/10;ba=size%10;
- temp=e2prom_read(0);
- }
- }
- if(mode==1) //手动模式
- {
- Y4;P0=0xfd;
- if(buzz_mode==1 && open_mode==1)
- {
- if(shidu < size) {Y5;P0=0x50;}
- if(shidu >= size) {Y5;P0=0x10;}
- }
- if(buzz_mode==1 && open_mode==0)
- {
- if(shidu < size) {Y5;P0=0x40;}
- if(shidu >= size) {Y5;P0=0x00;}
- }
- if(buzz_mode==0 && open_mode==1)
- {
- Y5;P0=0x10;
- }
- if(buzz_mode==0 && open_mode==0)
- {
- Y5;P0=0x00;
- }
- }
- }
- }
- void allinit(void)
- {
- Y5;P0=0x00; //关闭蜂鸣器和继电器
- Y6;P0=0xff; //打开数码管位选
- Y7;P0=0xff; //关闭数码管段选
- Y4;P0=0xff; //关闭led灯
- }
- void display1(uchar yi,uchar er) //数码管显示
- {
- Y6;P0=0x01;Y7;P0=tab[yi];delayms(1);
- Y6;P0=0x02;Y7;P0=tab[er];delayms(1);
- }
- void display2(uchar san,uchar si)
- {
- Y6;P0=0x04;Y7;P0=tab[san];delayms(1);
- Y6;P0=0x08;Y7;P0=tab[si];delayms(1);
- }
- void display3(uchar wu,uchar liu)
- {
- Y6;P0=0x10;Y7;P0=tab[wu];delayms(1);
- Y6;P0=0x20;Y7;P0=tab[liu];delayms(1);
- }
- void display4(uchar qi,uchar ba)
- {
- Y6;P0=0x40;Y7;P0=tab[qi];delayms(1);
- Y6;P0=0x80;Y7;P0=tab[ba];delayms(1);
- }
- void keyscan(void) //独立按键处理
- {
- if(P30==0)
- {
- delayms(5);
- if(P30==0)
- {
- mode=~mode;
- }while(!P30);
- }
- else if(P31==0)
- {
- delayms(5);
- if(P31==0)
- {
- if(mode==0)
- {
- S6=~S6;
- if(S6==1)
- {e2prom_write(0,size);}
- }
- if(mode==1)
- {
- buzz_mode = ~buzz_mode;
- }while(!P31);
- }
- }
- else if(P32==0)
- {
- delayms(5);
- if(P32==0)
- {
- if(mode==0) {size=size+1;}
- if(mode==1) {open_mode=1;}
- }while(!P32);
- }
- else if(P33==0)
- {
- delayms(5);
- if(P33==0)
- {
- if(mode==0) {size=size-1;}
- if(mode==1) {open_mode=0;}
- }while(!P33);
- }
- }
- void keyscan16() //矩阵按键
- {
- uchar temp;
-
- P44=0;P42=1;P3=0X7F;
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- delayms(5);
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- temp=P3;
- switch(temp)
- {
- case 0x7e: num=0;break;
- case 0x7d: num=4;break;
- case 0x7b: num=8;break;
- case 0x77: num=12;break;
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp=temp&0X0F;
- }
- }
- }
- P44=1;P42=0;P3=0XBF;
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- delayms(5);
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- temp=P3;
- switch(temp)
- {
- case 0xBe: num=1;break;
- case 0xBd: num=5;break;
- case 0xBb: num=9;break;
- case 0xB7: num=13;break;
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp=temp&0X0F;
- }
- }
- }
-
- P3=0XDF;P44=1;P42=1;
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- delayms(5);
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- temp=P3;
- switch(temp)
- {
- case 0xDe: num=2;break;
- case 0xDd: num=6;break;
- case 0xDb: num=10;break;
- case 0xD7: num=14;break;
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp=temp&0X0F;
- }
- }
- }
-
- P3=0XEF;P44=1;P42=1;
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- delayms(5);
- temp=P3;
- temp=temp&0X0F;
- if(temp!=0x0f)
- {
- temp=P3;
- switch(temp)
- {
- case 0xEe: num=3;break;
- case 0xEd: num=7;break;
- case 0xEb: num=11;break;
- case 0xE7: num=15;break;
- }
- while(temp!=0x0f)
- {
- temp=P3;
- temp=temp&0X0F;
- }
- }
- }
- }
复制代码 iic.ciic.h- #ifndef _IIC_H
- #define _IIC_H
- #define uchar unsigned char
- #define uint unsigned int
- //函数声明
- void IIC_Start(void);
- void IIC_Stop(void);
- void IIC_Ack(bit ackbit);
- void IIC_SendByte(unsigned char byt);
- bit IIC_WaitAck(void);
- unsigned char IIC_RecByte(void);
- uchar pcf8591(uchar addr);
- void e2prom_write(uchar addr,uchar dat);
- uchar e2prom_read(uchar addr);
- #endif
复制代码 ds1302.c- /*
- 程序说明: DS1302驱动程序
- 软件环境: Keil uVision 4.10
- 硬件环境: CT107单片机综合实训平台 8051,12MHz
- 日 期: 2011-8-9
- */
- #include <reg52.h>
- #include "ds1302.h"
- #include <intrins.h>
- sbit SCK=P1^7;
- sbit SDA=P2^3;
- sbit RST = P1^3; // DS1302复位
- void Write_Ds1302_Byte(unsigned char temp)
- {
- unsigned char i;
- for (i=0;i<8;i++)
- {
- SCK=0;
- SDA=temp&0x01;
- temp>>=1;
- SCK=1;
- }
- }
- void Write_Ds1302( unsigned char address,unsigned char dat )
- {
- RST=0;
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address);
- Write_Ds1302_Byte(dat);
- RST=0;
- }
- unsigned char Read_Ds1302 ( unsigned char address )
- {
- unsigned char i,temp=0x00;
- RST=0;
- _nop_();
- SCK=0;
- _nop_();
- RST=1;
- _nop_();
- Write_Ds1302_Byte(address);
- for (i=0;i<8;i++)
- {
- SCK=0;
- temp>>=1;
- if(SDA)
- temp|=0x80;
- SCK=1;
- }
- RST=0;
- _nop_();
- RST=0;
- SCK=0;
- _nop_();
- SCK=1;
- _nop_();
- SDA=0;
- _nop_();
- SDA=1;
- _nop_();
- return (temp);
- }
- void DS1302_init(uchar hour,uchar min,uchar s)
- {
- Write_Ds1302(0x84,hour);
- Write_Ds1302(0x82,min);
- Write_Ds1302(0x80,s);
- }
- uchar hour,min,s;
- void DS1302_read()
- {
- hour=Read_Ds1302(0x85);
- min= Read_Ds1302(0x83);
- s= Read_Ds1302(0x81);
- }
复制代码 ds1302.h- #ifndef __DS1302_H
- #define __DS1302_H
- #define uchar unsigned char
- #define uint unsigned int
- void Write_Ds1302_Byte(unsigned char temp);
- void Write_Ds1302( unsigned char address,unsigned char dat );
- unsigned char Read_Ds1302 ( unsigned char address );
- void DS1302_read();
- void DS1302_init(uchar hour,uchar min,uchar s);
- #endif
复制代码
|