Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp@1:581901598cda, 2019-12-02 (annotated)
- Committer:
- f3d
- Date:
- Mon Dec 02 12:13:04 2019 +0000
- Revision:
- 1:581901598cda
- Parent:
- 0:88449c646fbd
Added speed-up/slow-down code
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| f3d | 0:88449c646fbd | 1 | #include "mbed.h" |
| f3d | 0:88449c646fbd | 2 | #include <stm32l432xx.h> |
| f3d | 0:88449c646fbd | 3 | #include "sine.h" |
| f3d | 0:88449c646fbd | 4 | /* |
| f3d | 0:88449c646fbd | 5 | For register definitions see here: |
| f3d | 0:88449c646fbd | 6 | https://github.com/ARMmbed/mbed-os/blob/master/targets/TARGET_STM/TARGET_STM32L4/TARGET_STM32L432xC/device/stm32l432xx.h |
| f3d | 0:88449c646fbd | 7 | */ |
| f3d | 0:88449c646fbd | 8 | |
| f3d | 0:88449c646fbd | 9 | // Using the MBED libraries to configure the PWM outputs (its easier than |
| f3d | 0:88449c646fbd | 10 | // calculating all of the register contents by hand) |
| f3d | 0:88449c646fbd | 11 | PwmOut PhaATop(PA_8); |
| f3d | 0:88449c646fbd | 12 | PwmOut PhaBTop(PA_9); |
| f3d | 0:88449c646fbd | 13 | PwmOut PhaCTop(PA_10); |
| f3d | 0:88449c646fbd | 14 | PwmOut PhaABottom(PA_7); |
| f3d | 0:88449c646fbd | 15 | PwmOut PhaBBottom(PB_0); |
| f3d | 0:88449c646fbd | 16 | PwmOut PhaCBottom(PB_1); |
| f3d | 1:581901598cda | 17 | DigitalIn Slow(D4); |
| f3d | 1:581901598cda | 18 | DigitalIn Fast(D5); |
| f3d | 1:581901598cda | 19 | DigitalOut myled(LED1); |
| f3d | 1:581901598cda | 20 | #define MAX_DELTA 500 |
| f3d | 1:581901598cda | 21 | volatile int Delta=0; |
| f3d | 0:88449c646fbd | 22 | int AIndex=0; |
| f3d | 0:88449c646fbd | 23 | int BIndex=90; |
| f3d | 0:88449c646fbd | 24 | int CIndex=180; |
| f3d | 0:88449c646fbd | 25 | void TimerISR(void) |
| f3d | 0:88449c646fbd | 26 | { |
| f3d | 1:581901598cda | 27 | TIM1->ARR = 270+Delta; |
| f3d | 1:581901598cda | 28 | int Offset=Delta/2; |
| f3d | 1:581901598cda | 29 | TIM1->CCR1 = sine_table[AIndex]+Offset; |
| f3d | 1:581901598cda | 30 | TIM1->CCR2 = sine_table[BIndex]+Offset; |
| f3d | 1:581901598cda | 31 | TIM1->CCR3 = sine_table[CIndex]+Offset; |
| f3d | 0:88449c646fbd | 32 | AIndex++; |
| f3d | 0:88449c646fbd | 33 | if (AIndex >= PWM_STEPS) |
| f3d | 0:88449c646fbd | 34 | AIndex = 0; |
| f3d | 0:88449c646fbd | 35 | BIndex++; |
| f3d | 0:88449c646fbd | 36 | if (BIndex >= PWM_STEPS) |
| f3d | 0:88449c646fbd | 37 | BIndex = 0; |
| f3d | 0:88449c646fbd | 38 | CIndex++; |
| f3d | 0:88449c646fbd | 39 | if (CIndex >= PWM_STEPS) |
| f3d | 0:88449c646fbd | 40 | CIndex = 0; |
| f3d | 0:88449c646fbd | 41 | |
| f3d | 0:88449c646fbd | 42 | |
| f3d | 0:88449c646fbd | 43 | TIM1->SR &= ~0x3f; // ack the interrupt |
| f3d | 0:88449c646fbd | 44 | } |
| f3d | 0:88449c646fbd | 45 | void initTimer1() |
| f3d | 0:88449c646fbd | 46 | { |
| f3d | 0:88449c646fbd | 47 | |
| f3d | 0:88449c646fbd | 48 | TIM1->CR1 = 0; // make sure Counter is disabled before changing configuration |
| f3d | 0:88449c646fbd | 49 | TIM1->CR2 = 0; |
| f3d | 0:88449c646fbd | 50 | TIM1->ARR = 270; |
| f3d | 0:88449c646fbd | 51 | TIM1->PSC = 21; |
| f3d | 0:88449c646fbd | 52 | TIM1->CCR1 = 135; // 50% duty |
| f3d | 0:88449c646fbd | 53 | TIM1->CCR2 = 135; // 50% duty |
| f3d | 0:88449c646fbd | 54 | TIM1->CCR3 = 135; // 50% duty |
| f3d | 1:581901598cda | 55 | //// Enable complimentary outputs on channels 1 to 3 |
| f3d | 1:581901598cda | 56 | //TIM1->CCER = (1 << 0) + (1 << 2) + (1 << 4) + (1 << 6) + (1 << 8) + (1<<10); |
| f3d | 1:581901598cda | 57 | TIM1->CCER = (1 << 0) + (1 << 4) + (1 << 8); |
| f3d | 0:88449c646fbd | 58 | TIM1->SR = 0; // Clear flags. |
| f3d | 0:88449c646fbd | 59 | //TIM1->BDTR &= ~(0xff); |
| f3d | 0:88449c646fbd | 60 | //TIM1->BDTR |= (127); // set dead time to 127 clock cycles |
| f3d | 0:88449c646fbd | 61 | // Set up the interrupt handler |
| f3d | 0:88449c646fbd | 62 | TIM1->DIER = 1; // Want update interrupt |
| f3d | 0:88449c646fbd | 63 | NVIC_SetVector(TIM1_UP_TIM16_IRQn,(uint32_t) TimerISR); |
| f3d | 0:88449c646fbd | 64 | NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); |
| f3d | 1:581901598cda | 65 | __enable_irq(); // enable interrupts |
| f3d | 0:88449c646fbd | 66 | TIM1->CR1 |= 1; // enable counter |
| f3d | 0:88449c646fbd | 67 | |
| f3d | 0:88449c646fbd | 68 | } |
| f3d | 0:88449c646fbd | 69 | |
| f3d | 0:88449c646fbd | 70 | int main() { |
| f3d | 0:88449c646fbd | 71 | initTimer1(); |
| f3d | 0:88449c646fbd | 72 | while(1) { |
| f3d | 1:581901598cda | 73 | |
| f3d | 1:581901598cda | 74 | if ( (Slow == 0) || (Fast == 0) ) |
| f3d | 1:581901598cda | 75 | { |
| f3d | 1:581901598cda | 76 | // user pressed a button so light up an LED |
| f3d | 1:581901598cda | 77 | myled = 1; |
| f3d | 1:581901598cda | 78 | if (Slow == 0) |
| f3d | 1:581901598cda | 79 | { |
| f3d | 1:581901598cda | 80 | int NextDelta = Delta; |
| f3d | 1:581901598cda | 81 | NextDelta = NextDelta + 10; |
| f3d | 1:581901598cda | 82 | if (NextDelta > MAX_DELTA) |
| f3d | 1:581901598cda | 83 | NextDelta = MAX_DELTA; |
| f3d | 1:581901598cda | 84 | Delta = NextDelta; |
| f3d | 1:581901598cda | 85 | } |
| f3d | 1:581901598cda | 86 | if (Fast == 0) |
| f3d | 1:581901598cda | 87 | { |
| f3d | 1:581901598cda | 88 | int NextDelta = Delta; |
| f3d | 1:581901598cda | 89 | NextDelta = NextDelta - 10; |
| f3d | 1:581901598cda | 90 | if (NextDelta < 0) |
| f3d | 1:581901598cda | 91 | NextDelta = 0; |
| f3d | 1:581901598cda | 92 | Delta = NextDelta; |
| f3d | 1:581901598cda | 93 | } |
| f3d | 1:581901598cda | 94 | |
| f3d | 1:581901598cda | 95 | } |
| f3d | 1:581901598cda | 96 | else |
| f3d | 1:581901598cda | 97 | { |
| f3d | 1:581901598cda | 98 | myled = 0; |
| f3d | 1:581901598cda | 99 | } |
| f3d | 1:581901598cda | 100 | wait(0.1); // slow down loop to avoid bounces |
| f3d | 0:88449c646fbd | 101 | } |
| f3d | 0:88449c646fbd | 102 | } |