第十一届蓝桥杯 单片机设计与开发 省赛
在昨天举办了第一波11届蓝桥杯比赛,第2波大概在10月份左右,看了看比赛题目,这次题目对于我来说,程序设计题简单,客观题全靠蒙!
本次比赛涉及:
1.数码管显示
2.矩阵按键
3.AD
4.AT24C02
5. LED
6.定时器
进入正题,先看一下程序设计题目:
-
猜到了这次考试肯定考矩阵键盘,只不过他没有考全部的,只考了后两列,用电位器模拟电压输入和AT24C02都用到了 IIC,定时器用的是定时器0 , 代码功能全部实现
-
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);
void a24c02xie(unsigned char add,unsigned char dat);
unsigned char a24c02read(unsigned char add);
int getad(unsigned char add);
#endif
- iic.c
#include "reg52.h"
#include "intrins.h"
#include<iic.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_();_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;
}
//应答位控制
void IIC_Ack(bit ackbit)
{
if(ackbit)
{
SDA = 0;
}
else
{
SDA = 1;
}
somenop;
SCL = 1;
somenop;
SCL = 0;
SDA = 1;
somenop;
}
//等待应答
bit IIC_WaitAck(void)
{
SDA = 1;
somenop;
SCL = 1;
somenop;
if(SDA)
{
SCL = 0;
IIC_Stop();
return 0;
}
else
{
SCL = 0;
return 1;
}
}
//通过I2C总线发送数据
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总线上接收数据
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;
}
void a24c02xie(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();
}
unsigned char a24c02read(unsigned char add)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
return dat;
}
int getad(unsigned char add)
{
int dat;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
dat=IIC_RecByte();
IIC_Stop();
dat=dat*1.961;
return dat;
}
- main.c
#include<stc15f2k60s2.h>
#include<iic.h>
typedef unsigned int uint;
typedef unsigned char uchar;
int yi,er,san,si,wu,liu,qi,ba;
uchar code smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xc1,0x8c,0xc8,0xff,
0xc0&0x7f,0xf9&0x7f,0xa4&0x7f,0xb0&0x7f,0x99&0x7f,0x92&0x7f,0x82&0x7f,0xf8&0x7f,0x80&0x7f,0x90&0x7f};
int dianya,jishu,diancp,jiemian,flag,t,tt,liang,wuxiao,flagg,wuu;
void delay1ms(int ms);
void display1();
void display2();
void shangdian();
void Timer0Init(void) ; //1毫秒@12.000MHz
void juzhen();
main()
{
shangdian();
Timer0Init();
diancp=a24c02read(0);
diancp=diancp*10;
while(1)
{
juzhen();
dianya=getad(0x03);
if(jiemian==0)//数据界面
{
yi=10;er=san=si=wu=13;liu=dianya/100+14;qi=dianya%100/10;ba=dianya%10;
}
else if(jiemian==1)//参数界面
{
yi=11;er=san=si=wu=13;liu=diancp/100+14;qi=diancp%100/10;ba=diancp%10;
}
else if(jiemian==2)//计数界面
{
yi=12;er=san=si=wu=liu=13;qi=jishu/10;ba=jishu%10;
}
if(dianya>diancp)
{
flag=1;
t=tt=0;
TR0=0;
liang=0;
flagg=0;
}
else if(dianya<diancp)
{
TR0=1;
flagg=1;
}
if((flag==1)&&(flagg==1))
{
jishu++;
flag=0;
flagg=0;
}
if((liang==0)&&(jishu%2==0)&&(wuu==0))
{
P2=0x80;P0=0xFF;
}
else if((liang==0)&&(jishu%2==0)&&(wuu==1))
{
P2=0x80;P0=~(0x04);
}
else if((liang==0)&&(jishu%2==1)&&(wuu==0))
{
P2=0x80;P0=~(0x02);
}
else if((liang==0)&&(jishu%2==1)&&(wuu==1))
{ P2=0x80;P0=~(0x04|0x02);
}
else if((liang==1)&&(jishu%2==0)&&(wuu==0))
{P2=0x80;P0=~(0x01);
}
else if((liang==1)&&(jishu%2==0)&&(wuu==1))
{P2=0x80;P0=~(0x01|0x04);
}
else if((liang==1)&&(jishu%2==1)&&(wuu==0))
{P2=0x80;P0=~(0x01|0x02);
}
else if((liang==1)&&(jishu%2==1)&&(wuu==1))
{ P2=0x80;P0=~(0x01|0x02|0x04);
}
display1();
display2();
}
}
void shangdian()
{
P2=0xA0;P0=0x00;
P2=0x80;P0=0xFF;
P2=0xC0;P0=0xFF;
P2=0xE0;P0=0xFF;
}
void delay1ms(int ms)
{
int i;
for(;ms>0;ms--)
{
for(i=845;i>0;i--) ;
}
}
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x20; //设置定时初值
TH0 = 0xD1; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 0; //定时器0开始计时
EA=1;
ET0=1;
}
void time0() interrupt 1
{
t++;
if(t==1000)
{ t=0;
tt++;
if(tt==5)
{
liang=1;
}
}
}
void display1()
{
P2=0xC0;P0=0x01;
P2=0xE0;P0=smg[yi];
delay1ms(1);
P2=0xC0;P0=0x02;
P2=0xE0;P0=smg[er];
delay1ms(1);
P2=0xC0;P0=0x04;
P2=0xE0;P0=smg[san];
delay1ms(1);
P2=0xC0;P0=0x08;
P2=0xE0;P0=smg[si];
delay1ms(1);
}
void display2()
{
P2=0xC0;P0=0x10;
P2=0xE0;P0=smg[wu];
delay1ms(1);
P2=0xC0;P0=0x20;
P2=0xE0;P0=smg[liu];
delay1ms(1);
P2=0xC0;P0=0x40;
P2=0xE0;P0=smg[qi];
delay1ms(1);
P2=0xC0;P0=0x80;
P2=0xE0;P0=smg[ba];
delay1ms(1);
P2=0xC0;P0=0x80;
P2=0xE0;P0=0xff;
}
void juzhen()
{
uchar temp;
P44=1;P42=1;P3=0xdF;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay1ms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xdb:
if(jiemian==2){
wuu=0;
jishu=0;}
else {
wuxiao++;
if(wuxiao==3)
{ wuu=1;
wuxiao=0;
}}
break;
case 0xd7:
wuu=0;
jiemian++;
if(jiemian==2)
{
a24c02xie(0,diancp/10);
delay1ms(5);
}
if(jiemian==3) jiemian=0;
break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
P44=1;P42=1;P3=0xeF;
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
delay1ms(5);
temp=P3;
temp=temp&0x0f;
if(temp!=0x0f)
{
temp=P3;
switch(temp)
{
case 0xeb:
if(jiemian==1){
wuu=0;
diancp=diancp-50;
if(diancp==-50)
{
diancp=500;
}}
else {wuxiao++;
if(wuxiao==3)
{wuu=1;wuxiao=0;}
}
break;
case 0xe7:
if(jiemian==1){
wuu=0;
diancp=diancp+50;
if(diancp==550)
{
diancp=0;
}}
else {wuxiao++;
if(wuxiao==3)
{ wuu=1;
wuxiao=0;}}
break;
}
while(temp!=0x0f)
{
temp=P3;
temp=temp&0x0f;
}
}
}
}