第十届蓝桥杯单片机国赛源码
Posted 时光654
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第十届蓝桥杯单片机国赛源码相关的知识,希望对你有一定的参考价值。
这是本人第二次刷国赛程序了,感觉轻松了很多,主函数用了3个小时完成。
本次分享主函数,内部用到了一些巧的思想,而且几乎没有多余语句。
如果你都能理解,你一定会有一个提高。
(我可能会展示两天就删了)
#include "bsp_csb.h"
#include "bsp_ds18b20.h"
#include "bsp_iic.h"
#include "bsp_init.h"
#include "bsp_key.h"
#include "bsp_led.h"
#include "bsp_turn.h"
#include "sys_timer.h"
#include "sys_uart.h"
#include "stdio.h"
//****************************函数声明
void seg_pricedure();
void key_pricedure();
void per_pricedure();
//****************************全局变量
/*按键*/
unsigned char key_number,key_down,key_up,Key_old;
/*显示*/
unsigned char seg_turn[8];
unsigned char seg_buf[10];
unsigned char seg_temp;
unsigned char ucled;
/*减速*/
unsigned char key_slow_down;
unsigned char seg_slow_down;
unsigned int per_slow_down;
/*标志位*/
unsigned char running_mode;
// 0x00: 温度数据
// 0x01: 距离数据
// 0x02: 变更次数
// 0x10: 温度参数
// 0x11: 距离参数
/*外设*/
unsigned int temper_number; //温度值
unsigned char read_distance;//超声波距离
unsigned char change_data; //数据更改次数
struct number
{
unsigned char temper; //温度参数
unsigned char distance; //距离参数
};
struct number parameter = {30,35};
struct number old = {30,35};
bit dac_output_mode = 1; //DA 输出模式 0——模式0 1——模式1
/*串口*/
unsigned char uart_send_buf[12]; //串口发送缓存
unsigned char uart_read_buf[8];
unsigned char uart_temp_number = 0;
/*定时*/
unsigned long dida_number;
unsigned long key_delay;
void main()
{
timer1_init();
close_peripheral();
timer0_init();
uart1_init();
eeprom_read_fun(&change_data,0,1);
while(1)
{
per_pricedure();
key_pricedure();
seg_pricedure();
}
}
//****************************中断
void timer0() interrupt 1
{
dida_number++;
if(++key_slow_down == 20) key_slow_down = 0;
if(++seg_slow_down == 200) seg_slow_down = 0;
if(++per_slow_down == 400) per_slow_down = 0;
seg_disp_fun(seg_turn,seg_temp);
if(++seg_temp == 8) seg_temp = 0;
led_disp(ucled);
}
void uart1() interrupt 4
{
if(RI)
{
RI = 0;
uart_read_buf[uart_temp_number++] = SBUF;
}
}
//****************************按键
void key_pricedure()
{
if(key_slow_down) return;
key_slow_down = 1;
key_number = key_trigger();
key_down = (key_number ^ Key_old) & key_number;
key_up = (~key_number) & (key_number ^ Key_old);
Key_old = key_number;
if(key_down) key_delay = dida_number;
//************短按按键
if(dida_number - key_delay < 1000)
switch(key_up)
{
case 13:
running_mode &= 0xf0;
running_mode ^= 0x10;
if(((old.temper != parameter.temper) | (old.distance != parameter.distance)) && (running_mode == 0x00))
{
old.temper = parameter.temper;
old.distance = parameter.distance;
change_data++;
eeprom_wrire_fun(&change_data,0,1);
}
break;
case 12:
if((running_mode & 0x10) == 0x00)
{
if(++running_mode == 0x03) running_mode = 0x00;
}
else
{
if(++running_mode == 0x12) running_mode = 0x10;
}
break;
case 16: //-
if(running_mode == 0x10) //温度参数
{
parameter.temper -= 2;
if(parameter.temper <= 0) parameter.temper = 99;
}
else if(running_mode == 0x11) //距离参数
{
parameter.distance -= 5;
if(parameter.distance <= 0) parameter.distance = 95;
}
break;
case 17: //+
if(running_mode == 0x10) //温度参数
{
parameter.temper += 2;
if(parameter.temper > 99) parameter.temper = 0;
}
else if(running_mode == 0x11) //距离参数
{
parameter.distance += 5;
if(parameter.distance > 99) parameter.distance = 0;
}
break;
}
//************长按按键
else
switch(key_up)
{
case 12:
change_data = 0;
eeprom_wrire_fun(&change_data,0,1);
break;
case 13:
dac_output_mode ^= 1;
break;
}
}
//****************************显示
void seg_pricedure()
{
if(seg_slow_down) return;
seg_slow_down = 1;
switch(running_mode)
{
case 0x00:
sprintf(seg_buf,"C %5.2f",(temper_number / 16.0));
break;
case 0x01:
sprintf(seg_buf,"L %2d",(unsigned int)read_distance);
break;
case 0x02:
sprintf(seg_buf,"N %3d",(unsigned int)change_data);
break;
case 0x10:
sprintf(seg_buf,"P 1 %2d",(unsigned int)parameter.temper);
break;
case 0x11:
sprintf(seg_buf,"P 2 %2d",(unsigned int)parameter.distance);
break;
}
seg_turn_fun(seg_turn,seg_buf);
}
//****************************外设
void per_pricedure()
{
if(per_slow_down) return;
per_slow_down = 1;
temper_number = read_temperature_fun();
read_distance = read_csb_number();
//*************DAC
if(dac_output_mode) //DAC 开启状态
{
if(read_distance <= parameter.distance)
da_turn_fun(102);
else
da_turn_fun(204);
}
else //DAC 关闭状态
da_turn_fun(25);
//*************LED
if((temper_number>>4) > old.temper) ucled |= 0x01;
else ucled &= 0xfe;
if(read_distance < old.distance) ucled |= 0x02;
else ucled &= 0xfd;
if(dac_output_mode) ucled |= 0x04;
else ucled &= 0xfb;
//**************串口
if(uart_read_buf[uart_temp_number - 1] == '\\n')
{
uart_temp_number = 0;
if((uart_read_buf[0] == 'S') && (uart_read_buf[1] == 'T') && (uart_read_buf[2] == '\\r'))
sprintf(uart_send_buf,"$%2d,%2d.%2d\\r\\n",(unsigned int)read_distance,(temper_number>>4),((temper_number * 100) >> 4) % 100);
else if((uart_read_buf[0] == 'P') && (uart_read_buf[1] == 'A') && (uart_read_buf[2] == 'R') && (uart_read_buf[3] == 'A') && (uart_read_buf[4] == '\\r'))
sprintf(uart_send_buf,"#%2d,%2d\\r\\n",(unsigned int)old.distance,(unsigned int)old.temper);
else
sprintf(uart_send_buf,"ERROR\\r\\n");
uart_send_fun(uart_send_buf);
}
else if(uart_temp_number >= 7)
{
uart_temp_number = 0;
sprintf(uart_send_buf,"ERROR\\r\\n");
uart_send_fun(uart_send_buf);
}
}
以上是关于第十届蓝桥杯单片机国赛源码的主要内容,如果未能解决你的问题,请参考以下文章