EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring

Slides:



Advertisements
Similar presentations
Chapter 2 Walking in Circles Programming Loops in C.
Advertisements

Getting started with MPLAB Launch MPLAB Set Toolbar as in the next slide by clicking the leftmost icon Open a new Source file by choosing [FILE][NEW] Type.
Microsoft ® Office Word 2007 Training Table of Contents III: Use fields to create a TOC and create multiple TOCs Neeginan Institute of Applied Technology.
Introduction to HT-IDE3000 Micro-C development System Department.
Simple Microcontroller Programming with PIC16F88.
Chapter 4 NUMB3RS.
The 8051 Microcontroller and Embedded Systems
Chung-Ta King National Tsing Hua University
C Examples 1.
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.
Slides created by: Professor Ian G. Harris Inputs and Outputs PIC Vcc RA3 RA4 RA5  Make an LED toggle state when button is pressed  Need to read RA3,
Programing Concept Ken Youssefi/Ping HsuIntroduction to Engineering – E10 1 ENGR 10 Introduction to Engineering (Part A)
Debouncing Switches Mechanical switches are one of the most common interfaces to a uC. Switch inputs are asynchronous to the uC and are not electrically.
Infineon Technologies Corp. June 99 1 HOT167-1 Version 2.0 F PWMmax = x 50ns = 78 kHz Pulse Width Modulation Unit (PWM) (20 MHz)  4 independent.
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 Activating a FLASH memory “output line” Part 1 Main part of Laboratory 1 Also needed for “voice.
Using PDG with e2studio: Example
Railway Foundation Electronic, Electrical and Processor Engineering.
ATMEL AVR 8 bit RISC MICROCONTROLLERS
Cortex-M3 Debugging System
EEE527 Embedded Systems Lecture 8: Practical Interrupts Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring Web site:
Chapter 1 The First Flight Creating the first project and saying “Hello to the World”
Silicon Labs ToolStick Development Platform
Chapter 5 Interrupts.
Renesas Technology America Inc. 1 M16C/Tiny SKP Tutorial 2 Creating A New Project Using HEW4.
Embedded Systems Programming 1 ETEE 3285 Topic HW3: Coding, Compiling, Simulating.
Slides created by: Professor Ian G. Harris Test and Debugging  Controllability and observability are required Controllability Ability to control sources.
PowerFlex 755 DeviceLogix
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.
System Clocks.
Chapter 2 A Loop in the Pattern Designing the Main Loop and Timing.
Chapter 4 TIMER OPERATION
3-1 Digital I/O A group of I/O pins is called a PORT  A port is where data enters/leaves the system. Digital I/O pins are usually grouped into 8,16 or.
EEE527 Embedded Systems Lecture 10B:Chapter 11 Analogue to Digital Convertors (ADCs) (version 2: 25/11/13, see after slide 20 Ian McCrumRoom 5B18, Tel:
ATMEL AVR 8 bit RISC MICROCONTROLLERS a general comparison.
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.
Programmable Logic Training Course HDL Editor
Timers and Interrupts Anurag Dwivedi. Let Us Revise.
EEE527 Embedded Systems Lecture 12: Mini-Project Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring Web site:
Saxion University of Applied Sciences Advanced Microcontrollers A practical approach.
Chapter 5 - Interrupts.
C Examples 1. Download Links dsPIC30F4011/4012 Data Sheet dsPIC30F4013/3014 dsPIC30F Family Reference Manual MikroC MikroC Manual MikroC Quick Reference.
9/20/6Lecture 2 - Prog Model1 MicroBaby A simple micro-controller encompassing all the basics Start this class by organizing into groups.
Department of Electronic & Electrical Engineering Introduction to microcontrollers A microcontroller is a small computer on a single integrated circuit.
Lecture 4 General-Purpose Input/Output NCHUEE 720A Lab Prof. Jichiang Tsai.
Teaching Digital Logic courses with Altera Technology
Chapter Microcontroller
Microcontrollers JULES CALELLA. Microcontrollers vs Microprocessors  Microprocessors – do not contain RAM, ROM, I/O  Microcontrollers – The whole package.
NAM S.B MDLAB. Electronic Engineering, Kangwon National University 1.
Programming PIC 16F84A in Assembly. PIC16F84 pin-out and required external components.
Programming and Debugging with the Dragon and JTAG Many thanks to Dr. James Hawthorne for evaluating the Dragon system and providing the core content for.
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.
Microprocessors A practical approach..
Making a 24hr Timer.
Microcontrollers, Basics Tips and Tricks with PIC MCUs
PROGRAMMABLE LOGIC CONTROLLERS SINGLE CHIP COMPUTER
Topics Introduction Hardware and Software How Computers Store Data
Microcontroller Applications
Microprocessor Systems Design I
Reset Sources and Watchdog Timer
Topics Introduction Hardware and Software How Computers Store Data
CSCI1600: Embedded and Real Time Software
EECE.3170 Microprocessor Systems Design I
EECE.3170 Microprocessor Systems Design I
CSCI1600: Embedded and Real Time Software
EECE.3170 Microprocessor Systems Design I
Presentation transcript:

EEE527 Embedded Systems Lecture 2: Chapter 2: C and Using more MPLAB (X) + XC32 Ian McCrumRoom 5B18, Tel: voice mail on 6 th ring Web site: 24/09/13 "Adapted from the text “Programming 32-bit Microcontrollers in C – Exploring the PIC32, © 2008.” Lucio di Jasio

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C The Plan  The Main Loop  WHILE loops  Logic Expressions  Of TRUE and FALSE in C  DO loops  The first peripheral: Timer1  Debugging with MPLAB SIM  Animations (using single-step repetitively!)  The Logic Analyzer

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C The Structure of an (embedded) C Program  The main() function is where we place our application code  At power up or after a reset, special initialization code ( crt0 ) is executed first  When the main() function returns the _exit() function is called  Both crt0 and _exit() contain default code inserted automatically by the compiler crt0 main() _exit()

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C WHILE Loops while ( x) { // your code here… }

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Logic Expressions  true and false are just integers in C  false is zero  true is everything else!  Logic operators produce true/false results:  || the “logic OR” operator,  && the “logic AND” operator,  !the “logic NOT” operator  ==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.

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Strange Loops while ( 0) { // your code here… } while ( 1) { // your code here… }

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C A First Program: Loops.c #include main() { // initialization DDPCONbits.JTAGEN = 0; // disable the JTAG port TRISA = 0xff00; // PORTA pin 0..7 as output // application main loop while( 1) { PORTA = 0xff; // turn pin 0-7 on PORTA = 0; // turn all pin off }

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C MPLAB SIM: Animation  Use the Project Build checklist to compile and link the “loops.c” program.  Also use the MPLAB SIM simulator Setup checklist to prepare the software simulator.  Use the Animate mode (Debugger>Animate). In this mode, the simulator executes one C program line at a time, pausing shortly after each one to give us the time to observe the immediate results.  Control the simulation speed with the Debug>Settings dialog box, selecting the Animation/Real Time Updates tab, and modifying the Animation Step Time parameter

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Timer1

Timer1  A 16-bit timer  Compatible with 8-bit PIC microcontrollers and 16-bit microcontrollers  Operates off the internal (peripheral) clock or an external input signal  Has a dedicated low power (secondary) oscillator that can also be used to provide a clock signal to the entire system  Can be gated via a separate input pin  A 16-bit register allows for cyclical operation

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Using Timer1 for simple Delays while( TMR1 < DELAY) { // wait }  Tdelay = (Fpb) * Prescaler * DELAY  Fpb peripheral bus frequency  DELAY a constant defined by the user

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Loops.c /* ** Loops */ #include #define DELAY // 256ms main() { // 0. initialization DDPCONbits.JTAGEN = 0; // disable JTAGport, free up PORTA TRISA = 0xff00; // all PORTA as output T1CON = 0x8030; // TMR1 on, prescale 1:256 PB=36MHz PR1 = 0xFFFF; // set period register to max // 1. main loop while( 1) { //1.1 turn all LED ON PORTA = 0xff; TMR1 = 0; while ( TMR1 < DELAY) { // just wait here } // 1.2 turn all LED OFF PORTA = 0; TMR1 = 0; while ( TMR1 < DELAY) { // just wait here } } // main loop } // main

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C MPLAB SIM: Logic Analyzer  Select the Window->Simulator->Analyser.  Under File->Project Properties(Chap2) ->Simulator Settings->Trace-> switch “Data Collection” on.

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Adding Channels  Click on the add/remove PINs button in the logic analyser tab, to bring up the channel selection dialog box  select RA0 and click Add => (or whatever i/o pins you want to “see”  Click on OK

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Set Debug to “run” your program  Experiment with setting a breakpoint and using [F7]  Experiment with setting delay to 1, or even commenting it out all together.  Try a simpler program, dissassemble it and see how many machine cycles are used  Work out how the timings of the LSA (Logic State Analyser) relate to it.  Time a floating point multiplication.

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Note the zoom and delete trace buttons

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Summary In this lesson we learned:  The function of the Main Loop  WHILE and DO loops  Logic Expressions in C  Timer1  How to use the MPLAB SIM simulator:  Animate mode  Logic Analyzer window

Advanced Material

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Notes on Assembly language true | (binary) OR true gives true true || (logical) OR true gives true

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Notes for the 8-bit PIC MCU Experts  Timer0 is gone  All timers are now 16-bit wide  Each timer has a 16-bit period registers  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

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C More Notes  Notes for 16-bit PIC Experts  The peripheral bus and the system bus are now separate to allow better power control when executing code at the highest clock speeds  Notes for C experts  While several Real Time Operating Systems (RTOSs) are available for the PIC32, a large number of applications won’t need and won’t use one. By default, the C32 compiler assumes there is no operating system to return control to  Notes for MIPS experts  The core (32-bit) timer is available as an additional resource to the set of five peripheral timers compatible with the 16-bit PIC microcontrollers

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Tips and Tricks  When writing an application that is “Always ON”, consider the impossible:  Refresh 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,  Also make sure that inside the main loop the initialization functions are called when idling and no other critical task is pending so that every control register is re-initialized periodically

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Peripheral Libraries  Using the peripheral libraries #include  We can replace: TMR1 = 0; T1CON = 0x8030; PR1 = 0xFFFF;  With the following (more portable) code: WriteTimer1( 0); OpenTimer1( T1_ON | T1_PS_1_256, 0xFFFF);

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Suggested Excercises  Output a counter on PortA pins instead of the alternating on and off patterns  Use PortD if you have a PIC32 Starter Kit.  Use a rotating pattern instead of alternating on and off  Re-write the “loops” project using exclusively peripheral library functions to  Control PortA pins  Configure and read the timer  Disable the JTAG port if necessary

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Recommended Readings Ullman, L. & Liyanage, M. (2005) C Programming Peachpit Press, Berkeley, CA.  This is a fast reading and modern book, with a simple step by step introduction to the C programming language

Adapted from Di Jasio - Programming 32-bit Microcontrollers in C Online Resources  A wide perspective on programming languages and the problems related to coding and taming loops.  Your code gets out of control when your loops start knotting…