#define uchar unsigned char
#define uint unsigned int
#define OUT PORTA
uint high;
uint tatal;//define the full gram count value corresponding to the number of digits
uchar
uchar
uchar keyadd,keysub;//define key
void output1(uint t);//process duty cycle
void output2(void);//process displayed frequency
void add_sub(void);//addition and subtraction function
void DelayMs(uint i);//0.25ms
void display(uchar *p);//digital tube scanning function
/**************************************************************************/
void DelayMs(uint i)//0.25ms
{uchar j;
for(;i!=0;i--)
{for(j=250;j!=0;j--) {;}}
}
/************************************************************************/
void display(uchar *p)//digital tube scanning function
{
OUT=0x70+p[0]; DelayMs(15);
OUT=0xb0+p[1]; DelayMs(15);
OUT=0xd0+p[2]; DelayMs(15);
OUT=0xe0+p[3]; DelayMs(15);
}
/*************Assign values to compare registers********************************************/
/*8-bit count value is 255; 9-bit count value is 512; 10-bit count value is 1024; *************************************/
void set(uint a)//Realize dual channels, assign the same value to both channels
{OCR1AH=a>>8;OCR1BH=a>>8;
0;
}
/****************************************************************************/
/****************************************************************************/
void output1(uint t)//duty cycle display processing//Here t is the value stored in the data register
{uint counter;
unsigned long j;
j=(float)t*10000;
counter=j/tatal;
ta
counter=counter%1000;
ta
counter=counter%100;
ta
ta
PORTB=0;
}
/*****************************************************************************/
/****************************************************************************/
void output2(void)//Processing displayed frequency
{uint i=1,jk,k;//Define i as the value to be processed corresponding to the number of bits,,, define jk as the value corresponding to the frequency division
uint counter;
if((TCCR1A&0X03)==1)
{i=510;tatal=255;}
else if((TCCR1A&0X03)==2)
{i=1022;tatal=511;}
else if((TCCR1A&0X03)==3)
{i=2046;tatal=1023;}
if((TCCR1B&0x07)==5)
jk=1024;
else if((TCCR1B&0x07)==4)
jk=256;
else if((TCCR1B&0x07)==3)
jk=64;
else if((TCCR1B&0x07)==2)
jk=8;
else if((TCCR1B&0x07)==1)
jk=1;
counter=8000000/i;//Select the value corresponding to the number of bits//8000000 corresponds to the M crystal
counter=counter/jk;//Select the frequency division
da
counter=counter%1000;
da
counter=counter%100;
da
da
PORTB=0XFF;
}
/****************************************************************************/
/*****************************Key circuit********************************************/
void add_sub(void)
{//Adjusting the value of Example 50 below can change the value to be added each time, that is, increase or decrease the speed;
while((PINC&0X01)==0) {keyadd=1;display(da
if(keyadd==1){high+=50;keyadd=0;}
while((PINC&0X02)==0) {keysub=1; display(data
if(keysub==1){high-=50;keysub=0;}
if(high>tatal)
high=tatal-1;
else if(high<51)
high=51;
}
/***************************************************************************/
/***************************************************************************/
void main(void)
{uchar i;
DDRD=0XFF;//When using PWM output, you must initialize its port
PORTD=0XFF;
DDRB=0XFF;//
PORTB=0XFF;
DDRC=0X00;//Port as keyboard input,
PORTC=0XFF;//initialize
DDRA=0XFF;
OUT=0X00;//IO port initialization;
DelayMs(150);
//TCCR1A=0XE3;//Dual PWM
TCCR1A=0X82;//Set when OC1A output matches upward, clear when matches downward The last two bits are pulse width modulation mode
//01:8 bit 10:9 bit 11:10 bit
TCCR1B=0X03;//001:1 frequency division 010:8 frequency division 011:64 frequency division 100:256 frequency division;101:1024 frequency division
high=500;//Time occupied by high level
output1(high);//Processing duty cycle
output2();//Processing frequency
high=tatal/2;
/*******************************************************/
while(1)
{
set(high);
output1(high);//Processing duty cycle
for(i=100;i!=0;i--)
{display(da
output2();//Processing frequency
for( i=100;i!=0;i--)
{ display(da
}
/*1. Due to the problem of C language (1) while((PINC&0X01)==0) {keyadd=1;display(da
if(keyadd==1){high+=50;keyadd=0;}
while((PINC&0X02)==0) {keysub=1; display(da
if(keysub==1){high-=50;keysub=0;}
Is there any error in the above program? No, this is the program I debugged well, but before I wrote it like this: while(PINC&0X01==0) {keyadd=1;display(da
if(keyadd==1){high+=50;keyadd=0;}
while(PINC&0X02==0) {keysub=1; display(da ta
if(keysub==1){high-=50;keysub=0;}
Because of this I wasted half a day; ------ In short, it is a priority issue, many problems that can be passed in theory, but cannot be passed in practice;
2 About the if else problem; --—— For example, is there any problem with the following program;
if((TCCR1A&0X03)==1)
{i=510;tatal=255;}
else if((TCCR1A&0X03)==2)
{i=1022;tatal=511;}
else if((TCCR1A&0X03)==3)
{i=2046;statal=1023;}
if((TCCR1B&0x07)==5)
jk=1024;
else if((TCCR1B&0x07)==4)
jk=256;
else if((TCCR1B&0x07)==3)
jk=64;
else if((TCCR1B&0x07)==2)
jk=8;
else if((TCCR1B&0x07)==1)
jk=1;
This is the program I debugged; I added an else in the middle at the beginning; I can't get it out, that is, these are two programs, that is to say, when an execution condition is encountered, the following program will not be executed. Don't debug the program like a fool, the important thing is to find the key to the problem;
3. After debugging and verification, the two PWMs of the timer can run simultaneously;
4. Initialize the IO port when the program is initialized; this also delayed me for a long time; in the future, remember to initialize the IO port when pwm is used as output.*/
Previous article:Research and development of wireless remote data acquisition terminal based on AVR single chip microcomputer
Next article:External asynchronous clock operation
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- Severe packet loss when transmitting large amounts of data using TI's CC1310
- Hardware Implementation of Asynchronous FIFO Based on FPGA
- Microcontroller low power design resources
- Codec is the key to PMP
- Exclusively sell Lumentum's high-quality optical communication module packaging and testing equipment, don't miss this opportunity!
- The Electronic Engineer Self-Study Handbook is here! Read good books for free to help electronic engineers grow!
- How to ensure high performance design of PCB
- AD5064 drives 16-bit da
- I bought a ZT303 multimeter.
- Design techniques for machine learning algorithm generation (provided by ST official, Chinese subtitles)