这是网上找到别人的一个spwm波程序,他说是三相的,但好像也不是,我烧进lm3s811了,测了下没有三相,但有一路倒是spwm波,另一路是PWM波。。。我也找到了周立功的spwm波资料,才发现这两个程序很相近,但是出来的不是spwm波形,,,哪位高人能帮我看下啊
附上源代码
#include"systemInit.h"
#include"pwm.h"
#include"timer.h"
unsigned int n,t;
const unsigned long ulTab[360] = /* 一个周期的SIN函数表 */
{ 1400, 1424, 1447, 1471, 1494, 1518, 1541, 1565, 1588, 1611, 1634, 1658, 1681, 1704, 1727,
1749, 1772, 1795, 1817, 1840, 1862, 1884, 1906, 1927, 1949, 1971, 1992, 2013, 2034, 2054,
2075, 2095, 2115, 2135, 2155, 2174, 2194, 2212, 2231, 2250, 2268, 2286, 2303, 2321, 2338,
2355, 2371, 2387, 2403, 2419, 2434, 2449, 2464, 2478, 2492, 2506, 2519, 2532, 2545, 2557,
2569, 2581, 2592, 2603, 2613, 2624, 2633, 2643, 2652, 2660, 2669, 2676, 2684, 2691, 2698,
2704, 2710, 2715, 2720, 2725, 2729, 2733, 2737, 2740, 2743, 2745, 2747, 2748, 2749, 2750,
2750, 2750, 2749, 2748, 2747, 2745, 2743, 2740, 2737, 2733, 2729, 2725, 2720, 2715, 2710,
2704, 2698, 2691, 2684, 2676, 2669, 2660, 2652, 2643, 2633, 2624, 2613, 2603, 2592, 2581,
2569, 2557, 2545, 2532, 2519, 2506, 2492, 2478, 2464, 2449, 2434, 2419, 2403, 2387, 2371,
2355, 2338, 2321, 2303, 2286, 2268, 2250, 2231, 2212, 2194, 2174, 2155, 2135, 2115, 2095,
2075, 2054, 2034, 2013, 1992, 1971, 1949, 1927, 1906, 1884, 1862, 1840, 1817, 1795, 1772,
1749, 1727, 1704, 1681, 1658, 1634, 1611, 1588, 1565, 1541, 1518, 1494, 1471, 1447, 1424,
1400, 1376, 1353, 1329, 1306, 1282, 1259, 1235, 1212, 1189, 1166, 1142, 1119, 1096, 1073,
1051, 1028, 1005, 983, 960, 938, 916, 894, 873, 851, 829, 808, 787, 766, 746, 725, 705,
685, 665, 645, 626, 606, 588, 569, 550, 532, 514, 497, 479, 462, 445, 429, 413,397,381,
366,351, 336, 322, 308, 294, 281, 268, 255, 243, 231, 219, 208, 197, 187, 176, 167, 157,
148, 140, 131, 124, 116, 109, 102, 96, 90, 85, 80, 75, 71, 67, 63, 60, 57, 55, 53, 52, 51,
50, 50, 50, 51, 52, 53, 55, 57, 60, 63, 67, 71, 75, 80, 85, 90, 96, 102, 109, 116, 124,
131, 140, 148, 157, 167, 176, 187, 197, 208, 219, 231, 243, 255, 268, 281, 294, 308, 322,
336, 351, 366, 381, 397, 413, 429, 445, 462, 479, 497, 514, 532, 550, 569, 588, 606, 626,
645, 665, 685, 705, 725, 746, 766, 787, 808, 829, 851, 873, 894, 916, 938, 960, 983, 1005,
1028, 1051, 1073, 1096, 1119, 1142, 1166, 1189, 1212, 1235, 1259, 1282, 1306, 1329, 1353, 1376
};
void PWM_Init(void)
{
SysCtlPWMClockSet(SYSCTL_PWMDIV_1); /* PWM时钟配置:不分频 */
SysCtlPeriEnable(SYSCTL_PERIPH_PWM); /* 使能PWM外设 */
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOD); /* 使能PD口外设 */
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOB); /* 使能PB口外设 */
SysCtlPeriEnable(SYSCTL_PERIPH_GPIOE); /* 使能PE口外设 */
/* 设置PD0,PD1为PWM0和PWM1*/
GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1);
/* 设置PB0,PB1为PWM2和PWM3 */
GPIOPinTypePWM(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
/* 设置PB0,PB1为PWM4和PWM5 */
GPIOPinTypePWM(GPIO_PORTE_BASE, GPIO_PIN_0 | GPIO_PIN_1);
PWMGenConfigure(PWM_BASE, PWM_GEN_0, /* 设置PWM发生器0为上下计数方式 */
PWM_GEN_MODE_UP_DOWN | /* 上下计数方式 */
PWM_GEN_MODE_SYNC | /* 两路PWM同步 */
PWM_GEN_MODE_DBG_STOP);
PWMGenConfigure(PWM_BASE, PWM_GEN_1, /* 设置PWM发生器1为上下计数方式 */
PWM_GEN_MODE_UP_DOWN | /* 上下计数方式 */
PWM_GEN_MODE_SYNC | /* 两路PWM同步 */
PWM_GEN_MODE_DBG_STOP);
PWMGenConfigure(PWM_BASE, PWM_GEN_2, /* 设置PWM发生器2为上下计数方式 */
PWM_GEN_MODE_UP_DOWN | /* 上下计数方式 */
PWM_GEN_MODE_SYNC | /* 两路PWM同步 */
PWM_GEN_MODE_DBG_STOP);
PWMGenPeriodSet(PWM_BASE, PWM_GEN_0, t); /* 设置PWM发生器0的频率17857Hz */
PWMGenPeriodSet(PWM_BASE, PWM_GEN_1, t); /* 设置PWM发生器1的频率17857Hz */
PWMGenPeriodSet(PWM_BASE, PWM_GEN_2, 6000);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, 2700);/* 设置PWM0脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, 2700);/* 设置PWM1脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, 2700);/* 设置PWM2脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, 2700);/* 设置PWM3脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_4, 2700);/* 设置PWM4脉冲宽度 */
PWMPulseWidthSet(PWM_BASE, PWM_OUT_5, 2700);/* 设置PWM5脉冲宽度 */
PWMOutputInvert(PWM_BASE, PWM_OUT_0_BIT |
PWM_OUT_1_BIT, true); /* PWM0和PWM3反相输出 */
PWMOutputInvert(PWM_BASE, PWM_OUT_2_BIT |
PWM_OUT_3_BIT, true);
PWMOutputInvert(PWM_BASE, PWM_OUT_4_BIT |
PWM_OUT_5_BIT, true);
PWMOutputState(PWM_BASE, PWM_OUT_0_BIT | PWM_OUT_1_BIT, true);
PWMOutputState(PWM_BASE, PWM_OUT_2_BIT | PWM_OUT_3_BIT, true);
PWMOutputState(PWM_BASE, PWM_OUT_4_BIT | PWM_OUT_5_BIT, true);
PWMGenEnable(PWM_BASE, PWM_GEN_0); /* 使能PWM发生器0 */
PWMGenEnable(PWM_BASE, PWM_GEN_1); /* 使能PWM发生器1 */
PWMGenEnable(PWM_BASE, PWM_GEN_2); /* 使能PWM发生器2 */
PWMSyncUpdate(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT |
PWM_GEN_2_BIT | PWM_GEN_3_BIT); /* 同步时间更新 */
PWMSyncTimeBase(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT |
PWM_GEN_2_BIT | PWM_GEN_3_BIT);
SysCtlDelay(10000000);
}
void TIMER_Init(void)
{
SysCtlPeriEnable(SYSCTL_PERIPH_TIMER0); /* 使能定时器0外设 */
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER); /* 设置定时器0为周期触发模式 */
TimerLoadSet(TIMER0_BASE, TIMER_A, t); /* 设置定时器装载值 */
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 设置定时器为溢出中断 */
TimerEnable(TIMER0_BASE, TIMER_A); /* 使能定时器0 */
}
int main(void)
{
jtagWait();
// clockInit();
SysCtlLDOSet(SYSCTL_LDO_2_75V);
SysCtlClockSet(SYSCTL_SYSDIV_4 | /* PLL4分频,系统时钟50M */
SYSCTL_USE_PLL | /* 使用PLL */
SYSCTL_OSC_MAIN |
SYSCTL_XTAL_6MHZ);
t=2800; /* 配置6MHz外部晶振作为主时钟 */
PWM_Init(); /* 初始化PWM */
TIMER_Init(); /* 初始化TIMER */
IntEnable(INT_TIMER0A); /* 使能Timer中断 */
IntMasterEnable(); /* 使能总中断 */
while(1);
}
void Timer0A_ISR (void)
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT); /* 清除定时器0中断 */
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, ulTab[n]);
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ulTab[n]);
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_2, ulTab[n]); /* 设置PWM2输出的周期 */
// PWMPulseWidthSet(PWM_BASE, PWM_OUT_3, ulTab[n]); /* 设置PWM3输出的周期 */
n++;
if(n<=180)
{
PWMOutputState(PWM_BASE, PWM_OUT_1_BIT, false);
PWMOutputState(PWM_BASE, PWM_OUT_0_BIT, true);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_0, ulTab[n]); // 设置PWM0输出的周期
}
else
{
PWMOutputState(PWM_BASE,PWM_OUT_0_BIT, false);
PWMOutputState(PWM_BASE,PWM_OUT_1_BIT, true);
PWMPulseWidthSet(PWM_BASE, PWM_OUT_1, ulTab[n]); // 设置PWM1输出的周期
}
if(n==360)
n = 0; /* 重复SPWM输出 */
PWMSyncUpdate(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT |
PWM_GEN_2_BIT | PWM_GEN_3_BIT);
/* 同步时间更新 */
PWMSyncTimeBase(PWM_BASE, PWM_GEN_0_BIT | PWM_GEN_1_BIT|
PWM_GEN_2_BIT | PWM_GEN_3_BIT);
TimerEnable(TIMER0_BASE, TIMER_A); /* 使能定时器0 */
}