Sinusoidal PWM for the STM32F302 and ihm07m1 board
Dependencies: mbed
Revision 0:31195a53c37c, committed 2020-03-07
- Comitter:
- f3d
- Date:
- Sat Mar 07 15:48:19 2020 +0000
- Commit message:
- Initial commit
Changed in this revision
diff -r 000000000000 -r 31195a53c37c main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Sat Mar 07 15:48:19 2020 +0000 @@ -0,0 +1,76 @@ +#include "mbed.h" +#include "pwm.h" +DigitalOut myled(LED1); +/* +PA8,9,10 are used to drive the PWM outputs. They are also connected back to TIM_CH1,CH2,CH3 +EN1,2,3 are pulled low and connected back to PC10,11,12 +PC13 = Blue button +PB13 = User LED (I think - if not it is PA5) +*/ + + +PwmOut PhA(PA_8); +PwmOut PhB(PA_9); +PwmOut PhC(PA_10); + + +DigitalOut En1(PC_10); +DigitalOut En2(PC_11); +DigitalOut En3(PC_12); +Serial pc(USBTX, USBRX); // tx, rx useful for debugging +volatile int CountA,CountB,CountC; + +void TimerISR(void) +{ + TIM1->CCR1 = PWM[CountA]; + TIM1->CCR2 = PWM[CountB]; + TIM1->CCR3 = PWM[CountC]; + CountA = (CountA + 1) % PWM_LENGTH; + CountB = (CountB + 1) % PWM_LENGTH; + CountC = (CountC + 1) % PWM_LENGTH; + TIM1->SR &= ~0x3f; // ack the interrupt + +} +void initTimer1() +{ + + TIM1->CR1 = 0; // make sure Counter is disabled before changing configuration + TIM1->CR2 = 0; + TIM1->PSC = 0; + TIM1->ARR = (PWM[0]*2)-1; + TIM1->CCR1 = 0; // 0% duty + TIM1->CCR2 = 0; // 0% duty + TIM1->CCR3 = 0; // 0% duty + // Enable timer outputs on channels 1,2,3 + TIM1->CCER = (1 << 0) + (1 << 4) + (1 << 8); + TIM1->SR = 0; // Clear flags. + TIM1->CR1 |= 1; // enable counter + + // Set up the interrupt handler + TIM1->DIER = 1; // Want update interrupt + NVIC_SetVector(TIM1_UP_TIM16_IRQn,(uint32_t) TimerISR); + NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); + __enable_irq(); // enable interrupts */ + +} +void start() +{ + + TIM1->CCR1 = 0; // 0% duty + TIM1->CCR2 = 0; // 0% duty + TIM1->CCR3 = 0; // 0% duty + En1 = 1; + En2 = 1; + En3 = 1; + CountA = 0; + CountB = PWM_LENGTH / 3; + CountC = (2 *PWM_LENGTH) / 3; +} +int main() { + initTimer1(); + start(); + while(1) { + wait(0.1); + myled = myled ^ 1; + } +}
diff -r 000000000000 -r 31195a53c37c mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Mar 07 15:48:19 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file
diff -r 000000000000 -r 31195a53c37c pwm.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pwm.h Sat Mar 07 15:48:19 2020 +0000 @@ -0,0 +1,6 @@ +#include <stdint.h> +uint16_t PWM[]={ +1500,1535,1571,1606,1641,1676,1711,1746,1781,1815,1849,1883,1917,1951,1984,2017,2049,2081,2113,2144,2175,2205,2235,2265,2294,2322,2350,2377,2403,2429,2455,2479,2503,2527,2549,2571,2592,2613,2632,2651,2669,2686,2703,2718,2733,2747,2760,2773,2784,2794,2804,2813,2820,2827,2833,2838,2843,2846,2848,2850,2850,2850,2848,2846,2843,2838,2833,2827,2820,2813,2804,2794,2784,2773,2760,2747,2733,2718,2703,2686,2669,2651,2632,2613,2592,2571,2549,2527,2503,2479,2455,2429,2403,2377,2350,2322,2294,2265,2235,2205,2175,2144,2113,2081,2049,2017,1984,1951,1917,1883,1849,1815,1781,1746,1711,1676,1641,1606,1571,1535,1500,1465,1429,1394,1359,1324,1289,1254,1219,1185,1151,1117,1083,1049,1016,983,951,919,887,856,825,795,765,735,706,678,650,623,597,571,545,521,497,473,451,429,408,387,368,349,331,314,297,282,267,253,240,227,216,206,196,187,180,173,167,162,157,154,152,150,150,150,152,154,157,162,167,173,180,187,196,206,216,227,240,253,267,282,297,314,331,349,368,387,408,429,451,473,497,521,545,571,597,623,650,678,706,735,765,795,825,856,887,919,951,983,1016,1049,1083,1117,1151,1185,1219,1254,1289,1324,1359,1394,1429,1465 + +}; +#define PWM_LENGTH ( sizeof(PWM) / sizeof(uint16_t) ) \ No newline at end of file