Chapter 3 Message in a Bottle Programming Loops in C.

Slides:



Advertisements
Similar presentations
What Is Java? The name Java is a trademark of Sun Microsystems
Advertisements

Lecture Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.
Working with C++ Data in Java Presented by: Jessica Winblad This Presentation © 2008 Jessica Winblad.
Chapter 2 Walking in Circles Programming Loops in C.
Constants, Variables, and Data Types
C Language Reference The C language is a general-purpose programming language known for its efficiency, economy, and portability. While these characteristics.
Copyright © 2003 Pearson Education, Inc. Slide 8-1 The Web Wizards Guide to PHP by David Lash.
Chapter 9 – One-Dimensional Numeric Arrays. Array u Data structure u Grouping of like-type data u Indicated with brackets containing positive integer.
Basic Data Structures in Java (c) IDMS/SQL News
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
Literals Why does this produce an error? float x = 2.5;
Custom Commands: Arxterra Control Panel & Android Application Creating A Custom Command Command Control Panel Interface Implementation Example Tommy Sanchez.
EZ-COURSEWARE State-of-the-Art Teaching Tools Teaching Tomorrow’s Technology Today.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Basic Types and Formatted I/O. C Variables Names (1) Variable Names u Names may contain letters, digits and underscores u The first character must be.
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Overview Type conversions Advanced types enum struct array Function parameters by value and by reference Layered Applications Business Logic Layer & Testing.
1 of 31 Images from Africa. 2 of 31 My little Haitian friend Antoine (1985)
1 of 32 Images from Africa. 2 of 32 My little Haitian friend Antoine (1985)
CSci 1130 Intro to Programming in Java
C Language Programming
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
OPTIMIZING C CODE FOR THE ARM PROCESSOR Optimizing code takes time and reduces source code readability Usually done for functions that are critical for.
Chapter 4 NUMB3RS.
Chapter 4 - NUMB3RS Learning to chose the right “types” Exploring The PIC32 - Lucio Di Jasio.
Lab7: Introduction to Arduino
How to use Arduino By: Andrew Hoffmaster.
Slides created by: Professor Ian G. Harris PIC Development Environment MPLAB IDE integrates all of the tools that we will use 1.Project Manager -Groups.
Chapter 6 Under the Hood. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
ECE 353 Computer Systems Lab II VHDL AND LABORATORY TOOLS TUTORIAL Professors Maciej Ciesielski & T. Baird Soules.
1 Key Concepts:  Why C?  Life Cycle Of a C program,  What is a computer program?  A program statement?  Basic parts of a C program,  Printf() function?
COMS W1004 Introduction to Computer Science June 1, 2009.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the structure of a C-language program. ❏ To write your first C.
Variables and constants Applications of Computer Programming in Earth Sciences Instructor: Dr. Cheng-Chien LiuCheng-Chien Liu Department of Earth Sciences.
Embedded Programming and Robotics Lesson 2 C Programming Refresher C Programming1.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
Introducing Java.
Slide 2: About the Textbook By: Dr. Mouaaz Nahas Embedded Systems Umm Al-Qura University Electrical Engineering Department 1.
EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Chapter 3 More Loops. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:
Introduction Purpose This course describes the process of installing the KPIT GNU toolchain on your PC. Objective Learn how easy it is to get information.
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Engineering Computing I Chapter 1 – Part A A Tutorial Introduction.
Java Programming Presented by Daniel Rosenthal Friday, November 30 th, 2007.
/* Documentations */ Pre process / Linking statements Global declarations; main( ) { Local Declarations; Program statements / Executable statements; }
Number Representation, Data Types and Elementary Programming Shirley Moore CS 1401 February 5-7, 2013.
Intro to Programming Web Design ½ Shade Adetoro. Programming Slangs IDE - Integrated Development Environment – the software in which you develop an application.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Main Hardware. Main PC to Board Connection via PX200 Main Hardware.
Microcontrollers, Microcomputers, and Microprocessors
Introduction to Labs Wenchao Cao, Teaching Assistant Department of EECS University of Tennessee.
Chapter 1: Introduction to Computers and Programming.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Programming Environments
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
Val Manes Department of Math & Computer Science
Java package classes Java package classes.
L7 – Assembler Directives
INC 161 , CPE 100 Computer Programming
B065: PROGRAMMING Variables 1.
Programming Language C Language.
Variables and Computer Memory
Variables and Constants
Presentation transcript:

Chapter 3 Message in a Bottle Programming Loops in C

Di Jasio - Programming 32-bit Microcontrollers in C The Plan Variable Declarations DO loops FOR loops Curious loops Arrays declaration and initialization Sending a Message to the world Using the Explorer 16 board Using the PIC32 Starter Kit

Di Jasio - Programming 32-bit Microcontrollers in C Preparation The following software tools will be used in this lesson: MPLAB IDE, Integrated Development Environment (v8.00 or later, free) MPLAB SIM, free software simulator (included in MPLAB installation) MPLAB C32, C compiler (free Student Edition) The following Hardware tools will be used if available: PIC32 Starter Kit or Explorer16 board + In circuit debugger of choice (ICD2 or REAL ICE) The following pieces of documentation will be used during this lesson: PIC32MX Datasheet –DS61143 (latest rev.) PIC32MX Family Reference Manual – DS61120 Section 12. I/O Ports Make sure they are available and/or installed and ready to use on your computer. You can download them from Microchip web site at: And

Di Jasio - Programming 32-bit Microcontrollers in C Variable Declarations signed // declaration MPLAB C32 range of values char c; // short s; // -32, ,767 int i; // -2,147,483,648..2,147,483,647 long l; // -2,147,483,648..2,147,483,647 unsigned // declaration MPLAB C32 range of values unsigned char c; // unsigned short s; // 0..65,535 unsigned int i; // 0..4,294,967,295 unsigned long l; // 0..4,294,967,295

Di Jasio - Programming 32-bit Microcontrollers in C Variable Declarations 64-bit types // declaration MPLAB C32 range of values long long l; // ranges from -2^63 to +2^63-1 unsigned long long l; // ranges from 0 to +2^64 Floating Point types float f; // 32-bit floating point double d;// 64-bit floating point long double d; // 64-bit floating point

Di Jasio - Programming 32-bit Microcontrollers in C DO Loops do { // your code here… } while ( x); The exit test is performed at the end of the loop, after the body ({ }) has been executed at least once.

Di Jasio - Programming 32-bit Microcontrollers in C FOR Loops Assuming i is defined as an integer type variable for ( i=0; i<5; i=i+1) { // insert your code here... // it will be executed for i=0, 1, 2, 3, 4 } Works as a WHILE loop, the exit test is performed before the body({ }) It is perfectly equivalent to: i = 0; // init the index/counter while ( i<5) { // insert your code here... // it will be executed for i= 0, 1, 2, 3, 4 i = i+1; // increment }

Di Jasio - Programming 32-bit Microcontrollers in C More Ways to Write the Main Loop Using a DO loop // main application loop do {... // insert your main loop here... } while ( 1) Using a FOR loop // main application loop for ( ; 1; ) {... // insert your main loop here... }

Di Jasio - Programming 32-bit Microcontrollers in C Increments and Decrements Incrementing a variable i++; is equivalent to: i = i+1; Decrementing a variable i--; is equivalent to: i = i-1;

Di Jasio - Programming 32-bit Microcontrollers in C Another Curious Loop do{ // your code here… } while ( 0); The body is executed once and only once!

Di Jasio - Programming 32-bit Microcontrollers in C Arrays Declarations char c[10]; // declares c as an array of 10 x 8-bit integers short s[10]; // declares s as an array of 10 x 16-bit integers int i[10]; // declares i as an array of 10 x 32-bit integers But remember to start counting from 0…N-1 Some use examples a = c[0]; // copy the value of the 1st element of c ito a c[1] = 123; // assign the value 123 to the second element i[2] = 12345; // assign the value 12,345 to the third element i[3] = 123* i[4]; // compute 123 x the value of the fifth element

Di Jasio - Programming 32-bit Microcontrollers in C Using FOR loops with Arrays This is the most common use of the FOR loop: sequential access to the elements of an array int a[10]; // array of 10 int a[0], a[1], a[2]... a[9] int i; // integer to be used as the loop index for ( i=0; i<10; i++) { a[ i] = 1; }

Di Jasio - Programming 32-bit Microcontrollers in C Sending a Message #include // 1. define timing constants #define SHORT_DELAY 400 #define LONG_DELAY 3200 // 2. declare and initialize an array with the message bitmap char bitmap[30] = { 0xff,// H 0x08, 0xff, 0, 0xff,// E 0x89, 0x81, 0, 0xff,// L 0x80, 0, 0xff,// L 0x80, 0, 0x7e,// O 0x81, 0x7e, 0, 0 };

Di Jasio - Programming 32-bit Microcontrollers in C Sending a Message // 3. the main program main() { // disable JTAG port DDPCONbits.JTAGEN = 0; // 3.1 variable declarations int i; // i will serve as the index // 3.2 initialization TRISA = 0xff00; // PORTA pins connected to LEDs are outputs T1CON = 0x8030; // TMR1 on, prescale 1:256 Tpb=36MHz PR1 = 0xFFFF; // max period (not used) // 3.3 the main loop while( 1) { // display loop, hand moving to the right for( i=0; i<30; i++) { // update the LEDs PORTA = bitmap[i]; // short pause TMR1 = 0; while ( TMR1 < SHORT_DELAY) { } } // for i // long pause, hand moving back to the left PORTA = 0; // turn LEDs off // long pause TMR1 = 0; while ( TMR1 < LONG_DELAY) { } } // main loop } // main

Di Jasio - Programming 32-bit Microcontrollers in C Testing with the Logic Analyzer 1. Build the project using the Project Build check list 2. Open the Logic Analyzer window 3. Click on the Channel button to add, in order, all the I/O pins from RA0 to RA7 connected to the row of LEDs. 4. The MPLAB SIM Setup and Logic Analyzer Setup checklists will help you make sure that you dont forget any detail. 5. Go back to the editor window and set the cursor on the first instruction of the section 6. Right click to select the context menu and choose the Run to Cursor command. This will let the program execute the entire portion containing the message output (3.3.1) and will stop just before the long delay. As soon as the simulation halts on the cursor line, you can switch to the Logic Analyzer window and verify the output waveforms.

Di Jasio - Programming 32-bit Microcontrollers in C Logic Analyzer Output

Di Jasio - Programming 32-bit Microcontrollers in C Testing with the Explorer 16 Requires: Explorer16 demonstration board MPLAB REAL ICE programmer and debugger 1. Use the Setup checklist 2. Use the Device Configuration checklist to verify that the device configuration bits are properly set for use with the Explorer16 demonstration board 3. Use the Programming checklist to program the PIC32 in circuit After dimming the light a bit in the room, you should be able to see the message flashing as you shake the board. Consider adjusting the timing constants for optimal speed Try the values 200 and 1600, respectively for the short and long delays

Di Jasio - Programming 32-bit Microcontrollers in C Testing with the PIC32 Starter Kit Lacking enough LEDs (only three are available) we will use Morse code to send the message: #include // 1. define timing constant #define DOT_DELAY // 2. declare and initialize an array with the message bitmap char bitmap[] = { // H.... 1,0,1,0,1,0,1,0,0,0, // E. 1,0,0,0, // L.-.. 1,0,1,1,1,0,1,0,1,0,0,0, // L.-.. 1,0,1,1,1,0,1,0,1,0,0,0, // --- 1,1,1,0,1,1,1,0,1,1,1, // end of word 0,0,0,0,0 };

Di Jasio - Programming 32-bit Microcontrollers in C Testing with the PIC32 Starter Kit // 3. the main program main() { // 3.1 variable declarations int i; // i will serve as the index // 3.2 initialization TRISD = 0; // all PORTD as output T1CON = 0x8030; // TMR1 on, prescale 1:256 PB=36MHz PR1 = 0xFFFF; // max period (not used) // 3.3 the main loop while( 1) { // display loop, spell a letter at a time for( i=0; i<sizeof(bitmap); i++) { PORTD = bitmap[i]; // short pause TMR1 = 0; while ( TMR1 < DOT_DELAY) { } } // for i } // main loop } // main

Di Jasio - Programming 32-bit Microcontrollers in C Summary In this lesson we learned: How to declare a few basic variable types including integers and floating point of different sizes. How to declare Array and how to initialize them We used Arrays and for loops to send a message using: A shaking LED display A Morse code display

Advanced Material

Di Jasio - Programming 32-bit Microcontrollers in C Notes for the Assembly Experts The ++ and -- operators are smart. If the variable they are applied to is a pointer (a variable type that contains a memory address) they actually increase the address by the exact number of bytes required to represent the quantity pointed to. For example, a pointer to 16-bit integers will increment its address by two, while a pointer to a 32-bit integer will increment its address by four, and so on… The increment and decrement operators can also be applied inside a generic expression to operate before or after a variable content is fetched. See a few examples below (assuming the initial conditions a=0 and b=1): Post-increment a = b++; // a = 1, b = 2 In this first case a is assigned the value of b first, b is incremented later. Pre-increment a = ++b; // a = 2, b = 2 In this second case, b is incremented first and then its (new) value is passed to a.

Di Jasio - Programming 32-bit Microcontrollers in C Notes for the 8-bit PIC MCU Experts In the PIC16 and PIC18, 8-bit architectures there is a strong incentive to use byte-sized integers wherever possible. In the PIC32 32-bit word-sized integers can be manipulated with the same efficiency. The only limiting factor, preventing us from always using 32-bit integers with the MPLAB C32 compiler, is the consideration of the relative scarcity of the RAM memory.

Di Jasio - Programming 32-bit Microcontrollers in C More Notes Notes for PIC Experts It can be confusing to see so many different options to represent a loop (while, do, for) In some situations the algorithm will dictate which one to use In most situations there will be a degree of freedom and more than one type of loop might do. Choose the one that makes the code more readable, and if it really doesnt matter, as in the main loop, just choose the one you like and be consistent. Notes for C experts Learn to keep the size of your variables to the strict minimum necessary. You will become a better embedded-control designer and ultimately … this is what engineering is all about.

Di Jasio - Programming 32-bit Microcontrollers in C Tips and Tricks It is one of the crt0 code responsibilities to initialize variable arrays by copying the contents of from a table in FLASH memory to RAM, immediately before the main program execution. Another useful service performed by the crt0 code is to initialize every remaining globally declared variable to 0. Both operations might require some time to be completed before the main program execution. If you have an urgent task to accomplish before the initializations use the following function: void _on_reset( void) { // something urgent that needs to be done immediately // after a reset or at power up your code here... }

Di Jasio - Programming 32-bit Microcontrollers in C Suggested Exercises Improve the display / hand synchronization, waiting for a button to be pressed before the hand sweep is started Add a switch to sense the sweep movement reversal and play the LED sequence backward on the back sweep

Di Jasio - Programming 32-bit Microcontrollers in C Recommended Readings Rony, P., Larsen D. & Titus J., 1976 THE 8080A BUGBOOK, MICROCOMPUTER INTERFACING AND PROGRAMMING Howard W. Sams & Co., Inc., Indianapolis, IN No high level language programming here, just the basics of assembly programming and hardware interfacing. (Too bad this book is already considered museum material, see link below).

Di Jasio - Programming 32-bit Microcontrollers in C Online Resources A link to the Bugbooks museum, 30 years since the introduction of the INTEL 8080 microprocessor and it is like centuries have already passed. Learn about the Morse code, its history and applications.