Forum: Mikrocontroller und Digitale Elektronik PWM Frequenz um 1/4 kleiner als berechnet


von Jochen D. (gaiusbonus)


Lesenswert?

Moin,

bisher bin ich hier nur lesend aktiv gewesen, aber jetzt komm ich 
endlich dazu aktiv zu programmieren und damit die Fragen und Probleme...

Hardware:
Pololu P-Star 25K50 Micro
Microchip PIC18F25K50
Internally clocked at 48 MHz, resulting in execution speeds up to 12 
million instructions per second (MIPS)
Precision 16 MHz crystal

Software:
MPLAB X IDE v4.01
XC8 v4.44

Ich habe das Problem, dass meine PWM Frequenz um 1/4 kleiner ist als 
berechnet.

Der Code:
1
void initPwm()
2
{
3
    /*
4
     * PWM resolution is 10 bits
5
     * don't use last 2 less significant bits CCPxCON,
6
     * so only CCPRxL have to be touched to change duty cycle
7
     */
8
    
9
    //Turn off CCP1 pin by setting TRISC bit HIGH
10
    TRISC2 = 1;
11
                
12
    // Postscaler = 0, Timer2 off, Prescaler = 4
13
    //T2CON = 0b00000000; // prescaler 1
14
    T2CON = 0b00000001; // prescaler 4
15
    //T2CON = 0b00000010; // prescaler 16
16
        
17
    //Clear Timer2
18
    TMR2 = 0x00;
19
        
20
    //Set up Period and Duty Cycle
21
    //PR2 = 0b01111100; //Load a Period Value 0b01111100 = 8kHz (124)
22
    PR2 = 0b01111100;
23
    
24
    //todo (set 0 here)
25
    //todo 50%
26
    CCPR1L = 0b00111110; //Load a Duty Cycle Value (0 = off)
27
        
28
    //Configure CCP module for PWM
29
    CCP1CON = 0b00001100;
30
        
31
    //Turn CCP1 pin back on (make it an output)
32
    TRISC2 = 0;
33
        
34
    //Start the PWM by turning on Timer2
35
    TMR2ON = 1;
36
}

Berechnet habe ich ein 8kHz Signal, messen tu ich an meinem analogen 
Oszi knapp 6kHz.
Und wo ich so schreibe, kann ich genau das nicht reproduzieren...
Jetzt messe ich bei gleichem Code 25kHz.

Ich vermute, dass das irgendwie mit der Quarzfrequenz zu tun hat.
8kHz wäre bei 16MHz (der ist verbaut)
6kHz bei 12MHz ([...]resulting in execution speeds up to 12 million 
instructions per second (MIPS)
25kHz bei 48MHz (Internally clocked at 48 MHz)

Eventuell habe ich gestern, als es spät war, irgendwelche Einstellungen 
probiert, die die Quelle für die Berechnung verändern?

Wo und was ich da überhaupt kann, hab ich noch keine Ahnung.

Irgendwer ne Idee?

Gruß vom Deister

von Miss Ratgeber (Gast)


Lesenswert?

Wenn du die Mechanik mit du da hantierst nicht ueberblicken
kannst: Fang halt kleiner an.

Z.B. in Assembler eine Warteschleife die ein Pin toggelt.
Das ganze vorher mit dem Simulator testen.

Und: Schaff dir vertrauenswuerdige Messtechnk an.

von Jochen D. (gaiusbonus)


Lesenswert?

Miss Ratgeber schrieb:
> Und: Schaff dir vertrauenswuerdige Messtechnk an.

Was ist vertrauenswürdiger als ein simples Oszi??

Ich hab mich noch nie mit den Konfigurations Bits beschäftigt.
Ich arbeite mit einem Bootloader um direkt per USB programmieren zu 
können.
Habe jetzt folgendes gefunden:
1
The PIC18F25K50 has several configuration bits in flash memory. For the P-Star, the values of those configuration
2
bits are shown in the table below. These configuration bits cannot be changed by the bootloader or an application
3
loaded by the bootloader; you will need to use an external programmer and erase the bootloader if you want to change
4
any of them.

Also hab ich mal die Configuration Bits im Code so eingestellt, wie sie 
angeblich fix vorgegeben sind.
Werde da noch nicht ganz schlau draus.
Vielleicht sieht da jemand mit Erfahrung was, bevor ich mich durch die 
Register geackert habe...

Wenn ich mit PWM_Period = (PRx+1)*4*(1/Fosc)*TMRx_Prescale rechne,
stimmen die Messungen, wenn ich als Fosc 48MHz annehme.
Ist die Aussage diverser Quellen falsch, das der Quarztakt für die 
Rechnung zu nutzen ist?
1
// PIC18F25K50 Configuration Bit Settings
2
3
// 'C' source line config statements
4
5
// CONFIG1L
6
#pragma config PLLSEL = PLL3X   // PLL Selection (3x clock multiplier)
7
#pragma config CFGPLLEN = ON    // PLL Enable Configuration bit (PLL Enabled)
8
#pragma config CPUDIV = NOCLKDIV// CPU System Clock Postscaler (CPU uses system clock (no divide))
9
#pragma config LS48MHZ = SYS48X8// Low Speed USB mode with 48 MHz system clock (System clock at 48 MHz, USB clock divider is set to 8)
10
11
// CONFIG1H
12
#pragma config FOSC = HSH       // Oscillator Selection (HS oscillator, high power 16MHz to 25MHz)
13
#pragma config PCLKEN = OFF     // Primary Oscillator Shutdown (Primary oscillator shutdown firmware controlled)
14
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Fail-Safe Clock Monitor disabled)
15
#pragma config IESO = OFF       // Internal/External Oscillator Switchover (Oscillator Switchover mode disabled)
16
17
// CONFIG2L
18
#pragma config nPWRTEN = ON     // Power-up Timer Enable (Power up timer enabled)
19
#pragma config BOREN = ON       // Brown-out Reset Enable (BOR controlled by firmware (SBOREN is enabled))
20
#pragma config BORV = 285       // Brown-out Reset Voltage (BOR set to 2.85V nominal)
21
#pragma config nLPBOR = ON      // Low-Power Brown-out Reset (Low-Power Brown-out Reset enabled)
22
23
// CONFIG2H
24
#pragma config WDTEN = SWON     // Watchdog Timer Enable bits (WDT controlled by firmware (SWDTEN enabled))
25
#pragma config WDTPS = 256      // Watchdog Timer Postscaler (1:256)
26
27
// CONFIG3H
28
#pragma config CCP2MX = RC1     // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
29
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
30
#pragma config T3CMX = RC0      // Timer3 Clock Input MUX bit (T3CKI function is on RC0)
31
#pragma config SDOMX = RB3      // SDO Output MUX bit (SDO function is on RB3)
32
#pragma config MCLRE = ON       // Master Clear Reset Pin Enable (MCLR pin enabled; RE3 input disabled)
33
34
// CONFIG4L
35
#pragma config STVREN = ON      // Stack Full/Underflow Reset (Stack full/underflow will cause Reset)
36
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
37
#pragma config ICPRT = OFF      // Dedicated In-Circuit Debug/Programming Port Enable (ICPORT disabled)
38
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)
39
40
// CONFIG5L
41
#pragma config CP0 = ON         // Block 0 Code Protect (Block 0 is code-protected)
42
#pragma config CP1 = OFF        // Block 1 Code Protect (Block 1 is not code-protected)
43
#pragma config CP2 = OFF        // Block 2 Code Protect (Block 2 is not code-protected)
44
#pragma config CP3 = OFF        // Block 3 Code Protect (Block 3 is not code-protected)
45
46
// CONFIG5H
47
#pragma config CPB = OFF        // Boot Block Code Protect (Boot block is not code-protected)
48
#pragma config CPD = OFF        // Data EEPROM Code Protect (Data EEPROM is not code-protected)
49
50
// CONFIG6L
51
#pragma config WRT0 = ON        // Block 0 Write Protect (Block 0 (0800-1FFFh) is write-protected)
52
#pragma config WRT1 = OFF       // Block 1 Write Protect (Block 1 (2000-3FFFh) is not write-protected)
53
#pragma config WRT2 = OFF       // Block 2 Write Protect (Block 2 (04000-5FFFh) is not write-protected)
54
#pragma config WRT3 = OFF       // Block 3 Write Protect (Block 3 (06000-7FFFh) is not write-protected)
55
56
// CONFIG6H
57
#pragma config WRTC = ON        // Configuration Registers Write Protect (Configuration registers (300000-3000FFh) are write-protected)
58
#pragma config WRTB = ON        // Boot Block Write Protect (Boot block (0000-7FFh) is write-protected)
59
#pragma config WRTD = OFF       // Data EEPROM Write Protect (Data EEPROM is not write-protected)
60
61
// CONFIG7L
62
#pragma config EBTR0 = ON       // Block 0 Table Read Protect (Block 0 is protected from table reads executed in other blocks)
63
#pragma config EBTR1 = OFF      // Block 1 Table Read Protect (Block 1 is not protected from table reads executed in other blocks)
64
#pragma config EBTR2 = OFF      // Block 2 Table Read Protect (Block 2 is not protected from table reads executed in other blocks)
65
#pragma config EBTR3 = OFF      // Block 3 Table Read Protect (Block 3 is not protected from table reads executed in other blocks)
66
67
// CONFIG7H
68
#pragma config EBTRB = OFF      // Boot Block Table Read Protect (Boot block is not protected from table reads executed in other blocks)
69
70
// #pragma config statements should precede project file includes.
71
// Use project enums instead of #define for ON and OFF.
72
73
#include <xc.h>

von . . (Gast)


Lesenswert?

Jochen D. schrieb:

> Ist die Aussage diverser Quellen falsch, das der Quarztakt für die
> Rechnung zu nutzen ist?

Die einzige verlässliche Datenquelle ist das Datenblatt des Chip.

Und da steht eindeutig und mehfach drin "Fosc/4".

von Ingo Less (Gast)


Lesenswert?

Miss Ratgeber schrieb:
> in Assembler
Nicht im Ernst...

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.