Introduction to Microcontrollers Lab Manual - Microchip
Introduction to Microcontrollers Lab Manual - Microchip
Introduction to Microcontrollers Lab Manual - Microchip
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
0.3 THE LAB<br />
0.2.5 I/O Ports<br />
The ability of the microcontroller <strong>to</strong> execute code is meaningless unless it can take<br />
inputs from its environment and drive outputs <strong>to</strong> control the environment. The digital<br />
I/O port is a primary method of doing just this.<br />
<strong>Microchip</strong>'s PIC microcontrollers organize the digital I/Os in<strong>to</strong> “ports”. Each port on the<br />
PIC24 is a collection of up <strong>to</strong> 16 individual I/Os, and they are named PORTA, PORTB,<br />
PORTC, etc. These I/Os are easily configurable and can be accessed individually or<br />
as a group. For now, we will discuss the ability <strong>to</strong> use them as digital inputs or digital<br />
outputs.<br />
All port pins have at least three registers directly associated with their operation as digital<br />
I/Os. The Data Direction register (TRISA, TRISB, TRISC, …) determines whether<br />
the pin is an input or an output. If the data direction bit is a ‘1’, then the pin is an input.<br />
If the data direction bit is a ‘0’, then the pin is an output.<br />
Hint:<br />
Think “I” for Input and change “I” (letter I) <strong>to</strong> “1” (one) in your mind.<br />
Think “O” for Output and change “O” (letter O) <strong>to</strong> “0” (zero) in your mind.<br />
All port pins are defined as inputs by default.<br />
To write data <strong>to</strong> a digital output, a write <strong>to</strong> the appropriate Output Latch Register (LATA,<br />
LATB, LATC, …) is required. To read data from a digital input read the appropriate Input<br />
Port Register (PORTA, PORTB, PORTC, …).<br />
Example code <strong>to</strong> configure port C bit 2 as output and set it low:<br />
BCLR TRISC, #2 Configure port C bit 2 as output<br />
BCLR PORTC, #2 Set port C bit 2 latch low<br />
Example code <strong>to</strong> configure port C bit 0 as input and perform a bit compare on that input:<br />
BSET TRISC, #0 Configure port C bit 0 as an input<br />
NOP Required between a port direction change and port write<br />
BTSS PORTC, #0 Check if port C bit 0 is low<br />
One instruction cycle is required between a port direction change or port write operation<br />
and a read operation of the same port. Typically, this instruction would be a NOP.<br />
0.3.1 Objective<br />
Program the PIC24 microcontroller <strong>to</strong> blink an LED at a one second interval (LED on<br />
for one second, then off for one second).<br />
0.3.2 Bonus<br />
Modify the code so that when one of the push but<strong>to</strong>ns is held, the LED blinks at twice<br />
the frequency. You may choose any of the four push but<strong>to</strong>ns. Note the push but<strong>to</strong>ns are<br />
mapped <strong>to</strong> the following ports:<br />
Push But<strong>to</strong>n PIC ® MCU Port<br />
SW1 Port G Bit 12<br />
SW2 Port G Bit 13<br />
SW3 Port G Bit 15<br />
SW4 Port C Bit 1<br />
2011 <strong>Microchip</strong> Technology Inc. DS51963A-page 9