Freescale MC9S08单片机芯定时器5
中心对齐的脉宽调制模式波形如图8-3所示。
在这种模式下,产生的脉宽调制信号的周期和占空比都是其对应的计数器的2倍。计数器的计数将经历递减和递增两个过程:每个周期的开始,TPMxMOD计数器开始从预置值递减;当递减到输出比较计数器的预置值时,输出电平翻转,计数器继续递减;当计数器递减到0后,开始递增的过程,递增到再次与输出比较计数器预置值相同时,输出电平翻转;计数器继续递增到TPMxMOD计数器预置值后,当前周期结束。由于存在2倍的关系,在中心对齐的脉宽调制模式下,TPMxMOD和输出比较计数器的预置值应限定在0x0001~0x7FFF之间。如果设置为0或者超出0x7FFF,将导致脉宽调制为0占空比或者100%占空比。
定时器中断程序设计:
# include<hidef.h> /* for EnableInterrupts macro */
# include<MC9S08AW60.h> /* include peripheral declarations */
char value;
void main(void){
DisableInterrupts; /* disable Interrupts */
/* Initialize Keybord */
PTADD=0x0F; /* Set
PTAPE=0XF0; /* Internal Pullup Enable bits 7-4 */
PTAD=0x00;
KBI1PE=0xF0; /* Enable
KBI1SC=0x06; /* Keybord Interrupt Enable and event on falling edges*/
/* sensitive*/
/* Initialize TPM */
TPM1SC=0x4F; /* Enable TOF Interrupt and use bus clock */
TPM1MOD=0x4400;
PTBDD=0xFF; /* Set Port B as output */
PTBD=0xFF;
value=0x00;
EnableInterrupts; /* enable interrupts */
for(;;){
__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
}
interrupt Keybord(){
char result;
unsigned int i;
/* Cross scan to acquire Key in code */
result=PTAD;
KBI1PE=0x00;
PTADD=0xF0;
PTAPE=0x0f;
PTAD=0x00;
result |=PTAD;
/* Change TPM time setting judge by key in code */
switch(result){
case 0xee:
TPM1MOD=0x4000;
break;
case 0xee:
TPM1MOD=0x4000;
break;
case 0xed:
TPM1MOD=0x3C00;
break;
case 0xeb:
TPM1MOD=0x3800;
break;
case 0xe7:
TPM1MOD=0x3400;
break;
case 0xde:
TPM1MOD=0x3000;
break;
case 0xdd:
TPM1MOD=0x2C00;
break;
case 0xdb:
TPM1MOD=0x2800;
break;
case 0xd7:
TPM1MOD=0x2400;
break;
case 0xbe:
TPM1MOD=0x2000;
break;
case 0xbd:
TPM1MOD=0x1C00;
break;
case 0xbb:
TPM1MOD=0x1800;
break;
case 0xb7:
TPM1MOD=0x1400;
break;
case 0x7e:
TPM1MOD=0x1000;
break;
case 0x7d:
TPM1MOD=0x0C00;
break;
case 0x7b:
TPM1MOD=0x0800;
break;
case 0x77:
TPM1MOD=0x0400;
break;
}
for(i=0;i<=20000;i++) __RESET_WATCHDOG();
/* Resume to initial state */
KBI1PE=0xF0;
PTADD=0x0F;
PTAPE=0xF0;
PTAD=0x00;
TPM1CNTH=0x00;
value=0x00;
PTBD=0xFF;
KBI1SC_KBACK=1; /* Keybord interrupt acknowledge */
}
interrupt TPM (){
value++;
PTBD=~value;
TPM1SC_TOF=0; /* TPM interrupt acknowledge */
}
把Keybord中断和TPM中断添加到中断向量表(在prm文件中)中;
VECTOR ADDRESS 0xFFD2 Keybord
VECTOR ADDRESS 0xFFEE TPM
注:整理自网络