蓝桥杯第四届
?
#ifndef _IIC_H
#define _IIC_H
//oˉêyéù?÷
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 ICC_read(unsigned char add);
#endif
/*
3ìDò?μ?÷: IIC×ü???y?ˉ3ìDò
èí?t?·?3: Keil uVision 4.10
ó2?t?·?3: CT107μ¥???ú×?o?êμ?μ??ì¨ 8051£?12MHz
è? ?ú: 2011-8-9
*/
#include "reg52.h"
#include "intrins.h"
#define somenop {_nop_();_nop_();_nop_();_nop_();_nop_();}
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
//×ü??òy???¨ò?
sbit SDA = P2^1; /* êy?Y?? */
sbit SCL = P2^0; /* ê±?ó?? */
//×ü?????ˉì??t
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
somenop;
SDA = 0;
somenop;
SCL = 0;
}
//×ü??í£?1ì??t
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
somenop;
SDA = 1;
}
//ó|′e??????
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
//μè′yó|′e
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//í¨1yI2C×ü??·¢?íêy?Y
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;
}
}
//′óI2C×ü??é??óê?êy?Y
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 ICC_read(unsigned char add)
{
unsigned char temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_Start();
return temp;
}
#include <reg52.h>
#include "iic.h"
sbit S6=P3^1;
sbit S7=P3^0;
unsigned char code SMGNO_P[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char guang;
void SMG();
unsigned char money=0;
unsigned char shuiliang=0;
unsigned char tt=0;
unsigned char yi=0,er=0,san=0,si=0;
void select573(unsigned char n)
{
switch(n)
{
case 4:P2=(P2&0x1f)|0x80;break;
case 5:P2=(P2&0x1f)|0xa0;break;
case 6:P2=(P2&0x1f)|0xc0;break;
case 7:P2=(P2&0x1f)|0xe0;break;
}
}
void init()
{
select573(4);
P0=0xff;
select573(5);
P0=0x00;
select573(6);
P0=0xff;
select573(7);
P0=0xff;
}
void SMG_bit(unsigned char pos,unsigned char dat)
{
select573(6);
P0=0x01<<pos;
select573(7);
P0=dat;
}
void delay(unsigned int t)
{
unsigned int i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
void keydelay(unsigned int t)
{
while(t--)
{
SMG();
}
}
void initTime0()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
}
void Time0()interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
tt++;
if(tt==2)
{
shuiliang=shuiliang+1; //??100ms,?ó0.01L??
yi=shuiliang/1000;
er=(shuiliang/100)%10;
san=(shuiliang/10)%10;
si=shuiliang%10;
tt=0;
if(shuiliang>9999)
{
select573(5);
P0=0x00;
money=shuiliang*0.5;
shuiliang=0;
}
}
}
void keyscan()
{
if(S7==0)
{
keydelay(5);
if(S7==0)
{
TR0=1;
select573(5);
P0=0x10;
}
while(!S7);
}
else if(S6==0)
{
keydelay(5);
if(S6==0)
{
TR0=0;
select573(5);
P0=0x00;
money=shuiliang*0.5;
yi=money/1000;
er=(money/100)%10;
san=(money/10)%10;
si=money%10;
shuiliang=0;
}
while(!S6);
}
}
void SMG()
{
SMG_bit(0,0xff);delay(10);
SMG_bit(1,SMGNO_P[0]&0x7f);delay(10);
SMG_bit(2,SMGNO_P[0]);delay(10);
SMG_bit(3,SMGNO_P[5]);delay(10);
SMG_bit(4,SMGNO_P[yi]);delay(10);
SMG_bit(5,SMGNO_P[er]&0x7f);delay(10);
SMG_bit(6,SMGNO_P[san]);delay(10);
SMG_bit(7,SMGNO_P[si]);delay(10);
select573(6);
P0=0xff;
select573(7);
P0=0xff;
}
void main()
{
init();
initTime0();
while(1)
{
guang=ICC_read(0x01);
if(guang>64)
{
select573(4);
P0=0xfe;
}
else
{
select573(4);
P0=0xff;
}
keyscan();
SMG();
}
}
|