注册会员
- 积分
- 190
- 威望
- 137
- 贡献
- 41
- 兑换币
- 10
- 注册时间
- 2012-3-19
- 在线时间
- 6 小时
- 毕业学校
- HUST
|
参考从清华购买最小系统版和USDB调试器附件的例程3的设置GPIO和系统程序初始化的方法,以及博客http://t.cn/zOVK9cT 中提供的产生PWM的例程,本人做了一些调试和修改,得到产生一对互补的方波的主程序,其他包含头文件在清华提供的例程中可以找到,在这重复。供大家参考,欢迎大家指正,因为本人也是刚学。软件是CW10.1, 硬件是K10 (从清华购买的做智能车赛用)代码如下:
#define PE_MCUINIT
#include <stdio.h>
#include "derivative.h" /* include peripheral declarations */
#include "globalDef.h"
#include"mcg.h"
#include "mcg.h"
void MCU_init(void); /* Device initialization function declaration */
#define GPIO_PIN_MASK 0x1Fu
#define GPIO_PIN(x) (((1)<<(x & GPIO_PIN_MASK)))
#define TERM_PORT UART0_BASE_PTR
#define AUX_PORT UART2_BASE_PTR
#define TERMINAL_BAUD 9600
#define AUXILIARY_BAUD 9600
#define BCLR(bit,Register) ((Register)&=~(1<<(bit))) /*clear the bit in Register*/
#define BSET(bit,Register) ((Register)|=(1<<(bit))) /*set the bit in Register*/
int main(void)
{
unsigned long counter;
uint8 ch1 = 0;
uint8 ch2 = 0;
uint8 coreClk_mhz = 0;
uint16 coreClk_khz = 0;
uint16 periphClk_khz = 0;
char reg;
reg = 0;
counter = 0;
oreClk_mhz = pll_init();
coreClk_khz = coreClk_mhz * 1000;
periphClk_khz = coreClk_khz / (((SIM_CLKDIV1 & SIM_CLKDIV1_OUTDIV2_MASK) >> 24)+ 1);
//=========================== Code for test GPIO==================================================
/* Turn on all port clocks */
SIM_SCGC5 = SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTC_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTE_MASK;
//
/* Set pin0,pin1 of PORTD as GPIO */
PORTD_PCR0=(0|PORT_PCR_MUX(1));
PORTD_PCR1=(0|PORT_PCR_MUX(1));
PORTD_PCR4=(0|PORT_PCR_MUX(1));
PORTD_PCR5=(0|PORT_PCR_MUX(1));
/* set pin0,pin1 to be output*/
GPIOD_PDDR=GPIO_PDDR_PDD(GPIO_PIN(0)|GPIO_PIN(1)|GPIO_PIN(4)|GPIO_PIN(5));
GPIOD_PDOR &= ~GPIO_PDOR_PDO(GPIO_PIN(0)|GPIO_PIN(1)|GPIO_PIN(4)|GPIO_PIN(5));
/*---------------------------------------------------------------*/
PORTC_PCR3= PORT_PCR_MUX(4); // 设置引脚C3引脚为FTM0_CH2功能
PORTC_PCR4= PORT_PCR_MUX(4); // 设置引脚C4引脚为FTM0_CH3功能
SIM_SCGC6|=SIM_SCGC6_FTM0_MASK;//使能FTM0时钟
FTM0_MODE |= FTM_MODE_WPDIS_MASK;//写保护禁止
FTM0_SC=FTM_SC_CLKS(1)|FTM_SC_PS(4);//时钟源来自总线, 16分频;
FTM0_MODE |=FTM_MODE_FTMEN_MASK;//FTMEN=1
FTM0_QDCTRL&=~FTM_QDCTRL_QUADEN_MASK;//禁止正交解码模式
FTM0_COMBINE|=FTM_COMBINE_COMBINE1_MASK;//FTM0通道2,3联合使用 FTM0_COMBINE|=FTM_COMBINE_COMP1_MASK;//FTM0通道2,3互补输出
FTM0_COMBINE|=FTM_COMBINE_SYNCEN1_MASK;//使能FTM0通道2,3 PWM同步
FTM0_COMBINE|=FTM_COMBINE_DTEN1_MASK;//死区插 FTM0_DEADTIME=FTM_DEADTIME_DTPS(3)|FTM_DEADTIME_DTVAL(63);
FTM0_SYNCONF|=FTM_SYNCONF_SWWRBUF_MASK;//使能MOD,CNTIN,CV寄存器的软件触发同步
FTM0_SYNC=FTM_SYNC_CNTMIN_MASK|FTM_SYNC_CNTMAX_MASK;//设置load point
FTM0_C2SC |= FTM_CnSC_ELSB_MASK;//ELSB=1,ELSA=0,左对齐,先高后低
FTM0_C2SC &= ~FTM_CnSC_ELSA_MASK;
FTM0_C3SC |= FTM_CnSC_ELSB_MASK;//ELSB=1,ELSA=0,左对齐,先高后低
FTM0_C3SC &= ~FTM_CnSC_ELSA_MASK;
FTM0_OUTMASK=0XF3;//通道2,3输出,屏蔽其它通道
FTM0_CNTIN=0;//FTM0计数器初始值为0
FTM0_MOD=37499;//结束值,周期为(MOD-CNTIN+1)*时钟周期=10ms
FTM0_C2V=5625;//高电平1.5ms
FTM0_C3V=20000;
FTM0_CNT=0;
/*set pin0,pin1,pin4 and pin5 as 1*/
GPIOD_PSOR=GPIO_PSOR_PTSO(GPIO_PIN(0)|GPIO_PIN(1)|GPIO_PIN(4)|GPIO_PIN(5));
/*clear pin0 and pin1*/
GPIOD_PCOR=GPIO_PCOR_PTCO(GPIO_PIN(0)|GPIO_PIN(1));
/*set pin0,pin1,pin4 and pin5 as 1*/
GPIOD_PSOR=GPIO_PSOR_PTSO(GPIO_PIN(0)|GPIO_PIN(1)|GPIO_PIN(4)|GPIO_PIN(5));
/*clear pin0 and pin1*/
GPIOD_PCOR=GPIO_PCOR_PTCO(GPIO_PIN(0)|GPIO_PIN(1));
for(;;) {
counter++;
FTM0_SYNC|=FTM_SYNC_SWSYNC_MASK;//使能软件触发
}
return 0;
}
|
|