三天速通蓝桥杯单片机(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;
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;
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;
P2 |=0x1f;
P0=seg_wela[wela];
P2=(P2 & 0x1f)|0xc0;
P2 |=0x1f;
P0=seg_dula[dula];
if(point)
P0 &=0x7f;
P2=(P2 & 0x1f)|0xe0;
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()
{
}
void Timer0Init(void)
{
AUXR &= 0x7F;
TMOD &= 0xF0;
TL0 = 0x18;
TH0 = 0xFC;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
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