











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!












Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Portable code can be retargeted to different processors
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
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
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
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.
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.
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.
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
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 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.
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).
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
for each major subsystem.
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.
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Bits in the RCON special function register tell us what type of reset occurred.
Check each bit, print a message, clear the bit after checking itafter checking it.
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
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-
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.
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-
Electrical & Computer Engineering – Microcomputers Dr. Jeff Jackson Lecture 8-