Hi Guys
I'm trying to enable PWM on the pic10f322.I've gotten it up and running but I'm having trouble setting the duty cycle.
It's outputting at 38khz as it should but the duty cycle is stuck at 90%. In fact no matter what I put in PWM2DCH and PWM2DCL, even if i zero those out, it's still 90% so i assume I've made a screwed on the initialization.
It's a basic setup, this is what I have:
It's probably something simple, but I've been staring at this for a couple of hours and I'm starting to go cross eyed. Anyone point out what I'm doing wrong?
I'm trying to enable PWM on the pic10f322.I've gotten it up and running but I'm having trouble setting the duty cycle.
It's outputting at 38khz as it should but the duty cycle is stuck at 90%. In fact no matter what I put in PWM2DCH and PWM2DCL, even if i zero those out, it's still 90% so i assume I've made a screwed on the initialization.
It's a basic setup, this is what I have:
Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 8000000
//__CONFIG (FOSC_INTOSC & BOREN_OFF & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & CP_OFF & LVP_OFF & LPBOR_OFF & BORV_HI & WRT_OFF);
#pragma config FOSC = INTOSC // Oscillator Selection bits (INTOSC oscillator: CLKIN function disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
#pragma config LPBOR = OFF // Brown-out Reset Selection bits (BOR disabled)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
void InitPWM();
void main()
{
PORTA = 0b00000000;
TRISA = 0b00000111;
ANSELA = 0b00000000;
InitPWM();
while(1)
{
}
}
void InitPWM()
{
PWM2CON = 0b00000000; // Clear PWM1CON
PR2 = 0b00110100; // Set period to 52
PWM2CON = 0b11000000; //Enable PWM Module Enable and Module Output Enable bit
PWM2DCH = 0b00000000; // Clear duty cycle registers
PWM2DCL = 0b00000000;
TMR2IF = 0; //Clear the timer 2 interrupt flag
T2CON = 0b00000000; //Set prescaler to 1 (T2CKPS<1:0>), enable to off(TMR2ON)
//and postscaler to 1:1(TOUTPS 3:0)
TMR2ON = 1; //Now enable timer 2
TRISA = 0b00000000; //Enable the pwm pin output driver on RA1 (and also make all other pins outpouts.)
//Set duty cycle to 50%
PWM2DCH = 0b11010000 ;
PWM2DCL = 0b10000000;
return;
}
}
It's probably something simple, but I've been staring at this for a couple of hours and I'm starting to go cross eyed. Anyone point out what I'm doing wrong?
Attachments
-
1.5 MB Views: 33