main.c- #include <STC15F2K60S2.h>
- #include "intrins.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;unsigned long int ml; uchar s;unsigned long int die;uchar g;
- void allinit(void);
- void Timer0Init(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();
- Timer0Init();
- while(1)
- {
- keyscan(); //独立按键
- // keyscan16(); //矩阵按键
- display1(10,0);
- display2(5,0);
- display3(wu,liu);
- display4(qi,ba);
- if(ml == 99990)
- {
- Y5;P0=0x00;
- ET0=0;EA=0;
- die=ml*5;
- wu=die/100000;
- liu=die%100000/10000;
- qi=die%10000/1000;
- ba=die%1000/100;
- }
- g=pcf8591(1);
- if(g<64)
- {Y4;P0=0xfe;}
- if(g>=64)
- {Y4;P0=0xff;}
- }
- }
- 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]&0x7f;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]&0x7f;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)
- {
- wu=0;liu=0;qi=0;ba=0;
- ET0=1;EA=1;
- Y5;P0=0x10;
- }while(!P30);
- }
- else if(P31==0)
- {
- delayms(5);
- if(P31==0)
- {
- die=ml*5;
- wu=die/100000;
- liu=die%100000/10000;
- qi=die%10000/1000;
- ba=die%1000/100;
- ET0=0;EA=0;
- Y5;P0=0x00;
- ml=0;
- }while(!P31);
- }
- else if(P32==0)
- {
- delayms(5);
- if(P32==0)
- {
- san=5;
- }while(!P32);
- }
- else if(P33==0)
- {
- delayms(5);
- if(P33==0)
- {
- si=6;
- }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;
- }
- }
- }
- }
- void Timer0Init() //1毫秒@11.0592MHz
- {
- AUXR |= 0x80; //定时器时钟1T模式
- TMOD &= 0xF0; //设置定时器模式
- TL0 = 0xCD; //设置定时初值
- TH0 = 0xD4; //设置定时初值
- TF0 = 0; //清除TF0标志
- TR0 = 1; //定时器0开始计时
- }
- void timer0 () interrupt 1
- {
- s++;
- if(s == 10)
- {
- s=0;
- ml++;
- wu=ml/10000;liu=ml%10000/1000;
- qi=ml%1000/100;ba=ml%100/10;
- }
- }
复制代码
iic.c
iic.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 add);
- #endif
复制代码
|