View previous topic :: View next topic |
Author |
Message |
ciprian_briscaru
Joined: 19 Jun 2010 Posts: 3
|
Problem with setup_adc_ports on PIC16F877A |
Posted: Sat Jun 19, 2010 10:57 am |
|
|
Hi guys,
I have recently tried the CCS Demo compiler for a school project. What I want to do is to make a square wave signal from a sinusoidal signal using PIC16F877A internal comparator.
So I stared a new project, from the wizard, selected the comparator configuration I wanted, selected it's inputs as analog inputs.
The problem is that when I list the ASM code generated for setup_adc_ports(AN0_AN1_AN3) it keeps writing at 0x1F address when it should write at 0x9F where the ADCON1 register is. At 0x1F is the ADCON0 register.
.................... setup_adc_ports(AN0_AN1_AN3);
0047: BCF 1F.0
0048: BCF 1F.1
0049: BSF 1F.2
004A: BCF 1F.3
I tried then using ASM code directly, but I got the same result:
#byte ADCON1 = 0x9F
.....
.................... #asm ASIS
.................... BCF ADCON1,0
005B: BCF 1F.0
.................... BCF ADCON1,1
005C: BCF 1F.1
.................... BSF ADCON1,2
005D: BSF 1F.2
.................... BCF ADCON1,3
005E: BCF 1F.3
.................... #endasm
For me it seems either a bad configuration of my PCWHD or a problem in the compiler.
What do you think?
I use CCS PCM C Compiler, Version 4.102d, 15399
and my header:
#include <16F877A.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT //No Power Up Timer
#FUSES NOPROTECT //Code not protected from reading
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD //No EE protection
#FUSES NOWRT //Program memory not write protected
#FUSES RESERVED //Used to set the reserved FUSE bits
#use delay(clock=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
|
|
MiniMe
Joined: 17 Nov 2009 Posts: 50
|
|
Posted: Sat Jun 19, 2010 11:48 am |
|
|
Hi,
First thing .. try to use C examples from help file. By compiling working c code you will have a opportunity to view assembly instructions file... every c command or statement and under it is assembly code... intructions.
Then compare tease 2 assembly program codes.
My experience tells that there are certain situations where the order of conf. bits or registers is important. |
|
|
ciprian_briscaru
Joined: 19 Jun 2010 Posts: 3
|
|
Posted: Sat Jun 19, 2010 1:04 pm |
|
|
Hi MiniMe,
Thanks for your reply and for your interest in trying to solve my problem.
As you advised, I compiled the Simple A/D (EX_ADMM.C) example write from the CCS web page.
This is the result I got:
Code: | .................... setup_adc_ports( RA0_ANALOG );
00AE: BSF 03.5
00AF: BCF 1F.0
00B0: BSF 1F.1
00B1: BSF 1F.2
00B2: BSF 1F.3 |
For me it is wery strange why a simple line of ASM code is compiled differently even if I used ASM ASIS directive:
Code: | .................... #asm ASIS
.................... BCF 0x9F,0
005B: BCF 1F.0
.................... #endasm |
Can you check out if this asm code compiled for PIC16f877A is generating the same output as mine?
Thanks,
Ciprian. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Jun 19, 2010 1:27 pm |
|
|
ciprian_briscaru wrote: | For me it is wery strange why a simple line of ASM code is compiled differently even if I used ASM ASIS directive:
Code: | .................... #asm ASIS
.................... BCF 0x9F,0
005B: BCF 1F.0
.................... #endasm |
| BCF 0x9F, 0 is an invalid asm instruction. If you check the 'Instruction Set Summary' chapter in the PIC16F877A datasheet you see specified that the address must be in the range 0 - 127. 0x9F is outside this range.
The reason for this limitation is that the PIC16 processors access the RAM memory in a banked fashion, each memory bank 0x80 bytes large. The selection for which bank is addressed is by the RP0:RP1 bits in the status register. If you check the list file you will see this selected a few lines before your line.
More details in chapter 2.2 of the PIC datasheet. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Jun 19, 2010 1:52 pm |
|
|
If you remove the "ASIS" directive, the compiler will add the ASM code to
select the bank.
Code: |
.... #asm ASIS // No Bank Selection will be done.
.... BCF ADCON1,0
000D: BCF ADCON1.PCFG0
.... #endasm
....
.... #asm // Bank Selection will be added.
.... BCF ADCON1,0
000E: BSF STATUS.RP0 // Select Bank 1
000F: BCF ADCON1.PCFG0
.... #endasm
|
The "ASIS" directive was added by CCS so that programmers who wanted
their #ASM code to be assembled literally (with no help from CCS) would
be satisfied. For most simple ASM code, it's best to leave off the ASIS. |
|
|
ciprian_briscaru
Joined: 19 Jun 2010 Posts: 3
|
|
Posted: Sat Jun 19, 2010 1:57 pm |
|
|
Thank you for your clarifying answer. Eveything makes sense now. |
|
|
|