[chbot] ADC on the DSPic

Carl Ranson chchrobotics@lists.linuxnut.co.nz
Wed, 27 Sep 2006 00:43:29 +1200


After switching to the model of waiting to see the interrupt flag I've 
discovered the problem.
It was set up to do an iterrupt after every conversion. This meant it 
was doing one and then stopping.
What I didn't realise is that starting it sampling again causes it to 
reset at the first input rather than cycling to the next available one.

Switching it to interrupt after every 4 means I see the four values in 
ADCBuff0-ADCBuff3 as expected.

Next step is to transform the values to a real distance and get it to 
indicate which is the closest.
cheers
CR


Michael Pearce wrote:

>Sorry about the delay responding - had to rebuild a computer 
>first.
>
>Following is the code I used to do single samples off the 
>ADC in a dsPIC30F
>
>Not the fastest, but I didn't need speed for that particular 
>project as the display only updated every 100ms or more.
>
>Hope this helps.
>
>Mike
>
>
>#ifndef _ADC12_H
>#define _ADC12_H
>/***************************************************
>*                      adc12.h
>***************************************************/
>
>extern void adc12_init(int adcio,int ref);
>extern int adc12_read(int channel);
>
>
>#define ADC12_REF_INT      0
>#define ADC12_REF_EXT_POS  1
>#define ADC12_REF_EXT_NEG  2
>#define ADC12_REF_EXT_BOTH 3
>
>#endif
>
>
>/**********************************************
>*                   adc12.c
>*
>* Simple Single convert functions for 12 Bit ADC
>*
>* Target: DSPIC30Fxxxx with 12 Bit ADC
>*
>* Started: 17 Jan 2006
>*
>* Author: Michael Pearce <mike@kiwacan.co.nz>
>*
>***********************************************
>*     VERSION  INFORMATION
>***********************************************
>* V0.0.0 - 17 Jan 2006
>**********************************************/
>#include <p30Fxxxx.h>
>#include "adc12.h"
>
>
>
>/**********************************************
>*              adc12_init
>**********************************************/
>void adc12_init(int adcio,int ref)
>{
> ADPCFG=adcio;    /* Setup ADC I/O ports */
> ADCON1=0x00E0;   
> ADCSSL=0x0000;  
> ADCON3=0x0F010; /* Sample time 15Tad, Tad =internal Tcy/2*/
>
> ADCON2=0x2000;  /* External + Ref, Interrupt every sample*/
>
> switch(ref)
> {
>  default:
>  case ADC12_REF_INT:
>   break;
>
>  case ADC12_REF_EXT_POS: 
>   ADCON2 |= 0x2000;
>   break;
>
>  case ADC12_REF_EXT_NEG:
>   ADCON2 |= 0x4000;
>   break;
>
>  case ADC12_REF_EXT_BOTH:
>   ADCON2 |= 0x6000;
>   break;
> }
>
>
> ADCHS=0x0000;
> ADCON1bits.ADON=1;
>}
>
>/**********************************************
>*              adc12_read
>* Take a single sample
>**********************************************/
>int adc12_read(int channel)
>{
> int ADCValue,*ADC16Ptr,count;
>
> ADCHS=channel;         /* Select Channel */
> 
> IFS0bits.ADIF=0;       /* Clear Interrupt Flag */
>
> ADCON1bits.ASAM = 1;   /* Start Sample & convert */
> while(!IFS0bits.ADIF); /* Wait for completion */
> ADCON1bits.ASAM=0;     /* Stop sample/convert */
> 
> return(ADCBUF0);
>
>}
>
>
>
>
>
>
>
>
>
>  
>