CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Help with 16F877A and ADC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rtracy



Joined: 13 Oct 2005
Posts: 7

View user's profile Send private message

Help with 16F877A and ADC
PostPosted: Thu Oct 13, 2005 1:23 pm     Reply with quote

I'm trying to write a basic program that will show that ADC is working on a PICDEM 2 board. I'd like to see a 5VDC signal on RA0 light RB3 and then scaled down from there. Using the variable resistor connected to RA0 I can vary the voltage from 0 to 5. No matter what values I use in the program, .350VDC will light RB3. What am I doing wrong?
Code:

void main()
{
   long value;
   int loop;
   
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);

while (true){

set_adc_channel(0);
delay_ms(5);
value = read_adc();
delay_ms(200);


if (value >= 2){
   output_high(pin_b0);}
if (value < 2)  {
   output_low(pin_b0);}

if (value >= 100){
   output_high(pin_b1);}
if (value < 100)  {
   output_low(pin_b1);}

if (value >= 512){
   output_high(pin_b2);}
if (value < 512)  {
   output_low(pin_b2);}

if (value >= 1001){
   output_high(pin_b3);}
if (value < 1001)  {
   output_low(pin_b3);}
   
}   
}

tophe59



Joined: 13 Oct 2005
Posts: 32
Location: France

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 1:27 pm     Reply with quote

I think the ADS is 10bits but the read is 8bits.

edit : have you test with 0 and 5vdc ?
rtracy



Joined: 13 Oct 2005
Posts: 7

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 1:41 pm     Reply with quote

tophe59 wrote:
I think the ADS is 10bits but the read is 8bits.

edit : have you test with 0 and 5vdc ?


Yes, I've tested with 0 and 5VDC. I've even tried using the external voltage references with the same results.

I added a #device adc=10 with the same results.
newguy



Joined: 24 Jun 2004
Posts: 1907

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 1:47 pm     Reply with quote

Just for the heck of it, replace "long value;" with "int16 value;". Make sure the #device adc=10 stays.

If that doesn't work, then try it with the adc set to 8 bits. You'll have to change "value" to be an 8 bit variable, and you'll need to change your comparison limits too.
rtracy



Joined: 13 Oct 2005
Posts: 7

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 2:55 pm     Reply with quote

I changed the ADC channel from 0 to 1 and it works fine. I haven't the slightest idea why it won't work correctly on channel 0.

set_adc_channel(0);

I'd really like to know what's going on if anyone has an idea.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 3:05 pm     Reply with quote

Post your compiler version and also post what PIC you're using.

Your compiler version can be found at the top of the .LST file in
the project directory. It will be a number like 3.191, or 3.236, etc.
rtracy



Joined: 13 Oct 2005
Posts: 7

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 4:01 pm     Reply with quote

PCM programmer wrote:
Post your compiler version and also post what PIC you're using.

Your compiler version can be found at the top of the .LST file in
the project directory. It will be a number like 3.191, or 3.236, etc.


PIC16F877A-I/P

Code:

CCS PCM C Compiler, Version 3.206, 24798               13-Oct-05 15:37

               Filename: C:\Temp\test.LST

               ROM used: 197 words (2%)
                         Largest free fragment is 2048
               RAM used: 9 (5%) at main() level
                         10 (6%) worst case
               Stack:    1 locations

*
0000:  MOVLW  00
0001:  MOVWF  0A
0002:  GOTO   019
0003:  NOP
....................  #include "C:\Temp\test.h"
....................  #include <16F877.h>
....................  //////// Standard Header file for the PIC16F877 device //////////////// 
.................... #device PIC16F877 
.................... #list 
.................... 
.................... #device adc=8 
.................... #use delay(clock=4000000) 
0004:  MOVLW  24
0005:  MOVWF  04
0006:  MOVF   00,W
0007:  BTFSC  03.2
0008:  GOTO   018
0009:  MOVLW  01
000A:  MOVWF  78
000B:  CLRF   77
000C:  DECFSZ 77,F
000D:  GOTO   00C
000E:  DECFSZ 78,F
000F:  GOTO   00B
0010:  MOVLW  4A
0011:  MOVWF  77
0012:  DECFSZ 77,F
0013:  GOTO   012
0014:  NOP
0015:  NOP
0016:  DECFSZ 00,F
0017:  GOTO   009
0018:  RETLW  00
.................... #fuses NOWDT,HS, NOPUT, NOPROTECT, BROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG 
....................   
.................... 
....................   
....................   
.................... void main() 
.................... { 
0019:  CLRF   04
001A:  MOVLW  1F
001B:  ANDWF  03,F
001C:  BSF    03.5
001D:  BSF    1F.0
001E:  BSF    1F.1
001F:  BSF    1F.2
0020:  BCF    1F.3
....................    long value; 
....................    int loop; 
....................   
....................    setup_adc_ports(ALL_ANALOG); 
*
0024:  BSF    03.5
0025:  BCF    1F.0
0026:  BCF    1F.1
0027:  BCF    1F.2
0028:  BCF    1F.3
....................    setup_adc(ADC_CLOCK_INTERNAL); 
0029:  BCF    03.5
002A:  BSF    1F.6
002B:  BSF    1F.7
002C:  BSF    03.5
002D:  BCF    1F.7
002E:  BCF    03.5
002F:  BSF    1F.0
....................    setup_psp(PSP_DISABLED); 
0030:  BSF    03.5
0031:  BCF    09.4
....................    setup_spi(FALSE); 
*
0021:  MOVLW  FF
0022:  BCF    03.5
0023:  MOVWF  20
*
0032:  BCF    03.5
0033:  BCF    14.5
0034:  BCF    20.5
0035:  MOVF   20,W
0036:  BSF    03.5
0037:  MOVWF  07
0038:  BCF    03.5
0039:  BSF    20.4
003A:  MOVF   20,W
003B:  BSF    03.5
003C:  MOVWF  07
003D:  BCF    03.5
003E:  BCF    20.3
003F:  MOVF   20,W
0040:  BSF    03.5
0041:  MOVWF  07
0042:  MOVLW  00
0043:  BCF    03.5
0044:  MOVWF  14
0045:  BSF    03.5
0046:  MOVWF  14
....................    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 
0047:  MOVF   01,W
0048:  ANDLW  C0
0049:  IORLW  08
004A:  MOVWF  01
....................    setup_timer_1(T1_DISABLED); 
004B:  BCF    03.5
004C:  CLRF   10
....................    setup_timer_2(T2_DISABLED,0,1); 
004D:  MOVLW  00
004E:  MOVWF  78
004F:  MOVWF  12
0050:  MOVLW  00
0051:  BSF    03.5
0052:  MOVWF  12
....................   
....................   
.................... while (true){ 
....................   
.................... set_adc_channel(1); 
0053:  MOVLW  08
0054:  MOVWF  78
0055:  BCF    03.5
0056:  MOVF   1F,W
0057:  ANDLW  C7
0058:  IORWF  78,W
0059:  MOVWF  1F
.................... delay_ms(5); 
005A:  MOVLW  05
005B:  MOVWF  24
005C:  CALL   004
.................... value = read_adc(); 
005D:  BSF    1F.2
005E:  BTFSC  1F.2
005F:  GOTO   05E
0060:  MOVF   1E,W
0061:  CLRF   22
0062:  MOVWF  21
.................... delay_ms(200); 
0063:  MOVLW  C8
0064:  MOVWF  24
0065:  CALL   004
....................   
....................   
.................... if (value >= 2){ 
0066:  MOVF   22,F
0067:  BTFSS  03.2
0068:  GOTO   06D
0069:  MOVF   21,W
006A:  SUBLW  01
006B:  BTFSC  03.0
006C:  GOTO   071
....................    output_high(pin_b7);} 
006D:  BSF    03.5
006E:  BCF    06.7
006F:  BCF    03.5
0070:  BSF    06.7
.................... if (value < 2)  { 
0071:  MOVF   22,F
0072:  BTFSS  03.2
0073:  GOTO   07C
0074:  MOVF   21,W
0075:  SUBLW  01
0076:  BTFSS  03.0
0077:  GOTO   07C
....................    output_low(pin_b7);} 
0078:  BSF    03.5
0079:  BCF    06.7
007A:  BCF    03.5
007B:  BCF    06.7
....................   
.................... if (value >= 100){ 
007C:  MOVF   22,F
007D:  BTFSS  03.2
007E:  GOTO   083
007F:  MOVF   21,W
0080:  SUBLW  63
0081:  BTFSC  03.0
0082:  GOTO   087
....................    output_high(pin_d1);} 
0083:  BSF    03.5
0084:  BCF    08.1
0085:  BCF    03.5
0086:  BSF    08.1
.................... if (value < 100)  { 
0087:  MOVF   22,F
0088:  BTFSS  03.2
0089:  GOTO   092
008A:  MOVF   21,W
008B:  SUBLW  63
008C:  BTFSS  03.0
008D:  GOTO   092
....................    output_low(pin_d1);} 
008E:  BSF    03.5
008F:  BCF    08.1
0090:  BCF    03.5
0091:  BCF    08.1
....................   
.................... if (value >= 120){ 
0092:  MOVF   22,F
0093:  BTFSS  03.2
0094:  GOTO   099
0095:  MOVF   21,W
0096:  SUBLW  77
0097:  BTFSC  03.0
0098:  GOTO   09D
....................    output_high(pin_d0);} 
0099:  BSF    03.5
009A:  BCF    08.0
009B:  BCF    03.5
009C:  BSF    08.0
.................... if (value < 120)  { 
009D:  MOVF   22,F
009E:  BTFSS  03.2
009F:  GOTO   0A8
00A0:  MOVF   21,W
00A1:  SUBLW  77
00A2:  BTFSS  03.0
00A3:  GOTO   0A8
....................    output_low(pin_d0);} 
00A4:  BSF    03.5
00A5:  BCF    08.0
00A6:  BCF    03.5
00A7:  BCF    08.0
....................   
.................... if (value >= 200){ 
00A8:  MOVF   22,F
00A9:  BTFSS  03.2
00AA:  GOTO   0AF
00AB:  MOVF   21,W
00AC:  SUBLW  C7
00AD:  BTFSC  03.0
00AE:  GOTO   0B5
....................    output_high(pin_c3);} 
00AF:  BCF    20.3
00B0:  MOVF   20,W
00B1:  BSF    03.5
00B2:  MOVWF  07
00B3:  BCF    03.5
00B4:  BSF    07.3
.................... if (value < 200)  { 
00B5:  MOVF   22,F
00B6:  BTFSS  03.2
00B7:  GOTO   0C2
00B8:  MOVF   21,W
00B9:  SUBLW  C7
00BA:  BTFSS  03.0
00BB:  GOTO   0C2
....................    output_low(pin_c3);} 
00BC:  BCF    20.3
00BD:  MOVF   20,W
00BE:  BSF    03.5
00BF:  MOVWF  07
00C0:  BCF    03.5
00C1:  BCF    07.3
....................   
.................... } 
00C2:  BSF    03.5
00C3:  GOTO   053
.................... } 
....................   
....................   
....................   
....................   
....................   
....................   
.................... 
00C4:  SLEEP

Configuration Fuses:
   Word  1: 3F7A   HS NOWDT NOPUT NOPROTECT BROWNOUT NOLVP NOCPD NOWRT NODEBUG

PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 13, 2005 4:39 pm     Reply with quote

I took your code and compiled it with PCM 3.206, and ran it on
a 16F877A on a PicDem2-Plus board. I did change the output pins
from the ones you're using, back to the LED pins of B0, B1, B2, and B3.

Then as I turn the trimpot on the board, the LEDs progressively turn on,
and then as I turn it the other way they progressively turn off.

So it works for me.

Maybe you accidently damaged pin A0 ?
rtracy



Joined: 13 Oct 2005
Posts: 7

View user's profile Send private message

PostPosted: Fri Oct 14, 2005 7:34 am     Reply with quote

Thank you for taking the time to test my code.

I can change back to channel 0 and it stops working on all three of my 877A chips. I'm going to just use the other channels for now.

PCM programmer wrote:
I took your code and compiled it with PCM 3.206, and ran it on
a 16F877A on a PicDem2-Plus board. I did change the output pins
from the ones you're using, back to the LED pins of B0, B1, B2, and B3.

Then as I turn the trimpot on the board, the LEDs progressively turn on,
and then as I turn it the other way they progressively turn off.

So it works for me.

Maybe you accidently damaged pin A0 ?
Guest








PostPosted: Mon Oct 17, 2005 9:24 pm     Reply with quote

Can you please post your setup? I'm trying to learn how to use the ADC, but it is not working for me. I am basically pulling up AN1 to VCC using a 1k resistor (datasheet recomends less than 2.5k) but it doesn't work. Here is my code:
Thanks for your help in advance

#include <16F874A.h>
#include <stdlib.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=10000000)


int k;
int16 source1;
long source2;


void main() {

set_tris_d(0x00); //set port d as outputs


setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL); //recommended for a 10 MHz signal
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);

delay_ms(100);
set_adc_channel(1);
delay_ms(100);
source1 = read_adc();
delay_ms(1000);

if(source1 >= 512){
delay_ms(100);
for(k=0; k<5; k++){
output_high(PIN_D2);
delay_ms(1000);
output_low(PIN_D2);
delay_ms(1000);
}
}

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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