//参数3: sclb
// Clock SB = Clock B / (2 * PWMSCLB) //参数4: ctl /*
control[CON67,CON45,CON23,CON01,PSWAI,PFRZ] PWM级联控制寄存器 CON67,CON45,CON23,CON01 0 单独一个通道
1 两个通道级联
PSWAI 0 等待模式禁止时钟输入 1 等待模式允许时钟输入 PFRZ 0 冻结模式允许PWM时钟输入 1 冻结模式禁止PWM时钟输入
//返回值:无 */
//************************************************************** void PWMGeneralInitial(byte prclk,byte scla,byte sclb,byte ctl) {
//禁止所有的PWM通道 PWME=0x00; //设置预分频参数 PWMPRCLK=prclk; //设置A分频参数 PWMSCLA=scla; //设置B分频参数 PWMSCLB=sclb; //级联配置 PWMCTL=ctl;
}
//*********************************************************** //函数名称:PWMConcatenateSetting //函数功能:PWM级联输出配置
//函数参数:1个byte类型,2个word类型
//参数1: channel代表了当前配置的PWM通道 //参数2: period 周期配置参数
/*
Left aligned output (CAEx = 0) PWMx Period = Channel Clock Period * PWMPERx
Center Aligned Output (CAEx = 1) PWMx Period = Channel Clock Period * (2 * PWMPERx)
*/
//参数3: duty 占空比配置参数 /*
Polarity = 0 [(PWMPERx-PWMDTYx)/PWMPERx] * 100%
(PPOL
x
=0)
Duty
Cycle
=
Polarity = 1 (PPOLx = 1) Duty Cycle = [PWMDTYx / PWMPERx] * 100%
*/ //返回值:无
//************************************************************** void PWMConcatenateSetting(byte channel,word period,word duty) {
if(channel>7) channel=7; switch(channel) { case 0:
case 1:PWMDisable(0); //禁止通道0 PWMDisable(1); //禁止通道1
PWMPER01=period; //设置周期寄存器 PWMDTY01=duty; //设置占空比寄存器 PWMEnable(0); //使能通道0; PWMEnable(1); //使能通道1; break; case 2:
case 3:PWMDisable(2); //禁止通道2 PWMDisable(3); //禁止通道3
PWMPER23=period; //设置周期寄存器 PWMDTY23=duty; //设置占空比寄存器 PWMEnable(2); //使能通道2; PWMEnable(3); //使能通道3; break; case 4:
case 5:PWMDisable(4); //禁止通道4 PWMDisable(5); //禁止通道5
PWMPER45=period; //设置周期寄存器 PWMDTY45=duty; //设置占空比寄存器 PWMEnable(4); //使能通道4; PWMEnable(5); //使能通道5; break; case 6:
case 7:PWMDisable(6); //禁止通道6 PWMDisable(7); //禁止通道7
PWMPER67=period; //设置周期寄存器 PWMDTY67=duty; //设置占空比寄存器 PWMEnable(6); //使能通道6; PWMEnable(7); //使能通道7; break;
default:break; } }
定时器应用程序
#include
// 函数声明
void OutputCompare_Init(void);; void Service_WD(void); void SetBusClock_24MHz(void);
// 全局变量
uint Timer7_Cnt=0;
void main(void) {
/* put your own code here */
SetBusClock_24MHz(); OutputCompare_Init();
EnableInterrupts;
for(;;) {
_FEED_COP(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */ }
void OutputCompare_Init(void) {
TSCR1_TEN = 0; /* Disable Timer module before adjusting registers. */ TIOS_IOS7 = 1; /* Set Channel 0 as output compare. */ TCTL1_OM7 = 0; /* Set channel 0 to toggle when a Timer match occurs. */ TCTL1_OL7 = 1; /* Set channel 0 to toggle when a Timer match occurs. */ TC7 = 0x4926; /* Set a value for channel 0 timer compare. */ TIE_C7I = 1; /* Enable channel 0 interrupt, handled by function TIM0ISR. */ TSCR1_TSWAI = 1; /* Disables the timer module while in wait mode. */ TSCR1_TSFRZ = 1; /* Disables the timer counter while in freeze mode. */ TSCR2_PR = 0x7; /* Set prescaler to divide by 128 TSCR2_TCRE = 1;
TSCR1_TEN = 1; /* Timer Enable.
//中断周期:0x4926*128/24MHz = 100ms }
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt VectorNumber_Vtimch7 TIM7_ISR(void) {
Timer7_Cnt++;
TFLG1 = TFLG1_C7F_MASK; /* Clear channel 0 flag. */
}
#pragma CODE_SEG DEFAULT
// 看门狗
void Service_WD(void) {
CPMUARMCOP = 0x55; CPMUARMCOP = 0xAA; }
void SetBusClock_24MHz(void) {
CPMUOSC_OSCE = 1; /* enable ext osc */
/*
Initialise the system clock from a 16 MHz Crystal, 24 MHz Bus CLK (48 MHz VCO, 48 MHz PLL)
*/ */ */
CPMUSYNR = 0x00 | 0x05; /* VCOFRQ[7:6], SYNDIV[5:0] */
CPMUREFDIV = 0x20 | 0x03; /* REFFRQ[7:6], REFDIV[3:0] */
CPMUPOSTDIV = 0x00; /* POSTDIV = 0 FPLL = FVCO */
while(!CPMUFLG_LOCK); /* wait for VCO to stabilize*/
Service_WD();
CPMUCLKS_PLLSEL = 1; /* Switch clk to use PLL */ }
SCI应用程序
#include
// 函数声明
void SCI0_Init(void);
void SCI0_BR(unsigned long br); void SCI0_SendByte(char ch); void Service_WD(void);
void SetBusClock_24MHz(void);
// 全局变量 char SCI_Flag = 0; char SCI_Rev = 0;
void main(void) {
/* put your own code here */
SetBusClock_24MHz();
SCI0_BR(38400); SCI0_Init(); EnableInterrupts;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库freescale 单片机MC9S12G128应用程序(PWM,Timer,ADC……)(2)在线全文阅读。
相关推荐: