Top Banner
Chapter 2 Chapter 2 Walking in Circles Walking in Circles Programming Loops in C
26

Chapter 2 Walking in Circles Programming Loops in C.

Mar 26, 2015

Download

Documents

Olivia Morrow
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Chapter 2 Walking in Circles Programming Loops in C.

Chapter 2Chapter 2 Walking in Circles Walking in Circles

Programming Loops in C

Page 2: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

The PlanThe 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 The Logic Analyzer

Page 3: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

PreparationPreparationThe following 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 pieces of documentation will be used during this lesson: PIC32MX Datasheet –DS61143 (latest rev.) PIC32MX Family Reference Manual – DS61120

Section 12. I/O PortsMake sure they are available and/or installed and ready to use on your

computer.You can download them from Microchip web site at:

http://www.microchip.com/mplab And

http://www.microchip.com/c32

Page 4: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

The Structure of an The Structure of an (embedded) C Program(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()

Page 5: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

WHILE LoopsWHILE Loops

while ( x){ // your code here…}

Page 6: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Logic ExpressionsLogic 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.

Page 7: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Strange LoopsStrange Loops

while ( 0){ // your code here…}

while ( 1) { // your code here…}

Page 8: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

A First Program: Loops.cA First Program: Loops.c

#include <p32xxxx.h>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 } }

Page 9: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

MPLAB SIM: AnimationMPLAB 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

Page 10: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Timer1Timer1

Page 11: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Timer1Timer1 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

Page 12: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Using Timer1 for simple DelaysUsing Timer1 for simple Delays

while( TMR1 < DELAY){ // wait} Tdelay = (Fpb) * Prescaler * DELAY Fpb peripheral bus frequency DELAY a constant defined by the user

Page 13: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Loops.cLoops.c/*** Loops*/#include <p32xxxx.h>#define DELAY 36000 // 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

Page 14: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

MPLAB SIM: Logic AnalyzerMPLAB SIM: Logic Analyzer Select the Debug>Settings dialog box and then choose

the Osc/Trace tab. In the Tracing options section, check the Trace All box. Now you can open the Analyzer window, from the View-

>Simulator Logic Analyzer menu.

Page 15: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Adding Channels Adding Channels Click on the Channels button, to bring up the channel

selection dialog box select RA0 and click Add => Click on OK

Page 16: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Page 17: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

SummarySummary

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

Page 18: Chapter 2 Walking in Circles Programming Loops in C.

Advanced MaterialAdvanced Material

Page 19: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Notes for the Assembly ExpertsNotes for the Assembly Experts

11110101 true | (binary) OR

00001000 true

gives 11111101 true

11110101 true|| (logical) OR

00001000 true

gives 00000001 true

Page 20: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Notes for the 8-bit PIC MCU ExpertsNotes 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

Page 21: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

More NotesMore 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

Page 22: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Tips and TricksTips 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

Page 23: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Peripheral LibrariesPeripheral Libraries Using the peripheral libraries

#include <plib.h>

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);

Page 24: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Suggested ExcercisesSuggested 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

Page 25: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Recommended ReadingsRecommended ReadingsUllman, L. & Liyanage, M. (2005)C ProgrammingPeachpit Press, Berkeley, CA.

This is a fast reading and modern book, with a simple step by step introduction to the C programming language

Page 26: Chapter 2 Walking in Circles Programming Loops in C.

Di Jasio - Programming 32-bit Microcontrollers in C

Online ResourcesOnline Resources

http://en.wikipedia.org/wiki/Control_flow#Loops A wide perspective on programming languages and

the problems related to coding and taming loops.

http://en.wikipedia.org/wiki/Spaghetti_code Your code gets out of control when your loops start

knotting…