Neon lamp based on STC51 single chip microcomputer

Publisher:Heavenly999Latest update time:2024-04-07 Source: elecfansKeywords:STC51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Design requirements:#

Using PWM to drive 8 LEDs

The human eye cannot detect when all lights go out

Lighting should have animation effects


Design Overview:

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


PWM is a square wave that can realize digital signal control of analog circuits. It has two important parameters: cycle or frequency, and duty cycle. Duty cycle = high level time/cycle, with a minimum of 0% and a maximum of 100%. The duty cycle is adjusted to control the proportion of high and low levels, thereby regulating the brightness and darkness of the LED light. In STC89C52, the P1 port is valid at low level. When the P1 port is set to 0, the LED light is on, and when it is set to 1, the light is off. If the level output signal of the P1 port is adjusted to 50% high and 50% low, that is, the high level and the low level account for 50% in a cycle, the LED light controlled by the P1 port will flicker.


Many 51 single-chip microcomputer development boards on the market are similar, and all use the P1 port to control the LED lights, so the schematic diagram and physical picture of the smart car are not provided here.


source code:#

Software simulation of PWM square wave is usually implemented using a timer.


Reference code 1:


#include

//Define a global variable pwm, and control the PWM duty cycle by accumulating the global variable

unsigned int pwm = 0;

/*

Use the timer and I/O port to output PWM square wave to achieve the brightness change of 8 LED lights

*/


/*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 4th light is on

}

else if(pwm == 4000)

{

P1 = 0xff;

}

else if(pwm == 4500)

{

P1 = 0xef; //The 5th light is on

}

else if(pwm == 5000)

{

P1 = 0xff;

}

else if(pwm == 5500)

{

P1 = 0xdf; //The 6th light is on

}

else if(pwm == 6000)

{

P1 = 0xff;

}

else if(pwm == 6500)

{

P1 = 0xbf; //The 7th 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; //Change the state of the bit through the shift operator "<<"

TMOD &= ~(1<<0); //Set the timer/counter to work in mode 2


TMOD &= ~(1<<2); //Select the timing working mode

TMOD &= ~(1<<3); //Gate bit: start the timer by the run control bit TR


TL0 = 156;

TH0 = 156; //100us enters an interrupt, 0.1 milliseconds


ET0 = 1; //Timer 0 interrupt

EA = 1; //CPU interrupt

TR0 = 1; //Start timer 0

while(1) //Prevent the program from running away

;

}

Reference code 2:


/*The first file is a custom header file

The pwm_led_ctl function is declared in this header file

*/

#ifndef _LED_H

#define _LED_H


//Control a light

char pwm_led_ctl(unsigned int led_num);

#endif


/*

Second file

This source file is used to implement the pwm_led_ctl function

*/

#include

#include "led.h"

//Software simulates PWM square wave and adjusts 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<

}

else if(pwm == 2000)

{

P1 |= 1<

}

}

return 0;

}


/*

The third file

Main function source file

*/

#include

#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; //Change the state of the bit through the shift operator "<<"

TMOD &= ~(1<<0); //Set the timer/counter to work in mode 2


TMOD &= ~(1<<2); //Select the timing working mode

TMOD &= ~(1<<3); //Gate bit: start the timer by the run control bit TR


TL0 = 156;

TH0 = 156; //100us enters an interrupt, 0.1 milliseconds


ET0 = 1; //Timer 0 interrupt

EA = 1; //CPU interrupt

TR0 = 1; //Start timer 0

while(1) //Prevent the program from running away

;

}


Keywords:STC51 Reference address:Neon lamp based on STC51 single chip microcomputer

Previous article:Analysis of the interrupt response process of MCS-51
Next article:Automatic doorbell based on STC51 single chip microcomputer

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号