Simple PWM using the STM32F302 board
Dependencies: mbed
Revision 0:d5be69c77549, committed 2020-03-07
- Comitter:
- f3d
- Date:
- Sat Mar 07 15:47:32 2020 +0000
- Commit message:
- Changed I/O to leave DIAG pins as inputs
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Mar 07 15:47:32 2020 +0000
@@ -0,0 +1,73 @@
+#include "mbed.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)
+*/
+
+#define PWM_PERIOD_COUNT 999
+
+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 = CountA;
+ TIM1->CCR2 = CountB;
+ TIM1->CCR3 = CountC;
+ CountA = (CountA + 1) % PWM_PERIOD_COUNT;
+ CountB = (CountB + 1) % PWM_PERIOD_COUNT;
+ CountC = (CountC + 1) % PWM_PERIOD_COUNT;
+ TIM1->SR &= ~0x3f; // ack the interrupt
+
+}
+void initTimer1()
+{
+
+ TIM1->CR1 = 0; // make sure Counter is disabled before changing configuration
+ TIM1->CR2 = 0;
+ TIM1->PSC = 1;
+ TIM1->ARR = PWM_PERIOD_COUNT;
+ 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;
+
+}
+int main() {
+ initTimer1();
+ start();
+ while(1) {
+ wait(0.1);
+ myled = myled ^ 1;
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Sat Mar 07 15:47:32 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/mbed_official/code/mbed/builds/65be27845400 \ No newline at end of file