#include <stdint.h>
#include <stdbool.h>
#include "inc/tm4c123gh6pm.h" //Register Definitions
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/sysctl.h"
#include "driverlib/interrupt.h"
#include "driverlib/gpio.h"
#include "driverlib/uart.h"
#include "driverlib/systick.h"
#include "driverlib/pin_map.h"
#include "driverlib/pwm.h"
#include "driverlib/timer.h"
#define uchar unsigned char
#define uint unsigned int
#define delay_ms(n); SysCtlDelay(n*(SysCtlClockGet()/3000));
void Timer0_Init(void);
float pwm[301]={0.5,0.489529,
0.479062,0.468605,0.458161,0.447736,0.437333,0.426958,0.416616,0.406309,0.396044,0.385825,0.375655,0.36554,0.355484,0.345492,0.335567,0.325714,0.315938,0.306242,0.296632,0.28711,0.277682,0.268352,0.259123,0.25,0.240986,0.232087,0.223304,0.214643,0.206107,0.1977,0.189426,0.181288,0.17329,0.165435,0.157726,0.150168,0.142764,0.135516,0.128428,0.121502,0.114743,0.108153,0.101735,0.095492,0.089425,0.083539,0.077836,0.072318,0.066987,0.061847,0.056898,0.052144,0.047586,0.043227,0.039068,0.035112,0.031359,0.027812,0.024472,0.02134,0.018419,0.015708,0.013211,0.010926,0.008856,0.007002,0.005364,0.003943,0.002739,0.001754,0.000987,0.000439,0.00011,0,0.00011,0.000439,0.000987,0.001754,0.002739,0.003943,0.005364,0.007002,0.008856,0.010926,0.013211,0.015708,0.018419,0.02134,0.024472,0.02134,0.031359,0.035112,0.039068,0.043227,0.047586,0.052144,0.056898,0.061847,0.066987,0.072318,0.077836,0.083539,0.089425,0.095492,0.101735,0.108153,0.114743,0.121502,0.128428,0.135516,0.142764,0.150168,0.157726,0.165435,0.17329,0.181288,0.189426,0.1977,0.206107,0.214643,0.223304,0.232087,0.240986,0.25,0.259123,0.268352,0.277682,0.28711,0.296632,0.306242,0.315938,0.325714,0.335567,0.345492,0.355484,0.36554,0.375655,0.385825,0.396044,0.406309,0.416616,0.426958,0.437333,0.447736,0.458161,0.468605,0.479062,0.489529,0.5,0.510471,0.520938,0.531395,0.541839,0.552264,0.562667,0.573042,0.583384,0.593691,0.603956,0.614175,0.624345,0.63446,
0.644516,0.654508,0.664433,0.674286,0.684062,0.693758,0.703368,0.71289,0.722318,0.731648,0.740877,0.75,0.759014,0.767913,0.776696,0.785357,0.793893,0.8023,0.810574,0.818712,0.82671,0.834565,0.842274,0.849832,0.857236,0.864484,0.871572,0.878498,0.885257,0.891847,0.898265,0.904508,0.910575,0.916461,0.922164,0.927682,0.933013,0.938153,0.943102,0.947856,0.952414,0.956773,0.960932,0.964888,0.968641,0.972188,0.975528,0.97866,0.981581,0.984292,0.986789,0.989074,0.991144,0.992998,0.994636,0.996057,0.997261,0.998246,0.999013,0.999561,0.99989,1,0.99989,0.999561,0.999013,0.998246,0.997261,0.996057,0.994636,0.992998,
0.991144,0.989074,0.986789,0.984292,0.981581,0.97866,0.975528,0.972188,0.968641,0.964888,0.960932,0.956773,0.952414,0.947856,0.943102,0.938153,0.933013,0.927682,0.922164,0.916461,0.910575,0.904508,0.898265,0.891847,0.885257,0.878498,0.871572,0.864484,0.857236,0.849832,0.842274,0.834565,0.82671,0.818712,0.810574,0.8023,0.793893,0.785357,0.776696,0.767913,0.759014,0.75,0.740877,0.731648,0.722318,0.71289,0.703368,0.693758,0.684062,0.674286,0.664433,0.654508,0.644516,0.63446,0.624345,0.614175,0.603956,0.593691,0.583384,0.573042,0.562667,0.552264,0.541839,0.531395,0.520938,0.510471,0.5};
uint index_1=0; //A相位查表数值
uint index_2=100; //B相位查表数值
uint index_3=200; //C相位查表数值
float duty1=0.5,duty2=0.5,duty3=0.5;
/******************************************************************************************************************
*函数名: PWMInit()
*描 述:PWM初始化函数
*输 入:无
*线 路:PF2<->M1PWM6
PF4<->M1PWM7
PWM_GEN_代表PWM波生成器
PWM1_BASE
******************************************************************************************************************/
void PWMInit(void)
{
//配置PWM时钟(设置USEPWMDIV分频器)
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); //PWM时钟 16M
//使能时钟
SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1); //使能PWM模块1时钟
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF); //使能GPIOF时钟
//使能引脚复用PWM功能
GPIOPinTypePWM(GPIO_PORTF_BASE,GPIO_PIN_1); //定义PF1输出引脚
GPIOPinTypePWM(GPIO_PORTF_BASE,GPIO_PIN_2);
GPIOPinTypePWM(GPIO_PORTF_BASE,GPIO_PIN_3);
//PWM信号分配
GPIOPinConfigure(GPIO_PF1_M1PWM5); //PF1->PWM模块1信号5
GPIOPinConfigure(GPIO_PF2_M1PWM6); //PF2->PWM模块1信号6
GPIOPinConfigure(GPIO_PF3_M1PWM7); //PF3->PWM模块1信号7
//配置PWM发生器
//模块1->发生器3->上下计数,不同步
PWMGenConfigure(PWM1_BASE,PWM_GEN_3,PWM_GEN_MODE_UP_DOWN|PWM_GEN_MODE_NO_SYNC);
PWMGenConfigure(PWM1_BASE,PWM_GEN_2,PWM_GEN_MODE_UP_DOWN|PWM_GEN_MODE_NO_SYNC);
//配置PWM周期
PWMGenPeriodSet(PWM1_BASE,PWM_GEN_3,421); //64*10^3/16/10^6=4ms
PWMGenPeriodSet(PWM1_BASE,PWM_GEN_2,421);
//使能PWM模块1输出
PWMOutputState(PWM1_BASE,PWM_OUT_5_BIT,true);
PWMOutputState(PWM1_BASE,PWM_OUT_6_BIT,true);
PWMOutputState(PWM1_BASE,PWM_OUT_7_BIT,true);
//使能PWM发生器
PWMGenEnable(PWM1_BASE,PWM_GEN_3);
PWMGenEnable(PWM1_BASE,PWM_GEN_2);
}
/******************************************************************************************************************
*函数名: SetDuty(uint32_t ui32Base,uint32_t ui32PWMOut,float duty)
*描 述:PWM初始化函数
*输 入:PWM模块编号、信号编号、占空比
******************************************************************************************************************/
void SetDuty(uint32_t ui32Base,uint32_t ui32PWMOut,float duty)
{
uint32_t ui32Gen;
uint32_t ui32OutBits;
switch(ui32PWMOut)
{
case PWM_OUT_0: ui32Gen=PWM_GEN_0,ui32OutBits=PWM_OUT_0_BIT; break;
case PWM_OUT_1: ui32Gen=PWM_GEN_0,ui32OutBits=PWM_OUT_1_BIT; break;
case PWM_OUT_2: ui32Gen=PWM_GEN_1,ui32OutBits=PWM_OUT_2_BIT; break;
case PWM_OUT_3: ui32Gen=PWM_GEN_1,ui32OutBits=PWM_OUT_3_BIT; break;
case PWM_OUT_4: ui32Gen=PWM_GEN_2,ui32OutBits=PWM_OUT_4_BIT; break;
case PWM_OUT_5: ui32Gen=PWM_GEN_2,ui32OutBits=PWM_OUT_5_BIT; break;
case PWM_OUT_6: ui32Gen=PWM_GEN_3,ui32OutBits=PWM_OUT_6_BIT; break;
case PWM_OUT_7: ui32Gen=PWM_GEN_3,ui32OutBits=PWM_OUT_7_BIT; break;
}
//配置占空比
PWMPulseWidthSet(ui32Base, ui32PWMOut, PWMGenPeriodGet(ui32Base, ui32Gen)*duty);
PWMOutputState(ui32Base, ui32OutBits, true);
//使能发生器模块
PWMGenEnable(ui32Base, ui32Gen);
}
int main()
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN); //系统时钟16M
PWMInit();
Timer0_Init();
while(1)
{
SetDuty(PWM1_BASE,PWM_OUT_5,duty1);
SetDuty(PWM1_BASE,PWM_OUT_6,duty1);
SetDuty(PWM1_BASE,PWM_OUT_7,duty3);
}
}
void TIMER0A_Handler(void) //中断服务函数
{
TimerIntClear(TIMER0_BASE,TIMER_TIMA_TIMEOUT); //将A通道中断标志位清除
index_1++; //A相位查表数值
index_2++; //B相位查表数值
index_3++; //C相位查表数值
duty1=pwm[index_1];
duty2=pwm[index_2];
duty3=pwm[index_3];
if(index_1>=300) {index_1=0;}
if(index_2>=300) {index_2=0;}
if(index_3>=300) {index_3=0;}
}
void Timer0_Init()//中断初始化
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);//定时器0使能
TimerConfigure(TIMER0_BASE,TIMER_CFG_PERIODIC);//32位周期定时器
TimerLoadSet(TIMER0_BASE,TIMER_A,SysCtlClockGet()/15001-1);//设定装载值
IntEnable(INT_TIMER0A);//总中断使能
TimerIntEnable(TIMER0_BASE,TIMER_TIMA_TIMEOUT); //中断输出设置模式,设置模式;
IntMasterEnable();
TimerEnable(TIMER0_BASE,TIMER_A); //定时器使能开始计数
}