Universal PWM subroutine

Publisher:EtherealJourneyLatest update time:2016-09-14 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

#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 ta[5];//display duty cycle
uchar ta0[4];//display frequency
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 [0]=counter/1000; 
 counter=counter%1000; 
 ta [1]=counter/100; 
 counter=counter%100; 
 ta [2]=counter/10; 
 ta [3]=counter%10; 
 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 ta0[0]=counter/1000; 
 counter=counter%1000; 
 da ta0[1]=counter/100; 
 counter=counter%100; 
 da ta0[2]=counter/10; 
 da ta0[3]=counter%10;
 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 ta);} 
      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 ta);add_sub();}//Followed by addition and subtraction function
  output2();//Processing frequency
 for( i=100;i!=0;i--)
     { display(da ta0);add_sub();}//Followed by addition and subtraction function
     }

/*1. Due to the problem of C language (1) while((PINC&0X01)==0) {keyadd=1;display(da ta);} 
      if(keyadd==1){high+=50;keyadd=0;}
 while((PINC&0X02)==0) {keysub=1; display(da ta);}
      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 ta);} 
      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.*/

Reference address:Universal PWM subroutine

Previous article:Research and development of wireless remote data acquisition terminal based on AVR single chip microcomputer
Next article:External asynchronous clock operation

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号