This example sets up a button to adjust the square wave of different frequencies (multiples of 100) from 100-1000hz. In addition, serial port communication is added, and the corresponding frequency can be set through the numbers sent by the serial port;
#include
#include "uart.h"
sbit PWMOUT = P1^0;
sbit led = P2^2;
sbit KEY1 = P1^2;
sbit KEY2 = P3^3;
unsigned char HighRH = 0; //high byte of high level reload value
unsigned char HighRL = 0; //low byte of high level reload value
unsigned char LowRH = 0; //high byte of low level reload value
unsigned char LowRL = 0; //low byte of low level reload value
unsigned int fr;
unsigned int k=0,t,i;
unsigned int s = 100;
unsigned char a[3];
unsigned int b=0;
void ConfigPWM(unsigned int fr, unsigned char dc);
void ConfigUART(unsigned int baud);
void delay(unsigned int n){ while (n--);}
void main()
{
bit backup1 = 1;
bit backup2 = 1;
bit keybuf1 = 1; // Temporary storage of key values, temporarily saving the scanned values of keys
bit keybuf2 = 1; // Temporary storage of key values, temporarily saving the scanned values of keys
//keybuf1 = KEY1; //temporarily save the current scan value
EA=1;
led = 0;
ConfigUART(9600); //Configure the baud rate to 9600
ConfigPWM(100,50);
//Timer1Init();
while (1)
{
if(k==1)
{
k=0;
ConfigPWM(s,50);
led = ~led;delay(2000);
}
keybuf1 = KEY1; //temporarily store the current scan value
if (keybuf1 != backup1) //The current value is not equal to the previous value, indicating that the key is in action
{
delay(1000); //delay about 10ms
if (keybuf1 == KEY1) //Judge whether the scan value has changed, i.e. the key is jittering
{
if (backup1 == 0) //If the previous value is 0, it means the current action is a pop-up
{
s = s-50;
}
backup1 = keybuf1; //Update the backup to the current value for the next comparison
}
ConfigPWM(s,50);
}
keybuf2 = KEY2; //temporarily store the current scan value
if (keybuf2 != backup2) //The current value is not equal to the previous value, indicating that the key is in action
{
delay(1000); //delay about 10ms
if (keybuf2 == KEY2) //Judge whether the scan value has changed, that is, the key is jittering
{
if (backup2 == 0) //If the previous value is 0, it means the current action is a pop-up
{
s = s+50;
}
backup2 = keybuf2; //Update the backup to the current value for the next comparison
}
ConfigPWM(s,50);
}
}
}
void ConfigUART(unsigned int baud)
{
SCON = 0x50; //Configure the serial port to mode 1
TMOD &= 0x0F; // Clear the control bit of T1
TMOD |= 0x20; //Configure T1 to mode 2
TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value
TL1 = TH1; //initial value equals reload value
ET1 = 0; //Interrupt T1
ES = 1; //Serial port interrupt
TR1 = 1; //Start T1
}
void ConfigPWM(unsigned int fr, unsigned char dc)
{
unsigned int high, low;
unsigned long tmp;
tmp = (11059200/12)/fr; //Calculate the count value required for one cycle
high = (tmp*dc) / 100; //Calculate the count value required for the high level
low = tmp - high; //Calculate the count value required for the low level
high = 65536 - high + 12; //Calculate the high level reload value and compensate for interrupt delay
low = 65536 - low + 12; //Calculate the low level reload value and compensate for interrupt delay
HighRH = (unsigned char)(high>>8); //Split the high-level reload value into high and low bytes
HighRL = (unsigned char)high;
LowRH = (unsigned char)(low>>8); // split the low level reload value into high and low bytes
LowRL = (unsigned char)low;
TMOD &= 0xF0; // Clear the control bit of T0
TMOD |= 0x01; //Configure T0 to mode 1
TH0 = HighRH; //Load T0 reload value
TL0 = HighRL;
ET0 = 1; // Enable T0 interrupt
TR0 = 1; //Start T0
PWMOUT = 1; //output high level
}
void InterruptTimer0() interrupt 1
{
if (PWMOUT == 1) //When the current output is high, load the low level value and output low level
{
TH0 = LowRH;
TL0 = LowRL;
PWMOUT = 0;
}
else //When the current output is low level, load the high level value and output high level
{
TH0 = HighRH;
TL0 = HighRL;
PWMOUT = 1;
}
}
/*void Timer1Init()
{
TMOD|=0X10; //Select timer 1 mode, working mode 1, only use TR1 to start.
TH1=0XFC; //Assign initial value to the timer, set the timer to 1ms
TL1=0X18;
ET1=1; //Enable timer 1 interrupt
EA=1; //Open the general interrupt
TR1=1; //Turn on the timer
}
void Timer1() interrupt 3
{
static u16;
TH1=0XFC; //Assign initial value to the timer, set the timer to 1ms
TL1=0X18;
i++;
if(i==1000)
{
i=0;
t++;
if(t%2==0)
{
SBUF=t/10;
SBUF=t%10;
}
}
}*/
void ComINT() interrupt 4 //interrupt receiving program
{
if(RI) //Judge whether the reception is complete. After the reception is completed, the hardware sets the RI bit
{
RI=0;
//SBUF=SBUF;
a[b]=SBUF;
b++;
if(b==3)
{
/*if(a[0]=='a'&&a[1]=='b'&&a[2]=='c')
{led = 0; delay(2000);}
else
{led = 1;}*/
b=0;s=(a[0]-0x30)*100+(a[1]-0x30)*10+(a[2]-0x30);
k=1;
//SBUF = 'T';
}
}
if(TI)
{
TI=0;
}
}
Previous article:Interrupt timer and running light
Next article:Use 51 single chip microcomputer to write a program to generate 38K square wave
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Principle of MCU reset
- Regarding the C216 issue, I want to make a combination lock, and I'm a newbie looking for advice from a master! !
- The use of SRAM in HPM6750 and the problems encountered
- Oscilloscope automobile turbocharger solenoid valve waveform and analysis
- Transformer voltage calculation
- Failure to send data in broadcast mode
- EEWORLD University - IGBT module technology, drive and application
- Mbed online compiler to be retired soon
- New China Mobile Onenet NB Development Board
- PicoPlanet – Development board with capacitive touch