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 in a PIC12F683

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



Joined: 02 Nov 2007
Posts: 26

View user's profile Send private message

...ADC in a PIC12F683
PostPosted: Thu May 07, 2009 6:13 am     Reply with quote

Hi...

Sincerely, i do not know what to do...

It's ridiculously simple: i want to read an analog input using a PIC12F683

I made it work ! But... sometimes !!!! Most of the times, i alwars have a "zero" output (int16 entree).

I am sure it has something to do with my configuration, but i am confused, (mainly because i was able to make it work !!! )

Thanks for your help !

My code : (in the code i "scan" all four 0-3 analog inputs, i put in around 2.5 volts on pin 7 (verified with a DMM) and, using hyperterminal, i get the output value and the RS232 works ! )


Code:

//                      _________   __________
//                     |         \_/          |
//                   __|      _               |__
//                  |  | Vdd (_)          Vss |  |
//              +5V |1 |                      | 8|------ Masse
//                  |__|                      |__|
//                     |                      |
//                   __|                      |__
//                  |  |              ICSPDAT |  |
//                  |2 | PIN_A5       AN0  40 | 7| Analog input
//                  |__| 45            PIN_A0 |__|
//                     |                      |
//                   __|                      |__
//                  |  | AN3          ICSPCLK |  |
//                  |3 | 44            AN1 41 | 6|
//                  |__| PIN_A4        PIN_A1 |__|
//                     |                      |
//                   __| ____                 |__
//         47k      |  | MCLR/Vpp      PIN_A2 |  |
//  +5V--/\/\/\/----|4 | 43              AN2  | 5| 9600 out
//                  |__| PIN_A3            42 |__|
//                     |                      |
//                     |______________________|
//
//
//
//
//////////////////////////////////////////////////////////////////////////////
// PIN_A0  40      //
// PIN_A1  41     //
// PIN_A2  42    //
// PIN_A3  43   //
// PIN_A4  44  //
// PIN_A5  45 //
///////////////
//   --------------------- (provient de PIC12F683.h) ------------------------
//////////////////////////////////////////////////////////////////////////////
//////// Program memory: 2048x14  Data RAM: 128  Stack: 8                   //
//////// I/O: 6   Analog Pins: 4                                            //
//////// Data EEPROM: 256                                                   //
//////// C Scratch area: 20   ID Location: 2000                             //
//////// Fuses: LP,XT,HS,EC_IO,NOWDT,WDT,CPD,NOCPD,PROTECT,NOPROTECT,NOMCLR //
//////// Fuses: MCLR,PUT,NOPUT,INTRC_IO,INTRC,RC_IO,RC,BROWNOUT,NOBROWNOUT  //
//////// Fuses: BROWNOUT_NOSL,BROWNOUT_SW,IESO,NOIESO,FCMEN,NOFCMEN         //
////////                                                                    //
//////////////////////////////////////////////////////////////////////////////
// Discrete I/O Functions: SET_TRIS_x(), OUTPUT_x(), INPUT_x(),
//                         PORT_B_PULLUPS(), INPUT(),
//                         OUTPUT_LOW(), OUTPUT_HIGH(),
//                         OUTPUT_FLOAT(), OUTPUT_BIT()
// Constants used to identify pins in the above are:

#include <12f683.h>
#device adc=10  // 10 bit ADC

///////////////////////////////////////////////////////////////////////////
                                                                         //
//#fuses INTRC_IO,nowdt,noprotect,put,brownout,nomclr,nolvp,nocpd        //
//#fuses EC_IO,nowdt,noprotect,put,brownout,nomclr,nocpd                 //
#FUSES INTRC_IO,NOWDT,PUT,NOPROTECT,BROWNOUT,NOMCLR                      //
                                                                         //
// EC_IO = External clock with I/O,                                      //
// intrc = internl RC oscillator                                         //
// NOWDT = no watch dog                                                 //
// NOPROTECT = no code protection program memory                       //
// PUT = power up timer                                               //
// NOMCLR = no master clear                                          //
// NOLVP = no low voltage programming                               //
// NOLVP = No low voltage programming (requires +13V)              //
// nocpd = no code protection data memory                         //
// BROWNOUT = brounout detect                                    //
                                                                //
/////////////////////////////////////////////////////////////////

#use delay (clock=1000000)
#use fast_io(A)

#use rs232 (baud=9600, xmit=PIN_A2,rcv=PIN_A3)


///////////////////////////////////////////////////////////////////
void ini_pic(void);                                              //
void lire_tension_batt(void);                                    //
int8 canal;                                                      //
int16 entree;                                                    //
///////////////////////////////////////////////////////////////////


// ***************** Sous-routines **************************

//////////////////////////////////////////////////////////////////
void ini_pic(void)                                              //
{                                                               //  III
   setup_adc_ports(all_analog|VSS_VDD); // ADC entre Vdd et Vss //   I
   setup_adc(ADC_CLOCK_DIV_16);                                 //   I
                                                                //  III
                                                                //
   setup_comparator(NC_NC);                                     //  N  N
   setup_vref(FALSE);                                           //  NN N
   setup_ccp1(ccp_off);                                         //  N NN
   setup_adc(ADC_OFF);                                          //  N  N
   setup_timer_0(RTCC_INTERNAL);                                //
   setup_timer_1(T1_DISABLED);                                  //  III
   setup_timer_2(T2_DISABLED,0,1);                              //   I
   output_A(0x04);                                              //   I
   set_tris_A(0b11111011);                                      //  III
   disable_interrupts(int_timer0);                              //
   disable_interrupts(global);                                  //  TTT
   setup_oscillator(OSC_1MHZ);                                  //   T
}                                                               //   T
//////////////////////////////////////////////////////////////////   T

/////////////////////////////////////////////////////////////////////////////
void lire_tension_batt(void)                                               //
{                                                                          //
///////////////////////////////////////////// Analog voltage read          //
   set_adc_channel(canal);                 //                              //
   delay_us(100);                          // (0-1023)                     //
   entree=Read_ADC(ADC_START_AND_READ);    //                              //
/////////////////////////////////////////////                              //
       // entree=0-1023;                                                   //
}                                                                          //
/////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void main(void)                                                       /////////
{                                                                     /////////
   int8 canal=0;                                                      /////////
   /*=-=*//*=-=*//*=-=*//*=-=*//*=-=*//*=-=*//*=-=*//*=-=*//*=-=*/    /////////
   ini_pic();                                                         /////////
                                                                      /////////
   delay_ms(20);                                                      /////////
                                                                      /////////
   while(1)                                                           /////////
   {                                                                  /////////
      lire_tension_batt();                                            /////////
      printf("  canal %d",canal);                                     /////////
      printf("--%ld",entree);                                         /////////
      delay_ms(500);                                                  /////////
      canal++;                                                        /////////
      if(canal>3)                                                     /////////
         canal=0;                                                     /////////
   }                                                                  /////////
}                                                                     /////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
Ttelmah
Guest







PostPosted: Thu May 07, 2009 7:37 am     Reply with quote

First thing to do, put back the NOLVP fuse.
Otherwise, if pins AN0, and AN1, are both low, when the power is applied, the chip will go into programming mode...
Obviously, this will be 'random', depending on how the lines are floating, and what the analog signal is doing.

Best Wishes
Guest








PostPosted: Thu May 07, 2009 10:38 am     Reply with quote

Thanks Thelma, but there is no low voltage programming mode in the F683. The "NOLVP" command is not recognized.
PICman



Joined: 02 Nov 2007
Posts: 26

View user's profile Send private message

PostPosted: Thu May 07, 2009 10:39 am     Reply with quote

OOps... forgot to login...

Thanks Thelma, but there is no low voltage programming mode in the F683. The "NOLVP" command is not recognized by the compiler.
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Thu May 07, 2009 11:28 am     Reply with quote

First thing I noticed is, that you read adc on port that you use for RS232.

Try to use this code, and report if it is working.


Code:
#include <12F683.h>
#device adc=10
#FUSES NOWDT, INTRC_IO, NOCPD, NOPROTECT, NOMCLR, PUT, NOBROWNOUT, NOIESO, NOFCMEN
#use delay(clock=8000000)
#define ADC1   PIN_A0
#define ADC1   PIN_A1
#define  TX   PIN_A2
#define  RX   PIN_A3
#use rs232(baud=9600,parity=N,xmit=PIN_A2,rcv=PIN_A3,bits=8)


void setup()
{

   setup_adc_ports(sAN0|sAN1|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC);
   setup_vref(FALSE);
   setup_oscillator(OSC_8MHZ);
 

}

void main()
{
   int16 AdValue;
   int8 Channel;
   
   setup();
   
   
   while(1)
   {
      for (Channel=0;channel>=1;channel++);
      {
         set_adc_channel(Channel);
         delay_us(50);
         AdValue=read_adc();
         printf("Chan: %d=%Lu",Channel,AdValue);
         delay_ms(500);
      }
   }
   
   
}
PICman



Joined: 02 Nov 2007
Posts: 26

View user's profile Send private message

PostPosted: Thu May 07, 2009 4:15 pm     Reply with quote

Thanks a lot Bungee !!!! Your code worked !!!

Now, it's time for me to study your code to know what was my error !

Again, thanks a lot ! (I don't know why, i got a lot of problems dealing with ADC, but with the help of this group, i wil get through it !)
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Thu May 07, 2009 4:19 pm     Reply with quote

I went through your code again ... Look at the marked spot.... Wink


Code:
  setup_adc_ports(all_analog|VSS_VDD); // ADC entre Vdd et Vss //   I
   setup_adc(ADC_CLOCK_DIV_16);                                 //   I
                                                                //  III
                                                                //
   setup_comparator(NC_NC);                                     //  N  N
   setup_vref(FALSE);                                           //  NN N
   setup_ccp1(ccp_off);                                         //  N NN
   setup_adc(ADC_OFF);                                          //  N  N      <-------- HERE
   setup_timer_0(RTCC_INTERNAL);                                //
   setup_timer_1(T1_DISABLED);                                  //  III
   setup_timer_2(T2_DISABLED,0,1);                              //   I
   output_A(0x04);                                              //   I
   set_tris_A(0b11111011);                                      //  III
   disable_interrupts(int_timer0);                              //
grace



Joined: 09 Feb 2016
Posts: 1
Location: Banned - spammer

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 3:13 am     Reply with quote

Sir,
Please can someone tell me the program compiler was used for this program?
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 3:20 am     Reply with quote

The CCS C compiler.

Any version for about the last seven years.

Since this is a forum _for_ CCS C. I wonder what compiler you expect to be being used.....
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 6:10 am     Reply with quote

just a comment...

Your ASCII ART shows a 47K resistor for _MCLR. That's very high. You should use a 4K7r.
If it really is a 47Kr the PIC _might_ randomly reset.
If it is a 4K7r just change your ASCII ART to agree with what you built.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19518

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 8:13 am     Reply with quote

The original thread, was 2009.

'Grace' has just come in asking what compiler it is for.

I doubt if anyone will change art that old!...
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 2:02 pm     Reply with quote

temtronic wrote:
just a comment...

Your ASCII ART shows a 47K resistor for _MCLR. That's very high. You should use a 4K7r.
If it really is a 47Kr the PIC _might_ randomly reset.


Hi Jay,

A 47K pull-up resistor on MCLR with no capacitor is what CCS recommends for use with their ICD-U40 and ICD-U64 Programmer/Debuggers. If this value is 'too high', and might cause the PIC to 'randomly reset', I'd sure appreciate it if you didn't mention that to any of my customers who otherwise seem quite happy! Laughing

I've used that same MCLR setup in all my PIC designs over the years, and have got literally thousands of units in the field with no issues!
_________________
John

If it's worth doing, it's worth doing in real hardware!
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Feb 09, 2016 2:42 pm     Reply with quote

OK, I'll keep quiet....
it was a 'gut' response to the ASCII ART..it just 'seemed' wrong, probably a leftover from my 7400 dayze.....

Jay
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