00001 00053 #include <includes.h> 00054 00055 #define ADCX_BUSY (SSPSR & (1<<4)) 00056 #define ADCX_TNF (SSPSR & (1<<1)) 00057 #define ADCX_RNE (SSPSR & (1<<2)) 00059 // Variables 00060 static volatile int adcx_index = 0; 00061 static int adcx_num_configs = 0; 00062 static ADCX_CHAN_CFG adcx_configs[16]; 00063 static signed short adcx_results[16]; //re-precated! for direct access to results 00064 00065 static volatile int adcx_status = ADCX_DONE; 00066 static volatile int adcx_wait_count = 0; 00068 static int adcx_count = 0; 00069 00075 void adcx_isr(void) __irq 00076 { 00077 unsigned char msb, lsb; 00078 int chan; 00079 signed short result; 00080 ADCX_CHAN_CFG * cfg; 00081 00082 EXTINT = (1<<1); // Clear External Interrupt flag; 00083 00084 if (adcx_num_configs != 0) { 00085 // ****** READ N-1th RESULT ****** // 00086 while (ADCX_RNE){ // If the Rx buffer is Not Empty, read data 00087 adcx_read_buffer(); //Clear out junk from command 00088 } 00089 msb = adcx_register_read(1); 00090 lsb = adcx_register_read(0); 00091 result = ((signed short)((((unsigned short)msb)<<8)+(unsigned short)lsb))>>2; //Convert (SIGNED data) to (14 bits, right justified) 00092 00093 // ****** SAVE RESULT ****** // 00094 cfg = &(adcx_configs[adcx_index]); 00095 chan = cfg->chan; 00096 adcx_results[chan] = result; 00097 00098 // ****** SEND Nth CONVERSION ****** // 00099 adcx_index = adcx_index + 1; 00100 adcx_convert_next(); 00101 } 00102 VICVectAddr = 0; //indicates end of interrupt service 00103 } 00104 00108 void adcx_convert_all(void){ 00109 if (adcx_status != ADCX_DONE){ 00110 error_occurred(ERROR_ADCX_DNF); 00111 } else { 00112 adcx_index = 0; 00113 //Start first conversion 00114 adcx_convert_next(); 00115 } 00116 00117 } 00118 00122 void adcx_convert_next(void){ 00123 ADCX_CHAN_CFG * cfg; 00124 if(adcx_index >= adcx_num_configs){ 00125 adcx_status = ADCX_DONE; //no more configs to convert 00126 cfg = NULL; 00127 adcx_count = 0; 00128 } else { 00129 adcx_status = ADCX_NOT_DONE; 00130 cfg = &(adcx_configs[adcx_index]); 00131 } 00132 if(cfg != NULL) adcx_convert_cfg(cfg); 00133 } 00134 00138 void adcx_convert_cfg(ADCX_CHAN_CFG * config) 00139 { 00140 adcx_count++; 00141 adcx_register_write(4, (1<<7)|(config->gain<<4)|(config->chan)); 00142 } 00143 00148 void adcx_write(unsigned short data){ 00149 if (!(ADCX_TNF)){ //Transmit buffer full; TNF (Transmit not full) bit 1 00150 error_occurred(ERROR_ADCX_TX_FULL); 00151 } 00152 SSPDR = data; 00153 } 00154 00160 void adcx_register_write(unsigned char reg, unsigned char data){ 00161 short command = (reg<<8)|data; 00162 while(!(ADCX_TNF)){} // wait if transmit buffer is full 00163 SSPDR = command; // register mode, write, 16 bit, big endian 00164 } 00165 00171 unsigned char adcx_register_read(unsigned char reg){ 00172 unsigned short read_in; 00173 while (!(ADCX_TNF)){} 00174 SSPDR = ((1<<6)|reg)<<8; //16 bits reg read instuction 00175 while (ADCX_BUSY){} 00176 read_in = SSPDR; 00177 return (unsigned char)(read_in & 0x00FF); 00178 } 00179 00184 unsigned short adcx_read_buffer(void){ 00185 unsigned short temp = 0; 00186 if (ADCX_RNE){ // If the Rx buffer is Not Empty (RNE), read data 00187 temp = SSPDR; 00188 } else { 00189 error_occurred(ERROR_ADCX_RX_EMPTY); 00190 } 00191 return temp; 00192 } 00193 00199 signed short adcx_get_result(char channel) 00200 { 00201 return adcx_results[channel]; 00202 } 00203 00208 void adcx_init(void) 00209 { 00210 // Setup ADC registers 00211 adcx_register_write(24,0x00); // SPI: MSB first, 3-wire mode, DIN stuff 00212 adcx_register_write(3,0x00); // 2s-comp output, manual readback, CCLK div = 1 00213 adcx_register_write(7,0x00); // Vref=2.5V, buffer and ref powered down (using external 5V reference) 00214 while(ADCX_BUSY){} //Wait for data to be sent 00215 while(ADCX_RNE){ //Read returning dummy byte to clear out buffer. 00216 adcx_read_buffer(); 00217 } 00218 } 00219 00225 void adcx_add_config(unsigned char channel, unsigned char gain) { 00226 if(adcx_num_configs<16) { 00227 adcx_configs[adcx_num_configs].chan = channel; 00228 adcx_configs[adcx_num_configs].gain = gain; 00229 adcx_num_configs++; 00230 } else { 00231 error_occurred(ERROR_ADCX_NUM_CFGS); 00232 } 00233 } 00234 00238 void adcx_conversion_wait(void){ 00239 adcx_wait_count = 0; 00240 while(adcx_status == ADCX_NOT_DONE) { 00241 adcx_wait_count++; 00242 } 00243 }
1.6.3