Presentation is loading. Please wait.

Presentation is loading. Please wait.

Embedded Programming Keeping Time Serial Data and PWM Signals A/D Conversion Data Formats and Constructs Interrupts MAE 156A Ardunio UNO Development Board.

Similar presentations


Presentation on theme: "Embedded Programming Keeping Time Serial Data and PWM Signals A/D Conversion Data Formats and Constructs Interrupts MAE 156A Ardunio UNO Development Board."— Presentation transcript:

1 Embedded Programming Keeping Time Serial Data and PWM Signals A/D Conversion Data Formats and Constructs Interrupts MAE 156A Ardunio UNO Development Board

2 MAE 156A 2 What is a microcontroller? A microcontroller (or MCU) is a single-chip computer that contains a processor core, memory, and various input/output peripherals. Programmable memory might be flash (re-writeable) or OTP (one-time programmable) UV erasable devices have largely been replaced by flash Peripherals may include: General-purpose digital input and output Analog-to-digital conversion Serial communications Microcontrollers are relatively cheap. The ATmega328 microcontroller costs about $4 McDonald's hamburger costs about $1 Microcontrollers are everywhere. Over 4 billion microcontrollers were sold in 2006 McDonald's sells 1.5 billion hamburgers each year in the US

3 MAE 156A 3 Clocks Program execution requires a device clock (heartbeat). On PIC16 devices, four (4) clock cycles are required to execute one (1) instruction The clock timing signal may generated in one of several ways: External crystal or ceramic resonator External oscillator circuit Internal oscillator circuit From the digitial output of another microcontroller Maximum clock frequency generally depends on external power Low-power chips (< 3V) may restrict clock frequency to less than 1MHz What's so special about a clock frequency of 32.768 kHz? The Arduino UNO uses an external 16 MHz crystal

4 MAE 156A 4 Timers Most microcontrollers have one or more built-in (hardware) timers. Timers are handy because they can run independently from your code. Your program can do something else while waiting for the timer to expire. Timer operation is governed by two registers. A control register defines operation of the timer. Clock signal Enable interrupt Pre-scale options allow for time scaling Timer value is held in a separate read/writeable (possibly multi-byte) data register. Timer can be set to start counting from a value other than zero

5 MAE 156A 5 Maximum Time The timer data register is typically 8- or 16-bits. How long before the timer overflows? 16 MHz clock, 8-bit timer starting at value 0 16 MHz clock, 16- bit timer starting at value 0 A timer pre-scaler increases the time before an overflow occurs. Atmega328 prescale options include 1:1, 1:8, 1:64, 1:256, 1:1024 16-bit timer, starting at value 0, 1:256 prescaler

6 MAE 156A 6 Pulse Width Modulation (PWM) A PWM signal is characterized by frequency and duty cycle Duty cycle is ratio of pulse width to waveform period Arduino environment offers the analogWrite() function for PWM output. 490 Hz frequency → 2 msec period Duty cycle is specified by an input variable ranging from 0 to 255 (100% duty cycle) Commercial servos used for airplane/car models are controlled with a PWM signal 50 Hz frequency → 20 msec period Pulse Width Period

7 MAE 156A 7 Serial Data Transmission Serial data is typically represented using the 7-bit ASCII character set. Example: letter 'G' is encoded as ASCII 0x47. 0x47 is hexidecimal notation, equivalent to decimal value of 71, or binary 01000111. BAUD = symbols transmitted per second For old modems, each “symbol” was one bit, so BAUD became identified as the data transmission rate in bits per second. Be careful! Serial data may use different voltage protocols! TTL: logic low = 0 V, logic high = 5 V RS-232: logic low = -12 V, logic high = +12 V Asynchronous transmission of serial data requires start/stop framing bits. 4idle321076570126543 start stop 8N1 = 8-bit data, one stop, no parity bit

8 MAE 156A 8 Analog/Digital (A/D) Conversion There are many ways to sample an analog signal to yield a digital result. Direct Conversion (bank of comparators) Successive Approximation (ATmega328) Ramp or Slope compare Sigma-Delta And lots more... When using an A/D input pin, be sure to pay attention to: The A/D process takes time The result has a finite resolution 10-bit A/D converter of ATmega328 can only distinguish 2 10 = 1024 different input values The analogRead() function in Arduino returns values between 0 and 1023 Beware of aliasing Use a low-pass filter to remove high frequency signal content 4 kHz signal frequency -> 8 kHz A/D sample rate -> 4 kHz low-pass filter

9 MAE 156A 9 Data Types The Arduino development environment supports many data types. Data arrays are also available The number of array elements is limited by memory Arrays are zero-indexed (MATLAB and FORTRAN are not) TypeNumber of BitsRange boolean10 (false) to 1 (true) int16-32,768 to 32,767 unsigned int160 to 65,535 long32-2,147,483,648 to 2,147,483,647 unsigned long320 to ginormous float32-3.4028235e+38 to 3.43028235e+38 double32same as float int myArray[10]={9,3,2,4,3,2,7,8,9,11}; // myArray[9] contains 11 // myArray[10] is not valid

10 MAE 156A 10 Integer Math Integer (fixed-width) math requires special handling so that overflow conditions are dealt with properly. What happens if arithmetic operations exceed range of the assigned data type? int x; x = -32,768; x = x - 1; // x now contains 32,767 -> rolls over in neg. direction x = 32,767; x = x + 1; // x now contains -32,768 -> rolls over

11 MAE 156A 11 Control Flow The for statement is used to repeat a block of statements enclosed in curly braces. int i; for (i = 0; i < 5; i = i + 1) { Serial.println(myPins[i]); // serial output } They are also useful for array operations. for (int i=0; i <= 255; i++) { analogWrite(PWMpin, i); // change PWM duty cycle delay(10); }

12 MAE 156A 12 Conditional Expressions if statements allow for a change in instruction order. if (pinFiveInput < 500) { // action A } else { // action B } while loops execute continuously until the conditional expression becomes false. var = 0; while(var < 200) { // do something repetitive 200 times var++; } DANGER! Do not use the single equal sign as a conditional test (e.g. if (x = 10) ). Instead use the double equal sign (e.g. if (x == 10) ), which tests whether x is equal to 10 or not.

13 MAE 156A 13 Interrupts An interrupt is a special process that interrupts normal program execution after a pre- defined event has occurred. An interrupt is handled in software by a special subroutine, usually called an Interrupt Service Routine (ISR) After the interrupt is handled, program execution continues at the point where the interrupt occurred. Here are some events that can cause an interrupt in the ATmega328: External pin change Timer overflow Transmit or receive buffers full A/D conversion complete More advanced microcontrollers allow the programmer to assign a priority to each type of interrupt.

14 MAE 156A 14 Interrupt Programming The Arduino UNO board has two external pins that can trigger an interrupt. In the following example, the main program loop does nothing useful until an external pin is brought to a logical high state (i.e. rising edge). void setup() { attachInterrupt(0, int0_ISR, RISING); } void loop() { // do nothing } void int0_ISR(void) { // insert useful actions here }

15 MAE 156A 15 Further Reading Arduino Development Environment Reference http://arduino.cc/en/Reference/HomePage Barrett, S.F., Arduino Microcontroller Processing for Everyone!, Morgan & Claypool Publishers, 2010. http://roger.ucsd.edu:80/record=b6931623~S9 Barrett, S.F., Embedded Systems Design with the Atmel AVR Microcontroller, Morgan & Claypool Publishers, 2010. http://roger.ucsd.edu:80/record=b6946729~S9


Download ppt "Embedded Programming Keeping Time Serial Data and PWM Signals A/D Conversion Data Formats and Constructs Interrupts MAE 156A Ardunio UNO Development Board."

Similar presentations


Ads by Google