你好 我做的题目是 PWM 输出频率是 20KHZ ±50hz跟 200HZ±2HZ 但是 我用TM4配置出的pwm 频率跳动范围大于50hz 跟 2hz 怎么解决呢?
#include "includes.h"
#include "oled.h"
uint32_t ulADC0_Value[8];
double ValueAvg;
uint32_t frequency;
void System_Init();
void PWM_Init ();
void AD_Init();
int main (void)
{
System_Init();
while(1)
{
//触发采集
ADCProcessorTrigger(ADC0_BASE, 0);
//等待采集结束
while(!ADCIntStatus(ADC0_BASE, 0, false)) ;
//获取采集结果 0-4095代表0-3.3V
ADCSequenceDataGet(ADC0_BASE, 0, ulADC0_Value);
ValueAvg = (ulADC0_Value[0] + ulADC0_Value[1] + ulADC0_Value[2] + ulADC0_Value[3] +
ulADC0_Value[4]+ulADC0_Value[5]+ulADC0_Value[6]+ulADC0_Value[7]+4)/8;
//GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2, 011);
if(ValueAvg<1500)
{
frequency = 6250;
}
else if(ValueAvg>=3000)
{
frequency = 125;
}
else
{
frequency=6250 – 2.04 * ValueAvg;
}
//设置PWM发生器1的频率,时钟频率/PWM分频数/n,16M/64/5000=50HZ
OLED_ShowString(0,0,"SHOW OLED",24);
OLED_ShowNum(0,13, ValueAvg,4,12);
OLED_ShowNum(0,24, frequency,4,12);
//
// OLED_ShowNum(0,48, Fre,5,12);
OLED_Refresh_Gram(); //
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0,frequency); //初始化10KHZ
//设置PWM0/PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,frequency/2);//50%占空比
//使能PWM0和PWM1的输出
// PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);
// //使能PWM发生器
// PWMGenEnable(PWM0_BASE, PWM_GEN_0);
}
}
void System_Init()
{
FPUEnable();
FPULazyStackingEnable();
//SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN |SYSCTL_XTAL_16MHZ);//16MHZ
SysCtlClockSet(SYSCTL_SYSDIV_2_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);//80MHZ
ValueAvg=0;
PWM_Init();
AD_Init();
OLED_Init();
OLED_ShowString(0,0,"ALIENTEK",24);
//OLED_ShowString(0,12, " OLED SHOW",24);
//OLED_ShowChar(0,24,text,12,0);
}
void PWM_Init ()
{
//long int PWM_Value=12500; //PWM占空比调节
//使能FPU
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);//使能PWM0模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);//使能PWM0和PWM1输出所在GPIO
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6);
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_7);//配置PH0/PH1为PWM功能
GPIOPinConfigure(GPIO_PB6_M0PWM0); //#define GPIO_PB6_M0PWM0 0x00011804
GPIOPinConfigure(GPIO_PB7_M0PWM1); //#define GPIO_PB7_M0PWM1 0x00011C04
//驱动电流8MA,推挽输出
GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_6,GPIO_STRENGTH_8MA,GPIO_PIN_TYPE_STD);
SysCtlPWMClockSet(SYSCTL_PWMDIV_64); // PWM时钟配置:不分频
//配置PWM发生器0:加减计数,不同步
PWMGenConfigure(PWM0_BASE,PWM_GEN_0,PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0,5000); //初始化10KHZ
//设置PWM0/PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0,2500);//50%占空比
//使能PWM0和PWM1的输出
PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);
//使能PWM发生器
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
}
void AD_Init()
{
//初始化ADC0/PE3
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_3);
/*设置ADC参考电压为外部3V
ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V);*/
//ADC外部参考电压始终为3.3V
//配置ADC采集序列 ADC0_BASE/采样序列号/处理器通过ADCProcessorTrigger()函数产生的一个触发/优先级,0最高
ADCSequenceConfigure(ADC0_BASE,0,ADC_TRIGGER_PROCESSOR, 0);
//配置采样序列发生器的步进 ADC0_BASE/采样序列号/步进值 (该参数决定了触发产生时ADC捕获采样序列的次序。
//对于第一个采样序列,其值可以是0~7;对于第二和第三个采样序列,其值从0~3;对于第四个采样序列,其值只能取0。)/
//选择被采样的通道,该步进可以定义成序列的末尾(ADC_CTL_END位),同时它也可以配置成在步进完成后产生一个中断(ADC_CTL_IE位)
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 6, ADC_CTL_CH0 );
ADCSequenceStepConfigure(ADC0_BASE, 0, 7, ADC_CTL_CH0 |ADC_CTL_END | ADC_CTL_IE);
//使能ADC采集序列
ADCSequenceEnable(ADC0_BASE, 0);
ADCIntClear(ADC0_BASE, 0);
ADCIntEnable(ADC0_BASE, 0);
}
Susan Yang:
您现在是自己设计的板子?有可能是硬件设计导致的。除了PWM是否还用到了其他功能?是否有尝试用TI例程试一下?
user5316406:
回复 Susan Yang:
你好,我用pwm历程试过了,他输出的频率也是会跳的,不稳
xyz549040622:
回复 user5316406:
不应该的吧,你把其他中断都关掉,只剩PWM试试,是否是其他高优先级的中断打断了PWM的进程。
user5316406:
回复 xyz549040622:
我 只开了PWM 可是他输出的频率 跳的有点大我换了其他的TM4 也一样
Susan Yang:
回复 user5316406:
是不是您的时钟不稳定导致的?
Susan Yang:
回复 user5316406:
很高兴您能解决问题!