当前位置:网站首页>The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
The 13th simulation problem of the single chip microcomputer provincial competition of the Blue Bridge Cup
2022-07-01 18:26:00 【Miseñor】
This time, it's still iic The channels of the photosensitive and potentiometer are interchanged , I don't know why , I also encountered such a situation when I was doing the national games last time ( See other articles on my blog ), Now I don't know why , I hope some big guys can answer questions and dispel doubts .( All functions have been realized )
subject :




Code :
main.c
#include <STC12C5A60S2.H>
#include"intrins.h"
#include"iic.h"
int smg_mode=0;
int count_f=0; //555 Count
int num_f=0; //555 Show
int r_1=0;// light
int r_2=0;// electric
int iic_mode=0;
int eeprom_f;
int eeprom_v;
int led_work=1;
int led=0xff;
int num_z;
sbit S4=P3^3;
sbit S5=P3^2;
sbit S6=P3^1;
sbit S7=P3^0;
char code xianshi[]={
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x8e,0xc8,0xc1,0xbf,0x7f};
void smg_display();
void get_z();
void choose_573(int i)
{
switch(i)
{
case(0):P2=(P2&0x1f)|0x00;break;
case(4):P2=(P2&0x1f)|0x80;break;
case(5):P2=(P2&0x1f)|0xa0;break;
case(6):P2=(P2&0x1f)|0xc0;break;
case(7):P2=(P2&0x1f)|0xe0;break;
}
}
void init_system()
{
choose_573(4);
P0=0xff;
choose_573(5);
P0=0x00;
choose_573(0);
}
//==========================================555
void Timer0Init(void) //100 Microsecond @11.0592MHz
{
AUXR |= 0x80; // Timer clock 1T Pattern
TMOD = 0x04; // Set timer mode
TL0 = 0xff; // Set the initial timing value
TH0 = 0xff; // Set the initial timing value
TF0 = 0; // eliminate TF0 sign
TR0 = 1; // Timer 0 Start timing
ET0=1;
EA=1;
}
void time0_service() interrupt 1
{
count_f++;
}
//==========================================
//==========================================iic
void get_guang()
{
r_1=guang();
}
void get_dian()
{
r_2=dian();
}
//==========================================
//========================================== interrupt
void Timer1Init(void) //5 millisecond @11.0592MHz
{
AUXR |= 0x40; // Timer clock 1T Pattern
TMOD &= 0x0F; // Set timer mode
TL1 = 0x00; // Set the initial timing value
TH1 = 0x28; // Set the initial timing value
TF1 = 0; // eliminate TF1 sign
TR1 = 1; // Timer 1 Start timing
ET1=1;
EA=1;
}
int count_1;int count_2;
void time1_service() interrupt 3
{
count_1++;
if(count_1>=180)
{
count_1=0;
num_f=count_f;
count_f=0;
get_guang();
get_dian();
get_z();
}
if(S7==0)
{
count_2++;
if(count_2>=200)
{
count_2=0;
if(led_work==1)
{
led_work=0;
}
else
{
led_work=1;
}
}
}
else
{
count_2=0;
}
}
//==========================================
//========================================== Key
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void key_board()
{
//S4
if(S4==0)
{
Delay10ms();
if(S4==0)
{
smg_mode++;
if(smg_mode==2)
{
iic_mode=0;
}
if(smg_mode>=3)
{
smg_mode=0;
}
}
while(!S4)
{
smg_display();
}
}
//S5
if(S5==0&&smg_mode==2)
{
Delay10ms();
if(S5==0&&smg_mode==2)
{
iic_mode++;
if(iic_mode>=2)
{
iic_mode=0;
}
}
while(!S5)
{
smg_display();
}
}
//S6
if(S6==0)
{
Delay10ms();
if(S6==0)
{
write_EEPROM(0x01,r_1);
Delay10ms();
write_EEPROM(0x02,r_1>>8);
}
while(!S6)
{
smg_display();
}
}
//S7
if(S7==0)
{
Delay10ms();
if(S7==0)
{
write_EEPROM(0x05,num_f);
Delay10ms();
write_EEPROM(0x06,num_f>>8);
}
while(!S7)
{
smg_display();
}
}
}
//==========================================
//==========================================eeprom
void eeprom_read()
{
eeprom_f=read_EEPROM(0x06);
eeprom_f=(eeprom_f<<8)|read_EEPROM(0x05);
// eeprom_f=(eeprom_f*256)+read_EEPROM(0x05);
eeprom_v=read_EEPROM(0x02);
eeprom_v=(eeprom_v<<8)|read_EEPROM(0x01);
}
//==========================================
//========================================== Period calculation
void get_z()
{
num_z=1000000/num_f;
}
//==========================================
//========================================== Nixie tube
void Delay400us() //@11.0592MHz
{
unsigned char i, j;
i = 5;
j = 74;
do
{
while (--j);
} while (--i);
}
void SMG(int wei,int dat)
{
choose_573(6);
P0=0x80>>(wei-1);
choose_573(7);
P0=xianshi[dat];
choose_573(0);
Delay400us();
choose_573(7);
P0=xianshi[10];
choose_573(0);
}
void smg_display()
{
if(smg_mode==0)
{
SMG(1,num_f%10);
if(num_f>=10)
{
SMG(2,(num_f%100)/10);
}
else
{
SMG(2,10);
}
if(num_f>=100)
{
SMG(3,(num_f%1000)/100);
}
else
{
SMG(3,10);
}
if(num_f>=1000)
{
SMG(4,(num_f%10000)/1000);
}
else
{
SMG(4,10);
}
if(num_f>=10000)
{
SMG(5,(num_f%100000)/10000);
}
else
{
SMG(5,10);
}
if(num_f>=100000)
{
SMG(6,(num_f%1000000)/100000);
}
else
{
SMG(6,10);
}
if(num_f>=1000000)
{
SMG(7,(num_f%10000000)/1000000);
}
else
{
SMG(7,10);
}
SMG(8,11);
}
if(smg_mode==1)
{
SMG(1,num_z%10);
if(num_z>=10)
{
SMG(2,(num_z%100)/10);
}
else
{
SMG(2,10);
}
if(num_z>=100)
{
SMG(3,(num_z%1000)/100);
}
else
{
SMG(3,10);
}
if(num_z>=1000)
{
SMG(4,(num_z%10000)/1000);
}
else
{
SMG(4,10);
}
if(num_z>=10000)
{
SMG(5,(num_z%100000)/10000);
}
else
{
SMG(5,10);
}
if(num_z>=100000)
{
SMG(6,(num_z%1000000)/100000);
}
else
{
SMG(6,10);
}
if(num_z>=1000000)
{
SMG(7,(num_z%10000000)/1000000);
}
else
{
SMG(7,10);
}
SMG(8,12);
}
if(smg_mode==2&&iic_mode==0)
{
SMG(1,r_1%10);
SMG(2,(r_1%100)/10);
SMG(3,(r_1%1000)/100);
SMG(3,15);
SMG(4,10);
SMG(5,10);
SMG(6,1);
SMG(7,14);
SMG(8,13);
}
if(smg_mode==2&&iic_mode==1)
{
SMG(1,r_2%10);
SMG(2,(r_2%100)/10);
SMG(3,(r_2%1000)/100);
SMG(3,15);
SMG(4,10);
SMG(5,10);
SMG(6,2);
SMG(7,14);
SMG(8,13);
}
}
//==========================================
//==========================================led
void led_display()
{
if(led_work==1)
{
//L3
if(smg_mode==0)
{
led=(led&0xfb)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfb)|0x04;
choose_573(4);
P0=led;
choose_573(0);
}
//L4
if(smg_mode==1)
{
led=(led&0xf7)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xf7)|0x08;
choose_573(4);
P0=led;
choose_573(0);
}
//L5
if(smg_mode==2)
{
led=(led&0xef)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xef)|0x10;
choose_573(4);
P0=led;
choose_573(0);
}
//L2
if(num_f>eeprom_f)
{
led=(led&0xfd)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfd)|0x02;
choose_573(4);
P0=led;
choose_573(0);
}
//L1
if(r_1>eeprom_v)
{
led=(led&0xfe)|0x00;
choose_573(4);
P0=led;
choose_573(0);
}
else
{
led=(led&0xfe)|0x01;
choose_573(4);
P0=led;
choose_573(0);
}
}
if(led_work==0)
{
led=0xff;
choose_573(4);
P0=led;
choose_573(0);
}
}
//==========================================
void main()
{
init_system();
Timer0Init();
Timer1Init();
while(1)
{
smg_display();
key_board();
// eeprom_v=read_EEPROM(0x01);
// eeprom_f=read_EEPROM(0x05);
eeprom_read();
led_display();
//
}
}
iic.c
/* Program description : IIC Bus driver Software environment : Keil uVision 4.10 Hardware environment : CT107 SCM comprehensive training platform 8051,12MHz Japan period : 2011-8-9 */
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
// Bus pin definition
sbit SDA = P2^1; /* cable */
sbit SCL = P2^0; /* Clock line */
void IIC_Delay(unsigned char i)
{
do{
_nop_();}
while(i--);
}
// Bus start condition
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
// Bus stop condition
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
// Send reply
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit; // 0: The reply ,1: Non response
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;
}
// adopt I2C The bus sends 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;
}
// from I2C Data is received on the bus
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;
}
int guang()
{
int temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x01);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
temp=temp*1.96+0.5;
return temp;
}
int dian()
{
int temp;
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x03);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(0x91);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
// temp=temp*1.96+0.5;
return temp;
}
void write_EEPROM(int add,int dat)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_SendByte(dat);
IIC_WaitAck();
IIC_Stop();
}
int read_EEPROM(int add)
{
int temp;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(add);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
temp=IIC_RecByte();
IIC_SendAck(1);
IIC_Stop();
return temp;
}
iic.h
#ifndef _IIC_H
#define _IIC_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);
int guang();
int dian();
void write_EEPROM(int add,int dat);
int read_EEPROM(int add);
#endif
边栏推荐
- The latest software scheme of the intelligent information management system of the armed police force
- Rust language - cargo, crates io
- Is the fund of futures account safe? How to open an account?
- What are the six steps of the software development process? How to draw software development flow chart?
- MES production equipment manufacturing execution system software
- Yolov5 practice: teach object detection by hand
- Classpath classpath
- Thinkphp6 - CMS multi wechat management system source code
- At present, where is the most formal and safe account opening for futures speculation? How to open a futures account?
- [PHP foundation] realize the connection between PHP and SQL database
猜你喜欢

Check log4j problems using stain analysis

Penetration practice vulnhub range Nemesis

Leetcode 1380. Lucky numbers in the matrix (save the minimum number of each row and the maximum number of each column)

t10_ Adapting to Market Participantsand Conditions

Localization through custom services in the shuttle application

Heavy disclosure! Hundreds of important information systems have been invaded, and the host has become a key attack target
![Htt [ripro network disk link detection plug-in] currently supports four common network disks](/img/e4/c06b94243cdbe391b8b2124ff30789.jpg)
Htt [ripro network disk link detection plug-in] currently supports four common network disks

Definition of rotation axis in mujoco

Extract the compressed package file and retrieve the password

Bug of QQ browser article comment: the commentator is wrong
随机推荐
Key points on February 15, 2022
Opencv map reading test -- error resolution
MySQL -- explain performance optimization
Detailed explanation of select in golang
MFC obtains local IP (used more in network communication)
Definition of rotation axis in mujoco
Happy new year | 202112 monthly summary
网上股票开户安全吗?是否可靠?
Basic usage of shell script
LeetCode 148. Sort linked list
Session layer of csframework, server and client (1)
徽商期货是正规期货平台吗?在徽商期货开户安全吗?
SPIE Western optoelectronics exhibition returned offline and successfully held a science and engineering event
Is it safe to open a stock account by mobile phone? What do you need to bring with you to open an account?
February 16, 2022 Daily: graph neural network self training method under distribution and migration
On the language internationalization of Yongzhong Office
The new server is packaged with the source code of H5 mall with an operation level value of several thousand
Apache iceberg source code analysis: schema evolution
Is it reasonable and safe to open a securities account for 10000 shares free of charge? How to say
Rotation order and universal lock of unity panel