系列文章目录
蓝桥杯温度记录器(示例)
蓝桥杯单片机模板1
蓝桥杯单片机模板2
蓝桥杯单片机各功能模块的使用
前言
本文主要是第二种框架模式,实现各个功能模块的调用。主要是键盘和数码管两个基本部分。至于其他各个芯片的使用将会集中在下一篇文章
提示:以下是本篇文章正文内容,下面案例可供参考
一、主程序
main.c
#include <stc15.h>
#include "sys.h"
bit flag_100ms = 0, flag_1s = 0, flag_5ms = 0, flag_500ms = 0;
void main(void)
{
AllInit();
Timer0Init();
Timer1Init();
while(1){
//此处是具体实现代码
KeyPress();
TubeShow();
}
}
sys.c
#include "sys.h"
/*全局初始化*/
void AllInit(void)
{
P2 = (P2 & 0x1f) | 0x80;//LED
P0 = 0xff;
P2 = (P2 & 0x1f) | 0xc0;//数码管
P0 = 0x00;
P2 = (P2 & 0x1f) | 0xa0;//蜂鸣器
P0 = 0x00;
P2 = P2 & 0x1f;
}
/*定时器0初始化*/
void Timer0Init(void)
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD = 0x04; //设置定时器0为16位自动重装载外部记数模式
TH0 = TL0 = 0; //设置定时器0初始值
TR0 = 1; //定时器0开始工作
}
/*定时器0*/
void Time0(void) interrupt 1
{
}
/*定时器1初始化*/
void Timer1Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x40; //定时器时钟1T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0xCD; //设置定时初值
TH1 = 0xD4; //设置定时初值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1;
}
/*定时器1*/
void Time1(void) interrupt 3 //设定标志位
{
static uint T1count1 = 0, T1count2 = 0;
T1count1 ++;
if(T1count1 % 2 == 0) //2ms
TubeScan(); //扫描数码管
if(T1count1 % 5 == 0) //5ms
flag_5ms = 1;
if(T1count1 % 100 == 0) //100ms
flag_100ms = 1;
if(T1count1 >= 500) //500ms
flag_500ms = 1;
T1count2 ++;
if(T1count2 >= 1000) //1s
flag_1s = 1;
KeyScan();
}
sys.h
#ifndef _SYS_H_
#define _SYS_H_
#include <stc15.h>
#include <intrins.h>
typedef unsigned int uint;
typedef unsigned char uchar;
extern bit flag_100ms, flag_1s, flag_5ms, flag_500ms;
void AllInit(void);
void Timer0Init(void);
void Timer1Init(void);
void TubeScan(void);
void TubeShow(void);
void KeyScan(void);
void KeyPress(void);
#endif
二、其他功能模块
1.键盘
key.c
#include "sys.h"
sbit KeyOut1 = P4^4;
sbit KeyOut2 = P4^2;
sbit KeyOut3 = P3^5;
sbit KeyOut4 = P3^4;
sbit KeyIn1 = P3^0;
sbit KeyIn2 = P3^1;
sbit KeyIn3 = P3^2;
sbit KeyIn4 = P3^3;
uchar code KeyMap[4][4] = { {0x01, 0x02, 0x03, 0x04},
{0x05, 0x06, 0x07, 0x08},
{0x09, 0x10, 0x11, 0x12},
{0x13, 0x14, 0x15, 0x16} };
uchar KeyBuff[4][4] = { {0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff},
{0xff, 0xff, 0xff, 0xff} };
uchar KeySta[4][4] = {{1,1,1,1},{1,1,1,1},{1,1,1,1},{1,1,1,1}};
uchar KeyBackup[4][4] = {{1,1,1,1}, {1,1,1,1}, {1,1,1,1}, {1,1,1,1}};
void KeyScan(void)
{
static uchar index = 0;
uchar i;
KeyBuff[0][index] = (KeyBuff[0][index] << 1) | KeyIn1;
KeyBuff[1][index] = (KeyBuff[1][index] << 1) | KeyIn2;
KeyBuff[2][index] = (KeyBuff[2][index] << 1) | KeyIn3;
KeyBuff[3][index] = (KeyBuff[3][index] << 1) | KeyIn4;
for(i = 0; i < 4; i ++)
{
if((KeyBuff[i][index] & 0x0f) == 0x00)
KeySta[i][index] = 0;
else if((KeyBuff[i][index] & 0x0f) == 0x0f)
KeySta[i][index] = 1;
}
index ++;
index &= 0x03;
switch(index)
{
case 0: KeyOut4 = 1; KeyOut1 = 0; break;
case 1: KeyOut1 = 1; KeyOut2 = 0; break;
case 2: KeyOut2 = 1; KeyOut3 = 0; break;
case 3: KeyOut3 = 1; KeyOut4 = 0; break;
}
}
void KeyAction(unsigned char keycode)
{
if(keycode == 0x01){ //S7
//主要键盘动作在此实现。如:按下按键S7,由模式0转换为模式1
mode=~mode;
}
else if(keycode == 0x05) //S6
else if(keycode == 0x09) //S5
else if(keycode == 0x13) //S4
else if(keycode == 0x02) //S11
else if(keycode == 0x06) //S10
else if(keycode == 0x10) //S9
else if(keycode == 0x14) //S8
else if(keycode == 0x03) //S15
else if(keycode == 0x07) //S14
else if(keycode == 0x11) //S13
else if(keycode == 0x15) //S12
else if(keycode == 0x04) //S19
else if(keycode == 0x08) //S18
else if(keycode == 0x12) //S17
else if(keycode == 0x16) //S16
}
void KeyPress(void)
{
uchar i, j;
for(i = 0; i < 4; i ++)
{
for(j = 0; j < 4; j ++)
{
if(KeyBackup[i][j] != KeySta[i][j])
{
if(KeySta[i][j] == 0)
KeyAction(KeyMap[i][j]);
KeyBackup[i][j] = KeySta[i][j];
}
}
}
}
2.显示程序
display.c(自动扫描数码管)
#include "sys.h"
uchar code table[]={0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,
0x00, 0x40};
uchar TubeBuff[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
uchar smg1, smg2, smg3, smg4, smg5, smg6, smg7, smg8;
void TubeScan(void)
{
static uchar index = 0;
P0 = 0x00;
P2 = (P2 & 0x1f) | 0xc0;
P0 = 0x01 << index;
P2 = P2 & 0x1f;
P0 = 0xff;
P2 = (P2 & 0x1f) | 0xe0;
P0 = ~TubeBuff[index];
P2 = P2 & 0x1f;
index ++;
index &= 0x07;
}
void TubeShow(void)
{
TubeBuff[0] = table[smg1];
TubeBuff[1] = table[smg2];
TubeBuff[2] = table[smg3];
TubeBuff[3] = table[smg4];
TubeBuff[4] = table[smg5];
TubeBuff[5] = table[smg6];
TubeBuff[6] = table[smg7];
TubeBuff[7] = table[smg8];
}
总结
本文主要是一个框架,有时可能会有不能实现的情况,但各模块实现应该是对的。使用顶部系列文章中蓝桥杯单片机模板1还是很稳的。(记得点个赞哟)