Forum: Mikrocontroller und Digitale Elektronik Timer 1 PWM Frequenz berechnen


von Ma B. (drumstick)


Lesenswert?

Guten Tag

Weis jemand eine Formel, wo mit dem Autoreloadwert, Systemfrequenz, und 
dem Prescaler die PWM - Frequenz ausrechnen kann??

Danke und Gruss!

M.B.

von spess53 (Gast)


Lesenswert?

Hi

Ja. Steht im Datenblatt.

MfG Spess

von Ma B. (drumstick)


Lesenswert?

hmm.. etwas stimmt aber nicht!?

OSC = 8000000 Hz
Prescale = 3
ARR = 100

ergibt 26,66 kHz !??

TM1_OSC = (1/(Osc/Prsc))*ARR

von Martin (Gast)


Lesenswert?

M. B. schrieb:
> hmm.. etwas stimmt aber nicht!?

Was stimmt nicht?
Je nach Timer Mode muß man gucken, von wo nach wo der Timer dabei zählt.

von Ma B. (drumstick)


Lesenswert?

1
/*            Bit 0,      CEN:            Counter enable:                 0 = Counter disabled / 1 = Counter enabled                                                                                                                                                                */
2
/*            Bit 1,      UDIS:           Update disable:                 The Update (UEV) event enabled                                                                                                                                                                            */
3
/*            Bit 2,      URS:            Update request source:          These events can be: Counter overflow/underflow, Setting the UG bit or Update generation through the slave mode controller                                                                                */
4
/*            Bit 3,      OPM:            One pulse mode:                 Counter is not stopped at update event                                                                                                                                                                    */
5
/*            Bit 4,      DIR:            Direction:                      -                                                                                                                                                                                                         */
6
/*            Bits 6:5,   CMS[1:0]:       Center-aligned mode selection:  Center-aligned mode 3. The counter counts up and down alternatively                                                                                                                                       */
7
/*            Bit 7,      ARPE:           Auto-reload preload enable:     TIMx_ARR register is not buffered                                                                                                                                                                         */
8
/*            Bits 9:8,   CKD[1:0]:       Clock division:                 tDTS=tCK_INT                                                                                                                                                                                              */
9
/*            Bits 15:10, Reserved:                                       always read as 0
10
11
12
13
/*    -> CCMR1:   COMPAREMODREG1 = 0x00000060;                                                                                                                                                                                                                                      */
14
/*                                                                                                                                                                                                                                                                                  */
15
/*            Bits 1:0,   CC1S:           Capture/Compare 1 selection:    CC1 channel is configured as output                                                                                                                                                                       */
16
/*            Bit 2,      OC1FE:          Output Compare 1 fast enable:   CC1 behaves normally depending on counter and CCR1 values even when the trigger is ON. The minimum delay to activate CC1 output when an edge occurs on the trigger input is 5 clock cycles                */
17
/*            Bit 3,      OC1PE:          Output Compare 1 preload enable:Preload register on TIMx_CCR1 disabled. TIMx_CCR1 can be written at anytime, the new value is taken in account immediately                                                                                */
18
/*            Bits 6:4,   OC1M:           Output Compare 1 mode:          PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1 else inactive. In downcounting, channel 1 is inactive (OC1REF=‘0’) as long as TIMx_CNT>TIMx_CCR1 else active (OC1REF=’1’)   */
19
/*            Bit 7,      OC1CE:          Output Compare 1 clear enable:  OC1Ref is not affected by the ETRF Input



Mit dem KO messe ich 37kHz!

von (unknown) (Gast)


Lesenswert?

Prescaler 3 (DREI) ist ein ziemlich exotischer Wert. Welcher µC hat den?

von Ma B. (drumstick)


Lesenswert?

Ich verwende den STM32F103VBT6 und habe einen L6235PD Motortreiber 
angeschlossen.

Wenn ich den Wert im CCR1 ändere, ändert ja die Frequemz nicht. Also 
benötige ich diesen Wert nicht für die Berechnung?

Danke und Gruss!

M.B.

von Ma B. (drumstick)


Lesenswert?

Ein Problem könnte sein, die hardware ist für den stm32f103vct6 
ausgelegt und einen externen Clock von 25MHz angelegt. Der uP ist aber 
ein stm32f103vbt6 und auf den internen 8MHz Clock eingestellt, jetzt 
muss ich den Prototypen trotzdem in Betrieb nehmen! :-(

von Ma B. (drumstick)


Lesenswert?

1
#define     PRESCALE      0x00000003 
2
#define     AUTORELOAD    0x00000064

von friedrich (Gast)


Lesenswert?

Hallo Drumstick!

a) Stimmt die Taktfrequenz am Timer? Sind die Vorteileer AHB1/2 richtig 
eingestellt?
b) Beachte: für einen Vorteiler von 3 muss (wenn ich das richtig im Kopf 
habe) der Wert "2" ins Register, als Vorteiler-1. Im übrigen ist dies 
auch beim eigentlichen Timerwert so!

Grüße

von friedrich (Gast)


Lesenswert?

...

c) Und im Übrigen ist die Taktferquenz auch relativ einfach von intern 
auf Extern einzustellen.

Grüße

von Ma B. (drumstick)


Lesenswert?

So viel ich weiss, muss ich für einen Vorteiler von 3 (Drei) auch eine 3 
schreiben. Bei den Timer 2, 3 und vier die ich nutze stimmt die Frequenz 
bzw. die Formel. Aber beim erzeugen des PWMs stimmt sie nicht. Ich weiss 
da wir einen falschen uP einsetzen (stm32f103vbt6 anstelle eines 
stm32f103vct6) machen wir uns selber Probleme. Aber der einzige 
Unterschied, der ich sehe liegt ja daran, dass die B Variante keinen DAC 
hat.

Gruss

Mario

von Ma B. (drumstick)


Lesenswert?

2. Beispiel:

1
#define     CLOCKCONFIG             0x00000000 //001D8402                                                                                           /**/
2
#define     CLOCKCONTROLL           0x01000081 //01000081                                                                                           /**/
3
#define     CLOCKENABLES            0x00000A75 
4
5
#define     CONRTOLLREG1A           0x00000060                                                                                                                                                                                                                                    /**/
6
#define     CONTROLLREG1B           0x00000061                                                                                                                                                                                                                                    /**/
7
#define     CONTROLLREG2            0x00000000                                                                                                                                                                                                                                    /**/
8
#define     PRESCALE                5                                                                                                                                                                                                                                    /**/
9
#define     AUTORELOAD              67//258                                                                                                                                                                                                                                    /**/
10
#define     COMPAREMODREG1          0x00000060                                                                                                                                                                                                                                    /**/
11
#define     COMPAREMODREG2          0x00000000                                                                                                                                                                                                                                    /**/
12
#define     COMPAREENABLEREG        0x00000011                                                                                                                                                                                                                                    /**/
13
#define     SLAVEMODECONTROLLREG    0x00000000                                                                                                                                                                                                                                    /**/
14
#define     COMPAREREG1             0x00000020                                                                                                                                                                                                                                    /**/
15
#define     COMPAREREG2             0x00000000                                                                                                                                                                                                                                    /**/
16
#define     STATUSREG               0x00000000                                                                                                                                                                                                                                    /**/
17
#define     INTERRUPTENABLEREG      0x00000002                                                                                                                                                                                                                                    /**/
18
#define     EVENTGENERATREG         0x00000003                                                                                                                                                                                                                                    /**/
19
#define     BRAKDEATTIMEREG         0x0000CCFF
20
21
#define     CONFIGREGLOWA           0x88888888                                                                                            /**/
22
#define     CONFIGREGHIGHA          0x888888BB                                                                                            /**/
23
24
25
26
27
void main(void)                                            //
28
{
29
30
    // Clocks
31
    RCC     ->  APB2ENR     =   CLOCKENABLES;               // Enable GPIOA, GPIOC, GPIOD, GPIOE, ADC1, Alternate function and Timer 1 clock
32
    RCC     ->  CFGR        =   CLOCKCONFIG;                // Clockconfigurations
33
    RCC     ->  CR          =   CLOCKCONTROLL;              // 8MHz, PLL enable
34
35
//    while(RCC -> CR   == (1<<1))                            // waiting for Clock is stabile
36
//    {                                                       //
37
//        __nop();                                            //
38
//    }                                                       //
39
40
    // GPIOs
41
   
42
    GPIOA   ->  CRH         =   CONFIGREGHIGHA;             // Ports configuriere as Input/Output
43
44
    // Timer
45
    // Timer1
46
    TIM1  ->  CR1     =   CONRTOLLREG1A;                    // Auto-Reload Register are puffered; Center Aligned Mode 3, up- and down. An Event is activate by under- and overflow ausgelöst.
47
    TIM1  ->  CR2     =   CONTROLLREG2;                     // Reset Controllregister 2
48
    TIM1  ->  PSC     =   PRESCALE;                         // Prescaler
49
    TIM1  ->  ARR     =   AUTORELOAD;                       // Auto-Reload Register
50
    TIM1  ->  CCMR1   =   COMPAREMODREG1;                   // Compare Mode Register 1  Outputcompare Mode
51
    TIM1  ->  CCMR2   =   COMPAREMODREG2;                   // Compare Mode Register 2  Output Compare 1 Mode; Output Compare 2 Mode 
52
    TIM1  ->  CCER    =   COMPAREENABLEREG;                 // Compare Enable Register; Compare 1 Output Enable; Compare 2 Output Enable
53
    TIM1  ->  SMCR    =   SLAVEMODECONTROLLREG;             // Slave Mode Controll Register
54
    TIM1  ->  CCR1    =   COMPAREREG1;                      // Compare Register 1
55
    TIM1  ->  CCR2    =   COMPAREREG2;                      // Compare Register 2
56
    TIM1  ->  SR      =   STATUSREG;                        // Status Register: Interrupt Flagbits
57
    TIM1  ->  DIER    =   INTERRUPTENABLEREG;               // Interrupt Enable Register: Interrupt Compare 1 und Compare 2 enable
58
    TIM1  ->  EGR     =   EVENTGENERATREG;                  // Event Generator Register: Update generation; Compare 1 Generation / Compare 2 Generation 
59
    TIM1  ->  BDTR    =   BRAKDEATTIMEREG;                  // Lock off, break inputs disable
60
    TIM1  ->  CR1     =   CONTROLLREG1B;                    // Timer 1 starten

Mit dem KO messe ich genau 10kHz

Aber die Berrechnung ergibt 23,88kHz

Hat sonst noch Jemand eine Idee??

Vielen Dank und Gruss!

M.B.

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.