|
|
View previous topic :: View next topic |
Author |
Message |
andyd
Joined: 08 Mar 2007 Posts: 30
|
16f886 PWM Problems |
Posted: Mon Apr 30, 2007 12:10 pm |
|
|
I'm trying to program the PWM on a 16F886, but am having some problems with it. Basically, the PWM output pulses between 0 & approx. 300mV, rather than 0 and 5V as it should. Also, it appears to come in "blocks" of 8 pulses at about 62 kHz when I look at it on a scope (which seems almost exactly the same as my SCL pin for my EEPROM for some reason). I've tried 2 different PICs so pretty sure they're not duff.
Relevant code is below:
Code: | // Define registers & bits
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte CCP2CON = 0x1D
#byte CCPR2L = 0x1B
#byte OSCCON = 0x8F
#byte ANSEL = 0x188
#byte T1CON = 0x10
#byte T2CON = 0x12
#byte PR2 = 0x92
|
(TIMER1 is disabled below as it shares a pin with CCP2, so just making sure it won't interfere)
Code: |
OSCCON = 0x71; // Use internal 8 MHz oscillator
ANSEL = 0x00; // Turn off analogue inputs
CCP2CON = 0x3; // Setup CCP2 as PWM
T1CON = 0x0; // Disable TIMER1
T2CON = 0x4; // Turn on TIMER2 and set pre- and postscalers to 1
PR2 = 0xF9; // Set PR2 value to 249 to give PWM frequency of 8 kHz |
Code: | // PWM output
#inline void writePWM(signed long sample)
{
unsigned long pwmout = 0;
pwmout = 0x8000 + sample; // Offset around 0x8000
pwmout = pwmout >> 8; // Scale to 8 bit by shifting right 8 bits
CCPR2L = pwmout; // Write resulting 8 bits to CCPR2L
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Apr 30, 2007 3:17 pm |
|
|
You have this line in your program:
Quote: | CCP2CON = 0x3; // Setup CCP2 as PWM |
The 16F886 data sheet says that "0x3" is an unused (reserved) setting:
Quote: |
REGISTER 11-2: CCP2CON: CCP2 CONTROL REGISTER
bit 3-0 CCP2M<3:0>: CCP2 Mode Select bits
0000 = Capture/Compare/PWM off (resets CCP2 module)
0001 = Unused (reserved)
0010 = Unused (reserved)
0011 = Unused (reserved)
0100 = Capture mode, every falling edge
0101 = Capture mode, every rising edge
0110 = Capture mode, every 4th rising edge
0111 = Capture mode, every 16th rising edge
1000 = Compare mode, set output on match (CCP2IF bit is set)
1001 = Compare mode, clear output on match (CCP2IF bit is set)
1010 = Compare mode, generate software interrupt on match
(CCP2IF bit is set, CCP2 pin is unaffected)
1011 = Compare mode, trigger special event (CCP2IF bit is set,
TMR1 is reset and A/D conversion is started if the ADC
module is enabled. CCP2 pin is unaffected.)
11xx = PWM mode. |
|
|
|
andyd
Joined: 08 Mar 2007 Posts: 30
|
|
Posted: Tue May 01, 2007 8:46 am |
|
|
That was the problem, thanks! Must have typed a couple of 1's in the wrong place in my hex calculator! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|