This program has 3 files listed below:
stc12c5a.h Header file download: http://www.51hei.com/mcu/3282.html
main.c
#include "stc12c5a.h"
#include "type.h"
#define FRE 22118400
#define TIME_FRE 10000 //Time 1/10000=0.0001s=0.1ms (This time is used to speed up the LED flashing frequency)
#define T0RELOAD (65536 - (FRE / TIME_FRE))
//***************************************Initialize variables
volatile U32 SYS_TIME = 0;
volatile U32 LED_PWM = 0;
//*****************************************延时函数
void delay(U32 dly)
{
U32 i;
while(--dly > 0)
{
for(i=0;i++;i<1000000);
}
}[page]
//********************************************Timer0 initialization function
void Timer0_Init()
{
TMOD &= 0XF0;
TMOD |= 0X01; //16-bit counting mode of timer 0
TH0 = T0RELOAD / 256;
TL0 = T0RELOAD % 256;
AUXR |= 0X80; //Counter 0 clock is Fosc, that is, 1T
PT0 = 1; //Timer 0 priority control bit, 1 is high priority 0 is low priority
EA = 1; //Open total interrupt
ET0 = 1; //Open timer 0 interrupt
TR0 = 1; //Start timer 0
}
//********************************************Main function
void main()
{
Timer0_Init();
//Modify the program according to actual situation
while(1)
{
P33 = 1;
if(P33 == 0) // KEY1
{
delay(5000);
if(P33 == 0)
{
LED_PWM = 0; // Turn off LED
}
}
P34 = 1;
if(P34 == 0) // KEY2 button is kept pressed, KED gradually becomes brighter
{
delay(5000);
if(P34 == 0)
{
if(LED_PWM < 100)
LED_PWM ++;
}
}
P35 = 1;
if(P35 == 0) // KEY3 button is kept pressed, KED gradually becomes darker
{
delay(5000);
if(P35 == 0)
{
if(LED_PWM >0)
LED_PWM --;
}
}
}
}
//*********************************************Timer0 interrupt
void Timer0_Routine() interrupt 1
{
TH0 = T0RELOAD / 256;
TL0 = T0RELOAD % 256;
//The following program should be modified according to the actual situation
SYS_TIME ++;
if(SYS_TIME < LED_PWM)
{
P0 = 0; //Turn on the LED
}
else
{
P0 = 0XFF; //Turn off the LED
}
if(SYS_TIME > 100)
{SYS_TIME = 0;}
}
type.h
#ifndef _TYPE_H_
#define _TYPE_H_
typedef unsigned char U8;
typedef signed char S8;
typedef unsigned int U16;
typedef signed int S16;
typedef unsigned long U32;
typedef signed long S32;
#endif
Previous article:Detailed explanation of original code, inverse code and complement code
Next article:About the use of extern in modular programming
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Keysight Technologies Helps Samsung Electronics Successfully Validate FiRa® 2.0 Safe Distance Measurement Test Case
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Innovation is not limited to Meizhi, Welling will appear at the 2024 China Home Appliance Technology Conference
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- I heard that iPhone 13 will add reverse charging function???
- About the parallel and series resistors at OUT in the crystal oscillator circuit
- Video animation: intuitive understanding of electromagnetic radiation
- Single-chip microcomputer controlled xc1004 four-axis SPI motion control chip information
- I would like to ask a basic question: Is the discharge speed of parallel capacitors determined by the ESR of the capacitors?
- How to choose the appropriate inductor and its parameters? | MPS invites you to learn practical tips and win gifts by commenting!
- I need help with soldering tips. Why does flux often splash out when I add tin to my soldering iron, burning my hands?
- What are the differences between RTU and DTU?
- Three key technologies of mobile robots
- FPGA implementation of terrestrial digital television symbol and carrier synchronization.pdf