/**********************************************************************************
* ADC_driver.c
* ADC module for the K70 ARM cortex M4 MCU @120MHz
* Isaiah Ryan, Corey Williamson, James Kintzele, Dustin Sjoerdsma 03/06/2014
*
* Modified by: Joshua Goertz and Dylan Kleist, 5/19/2015
***********************************************************************************
* Include Project Header File
**********************************************************************************/
#include "ADC_driver.h"
/**********************************************************************************
* Defines
**********************************************************************************/
#define TRUE 1
#define FALSE 0

#define ADC_CFG2_MUXSEL_MASK                     0x10u
#define ADC_CFG2_MUXSEL_SHIFT                    4
#define ADC_CFG2_MUXSEL(x)                       (((uint32_t)(((uint32_t)(x))<<ADC_CFG2_MUXSEL_SHIFT))&ADC_CFG2_MUXSEL_MASK)
/**********************************************************************************
* Public Resources
**********************************************************************************/
void ADCVariableInit(void);
void ADCInit(INT8U ADCnum);
INT32U ADCReadSample(INT32U Read_Channel);
void ADCStartSample(INT32U Sample_Channel);

/**********************************************************************************
* ADC Channel settings -  Set these values according to your ADC source settings
* 	ADC Number = Set to device number (0, 1, 2, 3)
* 	MUXSEL     = Mux select( 0 = Channel a, 1 = Channel b)
* 	ADCH       = Adc channel(Single Ended 4 - 23)
*
**********************************************************************************/

const INT32U Chan_setup[20][3] = { //[Source Number][ADC Number, MUXSEL, ADCH]
 {0, 0, 4} ,   /*  Channel 0 */
 {0, 0, 5} ,   /*  Channel 1 */
 {0, 0, 6} ,   /*  Channel 2 */
 {0, 0, 7} ,   /*  Channel 3 */
 {0, 0, 8} ,   /*  Channel 4 */
 {0, 0, 9} ,   /*  Channel 5 */
 {0, 0, 10},   /*  Channel 6 */
 {0, 0, 11},   /*  Channel 7 */
 {1, 0, 4} ,   /*  Channel 8 */
 {1, 0, 5} ,   /*  Channel 9 */
 {1, 0, 6} ,   /*  Channel 10 */
 {1, 0, 7} ,   /*  Channel 11 */
 {1, 0, 10},   /*  Channel 12 */
 {1, 0, 11},   /*  Channel 13 */
 {1, 0, 12},   /*  Channel 14 */
 {1, 0, 13},   /*  Channel 15 */
 {3, 1, 4} ,   /*  Channel 16 */
 {3, 1, 5} ,   /*  Channel 17 */
 {3, 0, 6} ,   /*  Channel 18 */
 {3, 0, 7}     /*  Channel 19 */
};

/******************************Function Definitions*******************************/
/**********************************************************************************
* ADCVariableInit()
* Description:  Initializes variables for the available MCU ADCs. Must be run
* prior to ADCInit(INT8U ADCnum).
*
**********************************************************************************/
//void ADCVariableInit(void){
//
//	tADC_Config adc0_config;  //structure holding configuration registers for ADC0
//	tADC_Config adc1_config;  //structure holding configuration registers for ADC1
//	tADC_Config adc3_config;  //structure holding configuration registers for ADC3
//	tADC_Config adc2_config;  //structure holding configuration registers for ADC2
//
//}
/**********************************************************************************
* ADCInit()
* Description: Performs intialization procedure for specified ADC. Uses pre-written
               module adc16v1 for access to structures and functions used for help
               in initialization.
* Written by: Isaiah Ryan
* Edited  by: Joshua Goertz and Dylan Kleist
* Changes:     Performs initialization for a specific ADC device. Defaults to
*              channel 4, clock speed defaults to 7.5MHz.
*
**********************************************************************************/
void ADCInit(INT8U ADCnum) {

	if(ADCnum == 0){
	    SIM_SCGC6 |= SIM_SCGC6_ADC0_MASK;  //enable clock for ADC0
		tADC_Config adc0_config;  //structure holding configuration registers for ADC0
	    /*configure control registers for ADC1*/
	    //7.5MHz clk, 16-bit Single Ended conversion mode
	    adc0_config.CONFIG1 = ADC_CFG1_ADIV(2)
	    		            | ADC_CFG1_MODE(3)
	    		            | ADC_CFG1_ADICLK(1);
	    adc0_config.CONFIG2 = 0x00;
	    adc0_config.COMPARE1 = 0x0000;
	    adc0_config.COMPARE2 = 0x0000;
	    adc0_config.STATUS2 = ADC_SC2_REFSEL(0);  //use default 3.3v range for ADC
	    adc0_config.STATUS3 = 0x00;
	    adc0_config.STATUS1A = ADC_SC1_ADCH(4);  //channel 4(Default), no interrupt
	    adc0_config.STATUS1B = 0x00;
	    adc0_config.PGA = 0x00000000;


	    //STORE VALUES/CALIBRATE
	    ADC_Config_Alt(ADC0_BASE_PTR, &adc0_config);//Store settings

	    ADC_Cal(ADC0_BASE_PTR);  //calibrate ADC0, device ready following calibration

	}else if(ADCnum == 1){
	    SIM_SCGC3 |= SIM_SCGC3_ADC1_MASK;  //enable clock for ADC1
		tADC_Config adc1_config;
	    /*configure control registers for ADC1*/
	    //7.5MHz clk, 16-bit Single Ended conversion mode
	    adc1_config.CONFIG1 = ADC_CFG1_ADIV(2)
	    		            | ADC_CFG1_MODE(3)
	    		            | ADC_CFG1_ADICLK(1);
	    adc1_config.CONFIG2 = 0x00;
	    adc1_config.COMPARE1 = 0x0000;
	    adc1_config.COMPARE2 = 0x0000;
	    adc1_config.STATUS2 = ADC_SC2_REFSEL(0);  //use default 3.3v range for ADC
	    adc1_config.STATUS3 = 0x00;
	    adc1_config.STATUS1A = ADC_SC1_ADCH(4);  //channel 4(Default), no interrupt
	    adc1_config.STATUS1B = 0x00;
	    adc1_config.PGA = 0x00000000;

	    ADC_Config_Alt(ADC1_BASE_PTR, &adc1_config);//Store settings

	    ADC_Cal(ADC1_BASE_PTR);  //calibrate ADC1, device ready following calibration

	}else if(ADCnum == 2){
	    SIM_SCGC6 |= SIM_SCGC6_ADC2_MASK;  //enable clock for ADC2
		tADC_Config adc2_config;
		/*configure control registers for ADC2*/
	    //7.5MHz clk, 16-bit Single Ended conversion mode
	    adc2_config.CONFIG1 = ADC_CFG1_ADIV(2)
	    		            | ADC_CFG1_MODE(3)
	    		            | ADC_CFG1_ADICLK(1);
	    adc2_config.CONFIG2 = 0x00;
	    adc2_config.COMPARE1 = 0x0000;
	    adc2_config.COMPARE2 = 0x0000;
	    adc2_config.STATUS2 = ADC_SC2_REFSEL(0);  //use default 3.3v range for ADC
	    adc2_config.STATUS3 = 0x00;
	    adc2_config.STATUS1A = ADC_SC1_ADCH(4);  //channel 4(Default), no interrupt
	    adc2_config.STATUS1B = 0x00;
	    adc2_config.PGA = 0x00000000;

	    ADC_Config_Alt(ADC2_BASE_PTR, &adc2_config);//Store settings

	    ADC_Cal(ADC2_BASE_PTR);  //calibrate ADC2, device ready following calibration

	}else if(ADCnum == 3){
	    SIM_SCGC3 |= SIM_SCGC3_ADC3_MASK;  //enable clock for ADC3
		tADC_Config adc3_config;
		/*configure control registers for ADC3*/
	    //7.5MHz clk, 16-bit Single Ended conversion mode
	    adc3_config.CONFIG1 = ADC_CFG1_ADIV(2)
	    		            | ADC_CFG1_MODE(3)
	    		            | ADC_CFG1_ADICLK(1);
	    adc3_config.CONFIG2 = 0x00;
	    adc3_config.COMPARE1 = 0x0000;
	    adc3_config.COMPARE2 = 0x0000;
	    adc3_config.STATUS2 = ADC_SC2_REFSEL(0);  //use default 3.3v range for ADC
	    adc3_config.STATUS3 = 0x00;
	    adc3_config.STATUS1A = ADC_SC1_ADCH(4);  //channel 4(Default), no interrupt
	    adc3_config.STATUS1B = 0x00;
	    adc3_config.PGA = 0x00000000;

	    ADC_Config_Alt(ADC3_BASE_PTR, &adc3_config);//Store settings

	    ADC_Cal(ADC3_BASE_PTR);  //calibrate ADC3, device ready following calibration

	}else{
		/*do nothing*/
	}

}
/**********************************************************************************
* ADCReadSample() - Needs access to const INT32U Chan_setup[][];
*
**********************************************************************************/
INT32U ADCReadSample(INT32U read_channel){//Needs a pointer to "Chan_setup" not access to the constant
	INT32U read_device;
	INT32U result;

	read_device = Chan_setup [read_channel][0];

	if(read_device == 0){
		while ((ADC0_SC1A & ADC_SC1_COCO_MASK) == 0) { /*wait for conversion complete flag*/ }
		result = ADC0_RA;
	}else if(read_device == 1){
		while ((ADC1_SC1A & ADC_SC1_COCO_MASK) == 0) { /*wait for conversion complete flag*/ }
		result = ADC1_RA;
	}else if(read_device == 2){
		while ((ADC2_SC1A & ADC_SC1_COCO_MASK) == 0) { /*wait for conversion complete flag*/ }
		result = ADC2_RA;
	}else if(read_device == 3){
		while ((ADC3_SC1A & ADC_SC1_COCO_MASK) == 0) { /*wait for conversion complete flag*/ }
		result = ADC3_RA;
	}else{
		result = 0;
	}

	return(result);
}

/**********************************************************************************
* ADCStartSample() - Any device can be sampled, if the device has not been
* initialized, a sample will not take place, and the results will be invalid
*
*
**********************************************************************************/
void ADCStartSample(INT32U sample_channel){//Needs a pointer to "Chan_setup" not access to the constant
	INT32U sample_device;
	INT32U sample_mux;
	INT32U sample_source;

	sample_device = Chan_setup [sample_channel][0];
	sample_mux = Chan_setup [sample_channel][1];
	sample_source = Chan_setup [sample_channel][2];

	if(sample_device == 0){
		ADC0_CFG2 = ((ADC0_CFG2 & ~ADC_CFG2_MUXSEL_MASK)
				  | ADC_CFG2_MUXSEL(sample_mux));
		ADC0_SC1A = ADC_SC1_ADCH(sample_source);
	}else if(sample_device == 1){
		ADC1_CFG2 = ((ADC1_CFG2 & ~ADC_CFG2_MUXSEL_MASK)
				  | ADC_CFG2_MUXSEL(sample_mux));
		ADC1_SC1A = ADC_SC1_ADCH(sample_source);
	}else if(sample_device == 2){
		ADC2_CFG2 = ((ADC2_CFG2 & ~ADC_CFG2_MUXSEL_MASK)
				  | ADC_CFG2_MUXSEL(sample_mux));
		ADC2_SC1A = ADC_SC1_ADCH(sample_source);
	}else if(sample_device == 3){
		ADC3_CFG2 = ((ADC3_CFG2 & ~ADC_CFG2_MUXSEL_MASK)
				  | ADC_CFG2_MUXSEL(sample_mux));
		ADC3_SC1A = ADC_SC1_ADCH(sample_source);
	}else{
		/* Invalid selection, do nothing */
	}

}

Previous Page