LAB 8: Program Design Pattern and Software Architecture

Slides:



Advertisements
Similar presentations
UBI >> Contents Chapter 7 Timers Laboratories MSP430 Teaching Materials Texas Instruments Incorporated University of Beira Interior (PT) Pedro Dinis Gaspar,
Advertisements

Review: Interrupts & Timers
Interrupts, Low Power Modes and Timer A (Chapters 6 & 8)
Chung-Ta King National Tsing Hua University
Chung-Ta King National Tsing Hua University
C Examples 1.
Chung-Ta King National Tsing Hua University
ECE 447 Fall 2009 Lecture 9: TI MSP430 Interrupts & Low Power Modes.
LAB 6: Serial Communication
7-1 Digital Serial Input/Output Two basic approaches  Synchronous shared common clock signal all devices synchronised with the shared clock signal data.
The 8051 Microcontroller and Embedded Systems
CS4101 嵌入式系統概論 Software UART Revisited Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan ( Materials from MSP430.
Network and Systems Laboratory nslab.ee.ntu.edu.tw.
INTERRUPTS PROGRAMMING
Chung-Ta King National Tsing Hua University
LAB 7: WDT+ and Low-Power Optimization
T IMERS - 2. O UTPUT U NIT Each capture/compare block contains an output unit. The output unit is used to generate output signals such as PWM signals.
Lecture 18: ADC Implementation Lecturers: Professor John Devlin.
ARM Timers.
1 4-Integrating Peripherals in Embedded Systems. 2 Introduction Single-purpose processors  Performs specific computation task  Custom single-purpose.
MSP430 Mixed Signal Microcontroller – Parte 2 Afonso Ferreira Miguel Source: slau056d – Texas instruments.
CPE 323 Introduction to Embedded Computer Systems: Watchdog Timer, Timer A Instructor: Dr Aleksandar Milenkovic Lecture Notes.
Lecture Set 9 MCS-51 Serial Port.
Page 1 D&C EBV Seminar June 2003 Motor Demo C868 Chevillot/Jansen June 2003 N e v e r s t o p t h i n k i n g. Infineon C868 Hands On Training CAPCOM6.
Universal Asynchronous Receiver/Transmitter (UART)
1 Lab 1: Introduction. 2 Configure ATMEL AVR Starter Kit 500 (STK500), a prototyping/development board for Lab1. ATmega16 ( V) is the chip used.
Lecture 11: TI MSP430 Timers Compare Modes
1. Registers Used in Timer/Counter  TH0, TL0, TH1, TL1  TMOD (Timer mode register)  TCON (Timer control register) 2.
ECE 447 Fall 2009 Lecture 7: MSP430 Polling and Interrupts.
Serial Communications Interface Module Slide #1 of 19 MC68HC908GP20 Training PURPOSE -To explain how to configure and use the Serial Communications Interface.
EE/CS-352: Embedded Microcontroller Systems Part V The 8051 Assembly Language Interrupts.
1 Run-to-Completion Non-Preemptive Scheduler. 2 In These Notes... What is Scheduling? What is non-preemptive scheduling? Examples Run to completion (cooperative)
Embedded Programming and Robotics Lesson 11 Arduino Interrupts 1.
ECE 382 Lesson 32 Lesson Outline Lab 6 Introduction Pulse Width Modulation Capture / Compare Example Lab 6 Tips Admin Lab#6 “prelab” due BOC lesson 33.
Chip Config & Drivers – Required Drivers:
Lab 7 Basic 1: Game of Memory
Introduction Why low power?
Introduction Why low power?
Recall the Container Thermometer
ECE 3430 – Intro to Microcomputer Systems
Lecture 8: TI MSP430 Interrupts, ISRs
CS4101 嵌入式系統概論 Serial Communication
CS4101 嵌入式系統概論 Interrupts Prof. Chung-Ta King
Lesson Outline Interrupts Admin Assignment #9 due next lesson
PWM and DC Motor Control
CS4101 Introduction to Embedded Systems Lab 6: Low-Power Optimization
Timer and Interrupts.
CS4101 Introduction to Embedded Systems Lab 1: General Purpose IO
4-Integrating Peripherals in Embedded Systems
Prof. Chung-Ta King Department of Computer Science
4-Integrating Peripherals in Embedded Systems
CS4101 嵌入式系統概論 General Purpose IO
MSP432 ARM Timer Programming
Homework Reading Machine Projects Labs
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 2: Timer and Clock Prof. Chung-Ta King Department of Computer Science National.
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 3: Interrupt Prof. Chung-Ta King Department of Computer Science National Tsing.
National Tsing Hua University CS4101 Introduction to Embedded Systems Lab 6: Serial Communication Prof. Chung-Ta King Department of Computer Science National.
ECE 3430 – Intro to Microcomputer Systems
ECE 3430 – Intro to Microcomputer Systems
CS4101 Introduction to Embedded Systems Lab 7: Serial Communication
CS4101 Introduction to Embedded Systems Lab 4: Interrupt
CS4101 Introduction to Embedded Systems Lab 7: Serial Communication
Lecture 9: TI MSP430 Interrupts & Low Power Modes
Microcontroller Labs Lab 1 – The LED Test Mode Dr. Gregg Chapman
ECE 3430 – Intro to Microcomputer Systems
EUSART Serial Communication.
CS4101 Introduction to Embedded Systems Lab 2: Basic IO and Timer
Prof. Chung-Ta King Department of Computer Science
MSP430 Clock System and Timer
ECE 3567 Microcontrollers Lab
Presentation transcript:

LAB 8: Program Design Pattern and Software Architecture CS 4101 Introduction to Embedded Systems LAB 8: Program Design Pattern and Software Architecture Chung-Ta King National Tsing Hua University

Introduction In this lab, we will learn To measure and evaluate the performance of embedded programs on MSP430 LaunchPad To design embedded programs using the state machine pattern

Recall Software UART in Lab 6 Second half of main(): TimerA_UART_init(); // Start Timer_A UART TimerA_UART_print("G2xx2 TimerA UART\r\n"); TimerA_UART_print("READY.\r\n"); for (;;) { // Wait for incoming character __bis_SR_register(LPM0_bits); // Echo received character TimerA_UART_tx(rxBuffer); } void TimerA_UART_print(char *string) { while (*string) TimerA_UART_tx(*string++);

TimerA_UART_tx() void TimerA_UART_init(void) { TACCTL0 = OUT; // Set TXD Idle as Mark = '1' TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int TACTL = TASSEL_2 + MC_2; // SMCLK, continuous mode } void TimerA_UART_tx(unsigned char byte) { while (TACCTL0 & CCIE); // Ensure last char TX'd TACCR0 = TAR; // Current state of TA counter TACCR0 += UART_TBIT; // One bit time till first bit TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int txData = byte; // Load global variable txData |= 0x100; // Add mark stop bit to TXData txData <<= 1; // Add space start bit

UART TXD ISR #pragma vector = TIMER0_A0_VECTOR __interrupt void Timer_A0_ISR(void) { static unsigned char txBitCnt = 10; TACCR0 += UART_TBIT; // Add Offset to CCRx if (txBitCnt == 0) { // All bits TXed? TACCTL0 &= ~CCIE; // All bits TXed, disable int txBitCnt = 10; // Re-load bit counter } else { if (txData & 0x01) { TACCTL0 &= ~OUTMOD2; // TX Mark '1’ TACCTL0 |= OUTMOD2;} // TX Space '0‘ txData >>= 1; txBitCnt--; } 5 5 5

Questions about the Code Where does the program halt waiting for interrupts between transmission of bits? How to know the duty cycle of TXD and RXD? A duty cycle is the time that MSP430 spends in active mode as a fraction of the duration of transmitting/receiving a bit in UART For 9600 baud, duration of a bit is 1/9600 sec. In full duplex, TXD and RXD may be intermixed. Thus, if duty cycle > 50%, bits may be missed!

Duty Cycle of UART RXD Signal on P1.2 CPU mode Total time in ISRs Active mode LPM3 Duty cycle = Total time in ISRs 10 bits  cycle time

Basic Lab 1 (1/2) Modify your software UART program developed in Lab 6. Run the UART with 9600 baud, 8-bit data, and 1 stop bit. Measure the duty cycle in transmitting a data byte (1 start, 8 data, 1 stop bits). Transmit the calculated value of duty cycle back to PC to show on the screen. Then, do the same for receiving a byte. Hint 1: Read TAR for the start and end time of ISR. Hint 2: Do not transform an integer to a float in ISR. Hint 3: The place where you put your code will affect the time you get.

Basic Lab 1 (2/2) To measure the receiving time on you LaunchPad, you should type a character at your PC terminal and let the LaunchPad calculate the duty cycle of receiving the typed character. Then LaunchPad transmits back the character and the measured values of the duty cycle. The values are printed. EX: type “a” and you will see on your screen output: a RX: 40% TX: 38%

Program Design Pattern

Recall Software UART Again The ISR handling RXD: #pragma vector = TIMER0_A1_VECTOR __interrupt void Timer_A1_ISR(void) { static unsigned char rxBitCnt = 8; static unsigned char rxData = 0; switch (__even_in_range(TA0IV, TA0IV_TAIFG)) { case TA0IV_TACCR1: // TACCR1 CCIFG - UART RX TACCR1 += UART_TBIT; // Add Offset to CCRx if (TACCTL1 & CAP) { // On start bit edge TACCTL1 &= ~CAP; // Switch to compare mode TACCR1 += UART_TBIT_DIV_2; // To middle of D0 } else { // Get next data bit rxData >>= 1;

ISR Handling RXD if (TACCTL1 & SCCI) { // Get bit from latch rxData |= 0x80; } rxBitCnt--; if (rxBitCnt == 0) { // All bits RXed? rxBuffer = rxData; // Store in global rxBitCnt = 8; // Re-load bit counter TACCTL1 |= CAP; // Switch to capture __bic_SR_register_on_exit(LPM0_bits); // Clear LPM0 bits from 0(SR) } break; Wake up main loop 12 12 12

ISR Actually Runs a State Machine Triggering events: Falling edge rxBitCnt == 0 Timer up Timer_up && rxBitCnt > 0 / Prepare RX next data bit Data Bit Timer_up / Prepare RX data bits Timer_up && rxBitCnt = 0 / Prepare RX stop bit Start Bit Stop Bit Drop_edge /Prepare RX stop bit Idle Timer_up / Return RX byte

Basic Lab 2 (1/2) Measure temperature at 2 Hz using ADC10 from internal temperature sensor Up Mode: Flash red LED at 2 Hz. Whenever the temperature keeps rising for N consecutive samplings, flash red LED at 10 Hz. Down Mode: Same as Mode 1, except flashing green LED and change flashing rate when the temperature drops for N consecutive samplings. Hint : Define a variable ‘SAMPLING_CNT’ to keep the value N, and for the convenience of demo, set the value to 2.

Basic Lab 2 (2/2) The two modes are toggled whenever the button is pressed. System starts at Up Mode. Draw a state machine describing this system, including states, triggering events, & actions. Hint: Timer-up may be considered as an event. Implement the system using state machine. The switch-statement for the state machine may be implemented in main() or in individual ISR that corresponds to a triggering event.

State Machine in main() (1/2) void main(void) { while(1) { // go to low-power mode // disable interrupt switch(state) { case state1: // check event flags // perform corresponding actions // state = next_state // clear event flags case state2: ...} // enable interrupt }

State Machine in main() (2/2) #pragma vector=XXXXX_VECTOR __interrupt void XXXXX_ISR(void) { // set event flags // wake up main() } #pragma vector=YYYYY_VECTOR __interrupt void YYYYY_ISR(void) {

Bonus Add a third mode: Up/Down Mode: Flash both LEDs at 2 Hz. Whenever the temperature keeps rising for N consecutive samplings, keep flashing red LED at 10 Hz. Whenever the temperature keeps dropping for N consecutive samplings, keep flashing green LED at 10 Hz. In all other cases, return to flash both LEDs at 2 Hz.

LAB_8 DEMO RULE CS 4101 Introduction to Embedded Systems 2012/11/12

Basic_1 Type a character on terminal and print "RX: N% TX: M%“ notice:the duty cycle must less then 50% (duty cycle < 50%). You have to explain how you calculate the duty cycle. Random questions.

Basic_2 Draw a state machine describing your system, including states, triggering events, & actions. (Using 1 page PPT to draw the state machine. 2. We will check the flow of the program is the same as the state machine 3. Random questions. The switch-statement for the state machine may be implemented in main() or in individual ISR

Precautions You need to demo again if you can’t explain clearly. Check the following requirement before demo your lab : 1. Finish your lab and check it can run correctly. 2. Make sure you really understand what your lab doing.