2011年10月31日 星期一

PWM generation

//=========================
//Project:     PWM generation
//Author:      Leo.Tseng
//Date:        2011/10/28
//MCU:         W78E054D
//CRYSTAL:     25Mhz
//Description: Use uart for input/output, set boudrate=9600,
//             Press "a" to increase PWM high duty, Press "d" to
//             decrease PWM high duty.
//             initination is working in 20% duty.
//=========================
#include  <reg52.h>
#include  <stdio.h>
void uart_initinal(int boudrate);
int TIMES=65536;     // 16bit counter 
int FREQ=10400;      // 200HZ = 10400 機械週期
int PWMHIGH=2080;    // 高電位佔20%=10400*0.2=2080個機械週期
int PWMLOW=8320;     // 低電位佔80%=10400*0.8=8320個機械週期
int PERIOD;
char ch;
sbit PWMOUT = P1^2;
sbit PWMOUT1 = P2^0;
sbit PWMOUT2 = P2^1;
//------------------------------------------------
//MAIN C function
//------------------------------------------------
void main (void)
{
 uart_initinal(9600);              // 起始 UART 設定,baudrate=9600
 //PWMOUT = 0;
 TMOD = (TMOD & 0xF0) | 0x01;      // 設定 T/C0 Mode
 PERIOD = TIMES-PWMHIGH;
 TH0 = PERIOD/256;
 TL0 = PERIOD%256;
 ET0 = 1;                         // 致能 Timer 0 Interrupts
 TR0 = 1;                         // 啟動 Timer 0 計時
 EA = 1;                          // Global Interrupt Enable
 PWMOUT = 1;
 PWMOUT1 = 1;
 PWMOUT2 = 1;

 printf("按 ""a"" 增加PWM工作週期20%%,按 ""d"" 減少PWM工作週期20%%\n");          //顯示操作訊息
 //printf("Press ""a"" to increase duty cycle, ""d"" to decrease duty cycle\n"); //顯示操作訊息
 while (1)
   {
    switch (ch=_getkey())                         
      {
       case '+':                                    // 增加高電位20/%
       case 'A':                                   //
       case 'a':if(PWMHIGH==300)                    //
                   PWMHIGH=2080;
                else if(PWMHIGH <= 6240)        
                        PWMHIGH+=2080;                       
                     else if(PWMHIGH >= 8320)
                            PWMHIGH=10100;
               
                PWMLOW=FREQ-PWMHIGH;
                //printf("a_PWMHIGH is %u\n",PWMHIGH);
                break;
      
       case '-':                                    // 減少高電位20/%
       case 'D':                                   //
       case 'd':if(PWMHIGH==10100)                  //
                  PWMHIGH=8320;
                else if(PWMHIGH >= 4160)
                       PWMHIGH-=2080;                        
                     else if(PWMHIGH <= 2080)
                            PWMHIGH=300;
               
                PWMLOW=FREQ-PWMHIGH;
                //printf("d_PWMHIGH is %u\n",PWMHIGH);
                break;
                
       default: break;                
      }
   }
}

//UART副程式使用timer1
void uart_initinal(int boudrate)      
{
 PCON  = 0x00;                                  // PCON: if SMOD=1(b7), Baud Rate is Doubled                         
 SCON  = 0x52;                                  // SCON: mode 1, 8-bit UART, enable rcvr                   
 TMOD  = (TMOD & 0x0f) | 0x20;                  // TMOD: timer 1, mode 2, 8-bit reload; keeps timer0 default.        
 TH1   = 256-(25000000/372/boudrate);           // TH1:  reload value for baud @ 25MHz, TH=0xF9   
 TR1   = 1;                                     // TR1:  timer 1 run                             
 //TI    = 1;                                   // TI:   set TI to send first char of UART   
}
//計時器0中斷1
void timer0_ISR(void) interrupt 1
{
 if(PWMOUT==1)
  {
   TR0=0;
   PERIOD = TIMES-PWMLOW;                    
   TH0= PERIOD/256;
   TL0= PERIOD%256;
   TR0=1;
   PWMOUT=0;
   PWMOUT1=0;
   PWMOUT2=0;
  }
  else
   {
     TR0=0;
     PERIOD = TIMES-PWMHIGH;                    
     TH0= PERIOD/256;
     TL0= PERIOD%256;
     TR0=1;
     PWMOUT=1;
     PWMOUT1=1;
     PWMOUT2=1;
    }
}

沒有留言:

張貼留言