Chapter 3 More Loops. Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:

Slides:



Advertisements
Similar presentations
Chapter 3 Message in a Bottle Programming Loops in C.
Advertisements

Chapter 2 Walking in Circles Programming Loops in C.
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
C Language.
Chapter 4 NUMB3RS.
INSTRUCTION SET ARCHITECTURES
Chapter 10- Instruction set architectures
Lab7: Introduction to Arduino
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.
Blackfin BF533 EZ-KIT Control The O in I/O Activating a FLASH memory “output line” Part 2.
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:
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Program Design and Development
Getting the O in I/O to work on a typical microcontroller Ideas of how to send output signals to the radio controlled car. The theory behind the LED controller.
AVR Programming CS-212 Dick Steflik. ATmega328P I/O for our labs To get data into and out of our Arduino its a little trickier than using printf and.
Blackfin BF533 EZ-KIT Control The O in I/O Activating a FLASH memory “output line” Part 2.
Getting the O in I/O to work on a typical microcontroller Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
Railway Foundation Electronic, Electrical and Processor Engineering.
Introduction to Arduino Prepared by R. Lamond.  “Arduino is an open-source electronics prototyping platform based on flexible, easy- to-use hardware.
‘C’ LANGUAGE PRESENTATION.  C language was introduced by Dennis Ritchie..  It is a programming language, which can make a interaction between user and.
Chapter 8 Communication Introduction to serial communication interfaces. Examples of use of the SPI module.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Part 1 Using the ARM board And start working with C Tutorial 5 and 6
Chapter 5 Interrupts.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
Chapter Introduction to Computers and Programming 1.
Introducing Java.
© The McGraw-Hill Companies, 2006 Chapter 1 The first step.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CIS Computer Programming Logic
EEE527 Embedded Systems Lecture 3: Chapter 1: Using MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
C Summary Wilmer Arellano. References Excerpts from the book: Predko, Myke. (2005). 123 PIC MICROCONTROLLER EXPERIMENTS FOR THE EVIL GENIOUS. USA: McGraw-Hill.
The Basic Stamp Instruction Set Architecture. The Microprocessor A microprocessor is a computer that typically has an architecture that is well suited.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
RM2D Let’s write our FIRST basic SPIN program!. The Labs that follow in this Module are designed to teach the following; Turn an LED on – assigning I/O.
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Chapter 2 Introducing the PIC Mid-Range Family and the 16F84A The aims of this chapter are to introduce: The PIC mid-range family, in overview The overall.
ECS642U Embedded Systems Cyclic Execution and Polling William Marsh.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Chapter 7 - Performance Understanding the PIC32 Clock and learning to optimize the memory interface for maximum performance.
EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring
Saxion University of Applied Sciences Advanced Microcontrollers A practical approach.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Processor Structure and Function Chapter8:. CPU Structure  CPU must:  Fetch instructions –Read instruction from memory  Interpret instructions –Instruction.
July 18, UCSD - R.A. de Callafon Short Intro to Micro Processors and I/O functions of our Kinetic Sculpture Control Box Raymond de Callafon.
Chapter 5 - Interrupts.
Microcontrollers, Microcomputers, and Microprocessors
CSIT 301 (Blum)1 Instructions at the Lowest Level Some of this material can be found in Chapter 3 of Computer Architecture (Carter)
1 TM 1 Embedded Systems Lab./Honam University ARM Microprocessor Programming Model.
Introduction 8051 Programming language options: Assembler or High Level Language(HLL). Among HLLs, ‘C’ is the choice. ‘C’ for 8051 is more than just ‘C’
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
Review A program is… a set of instructions that tell a computer what to do. Programs can also be called… software. Hardware refers to… the physical components.
Programming in Arduino Materials:Arduino Board Casperelectronics Pre Pres. Notes Photos from workshop?
CEng3361/18 CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT Spring 2007 Recitation 01.
1 Introduction to Engineering Spring 2007 Lecture 18: Digital Tools 2.
Chapter Overview General Concepts IA-32 Processor Architecture
INTRODUCTION TO ROBOTICS Part 5: Programming
EGR 2261 Unit 11 Pointers and Dynamic Variables
PROGRAMMABLE LOGIC CONTROLLERS SINGLE CHIP COMPUTER
Fundamentals of Computer Engineering
Chapter 10 It’s an Analog World
Introducing the PIC Mid-Range Family and the 16F84A
ECE 3567 Microcontrollers Lab
Presentation transcript:

Chapter 3 More Loops

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used in this lesson:  MPLAB X, Integrated Development Environment (v1.8 or later, free)  MPLAB XC16, C compiler (v1.11 or later, free) The following pieces of documentation will be used during this lesson:  PIC24FJ128GA010 Datasheet –DS39747 (latest rev.)  PIC24 Family Reference Manual - Section 14. Timers 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 16-bit Microcontrollers in C (Second Edition) do Loops do { // your code here... } while ( x);  Notice that we are also re-using the while keyword, don’t let it confuse you!  Quiz: How many times is the loop below going to be executed? do { // your code here... } while ( 0);

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Variables Declarations  Signed integer types:  char c; // from -128 to +127  int c; // from to  long c; // from -2,147,483,648 to + 2,147,483,647  Unsigned integer types:  unsigned char c; // from  unsigned int i; // from 0..65,535  unsigned long x; // from 0..4,294,967,295  Floating point types:  float f; // 32-bit single precision  long double d; // 64-bit double precision

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) for Loops A loop counting from 0 to 4: 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 } Can be expressed more concisely as: for ( i=0; i<5; i=i+1) { // insert your code here … // it will be executed for i=0, 1, 2, 3, 4 }

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Increment and Decrement  Introducing two new operators: Use ++ to increment a variable, i++; is equivalent to: i = i+1; Use -- to decrement a variable, i--; is equivalent to: i = i-1;  More on this later...

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) More Loop Examples  Use the increment operator: for ( i=0; i<5; i++) { // insert your code here … // it will be executed for i= 0, 1, 2, 3, 4 }  Then, a count down from 4 to 0: for ( i=4; i>=0; i--) { // insert your code here … // it will be executed for i= 4, 3, 2, 1, 0 }  Use the for loop to code an (infinite) main program loop(!?): main() { // 0. initialization code … // 1. the main application loop for ( ; 1; ) { … } } // main

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Arrays  Declare an array of integers with the following notation: char c[10]; // declares c as an array of 10 x 8-bit integers int i[10]; // declares i as an array of 10 x 16-bit integers long x[10]; // declares x as an array of 10 x 32-bit integers  Use the square bracket notation to access elements of an array: a = c[0]; // copy the 1 st element of c[] into a c[1] = 123; // assign 123 to the second element of c[] i[2] = 12345; // assign 12,345 to the third element of i[] x[3] = 123* i[4]; // compute 123 x the 5th element of i[] and // assign result to the 4th element of x[]  Use a for loop to access sequentially array elements: int a[10]; // declare array of 10 integers: a[0], a[1] … a[9] int i; // the loop index for ( i=0; i<10; i++) { a[ i] = 1; }

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Sending a Message #include // 1. define timing constant #define SHORT_DELAY 100 #define LONG_DELAY 800 // 2. declare and initialize an // array with the message bitmap char bitmap[30] = { 0b , // H 0b , 0b , 0b , 0b , // E 0b , 0b , 0b , 0b , // L 0b , 0b , 0b , // L 0b , 0b , 0b , // O 0b , 0b , 0b , 0b }; // 3. the main program main() { // 3.1 variable declarations int i; // i will serve as the index // 3.2 initialization TRISA = 0; // all PORTA as output T1CON = 0x8030; // TMR1 on, prescale 1:256 Tclk/2 // 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 TMR1 = 0; while ( TMR1 < LONG_DELAY) { } } // main loop } // main

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) See the Message Now? #include // 1. define timing constant #define SHORT_DELAY 100 #define LONG_DELAY 800 // 2. declare and initialize an // array with the message bitmap char bitmap[30] = { 0b , // H 0b , 0b , 0b , 0b , // E 0b , 0b , 0b , 0b , // L 0b , 0b , 0b , // L 0b , 0b , 0b , // O 0b , 0b , 0b , 0b }; // 3. the main program main() { // 3.1 variable declarations int i; // i will serve as the index // 3.2 initialization TRISA = 0; // all PORTA as output T1CON = 0x8030; // TMR1 on, prescale 1:256 Tclk/2 // 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 TMR1 = 0; while ( TMR1 < LONG_DELAY) { } } // main loop } // main

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) The Message on a Logic Analyzer

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Notes for the Assembly Experts  Most of times the MPLAB XC16 compiler tries to translate ++ and – with inc and dec assembly instructions.  This is not always possible though because the two operators are actually much smarter than that:  If they are applied to a pointer (which is 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 2,  a pointer to a 32-bit long integer will increment its address by 4, and so on.  To satisfy your curiosity, switch to the Disassembly Window and see how the MPLAB XC16 compiler chooses the best assembly code depending on the situation.  Loops in C can be confusing at first. In some situations the algorithm you are coding will dictate which one to use, but in many situations you will have a degree of freedom and more than one type might do. In case of doubt, choose the one that makes your code more readable!

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Notes for the PIC MCU Experts  Depending on the target microcontroller architecture, and ultimately the Arithmetic Logic Unit (ALU) size, operating on bytes versus operating on word quantities can make a big difference in terms of code compactness and efficiency.  While in 8-bit architectures, there is a strong incentive to use byte-sized integers wherever possible, in the PIC24 16-bit architecture word-sized integers can be manipulated just with the same efficiency.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Notes for C Experts  Even if some 16-bit microcontrollers have a relatively large RAM memory array, embedded control applications will always have to contend with the reality of cost and size limitations.  If you learned to program in C on a PC or a workstation, you probably never considered using anything smaller than an int as a loop index.  In Embedded Control, shaving one byte at a time off the requirements of your application might, in some cases, mean the ability to select a smaller model of microcontroller, saving fractions of a dollar that when multiplied by the thousands or millions of units (depending on production run rates), can mean real money saved from the bottom line.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Tips and Tricks  In this last exercise we declared an array called bitmap[] and we asked for it to be pre-filled with a specific series of values.  The array, being a data structure, resides in RAM during execution. But since RAM is volatile, the XC16 compiler has to copy the assigned values (in the curly brackets {} notation) from a non volatile memory (FLASH memory) before the main program execution is started.  This is the kind of task performed in the crt0 code segment.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Configuring the PPS (for GA1 and GB1 users) #include void InitPPS( void) { // SPI1 PPSOutput( PPS_RP15, PPS_SDO1); // SDO1 =RP15 F8/pin 53 // SPI2 PPSInput( PPS_SDI2, PPS_RP26); // SDI2 =RP26 G7/pin 11 PPSOutput( PPS_RP22, PPS_SCK2OUT); // SCK2 =RP21 G6/pin 10 PPSOutput( PPS_RP21, PPS_SDO2); // SDO2 =RP19 G8/pin 12 // UART PPSInput( PPS_U2RX, PPS_RP10); // U2RX =RP10 F4/pin 49 PPSInput( PPS_U2CTS, PPS_RPI32); // U2CTS=RP32 F12/pin40 PPSOutput( PPS_RP17, PPS_U2TX); // U2TX =RP17 F5/pin 50 PPSOutput( PPS_RP31, PPS_U2RTS); // U2RTS=RP31 F13pin 39 // IC PPSInput( PPS_IC1, PPS_RP2); // IC1 =RP2 D8/pin 68 // OC PPSOutput( PPS_RP19, PPS_OC1); // OC1 =RP11 D0/pin 72 PPSOutput( PPS_RP11, PPS_OC2); // OC2 =RP24 D1/pin 76 PPSOutput( PPS_RP24, PPS_OC4); // OC4 =RP22 D3/pin 78 }

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Special Mapping for GB1 PIM Users // GB110 PIM pin-remapping to accomodate additional USB pins // GB110 shares usage of D2/pin 77 between SDI1 and OC3 // pin 54 SDI1 is remapped to Explorer pin 77/D2 // NOTE: we will use it only as OC3 // pin 55 SCK1 is remapped to Explorer pin 25/B0 // NOTE: pin 55 is input only, connecting it to SCK1 // restricts usage to "slave mode" only // pin 56 RG3 is remapped to Explorer pin 89/G1 // pin 57 RG2 is remapped to Explorer pin 90/G0 #ifdef __PIC24FJ256GB110__ PPSOutput( PPS_RP23, PPS_OC3OUT); // OC3=RP23 D2/pin 77 #endif

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Intoducing the EX16.c Module /* ** EX16.c */ #include void InitEX16( void) { // if using a GA1 or GB1 PIM, initialize the PPS module #if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GA110__) #include InitPPS(); #endif // prepare Port A for use with LED bar LATA = 0; // all LED off TRISA = 0xFF00; // all output } // InitEX16

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) EX16.h Header File /* ** EX16.h ** ** Standard definitions for use with the Explorer16 board */ #ifndef _EX16 #define _EX16 #include #if defined(__PIC24FJ256GB110__) || defined(__PIC24FJ256GA110__) #include #endif #define FCY UL // instruction clock 16MHz // prototypes void InitEX16( void); // initialize the Explorer 16 board #endif

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Additional Configs for GA1/GB1  Add the following lines to the config.h file created in lesson 1: #if defined ( __PIC24FJ128GA010__ ) || defined (__PIC24FJ256GA110__) _CONFIG2( IESO_OFF // two speed start up disabled & FCKSM_CSDCMD // disable clock-swithcing/monitor & FNOSC_PRIPLL // primary oscillator: enable PLL & POSCMOD_XT) // primary oscillator: XT mode #else // GB1 configuration requires additional detail _CONFIG2( PLL_96MHZ_ON // enable USB PLL module & PLLDIV_DIV2 // 8MHz/2 = 4Mhz input to USB PLL & IESO_OFF // two speed start up disabled & FCKSM_CSDCMD // disable clock-swithcing/monitor & FNOSC_PRIPLL // primary oscillator: enable PLL & POSCMOD_XT) // primary oscillator: XT mode #endif

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Suggested Excercises  Improve the display / hand synchronization by 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 16-bit Microcontrollers in C (Second Edition) 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).  Shulman, S. (2003), Unlocking the Sky, Glenn Hammond Curtis and the race to invent the Airplane, Harper Collins, New York, NY  A beautiful recount of the “struggle to innovate” in the early days of aviation.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Online Resources  ugBook-Titles.html ugBook-Titles.html  A link to the “Bugbooks museum”. It is 40 years since the introduction of the INTEL 8080 microprocessor and it is like centuries have already passed.