这个简单的例子用来演示飞思卡尔的MC9S08AC16的PWM模块的用法,用的开发板是我做的NSS08Kit-R1型开发板,由于所有S08单片机的PWM模块都是高度相似的,因此,这个例子对于其他型号的S08单片机也应该适用
/******************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<
nicrosystem@gmail.com>
@ date:2009-04-06
********************************/
/*****************************
@说明:
本程序用来测试 MC9S08AC16的 PWM功能,使用TPM2的channel0和channel1,
首先输出edge aligned pwm,输出大约10秒后再改为输出 center pwm
如此循环
****************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "delay.h"
#define BUS_CLK 4000000L
//以查询方式发送一个字节
void uart2_send_byte_poll(unsigned char c) {
/*SCI2C2_TIE =0;
SCI2C2_TCIE=0;
SCI2C2_RIE=0;
SCI2C2_ILIE=0;*/
SCI2D=c;
while(SCI2S1_TC==0);
}
//以查询方式发送一个字符串
void uart2_send_string_poll(unsigned char *str) {
while(*str!=' '){
uart2_send_byte_poll(*str++);
}
}
//设置系统时钟模式,为FEE,8MHZ的系统主频,BUS CLK为4MHZ
void init_system_clock()
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
//初始化sci2
//设置sci2为8bit,一个停止位,无校验模式
//设置sci2发送,接收使能,中断全部禁止
//设置波特率为9600
void init_sci2()
{
unsigned int BaudRate;
BaudRate="BUS"_CLK/9600/16;
SCI2BDH=(unsigned char)(BaudRate>>8);
SCI2BDL=(unsigned char)(BaudRate&0x00ff);
SCI2C1=0x04;
SCI2C2=0x0c;
SCI2C3=0x00;
}
//设置tpm2 channel0,channel1为edge-aligned pwm
//low-true pulse,1khz frequency
void init_tpm2_pwm()
{
TPM2MODH=0x0f;
TPM2MODL=0xa0;
TPM2C0SC=0x24;
TPM2C1SC=0x24;
TPM2C0VH=0x02;
TPM2C0VL=0xff;
TPM2C1VH=0x05;
TPM2C1VL=0xff;
TPM2SC=0x08;
}
void main(void) {
EnableInterrupts; /* enable interrupts */
/* include your code here */
SOPT_COPE=0;//disable cop watchdog
init_system_clock();
init_sci2();
uart2_send_string_poll((unsigned char*)("start.....
"));
uart2_send_string_poll((unsigned char*)("output edge-aligned pwm....
"));
init_tpm2_pwm();
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
delay_1s(10);
//set center-aligned pwm output,high true pulse
uart2_send_string_poll((unsigned char*)("output center-aligned pwm....
"));
TPM2SC=0x00;
delay_1s(1);
TPM2C0SC=0x08;
TPM2C1SC=0x08;
TPM2SC=0x08;
TPM2SC_CPWMS=1;
delay_1s(10);
//set edge-aligned pwm output,low-true pulse
uart2_send_string_poll((unsigned char*)("output edge-aligned pwm....
"));
TPM2SC=0x00;
delay_1s(1);
TPM2C0SC=0x24;
TPM2C1SC=0x24;
TPM2SC=0x08;
} /* loop forever */
/* please make sure that you never leave main */
}
一周热门 更多>