STC89C51 microcontroller serial port debugging attention intensity program design for LCD display

Publisher:jiaohe1Latest update time:2023-06-27 Source: eefocusKeywords:STC89C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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


Keywords:STC89C51 Reference address:STC89C51 microcontroller serial port debugging attention intensity program design for LCD display

Previous article:Laser confocal scanning microscope based on AT89C51
Next article:Design of furnace temperature control system using AT89C51 microcontroller and temperature transmitter

Latest Microcontroller Articles
Change More Related Popular Components
Guess you like

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号