一、题目
历届的省赛和国赛的题目我已经在前面的文章(点击查看)里给大家分享了(网盘资源),需要的话,直接去下载,我在这里就不再赘述了。
二、hex文件
读者下载这个文件然后用烧录软件直接烧入单片机就可以用了!
链接:https://pan.baidu.com/s/1msXP3g9RfTRvvAbicNgG9w
提取码:52sv
三、主函数实现
提示:比赛过程中,仅仅主函数修改可能不够,有的时候需要注意,比赛官方给的各个驱动的代码是否写完整了,比如有时候,它的.h文件中就没有把这些写全,故意注释掉,你需要去对应的.c文件里找都需要一些什么函数,一个个都补全了才行。
另外,我的代码都是完全在一个文件中写完的,所以各位读者大大用起来就比较方便,可以直接拷贝我的.c文件也可以把内容复制粘贴走,放到你想要的地方去。
上代码:
# include "reg52.h"
# include "iic.h"
# include "absacc.h"
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int rb2 = 0; //存放读取的Rb2的数据
unsigned char mode = 1; //流水灯模式
unsigned int interval[4] = {
400,400,400,400}; //4种模式流水灯的流转间隔
unsigned char bright_grade = 4; //led灯的亮度等级
unsigned char duanma[18] = {
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f}; //段码值
unsigned char led_mode[24] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0x7e,0xbd,0xdb,0xe7,0xe7,0xdb,0xbd,0x7e}; //24种led流转模式
unsigned char led_stat = 0; //led模式对应的索引值
unsigned int count = 0; //定时器T0的计数变量
unsigned char k7 = 0; //k7的标志位
unsigned char k6 = 0; //k6的标志位
unsigned int smg_c = 0; //定时器数码管计数变量
unsigned char smg_f = 0; //数码管闪烁标志变量
unsigned char pwm_t = 100; //脉宽占空比
unsigned int pwm_c = 0; //脉宽计数变量
unsigned char pwm_f = 1; //灯亮灭标志变量
void ExecuteRb2 ();
void LEDRunning ();
//======================初始化函数=========================
void InitSystem()
{
XBYTE[0x8000] = 0xff;
XBYTE[0xa000] = 0x00;
}
//=========================================================
//====================24C02数据读写操作====================
unsigned char Read24C02(unsigned char addr)
{
unsigned char tmp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
tmp = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return tmp;
}
void Write24C02 (unsigned char addr,unsigned char dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(addr);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
//=========================================================
//====================读取Rb2电位器的值====================
void Readrb2 ()
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
rb2 = IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
}
//=========================================================
//=====================数码管显示函数======================
void ShowSMG_bit (unsigned char pos,unsigned char dat)
{
XBYTE[0xc000] = 0x01 << pos - 1;
XBYTE[0xe000] = dat;
}
void Delay_SMG (unsigned int t)
{
while (t--);
}
void AllSMG(unsigned char dat)
{
XBYTE[0xe000] = 0xff;
XBYTE[0xc000] = 0xff;
XBYTE[0xe000] = dat;
}
void ShowSMG_M ()
{
ShowSMG_bit(1,duanma[16]);
Delay_SMG(500);
if (((k6 == 1) & (smg_f == 1)) | (k6 == 0) | (k6 == 2))
{
ShowSMG_bit(2,duanma[mode]);
Delay_SMG(500);
}
ShowSMG_bit(3,duanma[16]);
Delay_SMG(500);
ShowSMG_bit(4,0xff);
Delay_SMG(500);
if (((k6 == 2) & (smg_f == 1)) | (k6 == 0) | (k6 == 1))
{
if (interval[mode - 1] > 999)
{
ShowSMG_bit(5,duanma[interval[mode - 1] / 1000]);
Delay_SMG(500);
}
ShowSMG_bit(6,duanma[(interval[mode - 1] / 100) % 10]);
Delay_SMG(500);
ShowSMG_bit(7,duanma[(interval[mode - 1] / 10) % 10]);
Delay_SMG(500);
ShowSMG_bit(8,duanma[interval[mode - 1] % 10]);
Delay_SMG(500);
}
AllSMG(0xff);
}
void ShowSMG_L ()
{
ShowSMG_bit(1,0xff);
Delay_SMG(500);
ShowSMG_bit(2,0xff);
Delay_SMG(500);
ShowSMG_bit(3,0xff);
Delay_SMG(500);
ShowSMG_bit(4,0xff);
Delay_SMG(500);
ShowSMG_bit(5,0xff);
Delay_SMG(500);
ShowSMG_bit(6,duanma[16]);
Delay_SMG(500);
ShowSMG_bit(7,duanma[bright_grade]);
Delay_SMG(500);
ShowSMG_bit(8,duanma[16]);
Delay_SMG(500);
AllSMG(0xff);
}
//=========================================================
//=====================定时器T0 100us======================
void InitTime0 ()
{
TMOD = 0x01;
TH0 = (65535 - 100) / 256;
TL0 = (65535 - 100) % 256;
ET0 = 1;
EA = 1;
TR0 = 1;
}
void ServiceTime0 () interrupt 1
{
TH0 = (65535 - 100) / 256;
TL0 = (65535 - 100) % 256;
if (k7 == 1)
{
count++;
}
if (count >= (interval[mode - 1] * 10))
{
led_stat++;
count = 0;
}
smg_c++;
if (smg_c >= 5000)
{
smg_c = 0;
if (smg_f == 0)
{
smg_f = 1;
}
else if (smg_f == 1)
{
smg_f = 0;
}
}
pwm_c++;
if (pwm_c >= pwm_t)
{
pwm_f = 0;
if (pwm_c >= 100)
{
pwm_c = 0;
pwm_f = 1;
}
}
}
//=========================================================
//=====================浏览按键============================
void Delay_Key (unsigned char t)
{
while (t--);
}
void ScanKey ()
{
if (S7 == 0)
{
Delay_Key(100);
if (S7 == 0)
{
while(S7 == 0)
{
ShowSMG_M ();
}
if (k7 == 0)
{
k7 = 1;
}
else if (k7 == 1)
{
k7 = 0;
}
}
}
else if (S6 == 0)
{
Delay_Key(100);
if (S6 == 0)
{
while(S6 == 0)
{
ShowSMG_M ();
}
if (k6 == 0)
{
k6 = 1;
}
else if (k6 == 1)
{
k6 = 2;
}
else if (k6 == 2)
{
k6 = 0;
}
}
}
else if (S5 == 0)
{
Delay_Key(100);
if (S5 == 0)
{
while(S5 == 0)
{
ShowSMG_M ();
}
if (k6 == 1)
{
mode++;
if (mode >= 4)
{
mode = 4;
}
}
else if (k6 == 2)
{
interval[mode - 1] = interval[mode - 1] + 100;
if (interval[mode - 1] >= 1200)
{
interval[mode - 1] = 1200;
}
if (mode == 1)
{
Write24C02(0x00,interval[mode - 1] / 10);
}
else if (mode == 2)
{
Write24C02(0x01,interval[mode - 1] / 10);
}
else if (mode == 3)
{
Write24C02(0x02,interval[mode - 1] / 10);
}
else if (mode == 4)
{
Write24C02(0x03,interval[mode - 1] / 10);
}
}
}
}
else if (S4 == 0)
{
Delay_Key(100);
if (S4 == 0)
{
if (k6 == 0)
{
while(S4 == 0)
{
ExecuteRb2 ();
LEDRunning ();
ShowSMG_L ();
}
}
else if (k6 == 1)
{
while(S4 == 0)
{
ShowSMG_M ();
}
mode = mode -1;
if (mode <=1)
{
mode = 1;
}
}
else if (k6 == 2)
{
while(S4 == 0)
{
ShowSMG_M ();
}
interval[mode - 1] = interval[mode - 1] - 100;
if (interval[mode - 1] <= 400)
{
interval[mode - 1] = 400;
}
if (mode == 1)
{
Write24C02(0x00,interval[mode - 1] / 10);
}
else if (mode == 2)
{
Write24C02(0x01,interval[mode - 1] / 10);
}
else if (mode == 3)
{
Write24C02(0x02,interval[mode - 1] / 10);
}
else if (mode == 4)
{
Write24C02(0x03,interval[mode - 1] / 10);
}
}
}
}
}
//=========================================================
//=======================LED工作===========================
void LEDRunning ()
{
if (mode == 1)
{
if (led_stat > 7)
{
led_stat = 0;
}
if (pwm_f == 1)
{
XBYTE[0x8000] = led_mode[led_stat];
}
else if (pwm_f == 0)
{
XBYTE[0x8000] = 0xff;
}
}
else if (mode == 2)
{
if (led_stat < 8 | led_stat > 15)
{
led_stat = 8;
}
if (pwm_f == 1)
{
XBYTE[0x8000] = led_mode[led_stat];
}
else if (pwm_f == 0)
{
XBYTE[0x8000] = 0xff;
}
}
else if (mode == 3)
{
if (led_stat < 16 | led_stat > 19)
{
led_stat = 16;
}
if (pwm_f == 1)
{
XBYTE[0x8000] = led_mode[led_stat];
}
else if (pwm_f == 0)
{
XBYTE[0x8000] = 0xff;
}
}
else if (mode == 4)
{
if (led_stat < 20 | led_stat > 23)
{
led_stat = 20;
}
if (pwm_f == 1)
{
XBYTE[0x8000] = led_mode[led_stat];
}
else if (pwm_f == 0)
{
XBYTE[0x8000] = 0xff;
}
}
}
//=========================================================
//=====================处理rb2的值=========================
void ExecuteRb2 ()
{
Readrb2 ();
if (rb2 <= 60)
{
bright_grade = 1;
pwm_t = 25;
}
else if ((rb2 > 60) & (rb2 <= 120))
{
bright_grade = 2;
pwm_t = 50;
}
else if ((rb2 > 120) & (rb2 <= 180))
{
bright_grade = 3;
pwm_t = 75;
}
else if (rb2 > 180)
{
bright_grade = 4;
pwm_t = 100;
}
}
//=========================================================
//========================主函数===========================
void main (void)
{
InitSystem();
interval[0] = Read24C02(0x00) * 10;
interval[1] = Read24C02(0x01) * 10;
interval[2] = Read24C02(0x02) * 10;
interval[3] = Read24C02(0x03) * 10;
InitTime0 ();
while (1)
{
ExecuteRb2 ();
ScanKey ();
if (k7 == 1)
{
LEDRunning ();
}
else if (k7 == 0)
{
XBYTE[0x8000] = 0xff;
}
ShowSMG_M ();
}
}
//=========================================================
四、整个工程文件
链接:https://pan.baidu.com/s/1ZeYarAOHqbxhSc6-p-8yzA
提取码:hgs1
直接打开这项目如果失败的话,可能是因为keil使用版本问题,我用的是keil3,出现问题的话,可以直接拷贝.c文件的内容,前面我也说了,我的实现过程我在一个.c文件中实现的,方便读者大大取用!
最后
有需要的小伙伴可以随时评论或者私信我,讨论学习过程中的问题,我会尽我所能提供一些帮助的
温馨提示: 关注我不容易让文章走丢哦!
蓝桥杯比赛 单片机组 历届省赛题目解答(代码加注释)剩余参见——https://blog.csdn.net/weixin_45386875/article/details/114136549