View previous topic :: View next topic |
Author |
Message |
paulw300568
Joined: 04 Oct 2010 Posts: 5
|
Help! PWM on PIC16F886 |
Posted: Mon Oct 04, 2010 2:47 pm |
|
|
Hi,
I'm trying to get the PWM module running on the 16F886, with the following code to give a 1MHz signal with 50% duty, but it doesn't want to work! I have got it working in assembler, but won't work in C. Anyone any ideas about what I'm missing please?
Code: |
#include <16F886.h>
#fuses INTRC_IO, NOWDT,PWRT,NOLVP
#use delay(clock=8000000)
void main(void)
{
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1, 2, 1);
set_pwm1_duty(4);
while(1);
} |
|
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Mon Oct 04, 2010 3:01 pm |
|
|
Mabey you should put:
it probably does not make a difference.... but its how i code my loops..
and ive never seen
whats wrong with plain old Void Main() ? _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 04, 2010 3:06 pm |
|
|
I assume you're using 10-bit PWM mode in assembler ?
In CCS, to invoke 10-bit mode, you need to specify the duty cycle
parameter as a 'long'. If it's a 8-bit constant, append an 'L' to it, or if it's
a variable, then declare it as an 'int16'. Example:
Quote: |
set_pwm1_duty(4L);
|
|
|
|
paulw300568
Joined: 04 Oct 2010 Posts: 5
|
|
Posted: Mon Oct 04, 2010 3:24 pm |
|
|
Hi,
Thanks for the replies so far. I have recoded it like the following, so accessing the registers directly (copying from my assembler code), and it still doesn't work! Definitely starting to drive me nuts now!
Code: |
#include <16F886.h>
#fuses INTRC_IO, NOWDT,PWRT,NOLVP
#use delay(clock=8000000)
#byte PR2 = 0x92
#byte CCPR1L = 0x15
#byte CCP1CON = 0x17
#byte OSCCON = 0x8F
#bit TMR2ON = 0x12.2
#byte ANSEL = 0x188
#byte ANSELH = 0x189
#byte TRISA = 0x85
#byte TRISB = 0x86
#byte TRISC = 0x87
#byte PORTA = 0x05
#byte PORTB = 0x06
#byte PORTC = 0x07
void main()
{
TRISA = 0xC0;
TRISB = 0x00;
TRISC = 0xB0;
PORTA = 0x00;
PORTB = 0x00;
PORTC = 0x00;
OSCCON = 0x77;
PR2=1;
CCPR1L = 0;
CCP1CON = 0x3C;
TMR2ON = 1;
while(1);
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 04, 2010 3:31 pm |
|
|
What was so wrong with doing it with my modification to your code ?
Did it not work ? If that's the case, post your compiler version.
I tested it with vs. 4.112.
If it worked, and you want to see what the assembly code was,
then go to Build Options and set the compiler's List file format to Symbolic.
Then re-compile and look at the .LST file. It will show you the assembler
code necessary to make it work. |
|
|
paulw300568
Joined: 04 Oct 2010 Posts: 5
|
|
Posted: Mon Oct 04, 2010 3:53 pm |
|
|
Hi PCM programmer,
No that didn't work. I'm using version PCM 4.007 with MPLAB IDE version 8.56.
I am wondering if it's my #fuses settings or something, because this just doesn't make sense.
Also, in another program I noticed that with this compiler if I use the following
set_tris_a(OxXX);
set_tris_b((OxXX);
set_tris_c(OxXX);
Where XX is some 8 bit number, then the compiler complains at set_tris_a(), throwing up about 40 or so errors, but it has no problem with
set_tris_b((OxXX);
set_tris_c(OxXX);
set_tris_a(OxXX);
Now, what is that about?
Regards,
Paul |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Oct 04, 2010 4:36 pm |
|
|
Quote: | I'm using version PCM 4.007
|
That's the problem. I wouldn't do anything with that early of a version.
It's just not worth it. It's just endless problems.
Here's the ASM code produced by vs. 4.112 for the 16F886.
Note that it's using a shadow register for TRISC, to avoid conflicts
with peripheral overrides.
Code: |
.................... void main(void)
.................... {
0004: CLRF FSR
0005: BCF STATUS.IRP
0006: MOVLW 1F
0007: ANDWF STATUS,F
0008: MOVLW 61
0009: BSF STATUS.RP0
000A: MOVWF OSCCON
000B: MOVF OSCCON,W
000C: MOVLW FF
000D: BCF STATUS.RP0
000E: MOVWF @TRIS_C
000F: BSF STATUS.RP0
0010: BSF STATUS.RP1
0011: MOVF ANSELH,W
0012: ANDLW C0
0013: MOVWF ANSELH
0014: BCF STATUS.RP1
0015: BCF ADCON1.VCFG0
0016: BCF ADCON1.VCFG1
0017: MOVLW 00
0018: BSF STATUS.RP1
0019: MOVWF ANSEL
001A: BCF STATUS.RP0
001B: CLRF CM1CON0
001C: CLRF CM2CON0
001D: CLRF CM2CON1
.................... setup_ccp1(CCP_PWM);
001E: BCF STATUS.RP1
001F: BCF @TRIS_C.2
0020: MOVF @TRIS_C,W
0021: BSF STATUS.RP0
0022: MOVWF TRISC
0023: BCF STATUS.RP0
0024: BCF PORTC.2
0025: BSF STATUS.RP0
0026: BCF TRISE.-
0027: MOVLW 0C
0028: BCF STATUS.RP0
0029: MOVWF CCP1CON
002A: BSF STATUS.RP0
002B: CLRF PWM1CON
002C: CLRF ECCPAS
002D: MOVLW 01
002E: MOVWF PSTRCON
....................
.................... setup_timer_2(T2_DIV_BY_1, 2, 1);
002F: MOVLW 00
0030: MOVWF @78
0031: IORLW 04
0032: BCF STATUS.RP0
0033: MOVWF T2CON
0034: MOVLW 02
0035: BSF STATUS.RP0
0036: MOVWF PR2
....................
.................... set_pwm1_duty(4L);
0037: MOVLW 01
0038: BCF STATUS.RP0
0039: MOVWF CCPR1L
003A: MOVF CCP1CON,W
003B: ANDLW CF
003C: MOVWF CCP1CON
....................
.................... while(1);
003D: GOTO 03D
Configuration Fuses:
Word 1: 2FE4 INTRC_IO NOWDT PUT NOLVP NODEBUG NOPROTECT BROWNOUT FCMEN MCLR NOCPD IESO
Word 2: 3FFF NOWRT BORV40
|
|
|
|
paulw300568
Joined: 04 Oct 2010 Posts: 5
|
|
Posted: Mon Oct 04, 2010 4:56 pm |
|
|
Hi,
OK thanks very much for taking the time to investigate that. I'll upgrade as soon as I get a chance.
P |
|
|
paulw300568
Joined: 04 Oct 2010 Posts: 5
|
|
Posted: Tue Oct 05, 2010 10:37 am |
|
|
Hi,
Just to let you know that I upgraded to the latest compiler version and my the code now works fine.
Thanks very much for all the help!
P |
|
|
|