Hi.
When I use this PWM code below to Nerdegutta (thanks by the way) I get it to work in ISIS Proteus,
but not when I try it on a breadboard with a pic 16f628A and a led. I can provide the Proteus schema if necessary.
I connected the led to RB3. I also tryed to use an transistor if the output current from the pic was to low to drive the led.
My question: Is there something I need to activate or do on the microcontroller to get PWM to work?
Any other tips or thoughts regarding this problem appreciated.
Thanks
Bjørn
C-Code:
When I use this PWM code below to Nerdegutta (thanks by the way) I get it to work in ISIS Proteus,
but not when I try it on a breadboard with a pic 16f628A and a led. I can provide the Proteus schema if necessary.
I connected the led to RB3. I also tryed to use an transistor if the output current from the pic was to low to drive the led.
My question: Is there something I need to activate or do on the microcontroller to get PWM to work?
Any other tips or thoughts regarding this problem appreciated.
Thanks
Bjørn
C-Code:
Rich (BB code):
/*
Program: main.c
Description: PWM Test
PIC: PIC 16 F 628
IDE: MPLAB
Compiler: Hi Tech C
Date: Mars 2011
Author: Jens Christoffersen
WEB: www.nerdegutta.org
*/
/* Include & definitions */
#define _XTAL_FREQ 4000000
/* Configuration */
__CONFIG (WDTDIS &
PWRTEN &
MCLREN &
BOREN &
LVPDIS &
DATUNPROT &
UNPROTECT &
INTIO);
/* Prototyping function */
/* Gobal variables */
static unsigned char retning;
static void Intr(void) interrupt 0
{
if(T0IF) // Har vi fått en timer interupt?
{
T0IF=0;
if(retning) // teller oppover
{
CCPR1L++;
if(CCPR1L == 255)
retning=0;
}
else
{
CCPR1L--;
if(CCPR1L == 0)
retning=1;
}
}
} // end static void Intr(void)
/* Functions */
/* Main Program */
void main()
{
retning=0;
TRISA = 0b11111111;
TRISB = 0b00000000;
PORTA = 0b00000000;
PORTB = 0b00000000;
PR2 = 255; // Setter PWM periode
CCPR1L = 1; // Setter PWM duty cycle
CCP1CON = 4|8; // Setter PWM mode
CCP1X = 1;
T2CON = 0x00;
T2CKPS0 = 1; // Set timer 2 prescaler to 1:16
T2CKPS1 = 1; // These bits are in T2CON
TMR2ON = 1; // Enable timer 2
// Set up timer0 interrupt
T0CS=0; // Internal clock source
PSA=0; // Assign prescaler to timer0
PS2=0; PS1=1; PS0=0;
INTCON=0;
GIE=1;
T0IE=1;
TMR0=0;
while (1); // Fortsetter inn i evigheten, og forbi
}