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



main.c
#include <STC15F2K60S2.H>
#include <ONEWIRE.H>
#include <IIC.H>
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0X1F;
}
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int smg_count;
unsigned char display_mode;
unsigned int temp;
unsigned int temp_count;
unsigned int Tmax_set=30;
unsigned int Tmin_set=20;
unsigned int Tmax=30;
unsigned int Tmin=20;
unsigned int da_count;
unsigned int led_count;
unsigned char led_ctrl=0xff;
bit led_flag;
void da(unsigned char date)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
void da_process()
{
if(da_count>10)
{
da_count=0;
if(temp<Tmin)
{
da(51*2);
}
if((Tmin<=temp)&&(temp<=Tmax))
{
da(51*3);
}
if(temp>Tmax)
{
da(51*4);
}
}
}
void led_process()
{
if(led_count>10)
{
led_count=0;
if(temp<Tmin)
{
led_ctrl&=~0x04;
}
else
{
led_ctrl|=0x04;
}
if((temp<=Tmax)&&(temp>=Tmin))
{
led_ctrl&=~0x02;
}
else
{
led_ctrl|=0x02;
}
if(temp>Tmax)
{
led_ctrl&=~0x01;
}
else
{
led_ctrl|=0x01;
}
if(led_flag)
{
led_ctrl&=~0x08;
}
else
{
led_ctrl|=0x08;
}
Device_ctrl(0x80,led_ctrl);
}
}
void temp_process()
{
if(temp_count>100)
{
temp_count=0;
temp=read_temp();
Init_temp();
}
}
void smg_show()
{
unsigned char i;
Device_ctrl(0xc0,0);
Device_ctrl(0xe0,~smg_display[i]);
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)
{
smg_display[0]=0x39;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=smg_du[temp/10];
smg_display[7]=smg_du[temp%10];
}
else if(display_mode==1)
{
smg_display[0]=0x73;
smg_display[1]=0x00;
smg_display[2]=0x00;
smg_display[3]=smg_du[Tmax_set/10];
smg_display[4]=smg_du[Tmax_set%10];
smg_display[5]=0x00;
smg_display[6]=smg_du[Tmin_set/10];
smg_display[7]=smg_du[Tmin_set%10];
}
}
}
unsigned char Trig_btn;
unsigned char Cont_btn;
unsigned int key_count;
bit key_set;
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
{
display_mode++;
if(display_mode==2)
{
if(Tmax_set>=Tmin_set)
{
Tmax=Tmax_set;
Tmin=Tmin_set;
led_flag=0;
}
else
{
Tmax=Tmax;
Tmin=Tmin;
Tmax_set=Tmax;
Tmin_set=Tmin;
led_flag=1;
}
display_mode=0;
}
}
if(Trig_btn==0x04)//s5
{
key_set=~key_set;
}
if(Trig_btn==0x02)//s6
{
if(display_mode==1)
{
if(key_set==0)
{
if(Tmax_set<99)
{
Tmax_set++;
}
}
else
{
if(Tmin_set<99)
{
Tmin_set++;
}
}
}
}
if(Trig_btn==0x01)//s7
{
if(display_mode==1)
{
if(key_set==0)
{
if(Tmax_set>0)
{
Tmax_set--;
}
}
else
{
if(Tmin_set>0)
{
Tmin_set--;
}
}
}
}
}
}
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 main()
{
Timer2Init();
while(1)
{
key_process();
smg_process();
temp_process();
da_process();
led_process();
}
}
void timer2service() interrupt 12
{
key_count++;
smg_count++;
temp_count++;
da_count++;
led_count++;
smg_show();
}iic.c
#include "iic.h"
#define DELAY_TIME 5
//I2C Bus internal delay function
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
//I2C Bus start signal
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
//I2C Bus stop signal
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Send a reply or non reply signal
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Waiting for an answer
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
//I2C The bus sends a byte of data
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
//I2C The bus receives a byte of data
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
iic.h
#ifndef __IIC_H
#define __IIC_H
#include <STC15F2K60S2.H>
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendAck(bit ackbit);
void IIC_SendByte(unsigned char byt);
unsigned char IIC_RecByte(void);
#endifonewire.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;
}
void Init_temp()
{
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
}
float read_temp()
{
float temp;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(high<<8)|low;
temp=temp*0.0625;
return temp;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include <STC15F2K60S2.H>
sbit DQ = P1^4;
float read_temp();
void Init_temp();
#endif
边栏推荐
- NLog use
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- 初识string+简单用法(二)
- Exchange rate query interface
- Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
- The page in H5 shows hidden execution events
- Uniapp uses canvas to generate posters and save them locally
- Halcon image rectification
- 蓝桥杯单片机省赛第五届
- 终日乾乾,夕惕若厉
猜你喜欢

Named block Verilog

MySQL之账号管理

Gradle foundation | customize the plug-in and upload it to jitpack

初识string+简单用法(二)

Generate random numbers that obey normal distribution

halcon图像矫正

焱融看 | 混合雲時代下,如何制定多雲策略
![[yolo3d]: real time detection of end-to-end 3D point cloud input](/img/5e/f17960d302f663db75ad82ae0fd70f.jpg)
[yolo3d]: real time detection of end-to-end 3D point cloud input
![[HCIA continuous update] working principle of OSPF Protocol](/img/bc/4eeb091c511fd563fb1e00c8c8881a.jpg)
[HCIA continuous update] working principle of OSPF Protocol

ThreadLocal详解
随机推荐
MySQL index, transaction and storage engine
[HCIA continuous update] working principle of OSPF Protocol
Kotlin basic learning 17
SAML2.0 notes (I)
NLog使用
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
近段时间天气暴热,所以采集北上广深去年天气数据,制作可视化图看下
【DesignMode】建造者模式(Builder model)
Pycharm2021 delete the package warehouse list you added
h5中的页面显示隐藏执行事件
aaaaaaaaaaaaa
Didi open source Delta: AI developers can easily train natural language models
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Named block Verilog
Global and Chinese market of handheld ultrasonic scanners 2022-2028: Research Report on technology, participants, trends, market size and share
蓝桥杯单片机省赛第七届
Kubernetes cluster storageclass persistent storage resource core concept and use
Halcon image rectification
Qt的网络连接方式
[yolo3d]: real time detection of end-to-end 3D point cloud input