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

ADC on 16F88

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



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

ADC on 16F88
PostPosted: Thu Jun 07, 2007 12:49 pm     Reply with quote

Hello,

I wrote the following code to do an AD conversion of an RSSI signal. The RSSI signal should be between 0-5V range.

For testing/learning purposes, I created this code to convert the voltage from A0, divide-it by ten and beep me the value of the conversion.

Interestingly I always get 8 or 6 beeps, doesn't matter if A0 is conencted to Vdd or Vss. That means it is not doing the conversion:

Vdd is 4.8 to 5.0V.


Code:

#include <16F88.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOPUT                    //No Power Up Timer
#FUSES MCLR                     //Master Clear pin enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#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 NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled

#use delay(clock=4000000)
#use rs232(baud=19600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)




#include "tracker_16F88.h"

#use fast_io (a)
#use fast_io (b)

#Define RSSI      Pin_A0
#Define Beeper    Pin_B1
#Define On        1
#Define Off       0

int8 iX;
int8 cX;
int16 dX;
int16 adc_value;
int16 rssi_value[5];
Int16 adc_conversion(Int16 adc_value);
Int16 value;
Int8  Servo_position;
Int16 RSSI_tenth;

Void Init();
void Beep();
void Convert();



void main()
{
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   set_adc_channel( 0 );

   set_tris_a(0b00000001);
   set_tris_b(0b00000000);
   
   Delay_ms(3000);
   Init();
    }
   
   while(true){

   Delay_ms(5000);
   rssi_value[0] = adc_conversion(value);
   RSSI_tenth = rssi_value[0]/10;
   for (dX=0; dX<RSSI_tenth; dX++){
         Beep();
         Delay_ms(500);

   }
}

Void Init(){
  for (ix=0; iX<2; iX++) {
         Beep();
         Delay_ms(150);
   }
}

Void Beep() {
     for ( cX = 0; cX < 80; cX++ ) {
     Output_high(Beeper);
     delay_us ( 180 );
     Output_low(Beeper);
     delay_us ( 160 );
     }
}

Int16 adc_conversion(int16 adc_value) {
int16 v[5];
int16 total;

      For(cX = 0; cX < 5; cX++) {
         delay_us(10);
         v[cX] = read_adc();
      }
      total = 0;
      For (cX = 0; cX < 5; cX++) {
            total = total + v[cX];
      }
      value = total/5;
      return(value);
}


What am I doing wrong?

Thanks, Ox.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 07, 2007 2:38 pm     Reply with quote

Your complaint is that you believe the ADC is not working.
One method to trouble-shoot this problem is to make a simple test
program that only tests the ADC.

I made the test program shown below and kept the settings the same
as in your program, such as an 8-bit ADC result and 4 MHz internal osc.
I tested it on a PicDem2-Plus board with PCM vs. 4.040. That board
is setup for a serial port on pins C6 and C7, so I jumpered over the
connections to pins B5 and B2 so it would work with a 16F88. As I
turned the trimpot on pin A0 from one side to the other, I got this
output, which is correct:
Quote:
00 00 00 00 22 48 72 A1 FD FF FF FF FF

Code:

#include <16F88.h>
#device adc=8
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B5, rcv=PIN_B2, ERRORS)

//=================================
void main()
{
int8 result;

setup_adc_ports(sAN0|VSS_VDD);
setup_adc(ADC_CLOCK_INTERNAL);
set_adc_channel(0);

while(1)
  { 
   result = read_adc();
   printf("%X ", result);
   delay_ms(500);
  }

}
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Thu Jun 07, 2007 3:13 pm     Reply with quote

your code won't compile with this extra brace.

Code:

void main()
{
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   set_adc_channel( 0 );

   set_tris_a(0b00000001);
   set_tris_b(0b00000000);
   
   Delay_ms(3000);
   Init();
    }      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
   
   while(true){

   Delay_ms(5000);
   rssi_value[0] = adc_conversion(value);
   RSSI_tenth = rssi_value[0]/10;
   for (dX=0; dX<RSSI_tenth; dX++){
         Beep();
         Delay_ms(500);

   }
}
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Thu Jun 07, 2007 4:22 pm     Reply with quote

Thank you. I stripped out the unnecessary routines from the code I posted, that's how the extra brace by mistake remained there.

I did not assume that the ADC is not working, I merely speculated over the response I've got.

If I feed the ADC with Vdd, than I should get a maximum value - I guess as a response - in case of an 8 bit = 256 or I believe this is 10 bit = 1024. Now 254 divided by 10 is 25.4, I should have heard 25 beeps - or 102 beeps...

I will try your simple program, since I do not have any debugging tools yet I have to use the beeper to count the values.

Thanks and I will report back later on the progress.
Ox.
kevcon



Joined: 21 Feb 2007
Posts: 142
Location: Michigan, USA

View user's profile Send private message

PostPosted: Fri Jun 08, 2007 6:58 am     Reply with quote

You might be able to send some messages out a software UART to hyperterminal as way of debugging, or if you are just trying to debug the ADC you can use the hardware UART.
oxxyfx



Joined: 24 May 2007
Posts: 97

View user's profile Send private message

PostPosted: Fri Jun 08, 2007 11:03 pm     Reply with quote

Thank you, I resolved the problem. Today I received my sample shipment from Microchip containing a 16F688 - with this I built an serial LCD display and finally got around to resolve the problem.

It is much easier to troubleshoot if one can see the values of the variables. Anyway with all this, I was going nuts tonight because I thought I may be the only one the ADC is not working for... After several hours finally I decided to change ADC channels and try on a different channel - when I suddenly realized that all this time I had A0 set to analog, but I connected A1 in the test circuit.

Of course after I rewired the thing, it started working exactly as it suppose to.

Thanks for all the help.

Ox.
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