I have recently been debugging the sensor program, and here is a summary of my experience.
How to debug a program
Method 1: LED display method, call this function led = 0 in the program; you can know where the program runs, why it goes wrong, and where it falls into an infinite loop.
Method 2: Serial port printing method. The serial port printing method can know what the function outputs. You only need to use the serial port interrupt in the program. Regarding how to use the serial port, I think I need to summarize the recent programming problems in a moment.
Now I want to summarize the serial port debugging method. Brother Tianxiang summarized the serial port debugging method in his book. Although he opened it at the beginning and understood his situation, he can use everything except the serial port. I don’t understand. Only now do I really understand what the real meaning of serial port interrupt is. Serial port interrupt can interrupt the execution of the microcontroller and allow the microcontroller to execute other functions while executing the main function.
// BH1750FVI IIC test program
//Use microcontroller STC89C51
// Crystal oscillator: 11.0592M
//Display: LCD1602
// Compilation environment Keil uVision2
//******************************************
#include
#include //Keil library
#include //Keil library
#include
#define uchar unsigned char
#define uint unsigned int
sbit SCL=P1^0; //IIC clock pin definition
sbit SDA=P1^1; //IIC data pin definition
#define Slave Ad dress 0x46 //Define the slave address of the device in the IIC bus, modify it according to the different ALT ADDRESS address pins
//When the ALT ADDRESS pin is connected to ground, the address is 0xA6, and when connected to power , the address is 0x3A
typedef unsigned char BY TE ;
typedef unsigned short WORD;
BYTE BUF[8]; //Receive data buffer area
uchar table[5]; //Display variables
int dis_data; //Variable
void delay_nms(unsigned int k);
void Init_BH1750(void);
void conversion(uint temp_data);
void Single_Write_BH1750(uchar REG_Address); //Single write data
uchar Single_Read_BH1750 (uchar REG_Address); //Single read internal register data
void Multiple_Read_BH1750(); //Continuously read internal register data
//----------------------------------------
void Delay5us();
void Delay5ms();
void BH1750_Start(); //Start signal
void BH1750_Stop(); //Stop signal
void BH1750_Sen dAC K (bit ack); //Reply ACK
bit BH1750_RecvACK(); //read ack
void BH1750_SendByte(BYTE dat); //IIC single byte write
BYTE BH1750_RecvByte(); //IIC single byte read
//----------------------------------
//************************************************ *********
void conversion (uint temp_data) // Data is converted into units, tens, hundreds, thousands, and ten thousand
{
table[0]=temp_data/10000+0x30;
temp_data=temp_data%10000; //remainder operation
table[1]=temp_data/1000+0x30;
temp_data=temp_data%1000; //remainder operation
table[2]=temp_data/100+0x30;
temp_data=temp_data%100; //remainder operation
table[3]=temp_data/10+0x30;
temp_data=temp_data%10; //remainder operation
table[4]=temp_data+0x30;
}
//Millisecond delay******************************
void delay_nms(unsigned int k)
{
unsigned int i,j;
for(i=0;i
{
for(j=0;j《121;j++)
{;}}
}
/****************************************
Delay 5 microseconds (STC90C52RC@12M)
This function needs to be adjusted for different working environments. Note that it needs to be modified when the clock is too fast.
When switching to a 1T MCU , please adjust this delay function
***************************************/
voidDelay5us()
{
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
_nop_();_nop_();_nop_();_nop_();
}
/****************************************
Delay 5 milliseconds (STC90C52RC@12M)
Different working environments require adjusting this function
When switching to a 1T MCU, please adjust this delay function
***************************************/
void Delay5ms()
{
WORD n = 560;
while (n--);
}
/****************************************
start signal
***************************************/
void BH1750_Start()
{
SDA = 1; //Pull the data line high
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SDA = 0; //Generate falling edge
Delay5us(); //Delay
SCL = 0; //pull the clock line low
}
/****************************************
stop signal
***************************************/
void BH1750_Stop()
{
SDA = 0; //Pull the data line low
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SDA = 1; //Generate rising edge
Delay5us(); //Delay
}
/****************************************
Send acknowledgment signal
Entry parameters: ack (0:ACK 1:NAK)
***************************************/
void BH1750_SendACK(bit ack)
{
SDA = ack; //Write response signal
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SCL = 0; //pull the clock line low
Delay5us(); //Delay
}
/****************************************
receive response signal
***************************************/
bit BH1750_RecvACK()
{
SCL = 1; //pull the clock line high
Delay5us(); //Delay
CY = SDA; //Read response signal
SCL = 0; //pull the clock line low
Delay5us(); //Delay
return CY;
}
/****************************************
Send one byte of data to the IIC bus
***************************************/
void BH1750_SendByte(BYTE dat)
{
BYTE i;
for (i=0; i«8; i++) //8-bit counter
{
dat "《= 1; //Move out the highest bit of data
SDA = CY; //Send data port
SCL = 1; //pull the clock line high
Delay5us(); //Delay
SCL = 0; //pull the clock line low
Delay5us(); //Delay
}
BH1750_RecvACK();
}
/****************************************
Receive one byte of data from the IIC bus
***************************************/
BYTE BH1750_RecvByte()
{
BYTE i;
BYTE dat = 0;
SDA = 1; //Enable internal pull-up and prepare to read data,
for (i=0; i«8; i++) //8-bit counter
{
dat "《= 1;
SCL = 1; //pull the clock line high
Delay5us(); //Delay
dat |= SDA; //Read data
SCL = 0; //pull the clock line low
Previous article:Laser confocal scanning microscope based on AT89C51
Next article:Design of furnace temperature control system using AT89C51 microcontroller and temperature transmitter
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs