今天又写了一遍第八届省赛试题,整体来说这一届试题不难,用到的外设也不多。

讯享网

不多说了,直接上代码:
main.c
/*========================================================================================== 文件名:main.c 作 用:主函数 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ # include "include.h" uint temp=0; uchar smgdatastore[8]={
0}; void main() {
Device_Init(); Timer0Init(); while(1) {
if(flag_200ms==1) {
flag_200ms=0; temp=DS18B20_Read(); } smg_show_interface(); smg_flashset(); keypress(); TEMP_trackle(); bell_trackle(); } }
讯享网
include.h
讯享网/*========================================================================================== 文件名:include.h 作 用:头文件包含 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _INCLUDE_H_ #define _INCLUDE_H_ # define uchar unsigned char # define uint unsigned int #include "STC15F2K60S2.h" #include "intrins.h" #include "enable138.h" #include "smg.h" #include "Timer0.h" #include "btn.h" #include "DS18B20.h" # include "handle.h" extern uchar smgdatastore[8]; extern uint temp; # endif
enable138.c
# include "enable138.h" void enable138(uchar x) {
P2&=0X1F; P2|=x<<5; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); P2&=0X1F; }
enable138.h
讯享网/*========================================================================================== 文件名:enable138.h 作 用:74HC138控制函数 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _ENABLE138_H_ #define _ENABLE138_H_ #include "include.h" #define LED 4 #define DU 7 #define WE 6 #define ULN2003 5 void enable138(uchar x); # endif
smg.c
#include "smg.h" uchar code smgDU[34]={
0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,//0 ~F 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x08,0x03,0x46,0x21,0x06,0x0E,//0.~F. 0xff,0xbf}; //灭 - uchar smgWE[8]={
0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; uchar displaydata[8]={
0}; uchar flashset=0x00; uint flashT=1000;//1s间隔亮灭 void smgDisplay() {
static uchar WhichWE=0; P0=0Xff; enable138(DU); P0=smgWE[WhichWE]; enable138(WE); P0=smgDU[displaydata[WhichWE]]; enable138(DU); WhichWE++; if(WhichWE==8) {
WhichWE=0; } } void smg_set(uchar *smgdatastore) {
uchar i; for(i=0;i<8;i++) {
displaydata[i]=*(smgdatastore+i); } } void flashsetservice() {
uchar i; static uint flashcount=0; flashcount++; if(flashcount<flashT/2) {
for(i=0;i<8;i++) {
if(flashset&(0x80>>i)) {
smgWE[i]=0x00; } else {
smgWE[i]=0x01<<i; } } } else if(flashcount<flashT) {
for(i=0;i<8;i++) {
smgWE[i]=0x01<<i; } } else {
flashcount=0; } }
smg.h
讯享网/*========================================================================================== 文件名:smg.h 作 用:数码管显示函数 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _SMG_H_ #define _SMG_H_ #include "include.h" void smgDisplay(); void smg_set(uchar *smgdatastore); void flashsetservice(); extern uchar flashset; # endif
Timer0.c

#include "Timer0.h" bit flag_200ms=0;//200ms读取一次温度值 void Timer0Init(void) //2毫秒@12.000MHz {
AUXR &= 0x7F; //定时器时钟12T模式 TMOD &= 0xF0; //设置定时器模式 TL0 = 0x30; //设置定时初值 TH0 = 0xF8; //设置定时初值 TF0 = 0; //清除TF0标志 TR0 = 1; //定时器0开始计时 EA=1; ET0=1; } void Timer0() interrupt 1//2ms {
static uchar tempcount=0; static uint Bellcount=0; tempcount++; if(tempcount>=100) {
flag_200ms=1; tempcount=0; } if((flashinterface==0)||(Show_interface==1)) {
Real_time_clock(); } flashsetservice(); smgDisplay(); keyscan(); if(Bell_enable==1) {
Bellcount++; LED_flash();//0.2S闪烁 if(Bellcount>=2500) {
Bellcount=0; Bell_enable=0; } } else if(Bell_enable==0)//任意按键按下关闭闹钟功能 {
P0=0XFF; enable138(LED); } }
Timer0.h
讯享网/*========================================================================================== 文件名:Timer0.h 作 用:定时器配置,中断扫描 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _TIMER0_H_ #define _TIMER0_H_ #include "include.h" extern bit flag_200ms; void Timer0Init(void) ; # endif
btn.c
# include "btn.h" uchar code keymap[4]={
0x77,0x66,0x55,0x44}; uchar keysta[4]={
1,1,1,1}; uchar keybackUP[4]={
1,1,1,1}; void keyscan()//按键状态扫描,放入中断 {
uchar i; static uchar keyBuff[4]={
0xff,0xff,0xff,0xff}; keyBuff[0]=keyBuff[0]<<1|S7; keyBuff[1]=keyBuff[1]<<1|S6; keyBuff[2]=keyBuff[2]<<1|S5; keyBuff[3]=keyBuff[3]<<1|S4; for(i=0;i<4;i++) {
if(keyBuff[i]==0xff) {
keysta[i]=1; } else if(keyBuff[i]!=0xff) {
keysta[i]=0; } else {
} } } void keyaction(uchar keycode) {
if(keycode==0x77) {
Bell_enable=0; flashinterface++; if(flashinterface>3) {
flashinterface=0; } } else if(keycode==0x66) {
Bell_enable=0; if((Show_interface==0)&&(flashinterface==0)) {
Show_interface=1; } else if((Show_interface==1)&&(flashinterface==0)) {
Show_interface=0; } } else if(keycode==0x55) {
Bell_enable=0; if(Show_interface==0)//时钟显示界面 {
switch(flashinterface) {
case 1:Real_time_H++;if(Real_time_H>23)Real_time_H=0;break; case 2:Real_time_M++;if(Real_time_M>59)Real_time_M=0;break; case 3:Real_time_S++;if(Real_time_S>59)Real_time_S=0;break; } } else if(Show_interface==1)//闹钟界面 {
switch(flashinterface) {
case 1:Set_Bell_time_H++;if(Set_Bell_time_H>23)Set_Bell_time_H=0;break; case 2:Set_Bell_time_M++;if(Set_Bell_time_M>59)Set_Bell_time_M=0;break; case 3:Set_Bell_time_S++;if(Set_Bell_time_S>59)Set_Bell_time_S=0;break; } } } else if(keycode==0x44) {
Bell_enable=0; if(Show_interface==0)//时钟显示界面 {
switch(flashinterface) {
case 1:Real_time_H--;if(Real_time_H>24)Real_time_H=23;break; case 2:Real_time_M--;if(Real_time_M>60)Real_time_M=59;break; case 3:Real_time_S--;if(Real_time_S>60)Real_time_S=59;break; } } else if(Show_interface==1)//闹钟界面 {
switch(flashinterface) {
case 1:Set_Bell_time_H--;if(Set_Bell_time_H>24)Set_Bell_time_H=23;break; case 2:Set_Bell_time_M--;if(Set_Bell_time_M>60)Set_Bell_time_M=59;break; case 3:Set_Bell_time_S--;if(Set_Bell_time_S>60)Set_Bell_time_S=59;break; } } } } void TEMP_trackle()//显示温度值按键检测 {
switch(keysta[3]) {
case 1:Temp_Show=0;break;//按键松开 case 0:Temp_Show=1;break;//按键按下 } } void keypress()//按键按下检测,放入main函数 {
uchar i; for(i=0;i<4;i++) {
if(keysta[i]!=keybackUP[i]) {
if(keybackUP[i]!=0) {
keyaction(keymap[i]); } keybackUP[i]=keysta[i]; } } }
btn.h
讯享网/*========================================================================================== 文件名:btn.h 作 用:按键控制 作 者:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _BTN_H_ #define _BTN_H_ #include "include.h" # define S7 P30 # define S6 P31 # define S5 P32 # define S4 P33 void keyscan(); void keyaction(uchar keycode); void keypress(); void TEMP_trackle(); # endif
handle.c
# include "handle.h" uchar Show_interface=0; // 0默认时钟,1为温度显示 bit Temp_Show=0;//控制温度界面显示,0时钟界面 , 1温度显示 uchar Real_time_H=23; uchar Real_time_M=59; uchar Real_time_S=50; uchar Set_Bell_time_H=0;//闹钟设置 uchar Set_Bell_time_M=0; uchar Set_Bell_time_S=0; uchar flashinterface=0;//控制数码闪烁 bit Bell_enable=0;//闹钟控制 void Device_Init() {
P0=0XFF; enable138(LED); P0=0XFF; enable138(DU); P0=0X00; enable138(WE); P0=0X00; enable138(ULN2003); } void Real_time_clock() {
static uint TimeCount=0; TimeCount++; if(TimeCount>=500) {
TimeCount=0; Real_time_S++; if(Real_time_S>59) {
Real_time_M++; Real_time_S=0; if(Real_time_M>59) {
Real_time_H++; Real_time_M=0; if(Real_time_H>23) {
Real_time_H=0; } } } } } void smg_show_interface() {
if((Show_interface==0)&&(Temp_Show==0))//时钟显示 {
smgdatastore[0]=Real_time_H/10%10; smgdatastore[1]=Real_time_H%10; smgdatastore[2]=33; smgdatastore[3]=Real_time_M/10%10; smgdatastore[4]=Real_time_M%10; smgdatastore[5]=33; smgdatastore[6]=Real_time_S/10%10; smgdatastore[7]=Real_time_S%10; smg_set(smgdatastore); } else if(Show_interface==1)//闹钟界面 {
smgdatastore[0]=Set_Bell_time_H/10%10; smgdatastore[1]=Set_Bell_time_H%10; smgdatastore[2]=33; smgdatastore[3]=Set_Bell_time_M/10%10; smgdatastore[4]=Set_Bell_time_M%10; smgdatastore[5]=33; smgdatastore[6]=Set_Bell_time_S/10%10; smgdatastore[7]=Set_Bell_time_S%10; smg_set(smgdatastore); } else if((Temp_Show==1)&&(flashinterface==0)&&(Show_interface==0)) {
smgdatastore[0]=32; smgdatastore[1]=32; smgdatastore[2]=32; smgdatastore[3]=32; smgdatastore[4]=32; smgdatastore[5]=temp/1000%10; smgdatastore[6]=temp/100%10; smgdatastore[7]=12; smg_set(smgdatastore); } } void smg_flashset() {
switch(flashinterface) {
case 0:flashset=0x00;break; case 1:flashset=0xc0;break; case 2:flashset=0x18;break;//000 1 1000 case 3:flashset=0x03;break;//00000011 } } void bell_trackle() {
if((Real_time_H==Set_Bell_time_H)&&(Real_time_M==Set_Bell_time_M)&&(Real_time_S==Set_Bell_time_S)&&(flashinterface==0)) {
Bell_enable=1; } } void LED_flash()//2ms {
static uchar LED_fLashcount=0; LED_fLashcount++; if(LED_fLashcount<100) {
P0=0XFE; enable138(LED); } else if(LED_fLashcount<200) {
P0=0Xff; enable138(LED); } else {
LED_fLashcount=0; } }
handle.h
讯享网/*========================================================================================== 文件名:handle.h 作 用:逻辑处理 设计人:流行的云~ 时 间:2020年5月3日 硬件支持:STC15F2K60S2 系统时钟:12MHZ ============================================================================================*/ #ifndef _HANDLE_H_ #define _HANDLE_H_ #include "include.h" extern uchar Show_interface; extern uchar flashinterface;//控制数码闪烁 extern bit Temp_Show;//控制温度界面显示,0时钟界面 , 1温度显示 extern uchar Real_time_H;//时钟时间 extern uchar Real_time_M; extern uchar Real_time_S; extern uchar Set_Bell_time_H;//闹钟时间 extern uchar Set_Bell_time_M; extern uchar Set_Bell_time_S; extern bit Bell_enable;//闹钟控制 // void Device_Init(); void Real_time_clock(); void smg_show_interface(); void smg_flashset(); void bell_trackle(); void LED_flash();//2ms # endif
DS18B20.c
#include "DS18B20.h" sbit DQ = P1^4; //单总线接口 //单总线延时函数 void Delay_OneWire(unsigned int t) {
t=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; } uint DS18B20_Read() {
uchar tmpL,tmpH; uint temp=0; EA=0; init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0x44); init_ds18b20(); Write_DS18B20(0xcc); Write_DS18B20(0xbe); tmpL=Read_DS18B20(); tmpH=Read_DS18B20(); temp=(tmpH<<8)|tmpL; temp=temp*6.25; EA=1; return temp; }
DS18B20.h
讯享网#ifndef _DS18B20_H_ #define _DS18B20_H_ #include "include.h" uint DS18B20_Read(); # endif
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/114659.html