The following is the program for IIC to memorize the number of times the microcontroller is powered on:
The program is as follows:
//Copyright: Single Chip Computer Network http://www.51hei.com
#include
#include
#define uchar unsigned char//macro definition
#define uint unsigned int
sbit scl=P2^0; //These two bits are defined to control the pins of IIC
sbit sda=P2^1;
uchar number[10]={ //Digital tube array definition
0x3f,
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x6f
};
void delayt(void)//Delay of digital tube
{
uint x,y;
for(x=0;x<5;x++)
for(y=0;y<120;y++);
}
void delay(void)//Adaptive delay of IIC
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
void start()//Start
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()//Stop
{
sda=0;
delay();
scl=1 ;
delay();
sda=1;
delay();
}
void init()//Initialization settings
{
sda=1;
scl=1;
}
void answer()//Response
{
uchar i;
scl=1;
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void noanser()//Non-response
{
scl=1;
delay();
sda=1;
delay( );
scl=0;
delay();
}
void writebyte(uchar dat)//Write byte
{
uchar i;
scl=0;
delay();
for(i=0;i<8;i++)
{
if(dat&0x80)
{
sda=1;
}
else
{
sda=0;
}
dat=dat<<1;
delay();
scl=1;
delay();
scl=0;
delay();
}
sda=1;
}
uchar readbyte()//Read byte
{
uchar i,dat;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay( );
dat=dat<<1;
if(sda)
{
dat++;
}
scl=0;
delay();
}
return dat;
}
void write_byte(uchar add,uchar dat)//Write byte encapsulation
{
init();
start();
writebyte(0xae);
answer();
writebyte(add);
answer();
writebyte(dat);
answer( );
stop();
}
uchar read_byte(uchar add)//Read byte encapsulation
{
uchar value;
init();
start();
writebyte(0xae);
answer();
writebyte(add);
answer();
start();
writebyte(0xaf);
answer();
value=readbyte();
noanser();
stop();
return value;
}
void main()
{
uchar temp,k,j;
temp=read_byte(0x10);//digital tube units and tens operation
k=temp/10;
j=temp%10;
temp++;
write_byte(0x10,temp);
delay();
while(1) //digital tube display
{
P1=number[k];
P0=0;
delayt();
P1=number[j];
P0=1;
delayt();
}
}
Previous article:Research and design of automatic observation control platform for solar radio telescope
Next article:Single chip microcomputer and stepper motor control
Recommended ReadingLatest update time:2024-11-17 02:40
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Here it comes, here comes the weekly review information~~
- MS5192T replaces AD7792
- Why does the output voltage of a power amplifier have errors? (Part 2)
- [Unmanned driving smart car based on ESP32 road sign identification] Unboxing ESP32-S2-KALUGA-1+K210Sipeed M1 docking station kit
- Challenge and collect cards | Light electric vehicles and power tools are just the right time, Infineon invites you to join the fun!
- script_recording_test_no_reply
- RT-thread introductory training
- [RVB2601 Creative Application Development] Power Monitoring
- Lenovo is marching into the "U" world with "wireless dual-core" and showing its general style again
- Pingtouge RISC-V Low Power Board-RVB2601 Development Board Quick Start Guide