Docsity

PIC24 Startup-Microcomputers-Lecture 08 Slides-Electrical and Computer Engineering, Slides of Microcomputers

Toggle sidebar
bg1
1
Microcomputers
PIC24 Startup
Dr. Jeff Jackson Lecture 8-1Electrical & Computer Engineering – Microcomputers
Cand Embedded Systems
•A P-based system used in a device (i.e, a car
engine) performing control and monitoring functions
is referred to as an embedded s
y
stem.
y
–The embedded system is invisible to the user
–The user only indirectly interacts with the embedded
system by using the device that contains the P
•Most programs for embedded systems are written in
C
Portable
code can be retargeted to different processors
Dr. Jeff Jackson Lecture 8-2Electrical & Computer Engineering – Microcomputers
–
Portable
–
code
can
be
retargeted
to
different
processors
–Clarity – C is easier to understand than assembly
–Compilers produce code that is close to manually-
tweaked assembly language in both code size and
performance
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download PIC24 Startup-Microcomputers-Lecture 08 Slides-Electrical and Computer Engineering and more Slides Microcomputers in PDF only on Docsity!

Microcomputers

PIC24 Startup

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

C and Embedded Systems

• A P-based system used in a device (i.e, a car

engine) performing control and monitoring functions

is referred to as an embedded systemy.

  • The embedded system is invisible to the user
  • The user only indirectly interacts with the embedded system by using the device that contains the P

• Most programs for embedded systems are written in

C

Portable code can be retargeted to different processors

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

  • Portable – code can be retargeted to different processors
  • Clarity – C is easier to understand than assembly
  • Compilers produce code that is close to manually- tweaked assembly language in both code size and performance

So Why Learn Assembly Language?

  • The way that C is written can impact assembly language size and performance - i.e., if the uint32 data type is used where uint8 would suffice, both performance and code size will suffer.
  • Learning the assembly language, architecture of the target P provides performance and code size clues for compiled C - Does the uP have support for multiply/divide? - Can it shift only one position each shift or multiple positions? (i.e, does it have a barrel shifter ?) H h i t l RAM d th P h?

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

  • How much internal RAM does the P have?
  • Does the P have floating point support?
  • Sometimes have to write assembly code for performance reasons.

C Compilation

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

Referring to Bits within Special Function

Registers

The compiler include file also has definitions for individual bits within special function registers. Can use these to access individual bits and bit fields:

PORTBbits.RB5 = 1; //set bit 5 of PORTB PORTBbits.RB2 = 0; //clear bit 2 of PORTB

if (PORTBbits.RB0) { //execute if-body if LSb of PORTB is '1' .... }

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

}

OSCCONbits.NOSC = 2; //bit field in OSSCON register

Referring to Bits within Special Function

Registers

Using registername.bitname requires you to remember both the register name and the bitname. For bitnames that are UNIQUE, can use just _bitname.

_RB5 = 1; //set bit 5 of PORTB _RB2 = 0; //clear bit 2 of PORTB

if (_RB0) { //execute if-body if LSb of PORTB is '1' .... }

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

}

_NOSC = 2; //bit field in OSSCON register

Variable Qualifiers, Initialization

If a global variable does not have an initial value, by default the runtime code initializes it to zero – this includes static arrays To prevent a variable from being initialized to zero

uint16 u16_k; //initialized to 0 uint8 u8_k = 4; //initialized to 4

_PERSISTENT uint8 u8_resetCount; //uninitialized, value // i k

arrays. To prevent a variable from being initialized to zero, use the _PERSISTENT macro in front of it:

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

// is unknown

The C runtime code is run before main() entry, so run on every power-up, every reset. Use _PERSISTENT variables to track values across processor resets.

C Macros, Inline Functions

The support library and code examples makes extensive use of C macros and Inline functions. The naming convention is all uppercase:pp

#define DEFAULT_BAUDRATE 57600

Macros, the left hand label is replaced by the right hand text

#define LED1 _RB

1 ()

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

static inline void CONFIG_RB1_AS_DIG_INPUT(){ DISABLE_RB1_PULLUP(); _TRISB1 = 1; _PCFG3 = 1; }

Inline functions expand without a subroutine call.

Powering the PIC24 μC

A Wall transformer (or NI ELVIS) provides a variable DC supply voltage (unregulatedsupply voltage (unregulated means that voltage can vary significantly depending on current being drawn). The LM2937-3.3 voltage regulator provides a regulated +3 3V Voltage will stay stable

With writing on device visible, input pin (+9 v) is left side middle

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

+3.3V. Voltage will stay stable up to maximum current rating of device.

input pin (+9 v) is left side, middle is ground, right pin is +3.3V regulated output voltage.

Aside: How does an LED work?

3.3V

Power on

Anode (long lead)

470 ohm

LED Cathode (short lead)

A diode will conduct current (turn on) when the anode is at approximately 0.7V higher than the cathode. A Light Emitting Di d (LED) it i ibl li ht h d ti th b i ht

current limiting resistor

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

Diode (LED) emits visible light when conducting – the brightness is proportional to the current flow. The voltage drop across LEDs used in the lab is about 2V. Current = Voltage/Resistance ~ (3.3v – LED voltage drop)/470  = (3.3v – 2.2V)/470 = 2.7 mA

Reset

10K ohm When reset button is pressed, the MCLR# pin is PIC24 μC VDD

VSS

0.1

MCLR#

Reset Switch

+3.3V brought to ground. This causes the PIC program counter to be reset to 0, so next instruction fetched will be from location 0 All Cs

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

location 0. All Cs have a reset line in order to force the C to a known state.

10K resistor used to limit current when reset button is pressed.

The Clock

The PIC24The PIC24 C hasC has many options for generating a clock; can use an external crystal or internal oscillator. We will use the internal clock

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

internal clock.

The PC Serial Interface

We use a special USB-to-Serial cable to connect our board to the PC. This serial interface outputs 3.3 V levels compatible with the PIC24 μC pins (careful, most USB-to-Serial cables use +/- 9V l l !!)

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

levels!!).

The serial interface will be used for ASCII input/output to PIC24 μC, as well as for downloading new programs via the Bully Serial Bootloader (winbootldr.exe).

ledflash_nomacros.c

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

ledflash.c

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

echo.c

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

Reading the PIC24 Datasheets

  • You MUST be able to read the PIC24 datasheets and find information in them. - The notes and book refer to bits and pieces of what you need to know but DO NOT duplicate everything that is contained in theknow, but DO NOT duplicate everything that is contained in the datasheet.
  • The datasheet chapters are broken up into functionality (I/O Ports, Timer0, USART) - In each chapters are sections on different capabilities (I/O ports have a section on each PORT).
  • The PIC24 Family reference manual has difference sections f h j b t

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

for each major subsystem.

  • The component datasheet for the PIC24HJ32GP202 has summary information, you will need to refer the family reference manual most often.

PIC24 Reset

MCLR# -- external reset button brings input low causes reset.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

RESET# instruction causes reset.

Power-on causes reset after voltage stabilizes.

What RESET type occurred?

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

Bits in the RCON special function register tell us what type of reset occurred.

printResetCause() function

Check each bit, print a message, clear the bit after checking itafter checking it.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

WDT Uses

Error Recovery : If the CPU starts a hardware operation to a peripheral, and waits for a response, can break the CPU from an infinite wait loop by reseting the CPU if a response does not come back in a particular time period.

Wake From Sleep Mode: If the CPU has been put in a low power mode (clock stopped), then can be used to wake the CPU after the WDT timeout period has elapsed.

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

Power Saving Modes

Sleep : Main clock stopped to CPU and all peripherals. Can be awoke by the WDT. Use the pwrsav #0 instruction.

IdleIdle : Main clock stopped to CPU but not the peripherals (UART: Main clock stopped to CPU but not the peripherals (UART can still receive data). Can be awoke by the WDT. Use the pwrsav #1 instruction.

Doze : Main clock to CPU is divided by Doze Prescaler (/2, /4, … up to /128). Peripheral clocks unaffected, so CPU runs slower, but peripherals run at full speed – do not have to change baud rate of h A

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

the UART.

reset.c Program

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

reset.c Program

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

reset.c Operation (continued)

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-

What do you have to know?

• Understand initial hookup schematic for the PIC

C

• Watchdog timer operationWatchdog timer operation

• Sleep mode operation

• Power consumption equation, static vs dynamic

power

• ledflash.c, echo.c, reset.c basic operation

Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-