Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362



Similar documents
EMBEDDED C USING CODEWARRIOR Getting Started Manual

Keil C51 Cross Compiler

Freescale Semiconductor, I

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

ELEG3924 Microprocessor Ch.7 Programming In C

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

Hitchhiker's Guide to CodeWarrior EE371, EE475 Fall 2005

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Lecture 22: C Programming 4 Embedded Systems

Why using ATmega16? University of Wollongong Australia. 7.1 Overview of ATmega16. Overview of ATmega16

Analog-to-Digital Converters. Transducers

SKP16C62P Tutorial 1 Software Development Process using HEW. Renesas Technology America Inc.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Using the HCS12 Serial Monitor on Wytec Dragon-12 boards. Using Motorola s HCS12 Serial Monitor on Wytec s Dragon-12 boards

An Introduction to MPLAB Integrated Development Environment

Definitions and Documents

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Question Bank Subject Name: EC Microprocessor & Microcontroller Year/Sem : II/IV

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Application Note: AN00141 xcore-xa - Application Development

8051 MICROCONTROLLER COURSE

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Embedded C Programming

PC Base Adapter Daughter Card UART GPIO. Figure 1. ToolStick Development Platform Block Diagram

Computer Organization and Components

Quick Start Tutorial. Using the TASKING* Software Development Tools with the Intel 8x930 Family Evaluation Board

Field Software Updates Using TPMS LF An example using the Low Frequency radio (LFR) for wireless software updating

Lab Experiment 1: The LPC 2148 Education Board

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

3. Programming the STM32F4-Discovery

Chapter 13. PIC Family Microcontroller

AN1754 APPLICATION NOTE

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

Serial Communications

Linux Driver Devices. Why, When, Which, How?

Lecture N -1- PHYS Microcontrollers

Informatica e Sistemi in Tempo Reale

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

Introduction to the Altera Qsys System Integration Tool. 1 Introduction. For Quartus II 12.0

MACHINE ARCHITECTURE & LANGUAGE

XBee/XBee PRO Code Development

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

DsPIC HOW-TO GUIDE Creating & Debugging a Project in MPLAB

Comparing RTOS to Infinite Loop Designs

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

V850E2/ML4 APPLICATION NOTE. Performance Evaluation Software. Abstract. Products. R01AN1228EJ0100 Rev Aug. 24, 2012

Tutorial - Creating Your Own Applications

EE8205: Embedded Computer System Electrical and Computer Engineering, Ryerson University. Multitasking ARM-Applications with uvision and RTX

USBSPYDER08 Discovery Kit for Freescale MC9RS08KA, MC9S08QD and MC9S08QG Microcontrollers User s Manual

Work with Arduino Hardware

Migrating Between MC9S08AC and MCF51AC Flexis Devices

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

How to use AVR Studio for Assembler Programming

The stack and the stack pointer

Implementing a Data Logger with Spansion SPI Flash

How to design and implement firmware for embedded systems

C++ INTERVIEW QUESTIONS

Application Note. Introduction AN2471/D 3/2003. PC Master Software Communication Protocol Specification

Introduction. What is an Operating System?

Introduction to Microcontrollers

The C Programming Language course syllabus associate level

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

Serial Communications

Assignment 09. Problem statement : Write a Embedded C program to switch-on/switch-off LED.

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

ADVANCED PROCESSOR ARCHITECTURES AND MEMORY ORGANISATION Lesson-17: Memory organisation, and types of memory

AN10866 LPC1700 secondary USB bootloader

Figure 1. CodeWarrior IDE with Processor Expert Workspace

A Practical Approach to Education of Embedded Systems Engineering

Eric Mitchell April 2, 2012 Application Note: Control of a 180 Servo Motor with Arduino UNO Development Board

AVR Butterfly Training. Atmel Norway, AVR Applications Group

Using C to Access Data Stored in Program Memory on the TMS320C54x DSP

Tutorial for MPLAB Starter Kit for PIC18F

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

SmartFusion csoc: Basic Bootloader and Field Upgrade envm Through IAP Interface

Application Note: AN00121 Using XMOS TCP/IP Library for UDP-based Networking

An Introduction To Simple Scheduling (Primarily targeted at Arduino Platform)

Using the CoreSight ITM for debug and testing in RTX applications

CodeWarrior Development Studio for Freescale S12(X) Microcontrollers Quick Start

C programming for embedded microcontroller systems.

Cornell University Electrical Engineering 476 Video Generation with AVR microcontrollers

Robust Over-the-Air Firmware Updates Using Program Flash Memory Swap on Kinetis Microcontrollers

Lab 1 Course Guideline and Review

Project 11: FreeRTOS TM Real-Time Control of a Stepper Motor

Lesson 10: Video-Out Interface

APPLICATION NOTE. Atmel AT02607: Wireless Product Development Using Atmel Studio and ASF. Atmel MCU Wireless. Description.

Arduino Lesson 5. The Serial Monitor

Buffer Management 5. Buffer Management

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Faculty of Engineering Student Number:

Below is a diagram explaining the data packet and the timing related to the mouse clock while receiving a byte from the PS-2 mouse:

USB Mass Storage Device Host Bootloader

Using the RDTSC Instruction for Performance Monitoring

MICROPROCESSOR AND MICROCOMPUTER BASICS

LC-3 Assembly Language

µtasker Document FTP Client

Transcription:

PURDUE UNIVERSITY Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362 Course Staff 1/31/2012

1 Introduction This tutorial is made to help the student use C language for programing the microcontroller. It assumes that the student knows how to program using C language. For more information about programming in C you can refer to online sources like: http://users.ece.utexas.edu/~valvano/embed/toc1.htm 2 Starting a new project in C The purpose of a using high level language like C is to speedup programming while focusing on functionality. Specific details, such as memory addresses, CPU registers, and assembly language instructions are transparent to the programmer. Such low level details are managed by the language compiler. In CodeWarrior, this is done automatically when a new C project is created. To create a new C project in CodeWarrior follow the steps below: Select File then New Project. The new project wizard will start. From Device and Connection step - Select your processor (MC9S12C32) and connection (P&E USB BDM Multilink). You may also select Full Chip Simulation if you wish to simulate the hardware. From Project Parameters step Select the programming language (C), the project name (ex. Test1.mcp), and the location for the files. Select Finish After CodeWarrior creates your project you will see the project window. In the left-hand window pane you will see a folder structure for the files that are part of your project. The Sources folder contains your source files. You should add you C files here. The Includes folder contains files specific to model of microcontroller you are using. The mc9s12c32.h file declares variable names you can use to read and write the internal registers of your microcontroller. The Libs Folder contains files to support the specific model of microcontroller being used as well as the ANSI-C libraries you may use. The Startup Code folder contains code used to allow the processor to run C-language code. The Linker Files folder, contains files used the CodeWarrior program to compile and link your program. The most important file is the.prm file that specifies the overall memory map of your program (Like RAM/ROM areas) and also provides the ability to register your interrupt service routines (ISR s). It defines SEGMENTS which identify portions of memory and your code and variables can be placed in those segments. The PLACEMENT section of the PRM file defines the names are associated with each segment. 1

3 First Program #include <hidef.h> /* common defines and macros */ #include <MC9S12C32.h> /* derivative information */ #pragma LINK_INFO DERIVATIVE "MC9S12C32" void main(void) { /* put your own code here */ EnableInterrupts; /* keep if interrupts used */ for(;;) { /* wait forever */ /* please make sure that you never leave this function */ Example 1. Skeleton main.c file generated by CodeWarrior. The MC9S12C32.h header has the definitions for the registers. Clearly EnableInterrupts enables interrupts. The microcontroller does not have an operating system that can take over control once the program is finished. This means the program should never exit (loop forever!). 4 Reading and Writing Internal Registers All the internal registers in the microcontroller are defined by variables and can be read and written in the same manner as any other C variable. The definitions for the registers and individual register bits are defined in the header file mc9s12c32.h which can be found under the Includes folder in your project. For example, ports A, B are defined by variables named PORTA, PORTB. You may perform operations on these variables just as you would any other. unsigned char var1 = PORTA; PORTA = 0xf4; /* write value 0xf4 to PORTA */ PORTB = 3 + PORTA; Individual bits in the registers can be accessed using the names defined for each bit. PORTA_BIT0 = 1; if ( PORTA_BIT0 = = 0) {... /* tests bit 0 of PORTA */ Alternatively, you can use bitwise operations such as AND and OR. 2

bit0 = PORTA & 0 x01; variable bit0 */ /* masks all but bit 1 of byte copied to When writing individual bits in the registers it is important to be aware that this will cause the microcontroller to first read the full eight-bit byte, modify the byte, and then write the full byte back. Some of the internal functions of the microcontroller are affected by reading a register so it is important to be aware of any potential side effects when accessing the registers. 5 Using Assembly Language Code in C You can write assembly instructions and call assembly language routines from within your C code by using the asm{ construct. Below are some guidelines for writing assembly code. Write each assembly instruction on a separate line. Labels must be defined on their own line. Global variables can be accessed using their name. Local variables in a function are stored as part of the stack frame and must be accessed using the stack pointer. The stack must be returned to its original state before starting your assembly code. 6 Standard C Library CodeWarrior includes a standard C library that contains many of the routines that programmers are used to having available to use in their programs. Routines like sprintf and sscanf can be used the same way as on larger systems. The location where printf should emit its output is very application specific. Some devices have a serial port, some have multiple and others do have none at all. Because the ANSI library cannot cover all those cases at once, the provided printf implementation calls a TERMIO_PutChar function which is not implemented in the library itself. A sample implementation of TERMIO_PutChar is provided in termio.c, however it's also perfectly fine to just implement "void TERMIO_PutChar(char ch);" as declared in termio.h on your own. The termio.c file contains a second method TERMIO_Init to be called before printf is used, but the ANSI library on its own is not using TERMIO_Init, so implementing this function is optional, of course the serial port controller has to be configured before it is used. In order to use termio.c, copy this file from the library into the project directory and then add it to the project. 7 Interrupt Service Routines Writing interrupts can be done in a couple of ways: 1. The keyword interrupt can be used in the routine declaration, as is shown below. void interrupt myisr( void) { // ISR code 3

2. Another way is to use a #pragma keyword to tell the compiler it is an ISR. # pragma TRAP_PROC void myisr( void) { // ISR code After you have written your ISR, you must enter its address into microcontroller s interrupt vector table. You can register your ISR in the vector table in the PRM file (under Project Settings/Linker Files) of your project. Use the instruction: VECTOR 7 timerisr This will register the routine named timerisr to the vector table entry 7. Note: choose vector number 7 or greater. The other way of initializing it is by address: VECTOR ADDRESS 0xFFF6 myisr where 0xFFF6 is the address of the interrupt vector you are using. You may also use the vector number rather than specifying the vector address. 8 Serial Communications Interface (SCI) The example below shows how to initialize the SCI and how to send and receive characters. void TERMIO_Init(void) { /* initializes the communication channel */ SCI0BDL = 156; /* baud rate 9600 at 8 MHz */ SCI0CR2 = 0x0C; /* 8 bit, TE and RE set */ char TERMIO_GetChar(void) { /* receives character from the terminal channel */ while (!(SCI0SR1 & 0x20)); /* wait for input */ return SCI0DRL; void TERMIO_PutChar(char ch) { /* sends a character to the terminal channel */ while (!(SCI0SR1 & 0x80)); /* wait for output buffer empty */ SCI0DRL = ch; 9 Analog-to-digital (ATD) The example below performs an ATD conversion. 4

void main(void) { ATDCTL2 = 0x80; // power up the ADC and disable interrupts delay_5ms(); // wait for ADC to warm up ATDCTL3 = 0x00; // select active background mode ATDCTL4 = 0x01; // select sample time = 2 ADC clks and set // prescaler to 4 (2 MHz) ADC_convert(); // perform conversion and change to usable value void ADC_convert() { ATDCTL5 = 0x03; // sets up ADC to perform a single conversion, // 4 conversions on a single channel, // and store the results ADR0H - ADR3H. while((atdstat & 0x8000)!= 0x8000){ // Wait for conversion to finish 10 Sample Demo Project To create a sample demo project which uses RTI and ATD follow the steps below: 1. File -> Startup Dialog -> Load Example Project 2. Choose HC12 -> HC12 SIMULATOR -> HCS12 Analog to Digital demos -> Frequency Meter 3. Fill the project name and location on the right side of the window and create the project. 5

Appendix A.1 Preprocessor Directives Preprocessor directives begin with # in the first column. Preprocessor commands are processed first by the compiler. The two most important are the macro definition (#define) used to define I/O ports and bit fields. A second important directive is the #include, which allows you to include another. A.2 Data types and variables Data type declaration Number of bits Range of values char k; 8 0..255 unsigned char k; signed char k; 8-128..+127 int k; 16-32768..+32767 signed int k; short k; signed short k; unsigned int k; 16 0..65535 unsigned short k; Table 2. Data type definitions for a variable k in the CodeWarrior C compiler. A volatile variable might represent a location that can be changed by other elements of the computer system at any time, such as a hardware register of a device connected to the CPU. The program would never detect such a change; without the volatile keyword, the compiler assumes that the current program 6

is the only part of the system that could change the value. The most common use of volatile variable definitions in embedded systems will be for the various I/O ports and module registers of the microcontroller. For example, the following defines the addresses of I/O ports A and B. #define PORTA (*((volatile unsigned char*)(0x0000))) #define PORTB (*((volatile unsigned char*)(0x0001))) A.3 Operators operation Meaning = assignment statement @ address of? Selection < less than > greater than! logical not (true to false, false to true) ~ 1's complement + Addition - Subtraction * multiply or pointer reference / Divide % modulo, division remainder logical or & logical and, or address of ^ logical exclusive or. used to access parts of a structure operation Meaning == equal to comparison <= less than or equal to >= greater than or equal to!= not equal to << shift left >> shift right ++ increment -- decrement && Boolean and Boolean or += add value to -= subtract value to *= multiply value to /= divide value to = or value to &= and value to ^= exclusive or value to <<= shift value left >>= shift value right %= modulo divide value to -> pointer to a structure Note the difference between bit-parallel operators & (AND) and (OR), which produce 8-bit results of the corresponding logical operation, and relational operators && (AND) and (OR), which produce TRUE/FALSE results (any non-zero value is considered TRUE and a zero value is considered FALSE ). Consider the difference between the following two statements, where a and b are 8-bit variables: if (a & b) /* bitwise AND of variables a and b */ if (a && b) /* requires a to be TRUE and b to be TRUE */ 7

In the first case, the bitwise logical AND of the 8-bit values of a and b is computed, and if all bits = 0, the result would be FALSE, otherwise the result would be TRUE. In the second case, the result is TRUE if variable a is TRUE and if variable b is TRUE. 8