Chapter 2 Walking in Circles Programming Loops in

  • Slides: 26
Download presentation
Chapter 2 Walking in Circles Programming Loops in C

Chapter 2 Walking in Circles Programming Loops in C

The Plan The Main Loop q WHILE loops q Logic Expressions q q Of

The Plan The Main Loop q WHILE loops q Logic Expressions q q Of TRUE and FALSE in C DO loops q The first peripheral: Timer 1 q Debugging with MPLAB SIM q Animations q The Logic Analyzer q Di Jasio - Programming 32 -bit Microcontrollers in C

Preparation The following tools will be used in this lesson: MPLAB IDE, Integrated Development

Preparation The following tools will be used in this lesson: MPLAB IDE, Integrated Development Environment (v 8. 00 or later, free) q MPLAB SIM, free software simulator (included in MPLAB installation) q MPLAB C 32, C compiler (free Student Edition) The following pieces of documentation will be used during this lesson: q PIC 32 MX Datasheet –DS 61143 (latest rev. ) q PIC 32 MX Family Reference Manual – DS 61120 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: http: //www. microchip. com/mplab And http: //www. microchip. com/c 32 q Di Jasio - Programming 32 -bit Microcontrollers in C

The Structure of an (embedded) C Program q q The main() function is where

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

WHILE Loops while ( x) { // your code here… } Di Jasio -

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

Logic Expressions q q true and false are just integers in C false is

Logic Expressions q q true and false are just integers in C false is zero true is everything else! Logic operators produce true/false results: q q q q q || && ! == != > >= < <= 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. Di Jasio - Programming 32 -bit Microcontrollers in C

Strange Loops while ( 0) { // your code here… } while ( 1)

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

A First Program: Loops. c #include <p 32 xxxx. h> main() { // initialization

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

MPLAB SIM: Animation q q Use the Project Build checklist to compile and link

MPLAB SIM: Animation q q 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 Di Jasio - Programming 32 -bit Microcontrollers in C

Timer 1 Di Jasio - Programming 32 -bit Microcontrollers in C

Timer 1 Di Jasio - Programming 32 -bit Microcontrollers in C

Timer 1 A 16 -bit timer q Compatible with 8 -bit PIC microcontrollers and

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

Using Timer 1 for simple Delays while( TMR 1 < DELAY) { // wait

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

Loops. c /* ** Loops */ #include <p 32 xxxx. h> #define DELAY 36000

Loops. c /* ** Loops */ #include <p 32 xxxx. h> #define DELAY 36000 // 256 ms main() { // 0. initialization DDPCONbits. JTAGEN = 0; // disable JTAGport, free up PORTA TRISA = 0 xff 00; // all PORTA as output T 1 CON = 0 x 8030; // TMR 1 on, prescale 1: 256 PB=36 MHz PR 1 = 0 x. FFFF; // set period register to max // 1. main loop while( 1) { //1. 1 turn all LED ON PORTA = 0 xff; TMR 1 = 0; while ( TMR 1 < DELAY) { // just wait here } // 1. 2 turn all LED OFF PORTA = 0; TMR 1 = 0; while ( TMR 1 < DELAY) { // just wait here } } // main loop } // main Di Jasio - Programming 32 -bit Microcontrollers in C

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

MPLAB SIM: Logic Analyzer q q q 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. Di Jasio - Programming 32 -bit Microcontrollers in C

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

Adding Channels q q q Click on the Channels button, to bring up the channel selection dialog box select RA 0 and click Add => Click on OK Di Jasio - Programming 32 -bit Microcontrollers in C

Di Jasio - Programming 32 -bit Microcontrollers in C

Di Jasio - Programming 32 -bit Microcontrollers in C

Summary In this lesson we learned: q The function of the Main Loop q

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

Advanced Material

Advanced Material

Notes for the Assembly Experts 11110101 | 00001000 gives 11111101 true (binary) OR true

Notes for the Assembly Experts 11110101 | 00001000 gives 11111101 true (binary) OR true 11110101 || 00001000 true (logical) OR true gives 00000001 true Di Jasio - Programming 32 -bit Microcontrollers in C

Notes for the 8 -bit PIC MCU Experts Timer 0 is gone q All

Notes for the 8 -bit PIC MCU Experts Timer 0 is gone q All timers are now 16 -bit wide q Each timer has a 16 -bit period registers q A new 32 -bit mode timer-pairing mechanism is available for Timer 2/3 and Timer 4/5 q A new external clock gating feature has been added on Timer 1 q Di Jasio - Programming 32 -bit Microcontrollers in C

More Notes q Notes for 16 -bit PIC Experts q q Notes for C

More Notes q Notes for 16 -bit PIC Experts q q Notes for C experts q q The peripheral bus and the system bus are now separate to allow better power control when executing code at the highest clock speeds While several Real Time Operating Systems (RTOSs) are available for the PIC 32, a large number of applications won’t need and won’t use one. By default, the C 32 compiler assumes there is no operating system to return control to Notes for MIPS experts q 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 Di Jasio - Programming 32 -bit Microcontrollers in C

Tips and Tricks q When writing an application that is “Always ON”, consider the

Tips and Tricks q 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 q Group the sequence of initialization instructions in one or more functions. q Call the functions once at power up, before entering the main loop, q 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 q Di Jasio - Programming 32 -bit Microcontrollers in C

Peripheral Libraries Using the peripheral libraries #include <plib. h> q We can replace: TMR

Peripheral Libraries Using the peripheral libraries #include <plib. h> q We can replace: TMR 1 = 0; T 1 CON = 0 x 8030; PR 1 = 0 x. FFFF; q With the following (more portable) code: Write. Timer 1( 0); Open. Timer 1( T 1_ON | T 1_PS_1_256, 0 x. FFFF); q Di Jasio - Programming 32 -bit Microcontrollers in C

Suggested Excercises q Output a counter on Port. A pins instead of the alternating

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

Recommended Readings Ullman, L. & Liyanage, M. (2005) C Programming Peachpit Press, Berkeley, CA.

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

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

Online Resources http: //en. wikipedia. org/wiki/Control_flow#Loops q A wide perspective on programming languages and the problems related to coding and taming loops. http: //en. wikipedia. org/wiki/Spaghetti_code q Your code gets out of control when your loops start knotting… Di Jasio - Programming 32 -bit Microcontrollers in C