Skip to content

Commit 9060a52

Browse files
author
mars
committed
double PWM frequency, exponential pwm table, reverse LED in triangle mode
1 parent 3981a74 commit 9060a52

3 files changed

Lines changed: 20 additions & 24 deletions

File tree

firmware/Source/TapTempo.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,10 @@ class TapTempo {
162162
setHigh();
163163
else if(counter >= goLow && isHigh)
164164
setLow();
165-
else
165+
else if(isSineMode())
166166
setLed(LED_FULL*(goHigh-counter)/goHigh);
167+
else
168+
setLed(LED_FULL*counter/goHigh);
167169
}
168170
}
169171
void setLow(){

firmware/Source/periph.c

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
#include "device.h"
55
#include "gpio.h"
66

7+
const uint16_t pwmvalues[LED_FULL+1] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 9, 9, 9, 10, 10, 11, 11, 12, 13, 13, 14, 15, 15, 16, 17, 18, 18, 19, 20, 21, 22, 23, 24, 26, 27, 28, 29, 31, 32, 34, 35, 37, 39, 41, 43, 45, 47, 49, 51, 54, 56, 59, 62, 65, 68, 71, 74, 78, 82, 86, 90, 94, 100 };
78

89
void initializeTimer(){
9-
uint16_t prescaler = (int16_t)(SystemCoreClock / 1000000) - 1; // 1Mhz clock
10-
uint16_t period = 1000000 / 20000; // 20 KHz for 1MHz prescaled
10+
uint16_t prescaler = (int16_t)(SystemCoreClock / 2000000) - 1; // 2Mhz clock
11+
uint16_t period = 1000000 / 20000; // 40 KHz for 2MHz prescaled
1112
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
1213
TIM_TimeBaseInitTypeDef init = {
1314
.TIM_Prescaler = prescaler,
@@ -18,17 +19,18 @@ void initializeTimer(){
1819
};
1920
TIM_TimeBaseInit(TIM3, &init);
2021
TIM_Cmd(TIM3, ENABLE);
22+
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
2123
}
2224
// TIM3_CH1 PA6 no remap
2325

2426
void setPWM(uint16_t pulse){
25-
TIM_OCInitTypeDef outputChannelInit = {0,};
26-
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
27-
outputChannelInit.TIM_Pulse = pulse;
28-
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
29-
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
30-
TIM_OC1Init(TIM3, &outputChannelInit);
31-
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
27+
static TIM_OCInitTypeDef init = {
28+
.TIM_OCMode = TIM_OCMode_PWM1,
29+
.TIM_OutputState = TIM_OutputState_Enable,
30+
.TIM_OCPolarity = TIM_OCPolarity_High
31+
};
32+
init.TIM_Pulse = pulse;
33+
TIM_OC1Init(TIM3, &init);
3234
}
3335

3436
void initializeLED(){
@@ -42,25 +44,17 @@ void initializeLED(){
4244
}
4345

4446
void setLed(uint16_t brightness){
45-
//void setLed(LedPin led){
46-
setPWM(brightness);
47-
/* clearPin(LED_PORT, led ^ (LED_RED|LED_GREEN)); */
48-
/* setPin(LED_PORT, led); */
47+
static uint16_t value = 0;
48+
if(brightness != value){
49+
setPWM(pwmvalues[brightness]);
50+
value = brightness;
51+
}
4952
}
5053

51-
/* void toggleLed(){ */
52-
/* togglePin(LED_PORT, LED_RED|LED_GREEN); */
53-
/* } */
54-
5554
char* getFirmwareVersion(){
5655
return HARDWARE_VERSION "-" FIRMWARE_VERSION ;
5756
}
5857

59-
bool isClockExternal(){
60-
/* return RCC_HSE_ON */
61-
return RCC_WaitForHSEStartUp() == SUCCESS;
62-
}
63-
6458
/* Unique device ID register (96 bits: 12 bytes) */
6559
uint8_t* getDeviceId(){
6660
const uint32_t* addr = (uint32_t*)0x1fff7a10;

firmware/Source/periph.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/* RED = LED_RED */
1818
/* } LedPin; */
1919

20-
#define LED_FULL 50
20+
#define LED_FULL 100
2121
/* LedPin getLed(); */
2222
void setLed(uint16_t brightness);
2323
/* void setLed(LedPin led); */

0 commit comments

Comments
 (0)