首页 > 其他分享 >第十四届蓝桥杯单片机省赛真题

第十四届蓝桥杯单片机省赛真题

时间:2024-04-04 21:30:44浏览次数:20  
标签:10 定时器 distance uchar void 蓝桥 单片机 省赛 include

逻辑部分纯手写简单 零基础模板套用即可

main.c

#include "smg.h"
#include "key.h"
#include "led.h"
#include "iic.h"
#include "onewire.h"
#include "ds1302.h"
#include "timer.h"
#include "uart.h"
#include "ult.h"
uchar display_state;
uchar LED[] = {0,0,0,0,0,0,0,0};
uchar SMG[] = {10,10,10,10,10,10,10,10};
uchar dot[] = {0,0,0,0,0,0,0,0};
uchar distance_shezhi = 30;
uchar light_value;
uint temp;
uchar state;
uchar distance_now;
void led_pro()
{
	LED[0] = (display_state == 0)?1:0;
	LED[1] = (display_state == 1)?1:0;
	LED[2] = (distance_now > distance_shezhi)?state:0;
}

void smg_pro()
{
	if(display_dly<200)return;
	display_dly = 0;
	distance_now = read_distance();
	switch(display_state)
	{
		case 0:
			SMG[0] = 11;
			SMG[1] = 1;
			SMG[2] = 10;
			SMG[3] = 10;	
			SMG[4] = 10;
			SMG[5]=(distance_now<100)?10:distance_now/100;
			SMG[6]=(distance_now<10)?10:distance_now/10%10;
			SMG[7]= distance_now %10;	
		break;
		case 1:
			SMG[0] = 11;
			SMG[1] = 2;
			SMG[2] = 10;
			SMG[3] = 10;	
			SMG[4] = 10;
			SMG[5] = distance_shezhi / 100;	
			SMG[6] = distance_shezhi / 10 % 10;
			SMG[7] = distance_shezhi % 10;				
	}
}	

void key_pro()
{
	uchar key;
	if(key_dly < 10) return;
	else key_dly = 0;
	key = key_scan2();
	switch(key)
	{
		case 13:
			display_state ++ ;
			if(display_state == 2)
				display_state = 0;
			break;
		case 14:
			if(display_state == 0)
				distance_shezhi = distance_now;
			break;
		case 15:
			if(display_state == 1)
				distance_shezhi = distance_shezhi + 10;
			break;
		case 16:
			if(display_state == 1)
			{
				if(distance_shezhi >= 10)
					distance_shezhi = distance_shezhi - 10;
				else if(distance_shezhi < 10)
					distance_shezhi = 0;
			}
			break;
		case 10:
			printf("Distance:%dcm\r\n",(uint)distance_now);
			break;
	}

}


void main()
{
	system_init();
	Timer1Init();
	EA = 1;
	UartInit();
	while(1)
	{

		led_pro();
		key_pro();
		smg_pro();
	
	}
}

smg.c

#include "smg.h"
code  segment[] = {
//   0    1    2    3    4    5    6    7    8    9	    熄灭   U
	 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,	0x00,0x3E
};

void smg(uchar * temp,uchar *dot,uchar pos)
{
	P0=0xff;
	hc573(7);
	P0=0x01<<pos;
	hc573(6);
	if(dot[pos]==0)
		P0=~segment[temp[pos]];   
	else
		P0=(~segment[temp[pos]]) & 0x7f;
	hc573(7);
}

led.c

#include "led.h"


void led(uchar * LED,uchar pos)
{
	static uchar temp=0xff;
	if(LED[pos])
		temp &= ~(0x01<<pos);
	else
		temp |= 0x01<<pos;		
	P0=temp;
	hc573(4);
}

timer.c

#include "timer.h"

unsigned long systick_ms;
uchar state_relay;//继电器控制
uchar pos;       //数码管led扫描
uchar key_dly;    //按键调度
uint display_dly,collect_dly; //显示调度,采集调度
uint fre;         //频率变量
uint count;       //1s计时
void Timer0Init(void)		//100微秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x9C;		//设置定时初值
	TH0 = 0xFF;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0 = 1;
}

void Timer0Init_Count(void)		//计数模式
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x05;   //16位不自动重装
	TL0 = 0;		//设置定时初值
	TH0 = 0;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计数
	ET0 = 1;
}

void Timer1Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0xBF;		//定时器时钟12T模式
	TMOD &= 0x0F;		//设置定时器模式
	TL1 = 0x18;		//设置定时初值
	TH1 = 0xFC;		//设置定时初值
	TF1 = 0;		//清除TF1标志
	TR1 = 1;		//定时器1开始计时
	ET1 = 1;
}

void timer0()  interrupt 1
{

}

void timer1()  interrupt 3
{
	if(++count == 200)
	{
		state = ~state;
		count=0;
	}
  systick_ms++;
	key_dly++;
  display_dly++;
  collect_dly++;
	smg(SMG,dot,pos);
	led(LED,pos);
	if(++pos == 8) pos = 0;
}

ult.c

#include "ult.h"
sbit TX = P1^0;  					// 发射引脚
sbit RX = P1^1;  					// 接收引脚

uchar read_distance(void)
{
  uchar distance,num = 10;
  TX = 0;
  CL = 0xF3;						// 设置定时初值
  CH = 0xFF;						// 设置定时初值
  CR = 1;							// 定时器0计时
  // TX引脚发送40KHz方波信号驱动超声波发送探头
  while(num--)
  {
    while(!CF);
    TX ^= 1;
		CL = 0xF3;						// 设置定时初值
		CH = 0xFF;						// 设置定时初值
    CF = 0;
  }
  CR = 0;
  CL = 0;							// 设置定时初值
  CH = 0;							// 设置定时初值
  CR = 1;
  while(RX && !CF);				// 等待收到脉冲
  CR = 0;
  if(CF)							// 发生溢出
  {
    CF = 0;
    distance = 255;
  }
  else							    // 计算距离
    distance = ((CH<<8)+CL)*0.017;
  return distance;
}

sys.c  && delay.c

#include "sys.h"

void hc573(uchar channel)
{
	switch(channel)
	{
		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;
	}
	P2 = P2 & 0x1f;
}

void system_init()
{
	P0 = 0x00;
	hc573(5);
	P0 = 0xff;
	hc573(4);
}

#include "delay.h"

void Delayms(uint xms)		//@12.000MHz
{
	unsigned char i, j;
	while(xms--)
	{
		i = 12;
		j = 169;
		do
		{
			while (--j);
		} while (--i);
	}
}

void Delayus(uint xus)		//@12.000MHz
{
	while(xus--)
	{
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
}

uart.c

#include "uart.h"

uchar Uart_Rxindex;
uchar Uart_Rxbuf[10];
void UartInit(void)					//[email protected]
{ 
  SCON = 0x50;						// 8位数据,可变波特率
  AUXR |= 0x01;						// 串口1选择定时器2为波特率发生器
  AUXR |= 0x04;						// 定时器2时钟为Fosc, 即1T
  T2L = 0x8F;						// 设定定时初值
  T2H = 0xFD; 						// 设定定时初值
  AUXR |= 0x10;						// 启动定时器2
  ES = 1;									// 允许串口中断
}

void Uart0(void) interrupt 4
{
  if(RI)
  {
		if(Uart_Rxindex == 10)
			Uart_Rxindex = 0;
    Uart_Rxbuf[Uart_Rxindex++] = SBUF;
    RI = 0;
  }
}
char putchar(char ch)
{
    SBUF = ch;
    while (TI == 0);   // 等待发送完成
    TI = 0; // 发送完成标志位置0
    return ch;
}

标签:10,定时器,distance,uchar,void,蓝桥,单片机,省赛,include
From: https://blog.csdn.net/yyyyyyysy/article/details/137382936

相关文章

  • 2022蓝桥杯大赛软件类国赛真题 卡牌
    importjava.util.Scanner;publicclassMain{staticfinalintN=200005;staticlongn,m;staticint[]a=newint[N];staticint[]b=newint[N];publicstaticvoidmain(String[]args){Scannersc=newScanner(System.in......
  • 第十四届蓝桥杯B组c/c++第五题接龙数列
    动态规划  接龙数列我打眼一看感觉得用栈stack,取出首位和末位全都入栈,每次弹出栈顶,获取此时的栈顶并弹出和下一个栈顶比较。整了老半天发现不行,原来是我脑子瓦特了。虽然没有用栈解决这道问题,但是,栈和队列都是非常重要的只是,不了解的同学们可以去学习一下,下面有传送门。......
  • 蓝桥杯算法题:开心
    https://www.lanqiao.cn/problems/3824/learningeg:n=1234,k=2可以简单的列出这些选项:●1+2+34●1+23+4●12+3+4利用DFS和回溯的思想,程序推导如下:将n分成左右两部分,l表示left左侧的值,r表示right右侧的值。先将l加入res,再将r作为新的n......
  • 蓝桥杯填九宫幻方
    通过回溯算法对未输入得数字进行全排列后,依次填入格子,判断是否符合条件。可以更改幻方的大小,来填充任意幻方#include<stdio.h>#include<stdlib.h>#include<stdbool.h>intboard[3][3];//保存输入的幻方intans[3][3][3];//填充后符合条件的幻方inttmp[3][3];//幻方......
  • 蓝桥杯——翻硬币
    题目小明正在玩一个"翻硬币"的游戏。桌上放着排成一排的若干硬币。我们用*表示正面,用o表示反面(是小写字母,不是零)。比如,可能情形是:**oo***oooo;如果同时翻转左边的两个硬币,则变为:oooo***oooo。现在小明的问题是:如果已知了初始状态和要达到的目标状态每次只能同时翻转......
  • 2024SMU蓝桥训练1补题
    B-航班时间思路:地理知识--时差计算-东加西减。此处去程和返程方向相反,时差相加必然抵消。那么就可以知道实际飞行时间ps:这题有点奇怪,本地跑不过样例,交上去是AC。本地跑过样例,交上去RE,WA。RE好像是因为输入的格式不够严格..D-飞机降落思路:全排列枚举ordfs--dfs类似之前电科......
  • 第十三届蓝桥杯 C/C++ 大学 A 组 排列距离 康托和逆康托展开
    偏个题:算阶乘int可以到12,longlong可以到20(12和20,好记)背景知识康托展开原理n个数字或者字符全排列(每个元素只用一次),从小到大按照字典序排列好,从0开始给他们编号,从字符串映射到编号就是康托展开。从最高位向低位计算:若取小于最高位的数字,则后面任意取都是小于这个排列的若......
  • 2015年蓝桥杯六届省赛大学B组真题
    2015年蓝桥杯六届省赛大学B组真题1.奖券数目 #include <iostream>using namespace std;int ans;bool check(int i){  while(i){    if(i%10==4)return false;    i/=10;  }  return true;}int main(){  int ans=0;  for(int i=10......
  • 2017省赛蓝桥杯B组
    2017省赛蓝桥杯B组5.购物单查看代码查看代码 #include<iostream>usingnamespacestd;intmain(){//请在此输入您的代码doublesum=180.90*0.88+10.25*0.65+56.14*0.9+104.65*0.9+100.30*0.88+297.15*0.5+26.75*0.65+130.62*0.5+240.28*0.58+270.62*0.8+115.8......
  • 第十四届省赛大学B组(C/C++)子串简写
    原题链接:子串简写程序猿圈子里正在流行一种很新的简写方法:对于一个字符串,只保留首尾字符,将首尾字符之间的所有字符用这部分的长度代替。例如 internationalization 简写成 i18n,Kubernetes 简写成 K8s,Lanqiao 简写成 L5o 等。在本题中,我们规定长度大于等于 K 的字......