C Language Programming, Interrupts and Timer Hardware

Similar documents
C Language Programming, Interrupts and Timer Hardware

Interrupt vectors for the 68HC912B32. The interrupt vectors for the MC9S12DP256 are located in memory from 0xFF80 to 0xFFFF.

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

Introduction to the MC9S12 Hardware Subsystems

EE 308 Spring Exam 1 Feb. 27

The MC9S12 Input Capture Function

What Happens When You Reset the MC9S12?

Exam 1 Feb. 23, 25, 27?

Comparison of C and Assembly How to compile a C program using CodeWarrior

What happens when an HC12 gets in unmasked interrupt:

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3

Using Input Capture on the 9S12

EE 308/MENG 483 Spring 2017

EE 308 Spring Lecture 28 March 30, 2012 Review for Exam 2. Introduction to the MC9S12 Expanded Mode

EE 308 Spring A software delay

Comparison of C and Assembly How to compile a C program using CodeWarrior

The MC9S12 Timer Output Compare Function Making an event happen at specific time on the HC12 The MC9S12 Output Compare Function

ECE 367 -Experiment #1 Fall 2012

Capturing the Time of an External Event Input Capture Subsystem

538 Lecture Notes Week 5

HC12 Built-In Hardware

EE Embedded Systems Design. Lessons Exceptions - Resets and Interrupts

538 Lecture Notes Week 5

ME 4447/ME Microprocessor Control of Manufacturing Systems/ Introduction to Mechatronics. Instructor: Professor Charles Ume

Introduction to Programming the 9S12 in C Huang Sections 5.2 and 5.3. You will be able to use all of the Motorola data manuals on the exam.

A B C D E F 0480 FE B F5 3B FC F3 E 1A 1D 2A 2D 3A 3D 4A 4D 5A 5D 6A 6D 7A 7D

Lab 2 Part 3 Assembly Language Programming and 9S12 Ports

EE 308 Spring Hello, World!

Interrupts. How can we synchronize with a peripheral? Polling

ATmega Interrupts. Reading. The AVR Microcontroller and Embedded Systems using Assembly and C) by Muhammad Ali Mazidi, Sarmad Naimi, and Sepehr Naimi

EE 308 Spring 2011 The MC9S12 in Expanded Mode How to get into expanded mode

Embedded Systems and Software

EE4390 Microprocessors

The MC9S12 Timer Input Capture Function

Lecture test next week

real-time kernel documentation

Interrupt and Timer ISRs. Student's name & ID: Partner's name(s) & ID(s): Your Section number & TA's name

By the end of Class. Outline. Homework 5. C8051F020 Block Diagram (pg 18) Pseudo-code for Lab 1-2 due as part of prelab

Using the stack and the stack pointer

Computer Systems Lecture 9

Dallas Semiconductor DS1307 Real Time Clock. The DS 1307 is a real-time clock with 56 bytes of NV (nonvolatile)

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

EXCEPTIONS ON THE 9S12

History of the Microprocessor. ECE/CS 5780/6780: Embedded System Design. Microcontrollers. First Microprocessors. MC9S12C32 Block Diagram

MC9S12 Address Space

8051 Microcontroller

80C51 Block Diagram. CSE Overview 1

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

Microprocessors and Microcontrollers (EE-231)

CSE3215 Embedded Systems Laboratory

538 Lecture Notes Week 1


ME 6405 Introduction to Mechatronics

MCO556 Practice Test 2

The modules in this lab room are 4 line by 16 character display modules. The data sheet/users manual for the module is posted on My.Seneca.

ECE 4510/5530 Microcontroller Applications Week 9

ME 4447/6405 Introduction to Mechatronics Instructor: Professor Charles Ume

Chapter 2. Overview of Architecture and Microcontroller-Resources

Asynchronous Data Transfer

ME 4447/6405. Microprocessor Control of Manufacturing Systems and Introduction to Mechatronics. Instructor: Professor Charles Ume LECTURE 6

EE 5340/7340 Motorola 68HC11 Microcontroler Lecture 1. Carlos E. Davila, Electrical Engineering Dept. Southern Methodist University

Pulse Accumulator on the HCS12

Immediate vs. Extended mode: Immediate values are marked with a # symbol. They also are different instructions when assembled.

Lab 9: On-Board Time Generation and Interrupts

Department of Electronics and Instrumentation Engineering Question Bank

Introduction to Embedded Systems

3. (a) Explain the steps involved in the Interfacing of an I/O device (b) Explain various methods of interfacing of I/O devices.

Introduction to Mechatronics

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

8051 Timers. Class 7 EE4380 Fall Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

Lab 1 MC9S12 Assembler and Monitor

Lab 1 MC9S12 Assembler and Monitor

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

8051 Microcontrollers

Interrupt and Timer ISRs. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

A 3-SPEED STEPPER MOTOR

1. Attempt any three of the following: 15

F2MC-8FX EEPROM Library

COMP 7860 Embedded Real- Time Systems: Threads

Microprocessors & Interfacing

Microcomputer Architecture and Programming

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Interrupt handling. Purpose. Interrupts. Computer Organization

EE475 Lab #3 Fall Memory Placement and Interrupts

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

EE445L Fall 2010 Final Version A Page 1 of 10

Intel x86 instruction set architecture

C Language Programming

Microcontrollers. Microcontroller

COMP2121: Microprocessors and Interfacing

F2MC-8FX EEPROM LIBRARY

8051 Peripherals. On-Chip Memory Timers Serial Port Interrupts. Computer Engineering Timers

Timers and interrupts

ARM Interrupts. EE383: Introduction to Embedded Systems University of Kentucky. James E. Lumpp

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

SquareWear Programming Reference 1.0 Oct 10, 2012

Today's plan: Announcements General Strategy Microcontroller programming concepts/last bits of assembly Activity 2

ECE 271 Microcomputer Architecture and Applications University of Maine

Lecture 15 February 20, 2012 Introduction to the MC9S12 Timer Subsystem What Happens when you Reset the MC9S12. Introduction to Interrupts

Transcription:

C Language Programming, Interrupts and Timer Hardware In this sequence of three labs, you will learn how to write simple C language programs for the MC9S12 microcontroller, and how to use interrupts and timers. WEEK 1 Introduction and Objectives The C programming language is used extensively in programming microprocessors. In this lab you will write some simple C programs which do the things you did in assembly language in the first lab. For example, the following C program increments Port B: /* A C language program to increment Port H on a 68HC12 */ #include <hcs12.h> /* get the HCS12DP256 definitions */ #define D_1MS (24000/13) /* inner delay loop takes 13 cycles*/ /* 24000 cycles = 1 ms */ /* for 24 MHz clock */ #define TRUE 1 /* a normal C define */ void delay(unsigned int ms) void main( ) /* the main program */ DDRH = 0xff; /* make all bits of Port B output */ PTH = 0; while (TRUE) /* do forever */ PTH = PTH + 1 /* increment Port B */ delay(100); /* wait 100 ms */ /* Function to delay ms milliseconds */ void delay(unsigned int ms) volatile int i; while (ms > 0 ) i = D_1MS; while ( i > 0) i = i 1 ; ms = ms 1; Figure 1. A C program to increment Port B. Page 1

Pre Lab For the pre lab write the program for Part 4 of this lab. The Lab 1. The procedure for compiling a program using the Gnu C compiler and the EGNU IDE is discussed in detail in Section 5.10 of the text. Start the EGNU IDE, and select File, then New source file. Type in the above C program, then select File, then Save unit as, and give it the name inc.c, and save it in your EE 308 directory. Select File, then New project, give the project an appropriate name (such as Lab3_Part1.prj), and save it in your EE 308 directory. (Note: The project name must be different than the C program base name. If the C program name is inc.c, the project name cannot be inc.prj.) When Project options pops up, click on the down arrow below Hardware Profiles, and select Dragon12. Click on Edit Profile, and make sure the addresses and lengths of the memory locations are reasonable. Make sure inc.c is part of your project. Compile and link your project by selecting Build, then Make. You should now have a file '*.s19' and a '*.dmp'. The *.s19 can be run on the MC9S12. 2. The Lab3_Part1.dmp contains the assembly language listing generated by the C compiler. Look at the file and try to understand what it does. Note that there may be some things which do not make sense to you. At the very least, find the assembly language code which increments Port H. 3. The file Lab3_Part1.dmp also shows the addresses of the start of the functions in the program, as well as the addresses of any global variables. (Since the inc.c program does not use any global variables, none will appear in the Lab3_Part1.dmp file. The local variables used in inc.c are allocated on the stack when they are needed.) Note that the function and variable names are denoted by the symbol <_var name>. Note also that there is a function <_exit>. Find the address of this function. Look at the file Lab3_Part1.s19.. This contains the op codes that will be loaded into the HC12. Reverse assemble the <_exit> function. What does it do? Load the file inc.s19 into your MC9S12 and run it. Verify that Port H increments. 4. Using the program inc.c as a model, write a C program to implement the program from Lab 2, week 3. Compile and run your program. Have an instructor verify that it works. 5. Look at the output of the GNU IDE and determine how many bytes the program takes (the length of the.text segment). Compare this to the length of last week s program written in assembly. Page 2

6. Put your program in the EEPROM at address 0x0400. Remember, when you put code into EEPROM you need to do some setup which D Bug12 normally does for you. You need to add the following as the first few instructions of your C program: /* Set system clock to 24 MHz */ CLKSEL = CLKSEL & ~0x80; REFDV = 0x03; SYNR = 0x05; while ((CRGFLG & 0x08) == 0) ; CLKSEL = CLKSEL 0x80; /* Set up serial port */ SCI0BDH = 0; SCI0BDL = 0x9c; SCI0CR1 = 0; SCI0CR2 = 0x0C; You will want the array which stores the turn signal patterns into the EEPROM (so the array will not disappear when you turn off power). You will want variables which will change as the program is executed to be placed in RAM. You can tell the compiler to put constant data (such as an array of patterns to be display on LEDs) immediately following the code (so the data will be loaded into EEPROM) by defining the data as type const. An example of setting up an array of type const is: const char table[] = 0xaa,0xbb,0xcc; Finally you need to tell the compiler to put the program into EEPROM. You can do this in one of two ways. In the directory with your project there should be a file called memory.x. You can edit memory.x to look like this: OUTPUT_FORMAT("elf32-m68hc12", "elf32-m68hc12","elf32-m68hc12") OUTPUT_ARCH(m68hc12) ENTRY(_start) SEARCH_DIR(C:\usr\lib\gcc-lib\m6811-elf\3.3.5-m68hc1x-20050515\m68hc12\mshort) MEMORY ioports (!x) : org = 0x0000, l = 0x400 eeprom (!i) : org = 0x0f00, l = 0x0100 data (rwx) : org = 0x1000, l = 0x1000 text (rx) : org = 0x0400, l = 0x0b00 PROVIDE (_stack = 0x3c00); This will tell the compiler to put text at address 0x400, which is in the EEPROM. Compile your program, load it into the 9S12, and verify that it runs correctly out of EEPROM. Page 3

Another way to do this is to go to the Options menu, select Project Options, and make sure the Hardware Profile is set to Dragon12. Then select Edit Profile, and set eeprom to start at f00 with length 100, set text to start at 400 with length b00, set data to start at 1000 with length 2c00, and set the stack to 3c00. Click OK to accept these settings. Then go to Build and select Create memory.x. (Note: you will have to change memory.x back to its old values when you want to reload programs into RAM.) Page 4

USING THE MC9S12 TIMER OVERFLOW INTERRUPT AND REAL TIME INTERRUPT WEEK 2 Introduction and Objectives Enabling an interrupt on the MC9S12 allows your program to respond to an external event without continually checking to see if that event has occurred. Once the event occurs, the MC9S12 interrupt subsystem will transfer control of your program to an interrupt service routine (ISR) to handle the event, and then return control to your original code sequence. In this week s lab you will write assembly and C language program which enable and use interrupts. The interrupts on the MC9S12 which are easiest to use are the Timer Overflow Interrupt and the Real Time Interrupt. These interrupts allow you to interrupt the microcontroller after a specified amount of time has passed. Pre Lab For the pre lab, write the programs for Sections f and g. Also, calculate the time asked for in Part e. The Lab a. Connect your MC9S12 to your computer. At the D Bug 12 prompt, display the contents of the TCNT register. Do this several times. How do the values compare? b. Use D Bug12 to modify the TSCR1 register to enable the counter. Repeat Part 1. c. Use D Bug12 to modify the TSCR1 register to disable the counter. Repeat Part 1. d. Start with the following do nothing program: #include hcs12.inc prog: equ $1000 stack: equ $3c00 loop: org prog ; origin of the program lds #stack ; initialization of the stack wai jmp loop Page 5

Add code to make PTH an output port. The add a Timer Overflow Interrupt to increment the four lower bits for PTH. Set the timer overflow rate to be 175 ms. You should increment the four lower bits of PTH in the interrupt service routine, and leave the four upper bits of PTH unchanged. Verify that the lower four bits of PTH function as an up counter. Note: To use interrupts in an assembly language program you will need to include the file hcs12.inc, which contains the addresses of the interrupt vectors. Here is a part of the hcs12.inc file showing the address of the Timer Overflow interrupt vector: UserTimerOvf equ $3E5E Suppose you want to use the Timer Overflow interrupt in your program. You would need to write a Timer Overflow ISR, with a label (say, tof_isr) at the first instruction of the ISR. To set the Timer Overflow interrupt vector in your program, you would include the following instruction in your program, before the instruction which enables interrupts: movw #tof_isr,usertimerovf e. Calculate how long it should take for the lower bits of PTH to count from 0x0 to 0xF and roll over to 0x0. Use a watch to measure the time. How do the two times agree? f. Add a Real Time Interrupt to your assembly language program. Set up the RTI to generate an interrupt every 65.536 ms. In the RTI interrupt service routine, implement a rotating bit on the four upper bits of PTH, while leaving the four lower bits of PTH unchanged. Verify that the bit takes the correct amount of time to rotate through the four upper bits. A rotating bit will look like this: _ _. g. Implement the same program in C. (What you need to do to use interrupts in C is discussed in the textbook, and described briefly in Section k below.) h. Change your C program so that you do not reset the Timer Overflow Flag in the Timer Overflow ISR. Does your up counter work? Does your rotating bit work? Why? Page 6

i. Restore your original C program from Part g. Now change your C program so that you do not reset the Real Time Interrupt Flag in the Real Time Interrupt ISR. Does your up counter work? Does your rotating bit work? Why? j. Restore your original C program from Part g. Change your C program so that you do not set the address for the Timer Overflow Interrupt. Run your program. What happens now? Why? k. To add interrupt vector in C you will need a file listing the addresses of the interrupt service routines. Note that the different versions of the HCS12 have different interrupt vectors, and hence use different vector files. The file vectors12.c (available on the EE 308 homepage) has the appropriate vectors for the MC9S12DP256 chip. Here is part of vectors12.h: #define VECTOR_BASE 0x3E00 #define INTERRUPT attribute ((interrupt)) #define _VEC16(off) *(volatile unsigned short *)(VECTOR_BASE + off*2) #define #define UserTimerOvf _VEC16(47) UserRTI _VEC16(56) This tells the compiler that, for example, UserTimerOvf is a pointer to a short (16 bit) number at address 0x3E5E (0x3E00 + 2*47). To compile your program with interrupts, include the vectors12.h file in your program. Then, before enabling interrupts, set the interrupt vectors for those interrupts you are using. For example, if you are using the Timer Overflow Interrupt, and the name of the Timer Overflow ISR is toi_isr(), put the following line in your program: UserTimerOvf = (unsigned short *)&toi_isr; Page 7

HCS12 TIMER INPUT CAPTURE AND OUTPUT COMPARE WEEK 3 Introduction and Objectives Last week you wrote programs using the MC9S12 Timer Overflow Interrupt and Real Time Interrupt. This week you will work with the timer Input Capture and Timer Output Compare functions. 1. Start with the following program, which is just a do nothing infinite loop: #include hcs12.h #include DBug12.h #include vectors12.h #define TRUE 1 main( ) DDRA = 0xff; /* make all bits of PORTA output */ PORTA = 0x00; while (TRUE) _asm( wai ); 2. Connect your debounced switch to input Capture 2 (PORTT, Bit 2), as shown below: 3. VCC +5v IC2 0v T R Figure 1. Circuit to measure speed of button pushing Page 8

The right part of Figure 1 is what the signal to IC2 will look like if you push the pushbutton twice. 4. Add to your program code to measure the number of timer ticks T R between the two falling edges of the signal in Figure 1. Set the prescaler so you unambiguously measure time difference of at least 250 ms. Use the printf() function to print out the result the number of timer ticks between pushes of the button. (Do not try to calculate the actual time as a floating point number.) You should write your program as an infinite loop so that after pressing the button twice your program will print out the result, then go wait for the next two passes. 5. Test your program on your EVBU. See how fast you can push the switch twice. Record several values in your lab notebook, and convert the times to seconds. 6. Add an Output Compare function on Bit 3 of PORTT to generate a 1kHz square wave. Use a logic probe to verify that Bit 3 of PORTT is toggling. 100 µs 1000 µs Repeat Figure 2. Friend signal for Part 6. 7. Add an Output Compare function on Bit 4 of PORTT to generate the following signal: The signal consists of five pulses which are high for 100 µs and low for 100 µs, followed by a 1000 µ s low signal. This signal then repeats. 8. Connect your output compare signal to a logic analyzer. Verify that the square wave has a 1kHz frequency and a 50% duty cycle, and that the other signal looks like the signal of Figure 2. Page 9