1 / 9

Chapter 11 It ’ s an Analog World

Chapter 11 It ’ s an Analog World. Learning to use the Analog to Digital Converter. Analog to Digital Converter. Reading a Potentiometer. First Conversion. int readADC( int ch) { AD1CHSbits.CH0SA = ch; // 1. select analog input

lluvia
Download Presentation

Chapter 11 It ’ s an Analog World

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 11 It’s an Analog World Learning to use the Analog to Digital Converter

  2. Analog to Digital Converter

  3. Reading a Potentiometer

  4. First Conversion int readADC( int ch) { AD1CHSbits.CH0SA = ch; // 1. select analog input AD1CON1bits.SAMP = 1; // 2. start sampling T1CON = 0x8000; TMR1 = 0; // 3. wait for sampling time while (TMR1 < 100); // AD1CON1bits.SAMP = 0; // 4. start the conversion while (!AD1CON1bits.DONE); // 5. wait conversion complete return ADC1BUF0; // 6. read result } // readADC

  5. 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; // 1. select input channel AD1CON1bits.SAMP = 1; // 2. start sampling while (!AD1CON1bits.DONE); // 3. wait conversion complete return ADC1BUF0; // 4. read conversion result } // readADC

  6. 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 <p32xxxx.h> #include <explore.h> #include <LCD.h> #include <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; // 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

  7. Sensing Temperature Temperature Sensor connection on Explorer16 board (DS51589) TC1047 Output Voltage vs. Temperature characteristics

  8. Reading Temperature Averaged temperature reading procedure: a = 0; for( j=0; j <10; j++) a += readADC( TSENS); // add up 10 readings i = a / 10; // 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;

  9. Second Example: Temp-Man // 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; } // main loop } // main 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

More Related