![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
avec thread
Fork of T2_STM32 by
pwm.h@4:b01a3ce6ef01, 2018-01-14 (annotated)
- Committer:
- ketingue
- Date:
- Sun Jan 14 17:39:09 2018 +0000
- Revision:
- 4:b01a3ce6ef01
- Parent:
- 2:ab0ccf9bb38c
test_pwm_01;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Tioneb | 2:ab0ccf9bb38c | 1 | #include "mbed.h" |
Tioneb | 2:ab0ccf9bb38c | 2 | |
Tioneb | 2:ab0ccf9bb38c | 3 | //SAVE_TEST 2 |
Tioneb | 2:ab0ccf9bb38c | 4 | |
Tioneb | 2:ab0ccf9bb38c | 5 | PwmOut mypwm(PWM_OUT); |
Tioneb | 2:ab0ccf9bb38c | 6 | |
Tioneb | 2:ab0ccf9bb38c | 7 | DigitalOut myled(LED1); |
Tioneb | 2:ab0ccf9bb38c | 8 | |
ketingue | 4:b01a3ce6ef01 | 9 | |
ketingue | 4:b01a3ce6ef01 | 10 | void InitializeTimer(int period = 500) |
ketingue | 4:b01a3ce6ef01 | 11 | { |
ketingue | 4:b01a3ce6ef01 | 12 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); |
ketingue | 4:b01a3ce6ef01 | 13 | |
ketingue | 4:b01a3ce6ef01 | 14 | TIM_TimeBaseInitTypeDef timerInitStructure; |
ketingue | 4:b01a3ce6ef01 | 15 | timerInitStructure.TIM_Prescaler = 40000; |
ketingue | 4:b01a3ce6ef01 | 16 | timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up; |
ketingue | 4:b01a3ce6ef01 | 17 | timerInitStructure.TIM_Period = period; |
ketingue | 4:b01a3ce6ef01 | 18 | timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1; |
ketingue | 4:b01a3ce6ef01 | 19 | timerInitStructure.TIM_RepetitionCounter = 0; |
ketingue | 4:b01a3ce6ef01 | 20 | TIM_TimeBaseInit(TIM4, &timerInitStructure); |
ketingue | 4:b01a3ce6ef01 | 21 | TIM_Cmd(TIM4, ENABLE); |
ketingue | 4:b01a3ce6ef01 | 22 | } |
ketingue | 4:b01a3ce6ef01 | 23 | |
ketingue | 4:b01a3ce6ef01 | 24 | |
ketingue | 4:b01a3ce6ef01 | 25 | void SendPWM_Carre(int temps, PinName pin) { |
Tioneb | 2:ab0ccf9bb38c | 26 | |
ketingue | 4:b01a3ce6ef01 | 27 | int middle-temps = roundUp_uint32(temps); |
ketingue | 4:b01a3ce6ef01 | 28 | PwmOut pwm(pin); |
ketingue | 4:b01a3ce6ef01 | 29 | pwm.period_ms(temps); |
ketingue | 4:b01a3ce6ef01 | 30 | pwm.pulsewidth_ms(middle-temps); |
Tioneb | 2:ab0ccf9bb38c | 31 | |
ketingue | 4:b01a3ce6ef01 | 32 | printf("pwm set to %.2f %%\n", pwm.read() * 100); |
ketingue | 4:b01a3ce6ef01 | 33 | |
ketingue | 4:b01a3ce6ef01 | 34 | while(1) { |
ketingue | 4:b01a3ce6ef01 | 35 | myled = !myled; |
ketingue | 4:b01a3ce6ef01 | 36 | wait(1); |
ketingue | 4:b01a3ce6ef01 | 37 | } |
ketingue | 4:b01a3ce6ef01 | 38 | |
ketingue | 4:b01a3ce6ef01 | 39 | void SendPWM_Nul(PinName pin) { |
ketingue | 4:b01a3ce6ef01 | 40 | |
ketingue | 4:b01a3ce6ef01 | 41 | |
ketingue | 4:b01a3ce6ef01 | 42 | PwmOut pwm(pin); |
ketingue | 4:b01a3ce6ef01 | 43 | |
ketingue | 4:b01a3ce6ef01 | 44 | pwm.period_ms(0); |
ketingue | 4:b01a3ce6ef01 | 45 | pwm.pulsewidth_ms(0); |
ketingue | 4:b01a3ce6ef01 | 46 | |
ketingue | 4:b01a3ce6ef01 | 47 | printf("pwm set to 0 \n"); |
Tioneb | 2:ab0ccf9bb38c | 48 | |
Tioneb | 2:ab0ccf9bb38c | 49 | while(1) { |
Tioneb | 2:ab0ccf9bb38c | 50 | myled = !myled; |
Tioneb | 2:ab0ccf9bb38c | 51 | wait(1); |
Tioneb | 2:ab0ccf9bb38c | 52 | } |
Tioneb | 2:ab0ccf9bb38c | 53 | } |