第十三届蓝桥杯单片机模拟冲刺-总结1
下面市小蜜蜂老师在公众号发送的题目,临近比赛,大家联系一下吧!
一、题目要求
二、代码如下:
#include "reg52.h"
#include "iic.h"
sbit S6=P3^1;
sbit S5=P3^2;
sbit S4=P3^3;
code unsigned char SMGDot_CA[]={0x40,0x79,0x24,0x30,0x19,
0x12,0x02,0x78,0x00,0x10};
code unsigned char SMGNoDot_CA[]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,
0x88,0x83,0xc6,0xa1,0x86};
unsigned int count=0; //定时器计数数值
unsigned int dat_v=0,temp=0; //电压数据采集数值
unsigned int dat_v1=300; //电压参数设置数值
unsigned int volt=0; //报警计数数值
unsigned char interface=0; //界面切换
unsigned char mode=0; //翻转标志
unsigned char led_stat=0; //LED变量标志
//unsigned char alarm=0; //报警标志
//数码管延时函数
void DelaySMG(unsigned int t)
{
while(t--);
}
//普通延时函数
void Delay(unsigned int k)
{
while(k--);
}
//锁存器选择函数
void SelectHC573(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;
case 1:P2=(P2&0x1f)|0x00;break;
}
P2=(P2&0x1f)|0x00;
}
//单个数码管显示函数
void DisplaySMG_Bit(unsigned char pos,unsigned char value)
{
P0=0x01<<pos;
SelectHC573(6);
P0=value;
SelectHC573(7);
}
//关闭所有数码管函数
void DisplaySMG_All(unsigned char dat)
{
P0=0xff;
SelectHC573(6);
P0=dat;
SelectHC573(7);
}
//数码管显示电压数据函数
void DisplaySMG_U(void)
{
DisplaySMG_Bit(0,0xc1); //-U- 1100 0001
DelaySMG(200);
DisplaySMG_Bit(0,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(1,0xff);
DelaySMG(200);
DisplaySMG_Bit(2,0xff);
DelaySMG(200);
DisplaySMG_Bit(3,0xff);
DelaySMG(200);
DisplaySMG_Bit(4,0xff);
DelaySMG(200);
DisplaySMG_Bit(5,SMGDot_CA[dat_v/100]);
DelaySMG(200);
DisplaySMG_Bit(5,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(6,SMGNoDot_CA[dat_v/10%10]);
DelaySMG(200);
DisplaySMG_Bit(6,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(7,SMGNoDot_CA[dat_v%10]);
DelaySMG(200);
DisplaySMG_Bit(7,0xff); //消隐
DelaySMG(200);
DisplaySMG_All(0xff);
DelaySMG(200);
}
//数码管显示参数设置函数
void DisplaySMG_P(void)
{
DisplaySMG_Bit(0,0x8c); //-P- 1000 1100
DelaySMG(200);
DisplaySMG_Bit(0,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(1,0xff);
DelaySMG(200);
DisplaySMG_Bit(2,0xff);
DelaySMG(200);
DisplaySMG_Bit(3,0xff);
DelaySMG(200);
DisplaySMG_Bit(4,0xff);
DelaySMG(200);
DisplaySMG_Bit(5,SMGDot_CA[dat_v1/100]);
DelaySMG(200);
DisplaySMG_Bit(5,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(6,SMGNoDot_CA[dat_v1/10%10]);
DelaySMG(200);
DisplaySMG_Bit(6,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(7,SMGNoDot_CA[dat_v1%10]);
DelaySMG(200);
DisplaySMG_Bit(7,0xff); //消隐
DelaySMG(200);
DisplaySMG_All(0xff);
DelaySMG(200);
}
//数码管显示报警计数函数
void DisplaySMG_S(void)
{
DisplaySMG_Bit(0,0x92); //-S- 1001 0010
DelaySMG(200);
DisplaySMG_Bit(0,0xff); //消隐
DelaySMG(200);
DisplaySMG_Bit(1,0xff);
DelaySMG(200);
DisplaySMG_Bit(2,0xff);
DelaySMG(200);
DisplaySMG_Bit(3,0xff);
DelaySMG(200);
DisplaySMG_Bit(4,0xff);
DelaySMG(200);
DisplaySMG_Bit(5,0xff); //消隐
DelaySMG(200);
if(volt/10!=0)
{
DisplaySMG_Bit(6,SMGNoDot_CA[volt/10]);
DelaySMG(200);
DisplaySMG_Bit(6,0xff); //消隐
DelaySMG(200);
}
DisplaySMG_Bit(7,SMGNoDot_CA[volt%10]);
DelaySMG(200);
DisplaySMG_Bit(7,0xff); //消隐
DelaySMG(200);
DisplaySMG_All(0xff);
DelaySMG(200);
}
//PCF8591电压采集函数
void PCF8591_ADC(void)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
dat_v=temp*5.0/255*100;
}
//按键函数
void ScanKey(void)
{
if(S4==0)
{
Delay(20);
if(S4==0);
{
while(S4==0)
{
DisplaySMG_U();
}
if(interface==0)
{
interface=1;
}
else if(interface==1)
{
interface=2;
}
else if(interface==2)
{
interface=0;
}
}
}
if(S5==0)
{
Delay(20);
if(S5==0)
{
while(S5==0)
{
DisplaySMG_P();
}
dat_v1=dat_v1-50;
if(dat_v1==-50)
{
dat_v1=500;
}
}
}
if(S6==0)
{
Delay(20);
if(S6==0)
{
while(S6==0)
{
DisplaySMG_P();
}
dat_v1=dat_v1+50;
if(dat_v1>=500)
{
dat_v1=0;
}
}
}
}
//LED显示函数
void LEDRunning(void)
{
// if(dat_v<dat_v1)
// {
// if(alarm==0)
// {
// alarm=1;
// }
// }
if(dat_v>dat_v1)
{
//alarm=0;
mode=0;
count=0;
volt=0;
led_stat|=0xff;
P0=led_stat;
SelectHC573(4);
}
}
//定时器函数
void Init_Timer(void)
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
ET0=1;
TR0=1;
EA=1;
}
//系统初始化函数
void SystemInit(void)
{
P0=0xff;
SelectHC573(4);
P0=0x00;
SelectHC573(5);
DisplaySMG_All(0xff);
}
//主函数
void main(void)
{
SystemInit();
Init_Timer();
while(1)
{
ScanKey();
PCF8591_ADC();
switch(interface)
{
case 0:DisplaySMG_U();break;
case 1:DisplaySMG_P();break;
case 2:DisplaySMG_S();break;
}
LEDRunning();
}
}
//定时期中断0服务函数
void Service_Time0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(dat_v<dat_v1/*alarm==1*/) //启动报警
{
count++;
if(count%20==0) //1s
{
if(volt!=99)
{
volt++;
}
if(mode==1) //启动闪烁标志
{
if((led_stat&0x80)==0x80)
{
led_stat=(~0x80);
}
else
{
led_stat|=0x80;
}
}
}
if(count>120)
{
led_stat&=(~0x07);
mode=1;
}
else if(count>80)
{
led_stat&=(~0x03);
}
else if(count>40)
{
led_stat&=(~0x01);
}
else
{
led_stat=0xff;
}
P0=led_stat;
SelectHC573(4);
}
}
三、运行结果
小伙伴们,明天就比赛了,在比赛之前熟悉各个模板吧!