Forum: Mikrocontroller und Digitale Elektronik Benötige Hilfe bei STM8S PWM Konfiguration


von Max M. (maxmicr)


Lesenswert?

Guten Abend,

ich versuche gerade einen PWM Channel vom STM8S so zu konfigurieren, 
dass eine mit dem Pin PD3 (Timer2 Channel 2) verbundene LED aufleuchtet. 
Das hier ist meine aktueller Stand:
1
#include "iostm8s003f3.h"
2
#include "stdint.h"
3
4
#define PIN 3
5
6
void initGPIO(){
7
  PD_DDR |= (1<<PIN);     //Set Pin as Output
8
  PD_CR1 |= (1<<PIN);     //Set Pin as PushPull
9
  PD_ODR |= (1<PIN);     //Set to High
10
}
11
12
void initTimer2(){
13
  TIM2_PSCR_PSC = 0x01;         //Set Prescaler
14
  
15
  TIM2_ARRL = 0x87;             //Set Auto reload to 4999 [125KHz / (4999+1) = 25Hz]
16
  TIM2_ARRH = 0x13;
17
  
18
  TIM2_CCMR2_OC2M = 0x07;       //PWM Mode 2
19
  TIM2_CCMR2_CC2S = 0x00;       //CC2 channel is configured as output        
20
  TIM2_CCER1_CC2P = 1;          //OC2 active low
21
  
22
  TIM2_CCR1L = 0xC4;            //Set Compare Register to 2500 [Duty Cycle = 50%]
23
  TIM2_CCR1H = 0x09;            
24
  
25
  TIM2_CR1_CEN = 1;             //Enable Timer
26
}
27
28
void main()
29
{
30
  initGPIO();
31
  initTimer2();
32
  CLK_CKDIVR = 0x07;            //Prescaler 128 (=125KHz)
33
  while(1){
34
  }
35
}

Ich hab versucht, mich an dieses Dokument zu halten:
http://www.st.com/content/ccc/resource/technical/document/application_note/db/ec/7e/c8/57/b2/44/ee/CD00296680.pdf/files/CD00296680.pdf/jcr:content/translations/en.CD00296680.pdf

Allerdings ist da kein Beispielcode gegeben.
Die LED leuchtet nicht, was hab ich übersehen?

von google (Gast)


Lesenswert?

Zu dieser App-Note gibt es auch Software bei ST.

von google (Gast)


Lesenswert?

Hier gibt es auch Software dazu:
Beitrag "RGB Moodlight mit STM8"

von Max M. (maxmicr)


Lesenswert?

google schrieb:
> Hier gibt es auch Software dazu:

Danke, der Hinweis war sehr nützlich. Ich habs tatsächlich zum laufen 
bekommen, hier ein kleines Beispiel das eine LED an Pin D3 auf- und 
wieder ableuchten lässt:
1
#include "iostm8s003f3.h"
2
#include "stdint.h"
3
#include "stdbool.h"
4
5
#define PIN 3
6
volatile int16_t pwmval = 0;
7
volatile bool increment = true;
8
9
#pragma vector = TIM2_OVR_UIF_vector
10
__interrupt void TIM2_OVR_IRQHandler(void){
11
  if(pwmval == 0){
12
    increment = true;
13
  } else if(pwmval == 32700){
14
    increment = false;
15
  }
16
  if(increment)
17
    pwmval+=100;
18
  else
19
    pwmval-=100;
20
  TIM2_CCR2H = (pwmval >> 8);
21
  TIM2_CCR2L = (pwmval&0xFF);  
22
23
  TIM2_SR1 &=~(1<<0);
24
}
25
26
void initGPIO(){
27
  PD_DDR |= (1<<PIN);     //Set Pin as Output
28
  PD_CR1 |= (1<<PIN);     //Set Pin as PushPull
29
  PD_ODR |= (1<PIN);     //Set to High
30
}
31
32
void initTimer2(){
33
  CLK_PCKENR1 = 0xFF;           //Enable Clock for Peripherals
34
  TIM2_PSCR_PSC = 0x00;         //Set Prescaler
35
36
  TIM2_CCMR2_OC2M = 0x07;       //PWM Mode 2
37
  TIM2_CCMR2_CC2S = 0x00;       //CC2 channel is configured as output
38
  TIM2_CCER1_CC2P = 1;          //OC2 active low
39
  TIM2_CCER1_CC2E = 1;          //Timer Output to Pin PD3       
40
  
41
  TIM2_IER_UIE = 1;             //Enable Update Interrupt
42
  TIM2_CR1_CEN = 1;             //Enable Timer
43
  asm("rim");                   //Enable Interrupts    
44
}
45
46
void main()
47
{
48
  initGPIO();
49
  initTimer2();
50
  CLK_CKDIVR = 0x00;
51
  while(1){ 
52
  }
53
}

: Bearbeitet durch User
Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.