当前位置:网站首页>The 7th Blue Bridge Cup single chip microcomputer provincial competition
The 7th Blue Bridge Cup single chip microcomputer provincial competition
2022-07-02 03:38:00 【Super 561】
difficulty : How to control pwm; The known frequency is 1kHZ, So the period is 1ms, Use 100us Timer for , Define a variable to record how much time has passed , If you want to output 20%, The first eight are 0 Let the eighth set 1 Tenth setting 0;
main.c
#include <STC15F2K60S2.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 Timer0Init() //100 Microsecond @12.000MHz
{
AUXR &= 0x7F; // Timer clock 12T Pattern
TMOD &= 0xF0; // Set timer mode
TL0 = 0x9C; // Set initial value of timing
TH0 = 0xFF; // Set initial value of timing
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
ET0=1;
EA=1;
}
void Device_ctrl(unsigned char p2date,unsigned char p0date)
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0X1F;
}
unsigned char mode_display[8];
unsigned char temp_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 char mode=1;
unsigned int time;
unsigned int time_count;
unsigned int temp;
unsigned int temp_count;
void temp_process()
{
if(temp_count>100)
{
temp_count=0;
temp=read_temp();
}
}
void smg_show()
{
unsigned char i;
Device_ctrl(0xc0,0);
if(display_mode==0)
{
Device_ctrl(0xe0,~mode_display[i]);
}
else
{
Device_ctrl(0xe0,~temp_display[i]);
}
Device_ctrl(0xc0,0x01<<i);
i=(i+1)%8;
}
void smg_process()
{
if(smg_count>3)
{
smg_count=0;
mode_display[0]=0x40;
mode_display[2]=0x40;
mode_display[3]=0x00;
temp_display[0]=0x40;
temp_display[1]=smg_du[4];
temp_display[2]=0x40;
temp_display[3]=0x00;
temp_display[4]=0x00;
temp_display[7]=0x39;
if(display_mode==0)
{
mode_display[1]=smg_du[mode];
mode_display[4]=smg_du[time/1000];
mode_display[5]=smg_du[time/100%10];
mode_display[6]=smg_du[time/10%10];
mode_display[7]=smg_du[time%10];
}
else
{
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>=5)
{
key_btn();
key_count=0;
if(Trig_btn==0x08)//s4
{
if(display_mode==0)
{
mode++;
if(mode==4)
{
mode=1;
}
}
}
if(Trig_btn==0x04)//s5
{
if(display_mode==0)
{
if(time<9939)
{
time=time+60;
}
}
}
if(Trig_btn==0x02)//s6
{
if(display_mode==0)
{
time=0;
}
}
if(Trig_btn==0x01)//s7
{
display_mode++;
if(display_mode==2)
{
display_mode=0;
}
}
}
}
unsigned int led_count;
void led_process()
{
if(led_count>5)
{
led_count=0;
if(time)
{
if(mode==1)
{
Device_ctrl(0x80,~0x01);
}
else if(mode==2)
{
Device_ctrl(0x80,~0x02);
}
else
{
Device_ctrl(0x80,~0x04);
}
}
else
{
Device_ctrl(0x80,0xff);
}
}
}
void main()
{
Timer2Init();
Timer0Init();
Device_ctrl(0xa0,0x00);
while(1)
{
smg_process();
key_process();
led_process();
temp_process();
}
}
unsigned int mode_count;
void timer0service() interrupt 1
{
mode_count++;
if(mode==1)
{
if(mode_count==8)
{P34=1;}
if(mode_count==10)
{P34=0;}
}
else if(mode==2)
{
if(mode_count==7)
{P34=1;}
if(mode_count==10)
{P34=0;}
}
else
{
if(mode_count==3)
{P34=1;}
if(mode_count==10)
{P34=0;}
}
}
void timer2service() interrupt 12
{
smg_count++;
smg_show();
key_count++;
time_count++;
led_count++;
temp_count++;
if(time_count>=1000)
{
time_count=0;
if(time)
{
time--;
}
}
}
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;
}
float read_temp()
{
unsigned char low,high;
float date;
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;
}
ONEWIRE.H
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include <STC15F2K60S2.H>
sbit DQ = P1^4;
float read_temp();
#endif
边栏推荐
- Class design basis and advanced
- 蓝桥杯单片机省赛第十二届第二场
- js生成随机数
- Download and use of the super perfect screenshot tool snipaste
- KL divergence is a valuable article
- Spark Tuning
- Discrimination between sap Hana, s/4hana and SAP BTP
- 接口调试工具模拟Post上传文件——ApiPost
- Verilog timing control
- Eight steps of agile development process
猜你喜欢
ThreadLocal详解
初识string+简单用法(二)
Didi open source Delta: AI developers can easily train natural language models
[untitled] basic operation of raspberry pie (2)
蓝桥杯单片机省赛第十一届
Named block Verilog
Unity脚本的基础语法(6)-特定文件夹
Pointer array & array pointer
Detailed explanation of the difference between Verilog process assignment
[HCIA continuous update] working principle of OSPF Protocol
随机推荐
Pointer array & array pointer
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
蓝桥杯单片机省赛第十一届第二场
Spark Tuning
Detailed explanation of ThreadLocal
Kotlin基础学习 17
[mv-3d] - multi view 3D target detection network
PHP array processing
《MATLAB 神经网络43个案例分析》:第41章 定制神经网络的实现——神经网络的个性化建模与仿真
Eight steps of agile development process
Kotlin 基础学习13
蓝桥杯单片机省赛第七届
Class design basis and advanced
数据库文件逻辑结构形式指的是什么
Getting started with MQ
一天上手Aurora 8B/10B IP核(5)----从Framing接口的官方例程学起
halcon图像矫正
Screenshot literacy tool download and use
知物由学 | 自监督学习助力内容风控效果提升
Discrimination between sap Hana, s/4hana and SAP BTP