Infinite loop problem of microcontroller program

Publisher:cocolangLatest update time:2016-01-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
There is the following single-chip computer program, whose function is to send characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 through the serial port, baud rate: 9600, no parity bit, 8 data bits, 1 stop bit.

#include                  
void Send(char signal)       //Data sending function
{
  SBUF=signal;
  while(TI==0);
  TI=0;
}

void main(void)             //main function
 

   int i;
   char a[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};   //Character table
   TMOD=0x20;
   SCON=0x40;
   PCON=0x00;
   TH1=0xfd;
   TL1=0xfd;
   TR1=1;
  for(i=0;i<10;i++)
  {
      Send(a[i]);          //Send characters in sequence
   }
}

By reading the program, we can know that when it is executed, the microcontroller will send out a total of ten characters from 0 to 9. The result of Keil simulation is the same. However, when the program is written into the development board and actually run, it becomes sending characters 0 to 9 over and over again, forming an infinite loop - it is difficult to explain why this happens.

In order to correct this phenomenon, we have to add a while(1) statement at the end of the program so that it will work normally.

The modified procedure is as follows:

#include                  
void Send(char signal)       //Data sending function
{
  SBUF=signal;
  while(TI==0);
  TI=0;
}

void main(void)             //main function
 

   int i;
   char a[10]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};   //Character table
   TMOD=0x20;
   SCON=0x40;
   PCON=0x00;
   TH1=0xfd;
   TL1=0xfd;
   TR1=1;
  for(i=0;i<10;i++)
  {
      Send(a[i]);          //Send characters in sequence
   }
   while(1);                
}

At least through this phenomenon we can know that Keil's simulation does not necessarily represent the actual situation.

Reference address:Infinite loop problem of microcontroller program

Previous article:Microcontrollers (MCUs) explained using common names
Next article:Memory allocation during microcontroller operation

Latest Microcontroller Articles
Change More Related Popular Components

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号