【蓝桥杯-单片机省赛】第十届
2022/4/20 23:13:08
本文主要是介绍【蓝桥杯-单片机省赛】第十届,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1.主函数main.c
#include "stc15f2k60s2.h" #include "hc138.h" #include "smg.h" #include "key.h" #include "fre.h" #include "iic.h" extern unsigned char flag_500ms; extern bit swit; extern bit dac_out; extern bit led_swi; extern bit smg_swi; long count = 0;//频率读取 unsigned int adc = 0;//rb2 电压读取 unsigned char led[6]={0,0,0,0,0,0}; void swit_fun(void); void dispay_fun(void); void dac_fun(void); void led_fun(void); void main(void) { all_init(); Timer2Init(); Timer0Init(); while(1) { BIN(); swit_fun(); dispay_fun();//界面切换 dac_fun();//DAC 输出 led_fun(); } } void led_fun(void) { if (led_swi == 1) { hc138(4); P0 = 0xff; P2 &= 0x1f; } else if (led_swi == 0) { /*--------- L1 与 L2----------*/ if (swit == 0) {//ADC L1 // hc138(4); // P0 = 0xfe; // P2 &= 0x1f; /*----L3-----*/ if ((adc>=150 && adc < 250) || adc>350) led[3] = 1; else led[3] = 0; /*-----L5--------*/ if (dac_out == 0)//2.0v led[5]=0; else led[5]=1; if (led[3] == 1 && led[5] == 1){ hc138(4); P0 = ~0x15; P2 &= 0x1f; } else if (led[3] == 0 && led[5] == 1) { hc138(4); P0 = ~0x11; P2 &= 0x1f; } else if (led[3] == 1 && led[5] == 0) { hc138(4); P0 = ~0x05; P2 &= 0x1f; } else { hc138(4); P0 = 0xfe; P2 &= 0x1f; } } else if (swit == 1) {//L2 /*----L4-----*/ if ((count>=1000 && count < 5000) || count>10000) led[4] = 1; else led[4] = 0; if (led[4] == 1) { hc138(4); P0 = ~0x0a; P2 &= 0x1f; } else { hc138(4); P0 = ~0x02; P2 &= 0x1f; } } } } void dac_fun(void) { if (dac_out == 0) DAC(103);//2.0v else if (dac_out == 1) DAC(adc); } void swit_fun(void) { if (swit == 0) adc = ADC_read(3)*1.963; else { if (flag_500ms >= 250) { count = Read_fre(); count *= 2; flag_500ms = 0; } } } void dispay_fun(void) { if (smg_swi == 1) { smgdat[0] = 21;//U smgdat[1] = 21; smgdat[2] = 21; smgdat[3] = 21; smgdat[4] = 21; smgdat[5] = 21; smgdat[6] = 21; smgdat[7] = 21; } else { if (swit == 0) { //电压测量 smgdat[0] = 23;//U smgdat[1] = 21; smgdat[2] = 21; smgdat[3] = 21; smgdat[4] = 21; smgdat[5] = adc /100 + 10; smgdat[6] = adc % 100 /10; smgdat[7] = adc % 10; } else if (swit == 1) { //频率测量 if (flag_500ms >= 250) { count = Read_fre(); count *= 2; flag_500ms = 0; } smgdat[0] = 22;//F smgdat[2] = 21; smgdat[1] = 21; smgdat[6] = count % 100 /10; smgdat[7] = count % 10; if (count % 100000/ 10000 != 0) { smgdat[2] = 21; smgdat[3] = count %100000/ 10000; smgdat[4] = count % 10000 /1000; smgdat[5] = count % 1000 /100; } else if (count % 10000 /1000 != 0) { smgdat[2] = 21; smgdat[3] = 21; smgdat[4] = count % 10000 /1000; smgdat[5] = count % 1000 /100; } else if (count % 1000 /100 != 0) { smgdat[2] = 21; smgdat[3] = 21; smgdat[4] = 21; smgdat[5] = count % 1000 /100; } } } }
2.频率测量
#include "stc15f2k60s2.h" void Timer0Init(void) //100微秒@12.000MHz { AUXR |= 0x80; //定时器时钟1T模式 TMOD &= 0xF0; //设置定时器模式 TMOD |= 0x04; TL0 = 0; //设置定时初值 TH0 = 0; //设置定时初值 TF0 = 0; //清除TF0标志 TR0 = 0; //定时器0开始计时 EA = 1; ET0 = 0; } unsigned int Read_fre(void) { unsigned int count_f = 0; TR0 = 0; count_f = (TH0<<8) | TL0; TH0 = 0; TL0 = 0; TR0 = 1; return count_f; }
3. iic.c ADC和DAC
// ADC 数 模 unsigned char ADC_read(unsigned char channel) { unsigned char temp; EA = 0; IIC_Start(); IIC_SendByte(0x90);//写入 IIC_WaitAck(); IIC_SendByte(channel);// 1 光敏电阻 3 滑动变阻器 IIC_WaitAck(); IIC_Start(); IIC_SendByte(0x91);//读 IIC_WaitAck(); temp = IIC_RecByte(); IIC_WaitAck(); IIC_Stop(); EA = 1; return temp; } // DAC 数 模 void DAC(unsigned char dat) { EA = 0; IIC_Start(); IIC_SendByte(0x90); IIC_WaitAck(); IIC_SendByte(0x40); IIC_WaitAck(); IIC_SendByte(dat); IIC_WaitAck(); IIC_Stop(); EA = 1; }
4.key.c 按键
#include "stc15f2k60s2.h" #include "hc138.h" sbit S7 = P3^0; sbit S6 = P3^1; sbit S5 = P3^2; sbit S4 = P3^3; /* 界面切换 0 电压测量 1 频率测量 */ bit swit = 0; /* DAC 输出模式 0 DAC=2.0 1 DAC=VRB2 */ bit dac_out = 0; /* LED 指示 0 开启 1 关闭 */ bit led_swi = 0; /* 数码管显示 0 开启 1 关闭 */ bit smg_swi = 0; void Delay5ms() //@12.000MHz { unsigned char i, j; i = 59; j = 90; do { while (--j); } while (--i); } void BIN(void) { if (S7 == 0) { Delay5ms(); if (S7 == 0) { if (smg_swi == 0) smg_swi = 1; else if (smg_swi == 1) smg_swi = 0; } while(!S7); } else if (S6 == 0) { Delay5ms(); if (S6 == 0) { if (led_swi == 0) led_swi = 1; else if (led_swi == 1) led_swi = 0; } while(!S6); } else if (S5 == 0) { Delay5ms(); if (S5 == 0) { if (dac_out == 0) dac_out = 1; else if (dac_out == 1) dac_out = 0; } while(!S5); } else if (S4 == 0) { Delay5ms(); if (S4 == 0) { if (swit == 0) swit = 1; else if (swit == 1) swit = 0; } while(!S4); } }
5.数码管显示 smg.c
#include "stc15f2k60s2.h" #include "hc138.h" unsigned char smgdat[8]={21,21,21,21,21,21,21,21}; unsigned char code SMG[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90, 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10, 0xbf,0xff,~0x71,~0x3e}; // F U unsigned char POS; unsigned char flag_500ms = 0; void Timer2Init(void) //2毫秒@12.000MHz { AUXR |= 0x04; //定时器时钟1T模式 T2L = 0x40; //设置定时初值 T2H = 0xA2; //设置定时初值 AUXR |= 0x10; //定时器2开始计时 /*后加的部分*/ EA=1; IE2|=0x04; } void display(unsigned char pos,dat){ hc138(6);P0=0x01<<pos;P2&=0x1f; hc138(7);P0=dat; P2&=0x1f; } void T2_Display()interrupt 12 { hc138(6);P0=0x00; display(POS,SMG[smgdat[POS]]); if(++POS==8)POS=0; flag_500ms++; }
这篇关于【蓝桥杯-单片机省赛】第十届的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-0601-电商商品中心解密:仅凭SKU真的足够吗?
- 2024-05-01为什么公共事业机构会偏爱 TiDB :TiDB 数据库在某省妇幼健康管理系统的应用
- 2024-04-26敏捷开发:想要快速交付就必须舍弃产品质量?
- 2024-04-26静态代码分析的这些好处,我竟然都不知道?
- 2024-04-26你在测试金字塔的哪一层?(下)
- 2024-04-26快刀斩乱麻,DevOps让代码评审也自动起来
- 2024-04-262024年最好用的10款ER图神器!
- 2024-04-2203-为啥大模型LLM还没能完全替代你?
- 2024-04-2101-大语言模型发展
- 2024-04-17基于SpringWeb MultipartFile文件上传、下载功能