系列文章目录
蓝桥杯温度记录器(示例)
蓝桥杯单片机模板1
蓝桥杯单片机模板2
蓝桥杯单片机各功能模块的使用
前言
第一次写博客,主要是为了记录一下自己一段时间的蓝桥杯单片机的学习,主要是学习的51单片机。By the way我使用的是keil5如果用别的版本,可能有点小问题,但应该还是能解决的。
提示:以下是本篇文章正文内容,下面案例可供参考
一、温度记录器(间隔两秒)
该题为第七届模拟题,稍微做了一点点修改,还是很简单的。下面是原题下载链接: link.
二、实现代码
1.主程序
main.c
#include <STC15.H>
#include "onewire.h"
#include "ds1302.h"
#include "key.h"
#include "intrins.h"
#define uchar unsigned char
uchar code smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x00,0x40};
uchar code smg_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar key_value;
uchar mode=0;//模式
uchar shi,fen,miao;
uchar Table[8];
uchar temp_range[10]; //使用数组,温度[10]
uchar length=2;
bit smg_flag;
bit led_flag;
bit light;
bit t_flag = 0;//读取温度标志位
uchar index=0;//索引
void ShowTable()
{
if(mode==0)//模式0显示界面
{
Table[0]=10;
Table[1]=10;
Table[2]=10;
Table[3]=10;
Table[4]=10;
Table[5]=11;
Table[6]=length/10;
Table[7]=length%10;
}
else if(mode==1)//模式1显示界面
{
EA=0;
miao = Read_Ds1302_Byte(0x81);
fen = Read_Ds1302_Byte(0x83);
shi = Read_Ds1302_Byte(0x85);
EA=1;
Table[0] = shi/16;
Table[1] = shi%16;
Table[3] = fen/16;
Table[4] = fen%16;
Table[6] = miao/16;
Table[7] = miao%16;
if(smg_flag == 1)
{
Table[2] = 11;
Table[5] = 11;
}
else if(smg_flag == 0)
{
Table[2] = 10;
Table[5] = 10;
}
}
else if(mode==2)
{
Table[0] = 11;
Table[1] = index/10;
Table[2] = index%10;
Table[3] = 10;
Table[4] = 10;
Table[5] = 11;
Table[6] = temp_range[index]/10;
Table[7] = temp_range[index]%10;
}
}
void ShowLed(uchar led_dat)
{
P2=0x80;P0=led_dat;P2=0x00;
}
void Timer0Init()
{
AUXR |= 0x80;
TMOD &= 0xF0;
TL0 = 0x40;
TH0 = 0xA2;
TF0 = 0;
TR0 = 1;
ET0=1;
EA=1;
}
void main(void)
{
static uchar i=0;
P2=0xa0;P0=0x00;P2=0x00; // close buzzer and relay
P2=0x80;P0=0xff;P2=0x00;
Timer0Init();
while(1)
{
key_value=read_key();
if(mode==0)
{
if(key_value==5)//按下S5,进入模式1
{
set_sfm(23,59,50);
index=0;
mode=1;
}
}
if(mode==2)
{
if(key_value==6)
{
light=0;//L1灭
index++;
if(index>=10) index=0;
}
if(key_value==7)//按下S7,返回模式1
{
mode=0;
index=0;
i=0;
}
}
if(mode == 1&&t_flag == 1) //读取温度
{
t_flag = 0;
temp_range[index]=ReadTemp(); //一个一个赋值
index++;
if(index >= 10) //采集到10个温度index复位,进入模式2
{
mode = 2;
light = 1; //L1闪烁
index = 0;
}
}
ShowTable(); //显示数码管
}
}
void timer0() interrupt 1 using 1
{
static int smg_count=0,i=0,t_count,count=0;
TL0 = 0x40;
TH0 = 0xA2;
smg_count++;
if(smg_count>=1)
{
smg_count=0;
P2=0xc0;P0=0x00;P2=0x00;
P2=0xe0;P0=~smg_du[Table[i]];P2=0x00;
P2=0xc0;P0=smg_wei[i];P2=0x00;
i++;
if(i>=8) i=0;
}
if(++count>=500)//1s
{
smg_flag = ~smg_flag;
led_flag = ~led_flag;
count = 0;
t_count++;
}
if(mode==1&&t_count==length)//经过length秒扫描一次温度
{
t_count=0;
t_flag=1;
}
else if(mode!=1)
{
t_count=0;
t_flag=0;
}
if(mode==2&&light==1)
{
if(led_flag==1)
{
ShowLed(0xfe);
}
else if(led_flag==0)
{
ShowLed(0xff);
}
}
else if(light==0)
{
ShowLed(0xff);
}
}
2.其他
key.c
#include <STC15.H>
#include "key.h"
#define key_input P3
#define key_state_0 0 //判断按键是否按下
#define key_state_1 1 //判断按键是否是抖动
#define key_state_2 2 //判断按键是否弹起
#define key_mask 0x0f //屏蔽不需要的IO
char read_key(void)
{
static char key_state=0;
char key_press,key_return=0;
key_press=key_input&key_mask;
switch(key_state)
{
case key_state_0: if(key_press!=key_mask) key_state=key_state_1;break;
case key_state_1:
if(key_press==(key_input&key_mask))
{
if(key_press==0x0e) key_return=7;
if(key_press==0x0d) key_return=6;
if(key_press==0x0b) key_return=5;
key_state=key_state_2;
}
else key_state=key_state_0;break;
case key_state_2: if(key_press==0x0f) key_state=key_state_0;break;
}
return key_return;
}
key.h
#ifndef __key_H
#define __key_H
char read_key(void);
#endif
ds1302.c
/*
程序说明: DS1302驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台 8051,12MHz
日 期: 2011-8-9
*/
#include <STC15.H>
#include "ds1302.h"
#include "intrins.h"
#define uchar unsigned char
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3; // DS1302复位
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
//设置时分秒
void set_sfm(uchar shi,uchar fen,uchar miao)
{
Write_Ds1302_Byte(0x8e,0);
Write_Ds1302_Byte(0x80,(miao/10)*16+miao%10);
Write_Ds1302_Byte(0x82,(fen/10)*16+fen%10);
Write_Ds1302_Byte(0x84,(shi/10)*16+shi%10);
Write_Ds1302_Byte(0x8e,0x80);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#define uchar unsigned char
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
void set_sfm(uchar shi,uchar fen,uchar miao);
#endif
onewire.c
/*
程序说明: 单总线驱动程序
软件环境: Keil uVision 4.10
硬件环境: CT107单片机综合实训平台(外部晶振12MHz) STC89C52RC单片机
日 期: 2011-8-9
*/
#include <STC15.H>
#include "onewire.h"
#define uchar unsigned char
sbit DQ = P1^4; //单总线接口
//单总线延时函数
void Delay_OneWire(unsigned int t) //STC89C52RC
{
t*=12;
while(t--);
}
//通过单总线向DS18B20写一个字节
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
//从DS18B20读取一个字节
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20设备初始化
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
//读温度
uchar ReadTemp()
{
uchar low,high,temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);//启动温度转换
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);//读取寄存器
low=Read_DS18B20();//低字节
high=Read_DS18B20();//高字节
low=low/16;
high=high*16;
temp=low+high;
return temp;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#define uchar unsigned char
uchar ReadTemp();
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif
总结
第一次发文,这只是一个尝试。以后可能会学点别的东西,总结总结经验再记录下来。接下来我会发点模板,这样再写的时候也就省去了很多重复的操作,专心思考核心代码即可。当然,最好还是深入了解一下各个功能模块,这样就可以随心所欲的写代码了。有点嘴碎了哈哈,希望能和大家一起学习吧。(记得点赞哟)