00001 /* 00002 00003 adcx.h 00004 00005 Nicolas Williamson and Thomas Craig - Summer 2009 00006 00007 */ 00008 00009 #ifndef __H_ADC_EXTERNAL__ 00010 #define __H_ADC_EXTERNAL__ 00011 00012 // Gains for adcx_convert function 00013 #define ADCX_GAIN_1 0 00014 #define ADCX_GAIN_2 1 00015 #define ADCX_GAIN_4 2 00016 #define ADCX_GAIN_5 3 00017 #define ADCX_GAIN_8 4 00018 #define ADCX_GAIN_10 5 00019 #define ADCX_GAIN_16 6 00020 #define ADCX_GAIN_20 7 00021 00022 // Channels to read in from 00023 #define ADCX_CH_0 8 00024 #define ADCX_CH_1 9 00025 #define ADCX_CH_2 10 00026 #define ADCX_CH_3 11 00027 #define ADCX_CH_4 12 00028 #define ADCX_CH_5 13 00029 #define ADCX_CH_6 14 00030 #define ADCX_CH_7 15 00031 // Differential channels to read in from: 00032 // 'P' is positive channel, and 'N' is negative channel 00033 // Ex. 'ADCX_CH_P1N2' uses positive ch1 and negative ch2 for the differential 00034 #define ADCX_CH_P0N1 0 00035 #define ADCX_CH_P2N3 1 00036 #define ADCX_CH_P4N5 2 00037 #define ADCX_CH_P6N7 3 00038 #define ADCX_CH_P1N0 4 00039 #define ADCX_CH_P3N2 5 00040 #define ADCX_CH_P5N4 6 00041 #define ADCX_CH_P7N6 7 00042 00043 //Flags 00044 #define ADCX_NOT_DONE (1) 00045 #define ADCX_DONE (2) 00046 //#define ADCX_FINISHING 3 00047 00051 typedef struct adcx_channel_config { 00052 unsigned char chan; 00053 unsigned char gain; 00054 } ADCX_CHAN_CFG; 00055 00056 //Functions 00057 void adcx_isr(void) __irq; 00058 00059 void adcx_add_config(unsigned char channel, unsigned char gain); 00060 void adcx_init(void); 00061 signed short adcx_get_result(char channel); 00062 00063 void adcx_conversion_wait(void); 00064 void adcx_convert_all(void); 00065 00066 void adcx_convert_next(void); 00067 void adcx_convert_cfg(ADCX_CHAN_CFG * config); 00068 00069 unsigned char adcx_register_read(unsigned char reg); 00070 void adcx_register_write(unsigned char reg, unsigned char data); 00071 void adcx_write(unsigned short data); 00072 unsigned short adcx_read_buffer(void); 00073 00074 /* HARDWARE SETUP 00075 // ******************************************************************************* 00076 // Initialize SSP/SPI1 For External ADC 00077 // ******************************************************************************* 00078 PCONP &= ~(1<<10); // power setting: disable SPI1 (bit 10) 00079 PCONP |= (1<<21); // power setting: enable SSP (bit 21!!!!!!!!!!!!!!!!!!!!!!) 00080 SSPCR1 = 0; // Disable SSP to allow setting changes 00081 SSPCR0 = 0x00000000; 00082 SSPCR0 = (15<<0) // data size: 16 bits (bits 0-3 = 15 means 16bit data) 00083 | (1<<6) // sclk high when idle 00084 | (1<<7) // sample on the second edge (rising edge) 00085 | (2<<8); // bit frequency = PCLK/(CPSDVSR*(SCR+1)) = 00086 // SSPCR0 &= ~(3<<4); // SPI mode (bits 4-5 = 0) 00087 // SSPCPSR &= ~(3<<0); //clear prescale divider 00088 SSPCPSR = (2<<0); // prescale divider 00089 PINSEL1 &= ~0x3FC; // clear P0.17~P0.20; 00090 // PINSEL1 |= (2<<2)|(2<<4)|(2<<6); // P0.17: SCK1, P0.18: MISO1, P0.19: MOSI1, P0.20: Manual SSEL 00091 PINSEL1 |= (2<<2)|(2<<4)|(2<<6)|(2<<8); // P0.17: SCK1, P0.18: MISO1, P0.19: MOSI1, P0.20: auto SSEL 00092 // FIO0DIR |= (1<<20); //SSEL is output 00093 // FIO0CLR = (1<<20); //SSEL low 00094 SSPCR1 = (1<<1); // enable SSP/SPI1 00095 PINSEL0 &= ~(3<<24); //ADC reset line set for GPIO operation (P0.12 bits 24/25) 00096 PINSEL2 &= ~(1<<3); // set trace port for GPIO use (bit 3 = 0) 00097 //Should happen automatically at startup, but just in case... 00098 //GPIO P1_16 is the ADC convert line 00099 FIO0DIR |= (1<<12); // set P0_12 (ADC reset) to be a digital output 00100 FIO1DIR |= (1<<16); // set P1_16 (ADC convert) to be a digital output 00101 FIO0SET = (1<<12); //set ADC reset line high 00102 FIO1CLR = (1<<16); //set ADC convert line low 00103 // adc_external interrupts 00104 PINSEL0 &=~(3<<6); 00105 PINSEL0 |= (3<<6); // Set Pin3 to EINT1 (ADCBUSY line) 00106 EXTMODE = (EXTMODE|(1<<1)) & ~EXTMODE_RB; // Set EINT1 (Bit 1) to be edge-sensitive 00107 EXTPOLAR &= ~(1<<1)&0x0F; // Set EINT1 to be sensitive on the falling-edge 00108 EXTINT = 0x0F;//1; 00109 */ 00110 00111 #endif // __H_ADC_EXTERNAL__