- UID
- 343930
- 性别
- 男
|
昨天写了个有中断服务的PWM,电平测试程序,但是编译时有个错误,希望版主和大家帮我改改!
///////////////////////////////////////
//本程序是为了测试PWM功能,并且对电机和舵机进行了参数测试
//PP0,PP1用于测试舵机P2,PP3,PP6,PP7用于测试电机
//外接晶振16M,Bus Clock=24M;
#include /* common defines and macros */
#include /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
#pragma interrupt_handler RTI_isr
void Delay(void);
void initialize_RTI(void);
void RTI_isr(void) ;
void main(void)
{
initialize_RTI();
EnableInterrupts;
unsigned char INMASK = 0x00;
unsigned char OUTMASK = 0xff;
//unsigned char PORT_Value = 0x00;
DDRA = INMASK;
DDRB = OUTMASK;
//外接16M晶振,Bus Clock=16M*(SYNR+1)/(REFDV+1)
CLKSEL_PLLSEL=1;
SYNR=2;
REFDV=1;
DDRB=0xff;
PORTB = 0xFF;
//通道0、1和2、3、6、7分别用于舵机、电机的PWM控制
PWME_PWME0 = 0x00;
PWMPRCLK = 0x04+0x40; //通道A口16分频,通道B口16分频,
PWMCLK=0x03+0xDC;//通道0、1、4用SA时钟源,2、3、6、7用SB时钟源
PWMSCLA=75; //SA口提供10kHz的频率 24MHZ/16/(2*75)=10K -0.1ms
PWMSCLB=3; //SB口提供250kHz的频率 24MHZ/16/(2*3)=250K -4us
PWMPOL=0x03+0xDC; //0、1、4、2、3、6、7通道先高电平
PWMCAE=0X00;//对齐方式默认 左对齐
PWMPER0 = 200; //舵机的频率是50Hz(20ms),20ms/0.1ms=200
PWMDTY0 = 9; //占空比9/200=4.5%,舵机左传
PWMPER1 = 200; //舵机的频率是50Hz(20ms),20ms/0.1ms=200
PWMDTY1 = 15; //占空比15/200=7.5%,舵机静止
PWMPER4 = 200; //舵机的频率是50Hz(20ms),20ms/0.1ms=200
PWMDTY4 = 20; //占空比20/200=10%,舵机右转
PWMPER2 = 250; //电机的频率为1000Hz
PWMDTY2 = 250; //占空比100%
PWMPER3 = 250; //电机的频率为976Hz
PWMDTY3 = 167; //占空比66.8%
PWMPER6 = 250; //电机的频率为976Hz
PWMDTY6 = 125; //占空比50%
PWMPER7 = 250; //电机的频率为976Hz
PWMDTY7 = 63; //占空比25%
DDRP = 0x03+0xDC; //控制输出
PWMCNT0 = 0WMCNT1 = 0;
PWMCNT2 = 0WMCNT3 = 0;
PWMCNT4 = 0WMCNT6 = 0;
PWMCNT7 = 0;
PWME=0x03+0xDC;//PWM通道0、1、4、2、3、6、7输出
for(;;) {
PORTB_BIT4 = ~PORTB_BIT4;
PORTB_BIT5 = ~PORTB_BIT5;
PORTB_BIT6 = ~PORTB_BIT6;
PORTB_BIT7 = ~PORTB_BIT7;
Delay();
}
/* please make sure that you never leave this function */
} //end main
void initialize_RTI(void)
{
/*-------------------定时中断初始化----*/
RTICTL=0xff; //产生定时中断
CRGINT_RTIE=1; //允许RTI中断
asm CLI;
}
void Delay(void)
{
unsigned int i,j;
for(i=0;i<1000;i++)
for(j=0;j<1000;j++)
{ }
}
//#pragma CODE_SEG NON_BANKED
void RTI_isr(void)
{ unsigned char PORT_Value = 0x00;
PORT_Value = PORTA;
PORTB_BIT0 = PORT_Value&0x01;
PORTB_BIT1 = PORT_Value&0x02;
PORTB_BIT2 = PORT_Value&0x04;
} |
|