1 / 19

Chapter 10 It ’ s an Analog World

Learn how to convert analog signals from a potentiometer and temperature sensor using the Analog to Digital Converter (ADC). Explore various applications and games in the analog world.

Download Presentation

Chapter 10 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 10 It’s an Analog World

  2. Checklist The following tools will be used in this lesson: • MPLAB X, Integrated Development Environment (v1.8 or later, free) • MPLAB XC16, C compiler (v1.11 or later, free) The following pieces of documentation will be used during this lesson: • PIC24FJ128GA010 Datasheet –DS39747 (latest rev.) Make sure they are available and/or installed and ready to use on your computer. You can download them from Microchip web site at: http://www.microchip.com/mplabx And http://www.microchip.com/xc16 Additional Hardware Tools: • Explorer 16/32 evaluation board • or equivalent board with 2x16 alphanumeric display (HD44780 compatible) • ICD3 In circuit Programmer Debugger (or ICD4, Real-ICE)

  3. The Analog to Digital Converter Sample & Hold

  4. Explorer 16 Potentiometer /* ** It's an analog world ** Converting the analog signal from a potentiometer */ #include <config.h> #define POT_CH 5 // 10k potentiometer on pin AN5 #define POTMASK 0xffdf // AN5 = analog inputs

  5. Sampling and Conversion Select Channel 1) Selected input pin is connected to S&H capacitor Sampling 2) S&H capacitor charges up (delay) Conversion 3) Input is disconnected and Conversion begins (delay) Result Available 4) Conversion is completed, the result is available

  6. Initializing the ADC void InitADC( int amask) { AD1PCFG = amask; // select analog input pins AD1CON1 = 0; // auto-convert after end of sampling AD1CSSL = 0; // no scanning required AD1CON2 = 0; // use MUXA, AVss and AVdd used as Vref AD1CON3 = 0x1F01; // sample time=31Tad, Tad=2xTcy=125ns AD1CON1bits.ADON = 1; // turn on the ADC } // InitADC NOTE: There is a published errata for the “original” Explorer16 board. Depending on the specific model of LCD display installed there is a potential leak through pin RB15 that can impact the performance of the A/D converter. The simplest work around consist in making sure that such pin is configured as an output so that the voltage at the pin is limited to one of the supply rails: 3V or ground. This can be accomplished adding the following two lines of code to the InitADC() function: _LATB15 = 0; _TRISB15 = 0;

  7. Conversion with Timed Sampling int ReadADC( int ch ) { AD1CHS = ch; // select analog input channel // start sampling, automatic conversion will follow AD1CON1bits.SAMP = 1; while (!AD1CON1bits.DONE); // wait to complete conversion return ADC1BUF0; // read the conversion result } // ReadADC • Since we initialized the ADC for “auto-conversion” after sampling in InitADC(), we can start the sampling process (SAMP) and then wait directly for the conversion complete flag (DONE).

  8. Testing the ADC main() { int pot, pos; // initializations InitADC( POTMASK ); // initialize the ADC and analog inputs TRISA = 0xff00; // all PORTA pins as outputs (LED 0..7) // main loop while( 1) { pot = ReadADC( POT_CH ) ; // select the POT input, convert // reduce the 10-bit result to a 3 bit value (0..7) // (divide by 128 or shift right 7 times) pos = pot >> 7; // turn on only the corresponding LED // 0 -> leftmost LED.... 7-> rightmost LED PORTA = (0x80 >> pos); } // main loop } // main

  9. A Delay Routine void Delayms( unsigned t) { T1CON = 0x8000; // enable tmr1, Tcy, 1:1 while (t--) // wait for t (msec) { TMR1 = 0; while ( TMR1 < (FCY/1000)); // wait 1ms } } // Delayms • This will come handy as we will design a couple of simple games next.

  10. Whack a Mole Game #include <EX16.h> #include <config.h> main () { int pot, pos, mole, mallet, c; // initializations TRISA = 0xff00; // select all PORTA pins as outputs // initialize ADC and analog inputs InitADC( POTMASK); // use the first reading to randomize the number generator srand( ReadADC( POT_CH)); // generate the first random position mole = 0x80 >> (rand() & 0x7); // init the counter c = 0; // main loop while( 1) { // select the POT input and convert pot = ReadADC( POT_CH); // reduce the 10-bit result to a 3 bit value (0..7) // (divide by 128 or shift right 7 times pos = pot >> 7; // turn on only the corresponding LED // 0 -> leftmost LED.... 7-> rigtmost LED mallet = (0x80 >> pos); // when the cursor hits the mole LED, // generate new one while (mallet == mole ) mole = 0x80 >> (rand() & 0x7); // display the user LED and 50% dim random LED if ((c & 0xf) == 0) PORTA = mallet + mole; else PORTA = mallet ; // counter to alternate loops c++; } // main loop } // main

  11. Measuring Temperature TC1047 Output Voltage vs. Temperature

  12. * ** Temp.c ** Converting analog signal from a TC1047 Temperature Sensor */ #include <EX16.h> #include <config.h> #define TEMP_CH 4 // ch 4 = TC1047 Temperature sensor #define TEMPMASK 0xffef // AN4 as analog input

  13. Reading Temperature (relative) main (){ int temp, ref, pos, j; // 1. initializations InitADC( TEMPMASK); // initialize ADC Explorer16 inputs TRISA = 0xff00; // select PORTA pins as outputs // 2. get the central bar reference temp = 0; for ( j= 0; j <16; j++) temp += ReadADC( TEMP_CH); // read the temperature ref = temp >> 4; // 3.0 main loop while( 1){ // 3.1 get a new measurement temp = 0; for ( j= 0; j<16; j++) { temp += ReadADC( TEMP_CH); // read the temperature } temp >>= 4; // averaged over 16 values Delayms( 500); // 1/2 second // 3.2 compare with initial reading, move bar 1 dot/C pos = 3 + (temp - ref); // 3.3 keep result in value range 0..7, keep bar visible if ( pos > 7) pos = 7; if ( pos < 0) pos = 0; // 3.4 turn on the corresponding LED PORTA = ( 0x80 >> pos); } // main loop } // main

  14. Tips ’n Tricks • If the sampling time required is longer than the maximum available option (32xTad) you can try and extend Tad first or, better, swap things around and enable the automatic sampling start (at the end of the conversion). This way the sampling circuit is always open, charging, whenever the conversion is not occurring. Manually clearing the SAMP bit will trigger the actual conversion start. • Further, it is possible to have a Timer periodically clear the SAMP control bit (one of the options for the SSRC bits in AD1CON1), and enabling the ADC end of conversion interrupt, will provide the widest choice of sampling periods possible for the least amount of MCU overhead possible. No waiting loops, only a periodic interrupt when the results are available and ready to be fetched.

  15. Another Game ... /* ** TEMPgame.c ** Playing with a Temperature Sensor */ #include <EX16.h> #include <config.h> main () { int temp, ref, pos, mallet, mole, j; // 1. initializations InitADC( TEMPMASK); // initialize the ADC and analog inputs TRISA = 0xff00; // all PORTA pins as outputs // 2. use the first reading to randomize a number generator srand( ReadADC( TEMP_CH)); // generate the first random position mole = 0x80 >> (rand() & 0x7); // 3. compute the average value for the initial reference temp = 0; for ( j= 0; j<16; j++) temp += ReadADC( TEMP_CH); // read the temperature ref = temp >> 4; // 4. main loop while( 1) { // 4.1 take the average temperature temp = 0; for ( j=0; j<16; j++){ temp += ReadADC( TEMP_CH); // read the temperature } temp >>= 4; // averaged over 16 values // 4.2 compare with the initial reading // and move the bar 1 pos. per C pos = 3 + (temp - ref); // 4.3 keep result in value range 0..7, keep bar visible if ( pos > 7) pos = 7; if ( pos < 0)pos = 0; // 4.4 turn on the corresponding LED mallet = ( 0x80 >> pos); // display result for 1/2 second for( j=0; j<128; j++) { // dim random LED if ((j & 0xf) == 0) PORTA = mallet + mole; else PORTA = mallet ; Delayms( 5); } // 4.5 when the cursor hits mole LED, generate new one while ( mallet == mole ) mole = 0x80 >> (rand() & 0x7); } // main loop } // main

  16. Notes for C Experts Even if the PIC24 has a fast divide instruction, there is no reason to waste any processor cycles. In embedded control, every processor cycle is precious. If the divisor is a power of two, the integer division can be best performed as a simple shift right by an appropriate number of positions with a computational cost that is at least an order of magnitude smaller than a regular division. If the divider is not a power of two, consider changing it if the application allows. In our last example, we could have opted for averaging 10 temperature samples, or 15 as well as 20, but we chose 16 because this made the division a simple matter of shifting the sum by 4 bits to the right (in a single cycle PIC24 instruction).

  17. Suggested Excercises • Use the ADC FIFO buffer to collect conversion results, set up Timer 3 for automatic conversion and the interrupt mechanism so that a call is performed only once the FIFO buffer is full and temperature values are ready to be averaged.

  18. Recommended Readings • Backer, Bonnie,“A Baker's Dozen: Real Analog Solutions for Digital Designers”, Newnes, Burlinghton, MA • For proper feed and care of an analog to digital converter look no further than Bonnie’s cookbook.

  19. Online Resources http://microchip.com/temp • Temperature sensors are available in many flavors and a choice of interface options including direct I2C™ or SPI™ digital output. http://microchip.com/adc • Microchip offers a variety of analog to digital converters(ADCs) to help you design better by delivering high accuracy and low power per given sampling speed. The broad portfolio comes with a variety of speed and resolution.

More Related