这是关于蓝桥杯备赛路上练习的小程序,蓝桥杯的竞赛路上希望大家互相勉励。
祝大家有个好成绩。
以下代码是我利用手头上小红板(JW-51)训练的小程序。
希望大家一同进步。
按下独立按键key3,可切换显示 {
0:滑动变阻器
1:光敏电阻
2:热敏电阻
}
#include <reg52.h>
#include <intrins.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit ser=P2^7;
sbit srclk=P2^6;
sbit rclk=P2^5;
sbit sda=P2^0;
sbit scl=P2^1;
u8 ADDR=0x90;
u8 strDEC[4];
u8 flag=0;
unsigned char code smgduan[]={0x3f,
0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};
unsigned char code smgwei[]={0xfe,
0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delayms(u16 n);
void send_595(u8 dat);
void display(u8 x,u8 y);
u8 *INT8UtostrDEC(u8 num);
void delay() //5us
{
_nop_();
_nop_();
_nop_();
_nop_();
}
void start()
{
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
void stop()
{
scl=0;
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void Master_ACK(bit i)
{
scl = 0;
_nop_();
if (i)
{
sda = 0;
}
else
{
sda = 1;
}
_nop_();
scl = 1;
delay();
scl = 0;
_nop_();
sda= 1;
_nop_();
}
bit Test_ACK()
{
scl = 1;
delay();
if (sda)
{
scl = 0;
_nop_();
stop();
return(0);
}
else
{
scl = 0;
_nop_();
return(1);
}
}
bit IICWRITE(u8 dat)
{
u8 i;
for(i = 0 ; i < 8 ; i++)
{
scl = 0;
_nop_();
if (dat & 0x80)
{
sda = 1;
_nop_();
}
else
{
sda = 0;
_nop_();
}
scl = 1;
_nop_();
dat <<= 1; }
scl = 0;
_nop_();
sda = 1;
_nop_();
}
u8 IICREDACK()
{
u8 dat,i;
scl= 0;
_nop_();
sda = 1;
_nop_();
for(i = 0 ; i < 8 ; i++)
{
scl = 1;
_nop_();
if (sda)
{
dat |= 0x01; //0000 0001
}
else
{
dat &= 0xfe; //1111 1110
}
_nop_();
scl = 0 ;
_nop_();
if(i < 7)
{
dat = dat << 1;
}
}
return(dat);
}
bit Iread(u8 s,u8 *value)
{
start();
IICWRITE(ADDR + 0);
if (!Test_ACK())
{
return(0);
}
IICWRITE(s+0x04);
Master_ACK(0);
start();
IICWRITE(ADDR + 1);
if (!Test_ACK())
{
return(0);
}
*value = IICREDACK();
Master_ACK(0);
stop();
return(1);
}
void main()
{
u8 ADC_Value;
EA=1;
EX0=1;
IT0=1;
while(1)
{
display(1,flag);
Iread(flag,&ADC_Value);
INT8UtostrDEC(ADC_Value);
display(4,strDEC[0]);
display(5,strDEC[1]);
display(6,strDEC[2]);
}
}
void display(u8 x,u8 y)
{
send_595(smgwei[x]);
send_595(smgduan[y]);
rclk=0;
rclk=1;
delayms(2);
}
void send_595(u8 dat)
{
u8 i;
for(i=0;i<8;i++)
{
if(dat&0x80)
ser=1;
else
ser=0;
srclk=0;
srclk=1;
dat=dat<<1;
}
}
void delayms(u16 n)
{
u8 i;
while(n--)
for(i=0;i<113;i++);
}
u8 *INT8UtostrDEC(u8 num)
{
u8 i2, i1, i0,i;
i2 = num / 100;
i1 = num % 100 / 10;
i0 = num % 100 % 10;
strDEC[i]=i2;
i++;
strDEC[i]=i1;
i++;
strDEC[i]=i0;
i++;
strDEC[i] = '\0';
return strDEC;
}
void Ex0() interrupt 0
{
EX0=0;
flag=flag+1;
if (flag==3)
flag=0;
EX0=1;
}说起来也奇怪,我用了 纯按键功能切换,标志位切换还有switch切换。结果都不尽人意,最后只好选择了中断切换,很搞不懂。
还有关于数值显示,显示精度也时常改变,不知道哪里出了问题。
这个程序是我一点一点扣键盘,抠出来的。不解之处也还要继续学习。
我要赚赏金
