//模数转换器实验程序解析
//头文件
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_ints.h"
#include "inc/hw_gpio.h"
#include "driverlib/fpu.h"
#include "driverlib/sysctl.h"
#include "driverlib/rom.h"
#include "driverlib/pin_map.h"
#include "grlib/grlib.h"
#include "driverlib/gpio.h"
#include "driverlib/adc.h"
#include "driverlib/pwm.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "driverlib/interrupt.h"
uint32_t S0=0,S1=0,S2=0,S3=0,S4=0,S5=0;
uint32_t ADC0_Value[6],Data[6]={0},goal[3];
uint32_t count;
void PWM_INIT(void)
{
//使能M0PWM模块
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM0);
//使能M0PWM0和M0PWM1输出所在GPIOB和GPIOG
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
//配置相应GPIO为PWM功能
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_4|GPIO_PIN_5);
GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_5|GPIO_PIN_4);//不同:PG4、PG5
// PWM时钟配置:不分频
SysCtlPWMClockSet(SYSCTL_PWMDIV_1);
//配置M0PWM发生器:加减计数
PWMGenConfigure(PWM0_BASE,PWM_GEN_0,PWM_GEN_MODE_UP_DOWN| PWM_GEN_MODE_NO_SYNC);
//设置M0PWM发生器的周期,时钟频率/PWM分频数/n,周期=80M/1/6000=
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_0, 6000);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_1, 6000);
PWMGenPeriodSet(PWM0_BASE, PWM_GEN_2, 6000);
//设置M0PWM0/M0PWM1输出的脉冲宽度
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_0, S0);//此处S0+S1<=6000
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_1, S1);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_2, S2);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_3, S3);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_5, S4);
PWMPulseWidthSet(PWM0_BASE, PWM_OUT_4, S5);
//使能M0PWM0和M0PWM1的输出
//PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);
//PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT |PWM_OUT_3_BIT), true);
//PWMOutputState(PWM0_BASE, (PWM_OUT_4_BIT |PWM_OUT_5_BIT), true);
//使能M0PWM发生器生成模块0
PWMGenEnable(PWM0_BASE, PWM_GEN_0);
PWMGenEnable(PWM0_BASE, PWM_GEN_1);
PWMGenEnable(PWM0_BASE, PWM_GEN_2);
}
void PWM_CTRL(uint32_t count,uint8_t a)//控制三相电机的启动、停止、正反转
{
if(a==0)
{
if(count>=128)
{S0=4800;S1=0;PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);}//正转
else if(0<count<128)
{PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), false);}//不转
else
{S0=0;S1=4800;PWMOutputState(PWM0_BASE, (PWM_OUT_0_BIT |PWM_OUT_1_BIT), true);}//反转
}
if(a==1)
{
if(count>=128)
{S2=4800;S3=0;PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT |PWM_OUT_3_BIT), true);}//正转
else if(0<count<128)
{PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT |PWM_OUT_3_BIT), false);}//不转
else
{S2=0;S3=4800;PWMOutputState(PWM0_BASE, (PWM_OUT_2_BIT |PWM_OUT_3_BIT), true);}//反转
}
if(a==2)
{
if(count>=128)
{S4=4800;S5=0;PWMOutputState(PWM0_BASE, (PWM_OUT_4_BIT |PWM_OUT_5_BIT), true);}//正转
else if(0<count<128)
{PWMOutputState(PWM0_BASE, (PWM_OUT_4_BIT |PWM_OUT_5_BIT), false);}//不转
else
{S4=0;S5=4800;PWMOutputState(PWM0_BASE, (PWM_OUT_4_BIT |PWM_OUT_5_BIT), true);}//反转
}
}
void ADC_INIT(void)
{
//初始化ADC0/PE3
//SysCtlADCSpeedSet(SYSCTL_ADCSPEED_500KSPS);//设置采样速度,默认1MHZ
SysCtlPeripheralEnable(SYSCTL_PERIPH_ADC0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeADC(GPIO_PORTD_BASE, GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
GPIOPinTypeADC(GPIO_PORTE_BASE, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3);
//设置ADC参考电压为外部3V,TM4C123GH6PM只能使用内部参考电压,3.3V
//ADCReferenceSet(ADC0_BASE, ADC_REF_EXT_3V);//若ADC_REF_INT,则指使用内部3V参考电压
//配置ADC采集序列
ADCSequenceConfigure(ADC0_BASE, 0,ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC0_BASE, 0, 0, ADC_CTL_CH0 );//PE3
ADCSequenceStepConfigure(ADC0_BASE, 0, 1, ADC_CTL_CH1 );//PE2
ADCSequenceStepConfigure(ADC0_BASE, 0, 2, ADC_CTL_CH2 );//PE1
ADCSequenceStepConfigure(ADC0_BASE, 0, 3, ADC_CTL_CH3 );//PE0
ADCSequenceStepConfigure(ADC0_BASE, 0, 4, ADC_CTL_CH4 );//PD3
ADCSequenceStepConfigure(ADC0_BASE, 0, 5, ADC_CTL_CH5 );//PD2
ADCSequenceStepConfigure(ADC0_BASE, 0, 6, ADC_CTL_CH6 | ADC_CTL_IE |ADC_CTL_END);//PD1
//ADCSequenceStepConfigure(ADC0_BASE, 0, 7, ADC_CTL_CH7 | ADC_CTL_IE |ADC_CTL_END);//PD0未使用
//使能ADC采集序列
ADCSequenceEnable(ADC0_BASE, 0);
//IntEnable(INT_ADC0SS0);
IntMasterEnable();
ADCIntEnable(ADC0_BASE, 0);
//ADCIntEnableEx(ADC0_BASE,ADC_INT_DMA_SS0);//中断触发设置
ADCIntClear(ADC0_BASE, 0);
}
int main(void)
{
uint8_t a=0,b=0;
//使能FPU
FPUEnable();
FPULazyStackingEnable();
//设置系统时钟为50MHz (400/2/4=50)
SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL |SYSCTL_XTAL_16MHZ | SYSCTL_OSC_MAIN);
UATR_INIT();
ADC_INIT();
PWM_INIT();
while(1)
{
//触发采集
ADCProcessorTrigger(ADC0_BASE, 0);
//等待采集结束
while(!ADCIntStatus(ADC0_BASE, 0, false));
//获取采集结果
ADCSequenceDataGet(ADC0_BASE, 0, ADC0_Value);
//延时
//SysCtlDelay(SysCtlClockGet() / 12);
PWM_CTRL(count,a);
}
} |