当前位置:网站首页>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);
#endif
ds1302.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
边栏推荐
- [designmode] builder model
- Oracle 常用SQL
- What kind of interview is more effective?
- In depth analysis of C language - variable error prone knowledge points # dry goods inventory #
- Go执行shell命令
- 蓝桥杯单片机省赛第九届
- Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
- aaaaaaaaaaaaa
- Work hard all day long and be alert at sunset
- 蓝桥杯单片机省赛第十一届第一场
猜你喜欢
"Analysis of 43 cases of MATLAB neural network": Chapter 41 implementation of customized neural network -- personalized modeling and Simulation of neural network
蓝桥杯单片机省赛第十二届第一场
Oracle的md5
In the era of programmers' introspection, five-year-old programmers are afraid to go out for interviews
[HCIA continuous update] overview of dynamic routing protocol
焱融看 | 混合云时代下,如何制定多云策略
MySQL index, transaction and storage engine
MySQL之账号管理
Which of PMP and software has the highest gold content?
Detailed explanation of ThreadLocal
随机推荐
High performance and low power cortex-a53 core board | i.mx8m Mini
[HCIA continuous update] overview of dynamic routing protocol
Interface debugging tool simulates post upload file - apipost
蓝桥杯单片机省赛第七届
UI (New ui:: MainWindow) troubleshooting
蓝桥杯单片机第六届温度记录器
Haute performance et faible puissance Cortex - A53 Core Board | i.mx8m mini
Kotlin基础学习 16
高性能 低功耗Cortex-A53核心板 | i.MX8M Mini
【DesignMode】建造者模式(Builder model)
Introduction to Robotics II. Forward kinematics, MDH method
Kotlin基础学习 14
蓝桥杯单片机省赛第五届
C#联合halcon脱离halcon环境以及各种报错解决经历
SAML2.0 notes (I)
蓝桥杯单片机省赛第十一届
Yan Rong looks at how to formulate a multi cloud strategy in the era of hybrid cloud
How to do medium and long-term stocks, and what are the medium and long-term stock trading skills?
aaaaaaaaaaaaa
Detailed explanation of the difference between Verilog process assignment