Chapter 10 It’s an Analog World

Slides:



Advertisements
Similar presentations
Chapter 3 Message in a Bottle Programming Loops in C.
Advertisements

Chapter 2 Walking in Circles Programming Loops in C.
Jordan Barry Victor Brzeski
Chapter 4 NUMB3RS.
EET 2261 Unit 14 INCOMPLETE Analog-to-Digital Conversion (ADC) & Digital-to-Analog Conversion (DAC)  Read.  Homework #13 and Lab #13 due next week. 
Lecture 17: Analog to Digital Converters Lecturers: Professor John Devlin Mr Robert Ross.
Data Acquisition ET 228 Chapter
Analog/Digital Subsystem
Chapter 11 It’s an Analog World Learning to use the Analog to Digital Converter.
Lecture 5: PIC32 development board, Timers ADC and interrupts,
1 Lab2: A/D Converter. 2 This circuit connects a variable voltage to an A/D port on the AVR mcu. Your software running on the AVR mcu will read the digital.
5-1 Analogue Input/Output Many sensors/transducers produce voltages representing physical data.  To process transducer data in a computer requires conversion.
Data acquisition and manipulation
H. Huang Transparency No.11-1 The 68HC11 Microcontroller Chapter 11: 68HC11 Analog to Digital Converter The 68HC11 Microcontroller Han-Way Huang Minnesota.
Debouncing Switches Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically.
Analogue Input/Output
Railway Foundation Electronic, Electrical and Processor Engineering.
Digital I/O Connecting to the Outside World
Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to Embedded Systems.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Chapter 5 Interrupts.
Data Converters ELEC 330 Digital Systems Engineering Dr. Ron Hayne
EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
Digital to Analogue Converter
Gauge Operation and Software by Scott A. Ager. Computer Recommendations 750 MHz Pentium III 64 Meg SRAM 40 Gig Hard Drive 1024 x 768 graphics CD Writer.
SIGMA-DELTA ADC SD16_A Sigma-Delta ADC Shruthi Sujendra.
1 LHO 13 The 8051CF020 and the University Daughter Card.
dsPIC DSC –Modul A/D Conversia A/D Registre programare
EEE527 Embedded Systems Lecture 10B:Chapter 11 Analogue to Digital Convertors (ADCs) (version 2: 25/11/13, see after slide 20 Ian McCrumRoom 5B18, Tel:
EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
CS224 I/O. Timers Timer1 is 16-bit timer 2^16-1 or 65,535 SYSCLK = 40MHz, Prescalers – 1:1, 1:8, 1:64, and 1:256 – Use T1CONbits.TCKPS=3 for 1:256 scaling.
By James Cockrell and Justin Loveless
Chapter 5 - Interrupts.
Analog to Digital Converter (ADC)
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
Electronic instrumentation Digitization of Analog Signal in TD
Sensing Algorithm using IR Sensor and ADC Soong-sil University. Robotics 기 정 두 원.
1 Lab 4: D/A Converter Lab 4: D/A Converter This is a simple resistive network for a D/A converter Port 1, Port 0 are digital inputs ==> 00 (minimum),
Microprocessors A practical approach..
CEng3361 CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT Spring 2011 Recitation 06.
Lesson 1 PLC BASICS. PLC Definition  Programmable Logic Controllers are industrial computers that control machine and other applications.  PLC have.
 Mini-Computer ◦ Microprocessor  The Brains  Arithmetic Logic Unit (ALU)  Control Unit  Program/ Data Storage  Peripherals (Input/Output)  Low-Cost.
MECH 373 Instrumentation and Measurements
Microcontrollers, Basics Tips and Tricks with PIC MCUs
Chapter 12: Analog-to-Digital Converter
Environment Temperature Monitor
Analog Comparator An analog comparator is available on pins PE2(AIN0), PE3(AIN1) The comparator operates like any other comparator. -when (+) exceeds (-)
Microcontroller basics
Microcontroller Applications
Prof. Chung-Ta King Department of Computer Science
Chapter 9 Alphanumeric Displays
AQA GCSE 7 Electronic systems processing Design and Technology 8552
Introduction to Microprocessors and Microcontrollers
Digital Acquisition of Analog Signals – A Practical Guide
Created by Luis Chioye Presented by Cynthia Sosa
Digital Theremin with LED
PIC18F458 Analog-to-Digital
EET 2261 Unit 14 INCOMPLETE Analog-to-Digital Conversion (ADC) & Digital-to-Analog Conversion (DAC) Read. Homework #13 and Lab #13 due next week. Quiz.
Lecture 13 A/D Converter & D/A Converter
ADC and DAC Programming in AVR
ADC and DAC Data Converter
MSP432™ MCUs Training Part 6: Analog Peripherals
輸出動作與輸入命令相同.
Prof. Chung-Ta King Department of Computer Science
UNIT 5 Analog signals.
PIC Analog Input Tom Igoe, NYU-ITP
ADC and DAC Data Converter
Introduction to Arduinos
ECE 3567 Microcontroller Lab
Presentation transcript:

Chapter 10 It’s an Analog World

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)

The Analog to Digital Converter Sample & Hold

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

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

Initializing the ADC NOTE: 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;

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).

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

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.

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

Measuring Temperature TC1047 Output Voltage vs. Temperature

Temp. c. Converting analog signal from a TC1047 Temperature Sensor * ** 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

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 for ( j= 0; j<16; j++) { } 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

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.

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

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).

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.

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.

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.