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




difficulty 1 How to calculate humidity , Read the 0x03 The value of the potentiometer , humidity =k voltage ; Five volts corresponds to a humidity of 99; therefore k You can calculate it
shidu=ad(0x03);
shidu=shidu*99/255;2 How to control the buzzer relay at the same time , Define two flag bits respectively , Then operate by defining flag bits
if((buzz_flag==0)&&(motor_flag==0))
{
P2=0XA0;MOTOR=0;BUZZ=0;P2=0;
}
if((buzz_flag==1)&&(motor_flag==0))
{
P2=0XA0;MOTOR=0;BUZZ=1;P2=0;
}
if((buzz_flag==0)&&(motor_flag==1))
{
P2=0XA0;MOTOR=1;BUZZ=0;P2=0;
}
if((buzz_flag==1)&&(motor_flag==1))
{
P2=0XA0;MOTOR=1;BUZZ=1;P2=0;
}main.c
#include <STC15F2K60S2.H>
#include <DS1302.H>
#include <IIC.H>
sbit BUZZ=P0^6;
sbit MOTOR=P0^4;
bit buzz_flag;
bit motor_flag;
unsigned int shidu;
unsigned int shidu_fazhi;
unsigned char shi;
unsigned char fen;
unsigned char miao;
unsigned char display_mode;
unsigned int buzzflag;
void write_date(unsigned char add,unsigned char date);
void Device_ctrl(unsigned char p2date,unsigned char p0date)// Latch control IO Mouth function
{
P0=p0date;
P2=P2&0X1F|p2date;
P2&=0x1f;
}
unsigned char trig_btn;
unsigned char cont_btn;
unsigned int key_count;
unsigned char key_flag;
void key_btn()// Key function
{
unsigned char readdate=P3^0xff;
trig_btn=readdate&(cont_btn^readdate);
cont_btn=readdate;
}
bit work_mode;
void key_process()// Key handling functions
{
if(key_count>5)
{
key_count=0;
key_btn();
if(trig_btn==0x08)//s4
{
if(work_mode==1)// Manual status
{
motor_flag=0;
}
else// Automatic status
{
shidu_fazhi--;
}
}
if(trig_btn==0x04)//s5
{
if(work_mode==1)// Manual status
{
motor_flag=1;
}
else// Automatic status
{
shidu_fazhi++;
}
}
if(trig_btn==0x02)//s6
{
if(work_mode==0)// Automatic operation mode
{
display_mode=1;
key_flag++;
if(key_flag>1)
{
display_mode=0;
key_flag=0;
write_date(0xaa,shidu_fazhi);
}
}
else// Manual mode
{
buzzflag=~buzzflag;
}
}
if(trig_btn==0x01)//s7
{
work_mode=~work_mode;// Change working mode
if(work_mode)// Manual mode
{
Device_ctrl(0x80,~0x02);
}
else// Automatic mode
{
Device_ctrl(0x80,~0x01);
}
}
}
}
void set_sfm(unsigned char shi,unsigned char fen,unsigned char miao)// Enter hour, minute and second
{
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);
}
unsigned char smg_display[8];
unsigned char smg_du[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
unsigned int smg_count;
void smg_show()// Nixie tube function
{
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()// Nixie tube interface display function
{
if(smg_count>3)
{
smg_count=0;
if(display_mode==0)
{
smg_display[0]=smg_du[shi/10];
smg_display[1]=smg_du[shi%10];
smg_display[2]=0x40;
smg_display[3]=smg_du[fen/10];
smg_display[4]=smg_du[fen%10];
smg_display[5]=0x00;
smg_display[6]=smg_du[shidu/10];
smg_display[7]=smg_du[shidu%10];
}
else
{
smg_display[0]=0x40;
smg_display[1]=0x40;
smg_display[2]=0x00;
smg_display[3]=0x00;
smg_display[4]=0x00;
smg_display[5]=0x00;
smg_display[6]=smg_du[shidu_fazhi/10];
smg_display[7]=smg_du[shidu_fazhi%10];
}
}
}
unsigned int ad_count;
unsigned char ad(unsigned char add)
{
unsigned char date;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return date;
}
void ad_process()
{
if(ad_count>=100)
{
ad_count=0;
shidu=ad(0x03);
shidu=shidu*99/255;
}
}
void write_date(unsigned char add,unsigned char date)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(date);
IIC_WaitAck();
IIC_Stop();
}
unsigned int read_date(unsigned char add)
{
unsigned int date;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
date=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return date;
}
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(); // Timer initialization
Device_ctrl(0xa0,0x00);
Device_ctrl(0x80,0xFF);
shidu_fazhi=read_date(0xaa);
if(work_mode)// Manual mode
{
Device_ctrl(0x80,~0x02);
}
else// Automatic mode
{
Device_ctrl(0x80,~0x01);
}
set_sfm(8,30,55);
while(1)
{
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;
smg_process();
key_process();
ad_process();
if(work_mode==0)
{
if(shidu<shidu_fazhi)
{
motor_flag=1;buzz_flag=0;
}
else
{
motor_flag=0;buzz_flag=0;
}
}
else
{
if(shidu<shidu_fazhi)
{
if(buzzflag)
{
buzz_flag=1;
}
else
{
buzz_flag=0;
}
}
else
{
buzz_flag=0;
}
}
if((buzz_flag==0)&&(motor_flag==0))
{
P2=0XA0;MOTOR=0;BUZZ=0;P2=0;
}
if((buzz_flag==1)&&(motor_flag==0))
{
P2=0XA0;MOTOR=0;BUZZ=1;P2=0;
}
if((buzz_flag==0)&&(motor_flag==1))
{
P2=0XA0;MOTOR=1;BUZZ=0;P2=0;
}
if((buzz_flag==1)&&(motor_flag==1))
{
P2=0XA0;MOTOR=1;BUZZ=1;P2=0;
}
}
}
void timer2service() interrupt 12 // Interrupt function
{
smg_count++;
smg_show();
key_count++;
ad_count++;
}iic.c
#include "iic.h"
#define DELAY_TIME 5
sbit SDA = P2^1;
sbit SCL = P2^0;
//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"
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);
#endifds1302.c
#include "ds1302.h"
sbit SCK = P1^7;
sbit SDA = P2^3;
sbit RST = P1^3;
// 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>
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
边栏推荐
- 滴滴开源DELTA:AI开发者可轻松训练自然语言模型
- This article describes the step-by-step process of starting the NFT platform project
- 高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
- Basic syntax of unity script (7) - member variables and instantiation
- In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
- 蓝桥杯单片机省赛第七届
- Getting started with MQ
- In wechat applet, the externally introduced JS is used in xwml for judgment and calculation
- Class design basis and advanced
- Fourier series
猜你喜欢

Kubernetes cluster storageclass persistent storage resource core concept and use

蓝桥杯单片机省赛第十二届第二场

Analyse de 43 cas de réseaux neuronaux MATLAB: Chapitre 42 opérations parallèles et réseaux neuronaux - - opérations parallèles de réseaux neuronaux basées sur CPU / GPU
![[untitled] basic operation of raspberry pie (2)](/img/b4/cac22c1691181c1b09fe9d98963dbf.jpg)
[untitled] basic operation of raspberry pie (2)

蓝桥杯单片机省赛第十一届第一场

Download and use of the super perfect screenshot tool snipaste

Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud

焱融看 | 混合云时代下,如何制定多云策略

Discrimination between sap Hana, s/4hana and SAP BTP

接口调试工具模拟Post上传文件——ApiPost
随机推荐
C # joint Halcon's experience of breaking away from Halcon environment and various error reporting solutions
0基础如何学习自动化测试?按照这7步一步一步来学习就成功了
初出茅庐市值1亿美金的监控产品Sentry体验与架构
What kind of interview is more effective?
Kotlin基础学习 17
It took me only 3 months to jump out of the comfort zone and become an automated test engineer for 5 years
蓝桥杯单片机省赛第十一届
js生成随机数
Unity脚本的基础语法(8)-协同程序与销毁方法
What is the logical structure of database file
Eight steps of agile development process
How to establish its own NFT market platform in 2022
leetcode-1380. Lucky number in matrix
Introduction to Robotics II. Forward kinematics, MDH method
"Analysis of 43 cases of MATLAB neural network": Chapter 42 parallel operation and neural network - parallel neural network operation based on cpu/gpu
Object oriented thinking
跳出舒适区,5年点工转型自动化测试工程师,我只用了3个月时间
h5中的页面显示隐藏执行事件
Grpc快速实践
Oracle 常用SQL