[chbot] ADC on the DSPic

Michael Pearce chchrobotics@lists.linuxnut.co.nz
Tue, 26 Sep 2006 21:44:18 +1200


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);

}









-- 
KIWACAN 
Electronics Design
Concept to Production

Mobile:  +64-21-249-5674
Phone:   +64-3-347-3311
Fax:     +64-3-347-2123
Website: http://www.kiwacan.co.nz