📄 pwm.c
字号:
#include <hidef.h> /* common defines and macros */
#include "all_head.h"
#include <mc9s12dg128.h> /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
/***************************************************************/
/*init_pwm(): initializes s12 pulses width modulation system */
/*choose the periods of PWMx are 2s */
// PORTA_BIT0 ---> PORTA_BIT2用于通道选择
/***************************************************************/
void PWM_Ini(void)
{
PWME = 0x00; //pwm unenable
PWMPRCLK = 0x77; //select clock prescale for pwm A = Bus/128 B = Bus/128
PWMSCLA = 250; //select scale for pwm SA= A/(2*250)
PWMSCLB = 250; //select scale for pwm SB= B/(2*250)
PWMCLK_PCLK0 = 1; //8bit PWM0 channal choose the clock SA
PWMCLK_PCLK1 = 1; //8bit PWM1 channal choose the clock SA
PWMCLK_PCLK2 = 1; //8bit PWM2 channal choose the clock SB
PWMCLK_PCLK3 = 1; //8bit PWM3 channal choose the clock SB
PWMCLK_PCLK4 = 1; //8bit PWM4 channal choose the clock SA
PWMCLK_PCLK5 = 1; //8bit PWM5 channal choose the clock SA
PWMCLK_PCLK6 = 1; //8bit PWM6 channal choose the clock SB
PWMCLK_PCLK7 = 1; //8bit PWM7 channal choose the clock SB
PWMPOL = 0xff; //select polarity first high
PWMCAE = 0x00; //select center or left aligned mode___left mode
PWMPER0 = 250; //select the period of pwm0
PWMPER1 = 250; //select the period of pwm1
PWMPER2 = 250; //select the period of pwm2
PWMPER3 = 250; //select the period of pwm3
PWMPER4 = 250; //select the period of pwm4
PWMPER5 = 250; //select the period of pwm5
PWMPER6 = 250; //select the period of pwm6
PWMPER7 = 250; //select the period of pwm7
PWMDTY0 = 100; //initial the duty of pwm0
PWMDTY1 = 100; //initial the duty of pwm1
PWMDTY2 = 100; //initial the duty of pwm2
PWMDTY3 = 100; //initial the duty of pwm3
PWMDTY4 = 100; //initial the duty of pwm4
PWMDTY5 = 100; //initial the duty of pwm5
PWMDTY6 = 100; //initial the duty of pwm6
PWMDTY7 = 100; //initial the duty of pwm7
PWME = 0xff; //enable the all of pwm channal
}
void PWM_Generate(char list,uchar duty)
{
switch(list)
{
case 0:
PWMDTY0 = duty;
break;
case 1:
PWMDTY1 = duty;
break;
case 2:
PWMDTY2 = duty;
break;
case 3:
PWMDTY3 = duty;
break;
case 4:
PWMDTY4 = duty;
break;
case 5:
PWMDTY5 = duty;
break;
case 6:
PWMDTY6 = duty;
break;
case 7:
PWMDTY7 = duty;
break;
default:
PWMDTY0 = 100;
break;
}
}
int Frequence_sample(void)
{
int temp;
temp = PACN10; //采用16位的脉冲累加器B进行计数
PACN10 = 0; //计数器清零
return temp;
}
int Feedback(void){
int temp;
temp = PACN10;
PACN10 = 0;
return temp;
}
void ChannelChoose(char num) //maybe need change because of the function of tlp521
{
switch(num)
{
case 0:
PORTA_BIT0 = 0;
PORTA_BIT1 = 0;
PORTA_BIT2 = 0;
break;
case 1:
PORTA_BIT0 = 1;
PORTA_BIT1 = 0;
PORTA_BIT2 = 0;
break;
case 2:
PORTA_BIT0 = 0;
PORTA_BIT1 = 1;
PORTA_BIT2 = 0;
break;
case 3:
PORTA_BIT0 = 1;
PORTA_BIT1 = 1;
PORTA_BIT2 = 0;
break;
case 4:
PORTA_BIT0 = 0;
PORTA_BIT1 = 0;
PORTA_BIT2 = 1;
break;
case 5:
PORTA_BIT0 = 1;
PORTA_BIT1 = 0;
PORTA_BIT2 = 1;
break;
case 6:
PORTA_BIT0 = 0;
PORTA_BIT1 = 1;
PORTA_BIT2 = 1;
break;
case 7:
PORTA_BIT0 = 1;
PORTA_BIT1 = 1;
PORTA_BIT2 = 1;
break;
default:
PORTA_BIT0 = 0;
PORTA_BIT1 = 0;
PORTA_BIT2 = 0;
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -