三天速通2023年蓝桥杯单片机(1)——基础大模块

本文提供了蓝桥杯单片机竞赛中涉及的基础模块实现,包括系统初始化、LED控制、七段数码管(Seg)显示、按键(Key)读取以及使用STC15F2K60S2芯片的相关操作。代码涵盖了定时器中断服务函数,用于控制显示更新频率。
摘要由CSDN通过智能技术生成

三天速通蓝桥杯单片机(1)——基础大模块

一.Init

#include <init.h>
void IniySystem()
{
    P0=0xff;
    P2=(P2 & 0x1f)|0x80;//灯
    P2 |=0x1f;
    
    P0=0x00;
    P2=(P2 & 0x1f)|0xa0;//蜂鸣器
    P2 |=0x1f;
}
#include <STC15F2K60S2.h>
void IniySystem();

二.LED

#include <led.h>
void Led_disp(unsigned char pos,enable)
{
    static unsigned char temp=0x00;
    static unsigned char tmep_old=0xff;
    if(enable)
        temp |=0x01<<pos;
    else
        temp &=~(0x01<<pos);
    if(temp !=temp_old)
    {
        P0=~temp;
        P2=(P2 & 0x1f)|0x80;//灯
        P2 |=0x1f;
        temp_old=temp;
    }
}
#include <led.h>
void Buzz(unsigned char enable)
{
    static unsigned char temp=0x00;
    static unsigned char tmep_old=0xff;
    if(enable)
        temp |=0x40;
    else
        temp &=~(0x40);
    if(temp !=temp_old)
    {
        P0=temp;  //改
        P2=(P2 & 0x1f)|0xa0;//改a
        P2 |=0x1f;
        temp_old=temp;
    }
}
#include <led.h>
void Relay(unsigned char enable)
{
    static unsigned char temp=0x00;
    static unsigned char tmep_old=0xff;
    if(enable)
        temp |=0x10;
    else
        temp &=~(0x10);
    if(temp !=temp_old)
    {
        P0=temp;  //改
        P2=(P2 & 0x1f)|0xa0;//改a
        P2 |=0x1f;
        temp_old=temp;
    }
}
#include <STC15F2K60S2.h>
void Led_disp(unsigned char pos,enable);
void Buzz(unsigned char enable);
void Relay(unsigned char enable);

三、Seg

#include<seg.h>
unsigned char seg_wela[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
unsigned char seg_dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//这里根据后续可以改
void Seg_Disp(unsigned char wela,dula,point)
{
    P0=0xff;
    P2=(P2 & 0x1f)|0xe0;//seg_disp
    P2 |=0x1f;
    
    P0=seg_wela[wela];
    P2=(P2 & 0x1f)|0xc0;//seg_command
    P2 |=0x1f;
    
    P0=seg_dula[dula];
    if(point)
        P0 &=0x7f;//第8位给0
    P2=(P2 & 0x1f)|0xe0;//seg_disp
    P2 |=0x1f;
}
#include <STC15F2K60S2.h>
void Seg_Disp(unsigned char wela,dula,point);

四、Key

#include<key.h>
unsigned char Key_Read()//如果是独立按键
{
    unsigned char temp=0;
    if(P33==0) temp=4;
    if(P32==0) temp=5;
    if(P31==0) temp=6;
    if(P30==0) temp=7;
    return temp;
}

unsigned char Key_Read()//如果是矩阵按键
{
    unsigned char temp=0;
    P44=0;P42=1;P35=1;P34=1;
    if(P33==0) temp=4;
    if(P32==0) temp=5;
    if(P31==0) temp=6;
    if(P30==0) temp=7;
    
    P44=1;P42=0;P35=1;P34=1;
    if(P33==0) temp=8;
    if(P32==0) temp=9;
    if(P31==0) temp=10;
    if(P30==0) temp=11;
    
    P44=1;P42=1;P35=0;P34=1;
    if(P33==0) temp=12;
    if(P32==0) temp=13;
    if(P31==0) temp=14;
    if(P30==0) temp=15;
    
    P44=1;P42=1;P35=1;P34=0;
    if(P33==0) temp=16;
    if(P32==0) temp=17;
    if(P31==0) temp=18;
    if(P30==0) temp=19;
    return temp;
}
#include <STC15F2K60S2.h>
unsigned char Key_Read();

五、主函数

/*头文件区域*/
#include<STC15F2K60S2.H>
#include <init.h>
#include<key.h>
#include<seg.h>
#include <led.h>
/*变量区域*/
unsigned char key_slow_down;
unsigned char key_val,key_up,key_down,key_old;
unsigned char seg_pos;
unsigned char seg_slow_down;
unsigned char seg_buf[8]={10,10,10,10,10,10,10,10};
unsigned char seg_point[8]={0,0,0,0,0,0,0,0};
unsigned char ucled[8]={0,0,0,0,0,0,0,0};

/*按键区域*/
void Key_Proc()
{
    if(key_slow_down) return;
    key_slow_down=1;
    
    key_val=Key_Read();
    key_up=key_val & (key_old ^ key_val);
    key_down=~key_val & (key_old ^ key_val);
    key_old=key_val;    
}

/*信息处理区域*/
void Seg_Proc()
{
    if(seg_slow_down) return;
    seg_slow_down=1;
}

/*其他显示区域*/
void Led_Proc()
{
    
}

/*定时器0中断初始化*/
void Timer0Init(void)		//1毫秒@12.000MHz
{
	AUXR &= 0x7F;		//定时器时钟12T模式
	TMOD &= 0xF0;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
    
    ET0=1;
    EA=1;
}
/*定时器0服务函数*/
void Timer0Serve() interrupt 1
{
    if(++key_slow_down==10) key_slow_down=0;
    if(++seg_slow_down==500) seg_slow_down=0;
    if(++seg_pos==8) seg_pos=0;
    Seg_Disp(seg_pos,seg_buf[seg_pos],seg_point[seg_pos]);
    Led_Disp(seg_pos,ucled[seg_pos]);
}


void main()
{
    IniySystem();
    Timer0Init();
    while(1)
    {
        Key_Proc();
        Seg_Proc();
        Led_Proc();
    }
}

可以先通过第十一届的省赛去练手,代码给在下面

https://pan.baidu.com/s/1FKsqAsDp782LZY7aL0e5SA?pwd=8908

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值