当前位置:网站首页>The 8th Blue Bridge Cup single chip microcomputer provincial competition
The 8th Blue Bridge Cup single chip microcomputer provincial competition
2022-07-02 03:38:00 【Super 561】


main,c
#include <STC15F2K60S2.H>
#include <DS1302.H>
#include <ONEWIRE.H>
void Timer2Init() //1 millisecond @12.000MHz
{
AUXR &= 0xFB; // Timer clock 12T Pattern
T2L = 0x18; // Set initial value of timing
T2H = 0xFC; // Set initial value of timing
AUXR |= 0x10; // Timer 2 Start timing
IE2|=0X04;
EA=1;
}
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0x1f;
}
unsigned char time_display[8];
unsigned char alarmtime_display[8];
unsigned char settime_display[8];
unsigned char temp_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned char smg_count;
unsigned char display_mode;
unsigned char shi,fen,miao;
unsigned char setshi,setfen,setmiao;
unsigned char alarmshi=0,alarmfen=0,alarmmiao=0;
unsigned int time_count;
unsigned char key_press;
unsigned int led_count;
unsigned int led_blink_count;
unsigned int temp;
unsigned int temp_count;
bit led_flag;
bit smg_flag;
float read_temp()
{
float date;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
date=(high<<8)|low;
date=date*0.0625;
return date;
}
void temp_process()
{
if(temp_count>100)
{
temp_count=0;
temp=read_temp();
}
}
void set_sfm(unsigned char shi,unsigned char fen,unsigned char 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);
}
void smg_show()
{
unsigned char i;
Device_ctrl(0xc0,0);
if(display_mode==0)
{
Device_ctrl(0xe0,~time_display[i]);
}
if(display_mode==1)
{
Device_ctrl(0xe0,~settime_display[i]);
}
if(display_mode==2)
{
Device_ctrl(0xe0,~alarmtime_display[i]);
}
if(display_mode==3)
{
Device_ctrl(0xe0,~temp_display[i]);
}
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
EA=0;
shi=Read_Ds1302_Byte(0x85);
fen=Read_Ds1302_Byte(0x83);
miao=Read_Ds1302_Byte(0x81);
shi=shi/16*10+shi%16;
fen=fen/16*10+fen%16;
miao=miao/16*10+miao%16;
EA=1;
if((shi==alarmshi)&&(fen==alarmfen)&&(miao==alarmmiao))
{led_flag=1;}
time_display[2]=0x40;
time_display[5]=0x40;
settime_display[2]=0x40;
settime_display[5]=0x40;
alarmtime_display[2]=0x40;
alarmtime_display[5]=0x40;
temp_display[0]=0x00;
temp_display[1]=0x00;
temp_display[2]=0x00;
temp_display[3]=0x00;
temp_display[4]=0x00;
temp_display[7]=0x39;
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)// Time display interface
{
time_display[0]=smg_du[shi/10];
time_display[1]=smg_du[shi%10];
time_display[3]=smg_du[fen/10];
time_display[4]=smg_du[fen%10];
time_display[6]=smg_du[miao/10];
time_display[7]=smg_du[miao%10];
}
if(display_mode==1)// Time setting interface
{
if(key_press==1)
{
if(smg_flag)
{
settime_display[0]=smg_du[setshi/10];
settime_display[1]=smg_du[setshi%10];
}
else
{
settime_display[0]=0x00;
settime_display[1]=0x00;
}
settime_display[3]=smg_du[setfen/10];
settime_display[4]=smg_du[setfen%10];
settime_display[6]=smg_du[setmiao/10];
settime_display[7]=smg_du[setmiao%10];
}
if(key_press==2)
{
if(smg_flag)
{
settime_display[3]=smg_du[setfen/10];
settime_display[4]=smg_du[setfen%10];
}
else
{
settime_display[3]=0x00;
settime_display[4]=0x00;
}
settime_display[6]=smg_du[setmiao/10];
settime_display[7]=smg_du[setmiao%10];
settime_display[0]=smg_du[setshi/10];
settime_display[1]=smg_du[setshi%10];
}
if(key_press==3)
{
if(smg_flag)
{
settime_display[6]=smg_du[setmiao/10];
settime_display[7]=smg_du[setmiao%10];
}
else
{
settime_display[6]=0x00;
settime_display[7]=0x00;
}
settime_display[0]=smg_du[setshi/10];
settime_display[1]=smg_du[setshi%10];
settime_display[3]=smg_du[setfen/10];
settime_display[4]=smg_du[setfen%10];
}
}
if(display_mode==2)// Alarm clock setting interface
{
if(key_press==1)
{
if(smg_flag)
{
alarmtime_display[0]=smg_du[alarmshi/10];
alarmtime_display[1]=smg_du[alarmshi%10];
}
else
{
alarmtime_display[0]=0x00;
alarmtime_display[1]=0x00;
}
alarmtime_display[3]=smg_du[alarmfen/10];
alarmtime_display[4]=smg_du[alarmfen%10];
alarmtime_display[6]=smg_du[alarmmiao/10];
alarmtime_display[7]=smg_du[alarmmiao%10];
}
if(key_press==2)
{
if(smg_flag)
{
alarmtime_display[3]=smg_du[alarmfen/10];
alarmtime_display[4]=smg_du[alarmfen%10];
}
else
{
alarmtime_display[3]=0x00;
alarmtime_display[4]=0x00;
}
alarmtime_display[6]=smg_du[alarmmiao/10];
alarmtime_display[7]=smg_du[alarmmiao%10];
alarmtime_display[0]=smg_du[alarmshi/10];
alarmtime_display[1]=smg_du[alarmshi%10];
}
if(key_press==3)
{
if(smg_flag)
{
alarmtime_display[6]=smg_du[alarmmiao/10];
alarmtime_display[7]=smg_du[alarmmiao%10];
}
else
{
alarmtime_display[6]=0x00;
alarmtime_display[7]=0x00;
}
alarmtime_display[0]=smg_du[alarmshi/10];
alarmtime_display[1]=smg_du[alarmshi%10];
alarmtime_display[3]=smg_du[alarmfen/10];
alarmtime_display[4]=smg_du[alarmfen%10];
}
}
if(display_mode==3)
{
temp_display[5]=smg_du[temp/10];
temp_display[6]=smg_du[temp%10];
}
}
}
unsigned char Trig_btn;
unsigned char Cont_btn;
unsigned int key_count;
void key_btn()
{
unsigned char readdate=P3^0XFF;
Trig_btn=readdate&(Cont_btn^readdate);
Cont_btn=readdate;
}
void key_process()
{
if(key_count>10)
{
key_count=0;
key_btn();
if(Trig_btn==0x08)//s4
{
if(display_mode==0)
{
display_mode=3;
}
if(display_mode==1)// Set the time
{
if(key_press==1)
{
if(setshi>0)
{
setshi--;
}
}
if(key_press==2)
{
if(setfen>0)
{
setfen--;
}
}
if(key_press==3)
{
if(setmiao>0)
{
setmiao--;
}
}
}
if(display_mode==2)// Alarm time setting
{
if(key_press==1)
{
if(alarmshi>0)
{
alarmshi--;
}
}
if(key_press==2)
{
if(alarmfen>0)
{
alarmfen--;
}
}
if(key_press==3)
{
if(alarmmiao>0)
{
alarmmiao--;
}
}
}
led_flag=0;
led_blink_count=0;
}
if(Trig_btn==0x04)//s5
{
if(display_mode==1)// Set the time
{
if(key_press==1)
{
if(setshi<23)
{
setshi++;
}
}
if(key_press==2)
{
if(setfen<59)
{
setfen++;
}
}
if(key_press==3)
{
if(setmiao<59)
{
setmiao++;
}
}
}
if(display_mode==2)// Alarm time setting
{
if(key_press==1)
{
if(alarmshi<23)
{
alarmshi++;
}
}
if(key_press==2)
{
if(alarmfen<59)
{
alarmfen++;
}
}
if(key_press==3)
{
if(alarmmiao<59)
{
alarmmiao++;
}
}
}
led_flag=0;
led_blink_count=0;
}
if(Trig_btn==0x02)//s6
{
display_mode=2;
if(display_mode==2)
{
key_press++;
if(key_press==4)
{
display_mode=0;
key_press=0;
}
}
led_flag=0;
led_blink_count=0;
}
if(Trig_btn==0x01)//s7
{
display_mode=1;
if(display_mode==1)
{
key_press++;
}
if(key_press==4)
{
display_mode=0;
set_sfm(setshi,setfen,setmiao);
key_press=0;
}
led_flag=0;
led_blink_count=0;
}
if(Trig_btn==0&Cont_btn==0)
{
if(display_mode==3)
{
display_mode=0;
}
}
}
}
void led_process()
{
if(led_flag)
{
led_count++;
if(led_count<200)
{Device_ctrl(0x80,~0x01);}
else
{
if(led_count<400)
{
Device_ctrl(0x80,0xff);
}
else
{
led_count=0;
led_blink_count++;
if(led_blink_count==12)
{
led_flag=0;
led_blink_count=0;
}
}
}
}
else
{
Device_ctrl(0x80,0xff);
}
}
void main()
{
Timer2Init();
set_sfm(23,59,50);
Device_ctrl(0xa0,0x00);
while(1)
{
smg_process();
key_process();
temp_process();
}
}
void timer2service() interrupt 12
{
smg_count++;
key_count++;
temp_count++;
smg_show();
if(display_mode==1)
{
time_count++;
if(time_count==1000)
{
smg_flag=~smg_flag;
time_count=0;
}
}
if(display_mode==2)
{
time_count++;
if(time_count==1000)
{
smg_flag=~smg_flag;
time_count=0;
}
}
led_process();
}ds1302.c
#include "ds1302.h"
// Write Bytes
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK = 0;
SDA = temp&0x01;
temp>>=1;
SCK=1;
}
}
// towards DS1302 Registers write data
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;
}
// from DS1302 Register reads data
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);
}
ds1302.h
#ifndef __DS1302_H
#define __DS1302_H
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp);
void Write_Ds1302_Byte( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302_Byte( unsigned char address );
#endif
onewire.c
#include "onewire.h"
// Single bus internal delay function
void Delay_OneWire(unsigned int t)
{
t=t*12;
while(t--);
}
// Single bus write operation
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);
}
// Single bus read operation
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 initialization
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;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include <STC15F2K60S2.H>
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char Read_DS18B20(void);
bit init_ds18b20(void);
#endif边栏推荐
- Fourier series
- UI (New ui:: MainWindow) troubleshooting
- NLog使用
- Exchange rate query interface
- Learn PWN from CTF wiki - ret2shellcode
- aaaaaaaaaaaaa
- VS2010 plug-in nuget
- This article describes the step-by-step process of starting the NFT platform project
- Failed to upgrade schema, error: “file does not exist
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
猜你喜欢

Download and use of the super perfect screenshot tool snipaste

Failed to upgrade schema, error: “file does not exist

This article describes the step-by-step process of starting the NFT platform project

Learn PWN from CTF wiki - ret2shellcode

Pycharm2021 delete the package warehouse list you added

Introduction to Robotics II. Forward kinematics, MDH method

Uniapp uses canvas to generate posters and save them locally

Retrofit's callback hell is really vulnerable in kotlin synergy mode
![[C Advanced] brother Peng takes you to play with strings and memory functions](/img/95/ab1bb0b3fa0b99e32233a5ca5d42a4.jpg)
[C Advanced] brother Peng takes you to play with strings and memory functions

Detailed explanation of ThreadLocal
随机推荐
Kotlin基础学习 14
Kotlin基础学习 16
数据库文件逻辑结构形式指的是什么
[数据库]JDBC
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
Custom classloader that breaks parental delegation
Kotlin basic learning 13
VS2010 plug-in nuget
Object oriented thinking
Unity脚本的基础语法(7)-成员变量和实例化
蓝桥杯单片机省赛第八届
Kotlin basic learning 16
Gradle foundation | customize the plug-in and upload it to jitpack
焱融看 | 混合雲時代下,如何制定多雲策略
傅里叶级数
《MATLAB 神经网络43个案例分析》:第42章 并行运算与神经网络——基于CPU/GPU的并行神经网络运算
Retrofit's callback hell is really vulnerable in kotlin synergy mode
Failed to upgrade schema, error: “file does not exist
Class design basis and advanced