Neon lamp based on stc51 single chip microcomputer

Time:2021-8-27

Neon lamp based on stc51 single chip microcomputer

Design requirements:

  • Use PWM to drive 8 LED lights
  • The human eye can’t see all the lights go out
  • The light should have animation effect

Design Overview:

   according to the design requirements, in order to more intuitively explain the pulse width modulation technology (PWM), the animation effect of neon lights is in the form of running lights. The required MCU chip is STC89C52, and the hardware tool used is an intelligent car based on STC89C52 developed by Huaqing vision. The trolley is equipped with the required LED lamp module, and 8 LED lamps are controlled by P1 port. STC89C52 is an 8-bit microcontroller with low power consumption and high performance. It is an enhanced version of 80C51 single chip microcomputer, but like 80C51 single chip microcomputer, it does not have PWM hardware module, so we need to write a program to simulate PWM square wave by software.

PWM is a square wave, which can realize digital signal control of analog circuit. It has two important parameters: cycle or frequency and duty cycle. Duty cycle = high level time / cycle, the minimum is 0% and the maximum is 100%. Adjust the duty cycle to control the proportion of high level and low level, and then adjust the on-off and light / dark changes of LED lamps. In STC89C52, P1 port is effective at low level. Set P1 port to 0, the LED light is on, and set 1 to off. If the level output signal of P1 port is adjusted to 50% of the high level and 50% of the low level, that is, the proportion of high level and low level in a cycle is 50%, the LED lamp controlled by P1 port will flash light and dark.

   many 51 single chip microcomputer development boards on the market are similar. They use P1 port to control LED lights, so the schematic diagram and physical diagram of intelligent car are not provided here.

Source code:

Software simulation PWM square wave is usually realized by timer.

Reference code 1:

#include 
//A global variable PWM is defined, and the duty cycle of PWM is controlled by accumulating the global variables
unsigned int pwm = 0;
/*
The timer and I / O port are used to output PWM square wave to realize the light and shade change of 8 LED lamps
*/

/*Timer interrupt service function*/
void Timer0() interrupt 1
{
	pwm++;
      if(pwm == 500)
      {
         P1 = 0xfe;// The first light is on
      }
      else if(pwm == 1000)
      {
   	     P1 = 0xff;// The first light is off
      }
	  else if(pwm == 1500)
      {
   	     P1 = 0xfd;// The second light is on
      }
	  else if(pwm == 2000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 2500)
      {
   	     P1 = 0xfb;// The third light is on
      }
	  else if(pwm == 3000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 3500)
      {
   	     P1 = 0xf7;// The fourth light is on
      }
	  else if(pwm == 4000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 4500)
      {
   	     P1 = 0xef;// The fifth light is on
      }
	  else if(pwm == 5000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 5500)
      {
   	     P1 = 0xdf;// The sixth light is on
      }
	  else if(pwm == 6000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 6500)
      {
   	     P1 = 0xbf;// The seventh light is on
      }
	  else if(pwm == 7000)
      {
   	     P1 = 0xff;
      }
	  else if(pwm == 7500)
      {
   	     P1 = 0x7f;// The 8th light is on
      }
	  else if(pwm == 8000)
      {
   	     P1 = 0xff;
		 pwm = 0;
      }
}

void main()
{
   TMOD |= 1<<1; 	  // By shift operator "<

Reference code 2:

/*The first file is a custom header file
PWM is declared in this header file_ led_ CTL function
*/
#ifndef _LED_H
#define _LED_H

//Control a light on
char pwm_led_ctl(unsigned int led_num);
#endif

/*
Second file
This source file is used to implement PWM_ led_ CTL function
*/
#include 
#include "led.h"
//The software simulates PWM square wave and regulates PWM duty cycle through variable PWM
char pwm_led_ctl(unsigned int led_num)
{
   unsigned int pwm;
   for(pwm = 0;pwm <= 2000;pwm++)
   {
     if(pwm == 1000)
     {
   	    P1 &= ~(1<
#include "led.h"

/*Timer interrupt service function*/
void Timer0() interrupt 1
{
	unsigned int i;
	for(i=0;i<8;i++)
	{
	   pwm_led_ctl(i);
	}
}

void main()
{
   TMOD |= 1<<1; 	  // By shift operator "<

Recommended Today

IOS video recording

This article makes a summary of the previous camera modules, including custom camera for video shooting, video processing and saving. Interested friends can make a reference Framework introduction avfoundation image.png Commonly used for media recording, editing, playing, audio recording and playing, video and audio decoding, etc Common classes: avcapturedevice, avcapturedeviceinput, avcapturephotooutput, avcapturevideopreviewlayerAvasset, avassetreader, avassetwriter, cmsamplebuffer, […]