Lecture 22: C Programming 4 Embedded Systems

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

Keil C51 Cross Compiler

Microcontroller Systems. ELET 3232 Topic 8: Slot Machine Example

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

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

Introduction to Data Structures

The programming language C. sws1 1

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

The stack and the stack pointer

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

ELEG3924 Microprocessor Ch.7 Programming In C

How to use AVR Studio for Assembler Programming

Interface and Simulation of a LCD Text Display

Top 10 Bug-Killing Coding Standard Rules

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

Introduction to Java

Introduction to MIPS Assembly Programming

AVR034: Mixing C and Assembly Code with IAR Embedded Workbench for AVR. 8-bit Microcontroller. Application Note. Features.

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

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)

Nemo 96HD/HD+ MODBUS

Informatica e Sistemi in Tempo Reale

Lab Experience 17. Programming Language Translation

MPLAB TM C30 Managed PSV Pointers. Beta support included with MPLAB C30 V3.00

X86-64 Architecture Guide

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

CS 3530 Operating Systems. L02 OS Intro Part 1 Dr. Ken Hoganson

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

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

Embedded C Programming

5 Arrays and Pointers

CS 241 Data Organization Coding Standards

ENGI E1112 Departmental Project Report: Computer Science/Computer Engineering

How To Write Portable Programs In C

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

arrays C Programming Language - Arrays

6.S096 Lecture 1 Introduction to C

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

Sources: On the Web: Slides will be available on:

13 Classes & Objects with Constructors/Destructors

CSC230 Getting Starting in C. Tyler Bletsch

Faculty of Engineering Student Number:

OpenACC 2.0 and the PGI Accelerator Compilers

Passing 1D arrays to functions.

System Calls and Standard I/O

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Embedded Software development Process and Tools: Lesson-4 Linking and Locating Software

Mixing C and assembly language programs Copyright 2007 William Barnekow <barnekow@msoe.edu> All Rights Reserved

Embedded Systems Design Course Applying the mbed microcontroller

TODAY, FEW PROGRAMMERS USE ASSEMBLY LANGUAGE. Higher-level languages such

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Chapter 5 Instructor's Manual

public static void main(string[] args) { System.out.println("hello, world"); } }

ASSEMBLY PROGRAMMING ON A VIRTUAL COMPUTER

C# and Other Languages

Chapter 3: Operating-System Structures. System Components Operating System Services System Calls System Programs System Structure Virtual Machines

Compiler Construction

Common Beginner C++ Programming Mistakes

Figure 1: Graphical example of a mergesort 1.

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

J a v a Quiz (Unit 3, Test 0 Practice)

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

Jorix kernel: real-time scheduling

Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers. 8-bit Atmel Microcontrollers. Application Note.

Chapter 6, The Operating System Machine Level

C / C++ and Unix Programming. Materials adapted from Dan Hood and Dianna Xu

CSI 402 Lecture 13 (Unix Process Related System Calls) 13 1 / 17

MatrixSSL Porting Guide

While Loop. 6. Iteration

Variables, Constants, and Data Types

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

Chapter 11: Input/Output Organisation. Lesson 06: Programmed IO

Moving from CS 61A Scheme to CS 61B Java

Chapter 7D The Java Virtual Machine

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Microtronics technologies Mobile:

Java CPD (I) Frans Coenen Department of Computer Science

Virtual Servers. Virtual machines. Virtualization. Design of IBM s VM. Virtual machine systems can give everyone the OS (and hardware) that they want.

Embedded system programming: HCS12

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

EXERCISE 3: String Variables and ASCII Code

Special Topics for Embedded Programming. Reference: The C Programming Language by Kernighan & Ritchie

MACHINE INSTRUCTIONS AND PROGRAMS

Semantic Analysis: Types and Type Checking

Software based Finite State Machine (FSM) with general purpose processors

Object Oriented Software Design

Goals for This Lecture:

1 The Java Virtual Machine

C++ Programming Language

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

REAL TIME OPERATING SYSTEM PROGRAMMING-II: II: Windows CE, OSEK and Real time Linux. Lesson-12: Real Time Linux

Help on the Embedded Software Block

Answers to Review Questions Chapter 7

Statements and Control Flow

Chapter 11 I/O Management and Disk Scheduling

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Lab 4.4 Secret Messages: Indexing, Arrays, and Iteration

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Transcription:

Lecture 22: C Programming 4 Embedded Systems

Today s Goals Basic C programming process Variables and constants in C Pointers to access addresses

Using a High Level Language High-level languages More human readable Less dependent on processors Less source code, generally C programming language Developed in 1972 by Dennis Ritchie at the Bell Laboratories. Named C because it is derived from an earlier language B. Closely related to the development of Unix OS. Unix was originally written in assembly language on a PDP-7 Needed to port PDP-11. It led to the development of an early version of C The original PDP-11 version of the Unix system was developed in assembly language. Later, most of the Unix kernel was rewritten in C. Well suited for embedded systems.

C for an Embedded System We won t explicitly discuss C syntax. We will focus on C for embedded systems. Topics that we will discuss on the next three lectures Definition of variables and constants Calling assembly program from C Using multiple files Parameter passing in C Interrupt handling in C

Constant Declaration #define C has a method of defining constants much like we define constants in assembly. Declaring constants does not use any memory just like in assembly. The values defined are used during compiling source code. is equivalent to

Basic Data Types Variables in C can be defined as either signed or unsigned. In assembly, programmers have responsibilities to choose right version of instruction when using comparison instructions. In C, the compiler chooses the proper comparison according to the variable types (signed or unsigned). Data Types char (character) 1 byte or 8 bits int (integer) 2 bytes or 16 bits (note: it depends on the processor. We have 16 bit integer since we are using 16 bit processor.) long 4 bytes or 32 bits ** You can use unsigned before the data type if you want to explicitly use unsigned data type.

Examples Convert the following C variable definition into assembly code. Note: C unsigned char count; char count; unsigned int rti_ints; long profit; unsigned char mylist[4]; The assembly declarations for signed and unsigned values are the same no distinction is made. Arrays in C use square brackets. Assembly In assembly, a label represents the address of the value. In C, a variable represents a value. But an array name (mylist) is the address of the first item in the array.

Volatile Variables for input ports should be defined as volatile A variables declared to be volatile will not be optimized by the compiler because the compiler must assume that the value can be changed at any time. int foo = 0; void bar (void) { while (!foo) ; } Optimize int foo = 0 void bar (void) { while (1) ; } What if I have an interrupt service routine something like this? interrupt void isr_foo (void) { if(button_pressed) foo = 1; }

Getting to Specific Addresses In the previous variable definitions, we created a variable that the complier assigned to some, random address, and any assignments to that variable name change the memory contents. So if we said unsigned char DDRB; DDRB = 0xFF; The one byte at address DDRB is changed. But.. Well.. This is not we want. In assembly, DDRB is used to refer to a control register at address 0x0003. How can we do this? Answer! Use pointers!!

Pointers in C In C, unlike assembly, a label (a variable) can represent either the value of a variable or the addresses of a variable. Pointers in C

Examples The following lines of code demonstrate how pointers function. Examples int* var1; var1 = 0x1000; *var1 = 0x1234;

Definitions for I/O Ports In your C program, you want to use the same labels used in assembly program. DDRB, PORTB, Here is a way. Examples Anatomy of the definitions and usages 0xFF will be set to address 0x0003 as the content of it.

Examples Set PORTB and P to all output: #define PORTB (*(char *) 0x0001) #define DDRB (*(char *) 0x0003) #define PORTP (*(char *) 0x0258) #define DDRP (*(char *) 0x025A) #define PORTH (*(char *) 0x0260) #define DDRH (*(char *) 0x0262) #define PIEH (*(char *) 0x0266) #define PIFH (*(char *) 0x0267) Wait for bit 0 of PORTH to be 1: Enable the left most 7 segment display (bit 3 of PORTP to 0) and disable the other three digits without affecting other bits. Clear the flag bit for PORTP bit 7

Variable Scope int c; /* Global variable */ void foo() { int c = 0; /* Declared in outer block */ do { int c = 0; /* This is another variable called c */ ++c ; /* this applies to inner c */ printf("\n c = %d ", c ); } while( ++c <= 3 ); /* This works with outer c */ } /* Inner c is dead, this is outer */ printf("\n c = %d\n", c ); All constants and variables have scope In other words, the values they hold are accessible in some parts of the program, where as in other parts, they don't appear to exist. There are 4 types of scope: block, function, file and program scope. Each one has its own level of scope.

Variable Scope One more example int sum; /* global variable */ /* temp is not available inside incsum */ void incsum(void){ sum++; } Void foo(void) { int temp = 3; sum = 0; } for(int count=0; count < 10; count++) { sum = sum + temp; } // count no longer exists in some compilers

Questions?

Wrap-up What we ve learned C programming in embedded systems

What to Come More about C programming in embedded systems