close

Naruwan板 用PWM控制LED 這次和上次比較 少了閃爍 因為PWM的頻率變快了


程式如下 此程式由笙泉的範例程式修改而來 如有侵權 請告知會立即刪除


///////////////////////////////////////////////////
//
//  FILE:       PWM_Demo.c
//  AUTHOR:     Neo (Hsin-Chih Lin)
//  COPYRIGHT:  (c) Megawin Technology Co., Ltd.
//
///////////////////////////////////////////////////



#include "REG_MPC82G516.H"


void Init_PWM();
void Set_Frequency(char mod_freq);
void Set_Duty(unsigned char module,unsigned int mod_timer);


///////////////////////////////////////////////////
//
//  Function:Main()
//  Purpose :PWM demo software
//           (Pulse Width Modulator)
//
///////////////////////////////////////////////////
void delay5ms()
{
    unsigned int j;
    for(j=0;j<4978;j++) ;
}


void Main()
{
    unsigned int i;
    unsigned char iro_cnt=0x01;


    Init_PWM();
    Set_Frequency(0xf0);


   CCON |= 0x40;    //Start PCA Counter
   TR0  = 1;      //Start Timer_0 Counter



  while(1)
 {
 while (iro_cnt==0x01)
    {


        for(i=0;i<0xff;i++)
        {
            Set_Duty(0,i);
            Set_Duty(1,0xff-i);
           
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


        for(i=0xff;i>0;i--)
        {
            Set_Duty(0,i);
            Set_Duty(1,0xff-i);
 
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


         iro_cnt=0x02;


    }


 while (iro_cnt==0x02)
    {


        for(i=0;i<0xff;i++)
        {


            Set_Duty(1,0xff-i);
            Set_Duty(2,i);            
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


        for(i=0xff;i>0;i--)
        {


            Set_Duty(1,0xff-i);
            Set_Duty(2,i);  
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


         iro_cnt=0x03;


    }
 while (iro_cnt==0x03)
    {


        for(i=0;i<0xff;i++)
        {
            Set_Duty(0,i);


            Set_Duty(2,0xff-i);            
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


        for(i=0xff;i>0;i--)
        {
            Set_Duty(0,i);


            Set_Duty(2,0xff-i);  
            delay5ms();
            delay5ms();
            delay5ms();
            delay5ms();
        }


         iro_cnt=0x01;


    }
 }


}
///////////////////////////////////////////////////
//
// Function:Init_PWM()
//  Purpose :Initial PWM parameter
//
///////////////////////////////////////////////////
void Init_PWM()
{
//  CMOD = 0x00;    //Set PCA Mode (PCA clock source as OSC/12)
//  CMOD = 0x02;    //Set PCA Mode (PCA clock source as OSC/2)
    CMOD = 0x04;    //Set PCA Mode (PCA clock source as Timer 0-Overflow)
//  CMOD = 0x06;    //Set PCA Mode (PCA clock source as ECI(P3.4))
    CCAPM0 = 0x42;    //Configure PCA Module0 to PWM
    CCAPM1 = 0x42;    //Configure PCA Module1 to PWM
    CCAPM2 = 0x42;    //Configure PCA Module2 to PWM
// CCAPM3 = 0x42;    //Configure PCA Module3 to PWM
// CCAPM4 = 0x42;    //Configure PCA Module4 to PWM(Only for MPC82G516)
// CCAPM5 = 0x42;    //Configure PCA Module5 to PWM(Only for MPC82G516)
 TMOD = 0x02;    //Set Timer 0 Mode to Mode2(Auto load TH to TL when overflow)


 AUXR2 = 0x80;    //T0x12 Frequency * 12


}
///////////////////////////////////////////////////
//
// Function:Set_Frequency(char mod_freq)
// Params.:
//    mod_freq:
//  You can setup frequency from
//  30Hz(mod_freq = 0) to
//  93.7KHz(mod_freq = 0xff, AUXR(2) = 0x80)
//
///////////////////////////////////////////////////
void Set_Frequency(char mod_freq)
{
 TH0  = mod_freq;   //Set Timer 0 Clock Start
}
///////////////////////////////////////////////////
//
// Function:Set_Duty(char module,char mod_timer)
//  Parames:
//   module:
//  Select which module do you want set.(0-5)
//
//   mod_timer:
//  Set duty length.(0-0x100)
//      0x0000 is all output high,
//      0x0100 is all output low
//
///////////////////////////////////////////////////
void Set_Duty(unsigned char module,unsigned int mod_timer)
{
 switch(module)
 {
  case 0:
            PCAPWM0 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP0H = mod_timer;
            break;
  case 1:
            PCAPWM1 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP1H = mod_timer;
            break;
  case 2:
            PCAPWM2 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP2H = mod_timer;
            break;
  case 3:
            PCAPWM3 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP3H = mod_timer;
            break;


  case 4:
            PCAPWM4 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP4H = mod_timer;
            break;
  case 5:
            PCAPWM5 =   (unsigned char) (mod_timer&0x0100) << 1;
            CCAP5H = mod_timer;
            break;


 }
}


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 tooru178 的頭像
    tooru178

    電子制作 DIY

    tooru178 發表在 痞客邦 留言(0) 人氣()