erstmal nen timer einschalten, in dem falle nehm ich timer 3:
1 | void timer3Init(void)
|
2 | {
|
3 | T3CON = 0x00; // Stops the Timer3 and reset control reg.
|
4 | TMR3 = 0x00; // Clear contents of the timer register
|
5 | PR3 = 0xFFFF; // Load the Period register
|
6 | _T3IF = 0; // Clear the Timer3 interrupt status flag
|
7 | _T3IE = 0; // Enable Timer3 interrupts
|
8 |
|
9 | T3CONbits.TCKPS = 0; // prescaler settings at 1:1
|
10 | T3CONbits.TON = 1; // Start Timer3 clock source set to the internal instruction cycle
|
11 | }
|
dann die pwm einschalten
1 | #define PWM_FREQ 2500
|
2 | #define PWM_PR FCY/PWM_FREQ
|
3 |
|
4 | void pwm1Init(void)
|
5 | {
|
6 | OC1CONbits.OCM = 0b000; // Disable Output Compare Module
|
7 | OC1R = PWM_PR/2; // Write the duty cycle for the first PWM pulse
|
8 | OC1RS = PWM_PR/2; // Write the duty cycle for the second PWM pulse
|
9 | PR3 = PWM_PR; // Write the cycle for complete cycle
|
10 | OC1CONbits.OCTSEL = 1; // Select Timer 3 as output compare time base
|
11 | OC1CONbits.OCM = 0b110; // PWM mode
|
12 | }
|
und dann genießen, in dem falle mit dem verhältnis 50/50.
aber um ehrlich zu sein, das alles steht im family datasheet... wo du
dann auch das initialisieren des PPS findest, sonst zuckt da ja sowieso
nichts :)