main.c程序
#include<stc15f2k60s2.h>
#include "iic.h"
#define uchar unsigned char
#define uint unsigned int
uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,
0xbf,0xff,0xc1,0x8c,0xc8};
uchar yi,er,san,si,wu,liu,qi,ba;
void delayms(int ms);
void allinit();
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 keyscan16();
void Timer0Init(void);
uchar jishu=0,yemian=0;
uint dianya,canshu,key_no=0,tt=0;
bit L1_kai=0,L1=0,bz=0;
void main()
{
allinit();
Timer0Init();
canshu=EEPROM_read(0x00)*10;
while(1)
{
dianya=AD_read(0x03)*1.91;
if(yemian==0)
{
yi=22;er=21;san=21;si=21;wu=21;liu=dianya/100+10;qi=dianya%100/10;ba=dianya%10;
}
else if(yemian==1)
{
yi=23;er=21;san=21;si=21;wu=21;liu=canshu/100+10;qi=canshu%100/10;ba=canshu%10;
}
else if(yemian==2)
{
if(jishu>=1000000){yi=24;er=jishu/1000000;san=jishu%1000000/100000;si=jishu%100000/10000;wu=jishu%10000/1000;liu=jishu%1000/100;qi=jishu%100/10;ba=jishu%10;}
else if(jishu>=100000){yi=24;er=21;san=jishu/100000;si=jishu%100000/10000;wu=jishu%10000/1000;liu=jishu%1000/100;qi=jishu%100/10;ba=jishu%10;}
else if(jishu>=10000){yi=24;er=21;san=21;si=jishu/10000;wu=jishu%10000/1000;liu=jishu%1000/100;qi=jishu%100/10;ba=jishu%10;}
else if(jishu>=1000){yi=24;er=21;san=21;si=21;wu=jishu/1000;liu=jishu%1000/100;qi=jishu%100/10;ba=jishu%10;}
else if(jishu>=100){yi=24;er=21;san=21;si=21;wu=21;liu=jishu/100;qi=jishu%100/10;ba=jishu%10;}
else if(jishu>=10){yi=24;er=21;san=21;si=21;wu=21;liu=21;qi=jishu/10;ba=jishu%10;}
else if(jishu>=0){yi=24;er=21;san=21;si=21;wu=21;liu=21;qi=21;ba=jishu;}
}
if(dianya>canshu){bz=1;}
else if(bz==1)
{
bz=0;
if(dianya<=canshu)
jishu++;
}
if(dianya<canshu){L1=1;}
else {L1=0;}
if(L1_kai==1){P2=0X80;P0&=0XFE;}
else if(L1_kai==0){P2=0X80;P0|=0X01;}
if(jishu%2==1){P2=0X80;P0&=0XFD;}
else {P2=0X80;P0|=0X02;}
if(key_no>=3){P2=0X80;P0&=0XFB;}
else {P2=0X80;P0|=0X04;}
keyscan16();
display1(yi,er);
display2(san,si);
display3(wu,liu);
display4(qi,ba);
}
}
void Timer0Init(void)
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0xA0;
TH0 = 0x15;
TF0 = 0;
TR0 = 1;
EA=1;ET0=1;
}
void time0() interrupt 1
{
if(L1==1)
{
if(++tt==1000)
{
tt=0;
L1_kai=1;
}
}
else if(L1==0)
{
L1_kai=0;tt=0;
}
}
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:key_no++;break;
case 0x7d:key_no++;break;
case 0x7b:key_no++;break;
case 0x77:key_no++;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:key_no++;break;
case 0xbd:key_no++;break;
case 0xbb:key_no++;break;
case 0xb7:key_no++;break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=1;P3=0xdf;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xde:key_no++;break;
case 0xdd:key_no++;break;
case 0xdb:
key_no=0;jishu=0;
break;
case 0xd7:
key_no=0;
if(yemian==0)yemian=1;
else if(yemian==1){yemian=2;EEPROM_write(0x00,canshu/10);delayms(2);}
else if(yemian==2)yemian=0;
break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=1;P3=0xef;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delayms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xee:key_no++;break;
case 0xed:key_no++;break;
case 0xeb:
key_no=0;
if(yemian==1)
{
if(canshu<=0)canshu=500;
else canshu=canshu-50;
}
break;
case 0xe7:
key_no=0;
if(yemian==1)
{
if(canshu>=500)canshu=0;
else canshu=canshu+50;
}
break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}
void delayms(int ms)
{
uint i,j;
for(i=ms;i>0;i--)
for(j=845;j>0;j--);
}
void allinit()
{
P2=0XA0;
P0=0X00;
P2=0X80;
P0=0XFF;
P2=0XC0;
P0=0XFF;
P2=0XFF;
P0=0XFF;
}
void display1(uchar yi,uchar er)
{
P2=0XC0;
P0=0X01;
P2=0XFF;
P0=tab[yi];
delayms(1);
P2=0XC0;
P0=0X02;
P2=0XFF;
P0=tab[er];
delayms(1);
}
void display2(uchar san,uchar si)
{
P2=0XC0;
P0=0X04;
P2=0XFF;
P0=tab[san];
delayms(1);
P2=0XC0;
P0=0X08;
P2=0XFF;
P0=tab[si];
delayms(1);
}
void display3(uchar wu,uchar liu)
{
P2=0XC0;
P0=0X10;
P2=0XFF;
P0=tab[wu];
delayms(1);
P2=0XC0;
P0=0X20;
P2=0XFF;
P0=tab[liu];
delayms(1);
}
void display4(uchar qi,uchar ba)
{
P2=0XC0;
P0=0X40;
P2=0XFF;
P0=tab[qi];
delayms(1);
P2=0XC0;
P0=0X80;
P2=0XFF;
P0=tab[ba];
delayms(1);
P2=0XC0;
P0=0X80;
P2=0XFF;
P0=0XFF;
}
iic.c程序
#include<stc15f2k60s2.h>
#include "iic.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0;i<8;i++)
{
if(byt&0x80)
{
SDA = 1;
}
else
{
SDA = 0;
}
somenop;
SCL = 1;
byt <<= 1;
somenop;
SCL = 0;
}
}
unsigned char IIC_RecByte(void)
{
unsigned char da;
unsigned char i;
for(i=0;i<8;i++)
{
SCL = 1;
somenop;
da <<= 1;
if(SDA)
da |= 0x01;
SCL = 0;
somenop;
}
return da;
}
unsigned char AD_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
unsigned char EEPROM_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0xA1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Stop();
return temp;
}
void EEPROM_write(unsigned char add,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xA0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
iic.h程序
#ifndef _IIC_H
#define _IIC_H
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);
unsigned char AD_read(unsigned char add);
unsigned char EEPROM_read(unsigned char add);
void EEPROM_write(unsigned char add,unsigned char dat);
#endif
第十二届蓝桥杯单片机省赛(第一场) 视频演示
|