Chapter 11 It’s an Analog World Learning to use the Analog to Digital Converter Analog to Digital Converter Di Jasio - Programming 32-bit Microcontrollers in C Reading a Potentiometer Di Jasio - Programming 32-bit Microcontrollers in C First Conversion int readADC( int ch) { AD1CHSbits.CH0SA = ch; AD1CON1bits.SAMP = 1; T1CON = 0x8000; TMR1 = 0; while (TMR1 < 100); AD1CON1bits.SAMP = 0; while (!AD1CON1bits.DONE); return ADC1BUF0; } // readADC Di Jasio - Programming 32-bit Microcontrollers in C // // // // // // // 1. select analog input 2. start sampling 3. wait for sampling time 4. start the conversion 5. wait conversion complete 6. read result Automating Sample Timing void initADC( int amask) { AD1PCFG = amask; // select analog input pins AD1CON1 = 0x00E0; // automatic conversion after sampling AD1CSSL = 0; // no scanning required AD1CON2 = 0; // use MUXA, use AVdd & AVss as Vref+/AD1CON3 = 0x1F3F; // Tsamp = 32 x Tad; AD1CON1bits.ADON = 1; // turn on the ADC } //initADC int readADC( int ch) { AD1CHSbits.CH0SA = ch; AD1CON1bits.SAMP = 1; while (!AD1CON1bits.DONE); return ADC1BUF0; } // readADC Di Jasio - Programming 32-bit Microcontrollers in C // // // // 1. 2. 3. 4. select input channel start sampling wait conversion complete read conversion result First Example: Pot-Man /* ** Pot-Man.c ** */ // configuration bit settings, Fcy=72MHz, Fpb=36MHz #pragma config POSCMOD=XT, FNOSC=PRIPLL #pragma config FPLLIDIV=DIV_2, FPLLMUL=MUL_18, #pragma config FPLLODIV=DIV_1 #pragma config FPBDIV=DIV_2, FWDTEN=OFF, CP=OFF, BWP=OFF #include #include #include #include <p32xxxx.h> <explore.h> <LCD.h> <ADC.h> main () { int a, r, p, n; // 1. initializations initLCD(); initADC( AINPUTS); // 2. use the first reading to randomize srand( readADC( POT)); // 3. init the hungry Pac p = '<'; // 4. generate the first random food bit position r = rand() % 16; Di Jasio - Programming 32-bit Microcontrollers in C // main loop while( 1) { // 5. select the POT input and convert a = readADC( POT); // 6. reduce the 10-bit result to a 4 bit value (0..15) // (divide by 64 or shift right 6 times a >>= 6; // 7. turn the Pac in the direction of movement if ( a < n) // moving to the left p = '>'; if ( a > n) // moving to the right p = '<'; // 8. when the Pac eats the food, generate more food while (a == r ) r = rand() % 16; // 9. update display clrLCD(); setLCDC( a); putLCD( p); setLCDC( r); putLCD( '*'); // 10. provide timing and relative position Delayms( 200); // limit game speed n = a; // memorize previous position } // main loop } // main Sensing Temperature Temperature Sensor connection on Explorer16 board (DS51589) Di Jasio - Programming 32-bit Microcontrollers in C TC1047 Output Voltage vs. Temperature characteristics Reading Temperature Averaged temperature reading procedure: a = 0; for( j=0; j <10; j++) a += readADC( TSENS); i = a / 10; // add up 10 readings // divide by 10 to average Resolving the temperature/voltage equation for T: T = (Vout - 500 mV) / 10 mV/C Where Vout = ADC reading * ADC resolution (3.3 mV/bit) T = (( i * 3.3) - 500) / 10; Di Jasio - Programming 32-bit Microcontrollers in C Second Example: Temp-Man main () { int a, i, j, n, r, p; // 1. initializations initADC( AINPUTS); // initialize the ADC initLCD(); // 2. use the first reading to randomize srand( readADC( TSENS)); // generate the first random position r = rand() % 16; p = '<'; // 3. compute initial average value a = 0; for ( j=0; j<10; j++) { a += readADC( TSENS); // read temp Delayms( 100); } i = a / 10; // average // main loop while( 1) { // 4. take the average value over 1 second a = 0; for ( j=0; j<10; j++) { a += readADC( TSENS); // read the temperature Delayms( 100); } a /= 10; // average result // 5. compare initial reading, move the Pac a = 7 + (a - i); // 6. keep the result in the value range 0..15 if ( a > 15) a = 15; if ( a < 0) a = 0; // 7. turn the Pac in the direction of movement if ( a < n) // moving to the left p = '>'; if ( a > n) // moving to the right p = '<'; // 8. as soon as the Pac eats the food, generate new while (a == r ) r = rand() % 16; // 9. update display clrLCD(); setLCDC( r); putLCD( '*'); setLCDC( a); putLCD( p); // 10. remember previous postion n = a; Di Jasio - Programming 32-bit Microcontrollers in C } // main loop } // main