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 support@ccsinfo.com

Problem with ADC and PIC16F688

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



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

Problem with ADC and PIC16F688
PostPosted: Sun Aug 07, 2011 1:09 pm     Reply with quote

Hi to all,

I'm trying to do a very simple thing with a PIC16F688, but for some reason, I'm failing to succeed!

All I want to do (for the moment) is to read a value from AN4, AN5 and AN6, and send it trough RS232. But, all I'm getting on the remote terminal is the value 0.

For an analog input voltage, I'm using a 5K pot, connected between VCC and GND.

In the code below, I'm only using the AN6.

main.h
Code:

#include <16F688.h>
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC                    //Internal RC Osc
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES NOCPD                    //No EE protection
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOIESO                   //Internal External Switch Over mode disabled

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C4,rcv=PIN_C5,bits=8)


main.c
Code:

#include "main.h"
#include <stdlib.h>

#define bkbhit (next_in!=next_out)
#define BUFFER_SIZE 16

BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;

#int_RDA
void serial_isr()
{
   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out) next_in=t;           // Buffer full !!
}

BYTE bgetc()
{
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}


void main()
{

   int16 x;


   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   
   setup_adc_ports(ALL_ANALOG|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_32);
   
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);

   setup_oscillator(OSC_8MHZ);
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   while(true)
   {
      set_adc_channel( 6 );
      delay_us(50);
      x=read_adc();
     
      printf("Value = %Lu\r\n",x);
      delay_ms(200);
   } 

}


This seems to be a pretty straight forward code, but, it doesn't work.
To make things even stranger, if I put some voltage on AN4 and AN5, and try to read any of those analog inputs, I get the value = 10 (?) ... is it something related to CCP2 ?

Can anyone help me, pointing out some sort of solution?

Thanks in advance.
Kind Regards
Lagaffe
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 07, 2011 1:29 pm     Reply with quote

What's your CCS compiler version ?

Also, is this a manufactured board, or did you make it yourself ?
If it's factory built, post the manufacturer and part number of the board.
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Sun Aug 07, 2011 1:35 pm     Reply with quote

Hi,

Sorry, I forgot to mentioned that.

The board, I've made it myself, its very simple, just a PIC16F688 with ICSP connector, a MAX232 with caps, a 100nF cap next to the VCC and GND of the pic, and a 10K resistor from MCLR to VCC.

The CCS Compiler is the 4.084 version.

Any idea?

Thanks in advance
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 07, 2011 2:51 pm     Reply with quote

It works for me. I installed vs. 4.084 and compiled your program.
I ran on a 16F688 on a Microchip "low pin count" board. I jumpered the
PICs Tx pin (and ground) over to a 3M breadboard with a Max232A chip
on it. I also jumpered AN6 (pin C2) to the center tap of a 5K trimpot on
the 3M breadboard. The other ends of the trimpot are connected to +5v
and ground. The output is displayed on TeraTerm on a PC. If I quickly
turn the trimpot through its range, this is what I get in the terminal
window. It's working.
Quote:

Value = 1023
Value = 1023
Value = 1018
Value = 741
Value = 495
Value = 507
Value = 420
Value = 222
Value = 25
Value = 0
Value = 0
Value = 0
Lagaffe



Joined: 14 Sep 2010
Posts: 14
Location: Portugal, TNV

View user's profile Send private message

PostPosted: Tue Aug 09, 2011 6:12 am     Reply with quote

This must be one of those things that won't work, and I can't figure out where the problem is.

I'm using the PDIP version of the PIC.

In the weekend, I'll revise all the hardware, and if no major problem arrises, then, I'll have to try on another pic.

I'll keep this post alive.

Thanks for your help.
Regards
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