Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.

Slides:



Advertisements
Similar presentations
Introduction to PIC Microcontrollers
Advertisements

Chapter 3 Message in a Bottle Programming Loops in C.
Chapter 2 Walking in Circles Programming Loops in C.
EMS1EP Lecture 4 Intro to Programming Dr. Robert Ross.
Simple Microcontroller Programming with PIC16F88.
Chapter 4 NUMB3RS.
Assembly Language for Intel-Based Computers, 4 th Edition Chapter 1: Basic Concepts (c) Pearson Education, All rights reserved. You may modify and.
Lab7: Introduction to Arduino
C Examples 1.
 Suppose for a moment that you were asked to perform a task and were given the following list of instructions to perform:
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.
Microprocessor and Microcontroller
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)
Chapter 4 Processor Technology and Architecture. Chapter goals Describe CPU instruction and execution cycles Explain how primitive CPU instructions are.
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.
Introduction to a Programming Environment
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.
Chapter 8 Communication Introduction to serial communication interfaces. Examples of use of the SPI module.
ARDUINO PROGRAMMING Working with the Arduino microcontroller.
Working with Arduino: Lesson #1: Getting Acquainted with the Kit EGN1007.
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Serial Communication ETEC 6416.
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.
Development. Development Environment Editor Assembler or compiler Embedded emulator/debugger IAR Embedded Workbench Kickstart Code Composer Essentials.
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
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 to Python
Timers The timers of the PIC16C7X microcontroller can be briefly described in only one sentence. There are three completely independent timers/counters.
Microprocessors A practical approach Subjects Goals for this module Results Subjects of the module Plans Questions.
1 Workshop Topics - Outline Workshop 1 - Introduction Workshop 2 - module instantiation Workshop 3 - Lexical conventions Workshop 4 - Value Logic System.
Chapter 0 Getting Started. Objectives Understand the basic structure of a C++ program including: – Comments – Preprocessor instructions – Main function.
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.
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
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.
Renesas Technology America Inc. 1 SKP8CMINI Tutorial 2 Creating A New Project Using HEW.
Chapter 5 - Interrupts.
LHO 22 C and the  The Silicon Labs ISE uses the Keil C51 compiler.  The code size is limiter to 2K  C has replaced PL/M (the original Intel high.
C Examples 1. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
Teaching Digital Logic courses with Altera Technology
Addressing Modes. Register Addressing Immediate Addressing Base Addressing Indexed Addressing PC-Relative Addressing.
Chapter Microcontroller
5-1-2 Synchronous counters. Learning Objectives: At the end of this topic you will be able to: draw a block diagram showing how D-type flip-flops can.
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
CEng3361/18 CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT Spring 2007 Recitation 01.
DEPARTMENT OF ELECTRONICS ENGINEERING V-SEMESTER MICROPROCESSOR & MICROCONTROLLER 1 CHAPTER NO microcontroller & programming.
One more PIC18F252 example and revision for exam B222L Branislav Vuksanovic, UoP, ECE.
The 8085 Microprocessor Architecture
Microprocessor Systems Design I
The 8085 Microprocessor Architecture
Chapter 1: The 8051 Microcontrollers
Computers & Programming Languages
Working with Arduino: Lesson #1: Getting Acquainted with the Kit
Chapter 10 It’s an Analog World
Introduction to Java, and DrJava part 1
Topics Introduction Hardware and Software How Computers Store Data
EECE.3170 Microprocessor Systems Design I
The 8085 Microprocessor Architecture
Introduction to Java, and DrJava
EECE.3170 Microprocessor Systems Design I
CISC101 Reminders Labs start this week. Meet your TA! Get help with:
Introduction to Java, and DrJava part 1
Review of Previous Lesson
ECE 3567 Microcontrollers Lab
Presentation transcript:

Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Checklist The following tools will be used throughout the course:  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) A New Project  Use the New Project wizard to create a new project  Call it: “2-ALoopInThePattern”  Use the New File wizard to create a new source file  Call it: Loop.c /* * File: Loop.c * Author: your name here * * Created current date here */ #include int main( void) { return 0; } But you can also use the form: void main( void) { // no return required } Or simply: main() { } But you can also use the form: void main( void) { // no return required } Or simply: main() { }

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) “while” Loops  In C there are several ways to design a loop. The first we are going to explore is the “while” loop: while ( x) { // your code here… }  Where ( X) is a logical expression  false is represented as the integer zero  true is represented by any integer except zero

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Logical Operators  || the logic OR operator,  && the logic AND operator,  ! the logic NOT operator  These operators consider their operands as logical (Boolean) values using the rule mentioned above, and they return a logical value.  Here are some trivial examples, assuming:  a = 17  b = 1  ( a || b) is true,  ( a && b) is true  ( !a) is false In other words, they are both “true”

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Comparison Operators  There are, then, a number of operators that compare numbers (integers of any kind and floating point values, too) and return logic values.  They are:  ==the equal-to operator,  !=the NOT-equal to operator.  >the greater-than operator.  >=the greater-or-equal to operator.  <the less-than operator.  <=the less-or-equal to operator.  Here are some examples, assuming:  a = 10  ( a > 1) is true  (-a >= 0) is false  ( a == 17) is false  ( a != 3) is true Composed of two equal signs to distinguish it from the assignment operator ‘=‘ we used in the previous lesson.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Curious Cases while ( 0) { // your code here… } while ( 1) { // your code here… } Will never execute this code! Will execute this code for ever!

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Writing a Main Loop  It is time to add a few new lines of code to the ‘loop.c’ source file and put the while loop to good use. main() { // init control registers TRISA = 0xff00; // all PORTA as output // main application loop while( 1) { PORTA = 0xff; PORTA = 0; } // main loop } // main

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Animation  NOTE: The Animation feature has been removed from MPLAB X!

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Not so fast, please!  Run > Run Program from the main menu.  MPLAB X will now recompile the program for immediate execution. It will be downloaded into the PIC24 flash memory and execution will start immediately  NOTE: When in run mode, there is no animated icon and MPLAB X seems inactive, but the target PIC24 is alive and executing continuously the application at full speed  Warning: You will not be able to see any flashing of the LED bar!  This is due to a limitation of our human eyes.  The PIC24 is actually turning the LEDs on and off but, assuming our default configuration of the main oscillator (32MHz), this is happening at the rate of several million times per second!

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Using a Timer  Timer1 Block Diagram:

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Configuring Timer1: T1CON  Use the following basic configuration:  Activate Timer1: TON = 1  Use the MCU clock as the source (Fosc/2): TCS = 0  Set the prescaler to the maximum value (1:256): TCKPS = 11  The input gating and synchronization functions are not required, since we use the MCU internal clock directly as the timer clock: TGATE = 0, TSYNC = 0  Do not worry about the behavior in IDLE mode, for now: TSIDL = 0 (default)  Once we assemble all the bits into a single 16-bit binary value, we get:  T1CON = 0b ;  Or in a more compact hexadecimal notation:  T1CON = 0x8030;

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) A Timed Loop #include #define DELAY main() { // init control registers TRISA = 0xff00; // all PORTA as output T1CON = 0x8030; // TMR1 on, prescale 1:256 Tclk/2 // main application loop while( 1) { //1. turn pin 0-7 on and wait for 1/4 of a second PORTA = 0xff; TMR1 = 0; while ( TMR1 < DELAY) { } // 2. turn all pin off and wait for a 1/4 of a second PORTA = 0; TMR1 = 0; while ( TMR1 < DELAY) { } } // main loop } // main

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Notes for Assembly Experts  Logic vs. Binary operators in C:  Binary logic operators take pairs of bits from each operand and compute the result according to the defined table of truth.  Logic operators, look at each operand (independently of the number of bits used) as a single Boolean value.  Example on byte sized operands: (true) binary OR logical OR (true) gives gives (true)

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Notes for PICmicro Experts  Difference between 8-bit PICmicros and the PIC24:  There is no Timer0  All timers are 16-bit wide.  Each timer has a 16-bit period registers (PR).  A new 32-bit mode timer-pairing mechanism is available for Timer2/3 and Timer4/5.  A new external clock gating feature has been added on Timer1.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Tips and Tricks  When designing applications that have to operate reliably on large time scales (months, years… ), consider providing a periodic refresh of the most important control registers of the essential peripherals used by the application.  Group the sequence of initialization instructions in one or more functions.  Call the functions once at power up, before entering the main loop, but also make sure that inside the main loop the initialization functions are called when no other critical task is pending and every control register is re-initialized periodically.

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Suggested Excercises  Output a counter on the PortA pins instead of the alternating on and off patterns.  Use a rotating pattern instead of alternating on and off

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Recommended Readings  Ullman, L. & Liyanage, M. (2005), C Programming, Peachpit Press, Berkeley, CA.  Adams, N. (2003), The flyers, in search of Wilbur and Orville Wright, Three Rivers Press, New York, NY

Di Jasio – Programming 16-bit Microcontrollers in C (Second Edition) Online Resources   A wide perspective on programming languages and the problems related to coding and taming loops.