Embedded_Systems_L3( MPLAB_C_Hardware)

advertisement
Embedded Systems
Lecture 3: Using MPLAB, C and the
Computer Hardware
Ian McCrum
Room 5B18, Tel: 90 366364 voice mail on 6th ring
Email: IJ.McCrum@Ulster.ac.uk Web site: http://www.eej.ulst.ac.uk
www.Microchip.com
• Makes microcontrollers, (memories, sensors analog
components and power supply chips.)
• PIC 10F,12F,16F and 18F – supported by the XC8 compilers
• PIC24F, dsPIC30F & dsPIC 33F - supported by the XC16 compiler
• PIC32MX and PIC32MZ supported by the XC32 compiler.
• Gives away MPLAB X IDE and XC family of compilers
• Has peripheral libraries and Harmony framework to ease use
• Has support forums and many users
Development boards
• Microchips’s own starter kits
• Fubarino modules
• Digilent boards (including arduino form factor)
Digilent INC DP32
•
•
•
•
PIC32MX250F128B – 128k Flash/32K SRAM/50MHz
4 LEDs, 2 Pushbuttons, Variable resistor, USB i/f
SPI (4 wires) I2C (2 wires) two UARTS (share pins)
Analogue input – set up for temperature
NB, link only
One of these
Pin 1
3V3 REG
DC IN
J6
MCLR
JP7
21
JP1
14
9
10
D+/RB10
RB0/PGD
RB1/PGC
RB2
RB3
4
5
6
7
RB4
11
BTN2
RB7
16
BTN3
LED 3
LED 2
LED 1
LED 0
RA3/OSC2
(CS)RA0
25
RB13(AN11)
(SDI)RA1
17
RB15(AN9)
(SD0)RA4
18
10k series
Resistors and
10k pullups,
Switches to ground
12
3
2
26 VR1
24
IC3
10k
I2C – or use PPS to set them to UART2
LEDs all via
4k7 to base
of transistors
‘1’ lights LED
10k Variable resistor 3v3 to 0v,
feed to slider via 1k
RA2/OSC1
(SCL1)RB8
2k2
3V3 0V
PGD PGC
RB5/USBID (for USB OTG)
(SDA1)RB9
2k2
PIC32MX250F128B
28 PIN DIL PACKAGE
(SCK1)RB14
To 8MHz XTAL Via 680R
and With 30pF caps
JP5
JP6
D-/RB11
MCLR
JP2
AVSS
22
1
VSS
JP3
MINI –
USB
For
power &
bootloa
ding
VBUS
AVDD
15
VDD
23
VUSB
20
VCAP
8 19 27
VSS
28
13
ICSP to PICKIT3
Programmer
SPI – or use PPS to set them to UART1
Supplies temperature as
Voltage, MCP9701A
Sensor, 3 pin TO92
Diagram of DP32
board, see full
schematic for
details!
Before running code we must configure the CPU. This requires careful study of the datasheet and is
tedious. However, it maybe that the default values will work, or at least the code below is worth noting
Good Practice – author * version number
You must include <xc.h>, stdio.h and stdlib.h declare a
prototypes for main and EXIT_SUCCESS
A function prototype for my waitabit function
Complete Process to get code to run
• Plug in PICKIT3 – do this before running
MPLAB. Check in “devices and printers”
section of the control panel that the device is
recognised and a driver installed.
• If using a ttl USB UART plug in now as well (see
week 4)
• Start MPLAB X and run the new project
wizard.
Screendumps of the 7 steps…
Lastly the project name and folder locations. – start
names with letters, avoid funny characters!
NB watch out that this is ticked on your second or subsequent
projects
Look at the project navigator and project
properties windows
Rightclick here to
change project
properties
Rightclick on
“Source Files and
select new main
– give the file a
sensible name
Make sure CPU is
correct
Make sure you
can see the
PICKIT3 here.
You may need
to reboot if
not!
If powering the board from the
PICKIT3
Rightclick on the project name and
select properties
A more complete
C Source file
Will have all these
Rather than
Hoping the last
person to
program the
configuration
fuses set them
Correctly
You should have
some
understanding of
what each of
these bits does
Study them using
the datasheet…
The Hardware
The PIC32MX1xx/2xx family
The PIC32MX256F128B chip
There is a datasheet on the PIC32MX1xx/2xx
and a reference manual on the PIC32MX
(downloadable as individual chapters)
I also like the book
“Beginner’s guide to Programming the PIC32” by Thomas Kibalo)
PIC32MX family
• Based on MIPs core with microchip legacy
peripherals
• E.g the PIC32MX256F128B has 256k words of flash
and 128kBytes (32kWords) of RAM
• Uses 3.3V logic (some of the pins can be destroyed
by applying 5 Volts!)
• 21 i/o lines, 9 channels of ADC (10 bit,1.1Msps)
• Two UARTs, two SPI, two I2C ports, 5 timers
• ‘B’ version is a 28 pin PDIP package
• Has pin mapping system to allow selected
peripherals to be mapped to certain pins – we will
use this for the UARTs This is called “Peripheral Pin
Select” or PPS.
Simple I/O using the PIC32MX
• User programmable pins; digital input, digital
output, analog input or connected to a
peripheral’s input or output. Some peripherals
can be selected using PPS.
• Special Function Registers provided to select and
control functions; these are the SFRs.
• SFRs allow outputting a 16 bit word, or setting,
clearing or toggling individual bits in an atomic
fashion (guaranteed one clock cycle)
Clues to functions of pins
Page 4 of the Datasheet (above) and pages 20-26 - part of which is shown below
Here we see pin 2 can be an analog pin (AN0) or a simple digital pin
as RA0 (input or output) or a re-mappable peripheral pin RPA0
• A pin’s output can come from either a peripheral or a digital data path.
The peripheral has to be enabled (on) to drive a pin.
• Pins have a TRIS SFR to set the digital direction, ‘0’=output, ‘1’=input
• Pins have a 16 bit PORT SFR allows reading or writing a pin. Only RA0, 1, 3
and 4 are used, though all 16 are available for PORTB.
• Pins have a LAT SFR, allows latching input (or output). Writing to LAT has
same affect as writing to PORT. (see diagram for details)
• Pins have SFRs to select weak pull-up, pull-down resistors or open drain;
CNPUx,CNPDx or ODCx
• As well as PORTx SFRs there are PORTSETx, PORTCLRx and PORTxINV. (x=A
or B)
• The SFRs are available as 16 bit WORDs or a struct of bits.
• There is input change registers that allow the i/o ports to generate
interrupts whenever a change occurs on a digital input pin. You enable this
though the CNENx and CNCONx registers and can check whether a change
has happened since the last time you read the PORTx bit using the
CNSTATx register. You can either poll (manually look at) the interrupt flag
registers or configure the PIC for interrupts.
Summary (see section 12 of ref man)
•
•
•
•
•
•
•
•
ANSELA=0000 ; // pins are NOT to be analog
TRISB=0x0000 (or TRISAbits.RA0=0x1F;)
ODCB=1;// make RB0 open drain
PORTB=0x00FF ; // output 8 ‘1’s
LATB=0x0055 ; // output 01010101
CNPUA=1; // set pullup on RA0
CNPDA=8;// set pulldown on RA3
PORTBINV=0x0001;// invert RB0, rest unchanged
We will cover change notification later – after we cover
interrupts. Note that most of the time you just need ANSELA if
using PORTA, TRISA and TRISB to set digital direction and then
you write or read from PORTA or PORTB accordingly - or use
PORTAbits.Rax or PORTBbits.RBx (x in this case is numeric)
Peripheral Pin Select (PPS)
The number of available pins is dependent on the particular device
and its pin count. Pins that support the PPS feature include the
designation “RPn” in their full pin designation, where “RP” designates
a remappable peripheral and “n” is the remappable port number
Not every peripheral pin can map to every RP pin, you must consult a table (two tables
actually, one for inputs and one for outputs. Analog and I2C peripherals never remap pins
E.G to map UART1 RX to a PIN
So U1RX can only map to bits RA2,4 or RB2, or 13 in the PIC32MX256F128B
On the DP32, RA2 is used by OSC1, RA4 is normally SDO1, RB2 is an LED and RB13
is free – but wired to a pad that can have a temperature sensor added.
For U1TX
So U1TX can only be output
from RA0, RB3,4,7 or 15
Similarly for UART2
NB, link only
One of these
Further restraints – the DP32 schematic
Pin 1
3V3 REG
DC IN
J6
MCLR
JP7
21
JP1
14
9
10
D+/RB10
RB0/PGD
RB1/PGC
RB2
RB3
4
5
6
7
RB4
11
BTN2
RB7
16
BTN3
LED 3
LED 2
LED 1
LED 0
RA3/OSC2
(CS)RA0
25
RB13(AN11)
(SDI)RA1
17
RB15(AN9)
(SD0)RA4
18
10k series
Resistors and
10k pullups,
Switches to ground
12
3
2
26 VR1
24
IC3
10k
I2C – or use PPS to set them to UART2
LEDs all via
4k7 to base
of transistors
‘1’ lights LED
10k Variable resistor 3v3 to 0v,
feed to slider via 1k
RA2/OSC1
(SCL1)RB8
2k2
3V3 0V
PGD PGC
RB5/USBID (for USB OTG)
(SDA1)RB9
2k2
PIC32MX250F128B
28 PIN DIL PACKAGE
(SCK1)RB14
To 8MHz XTAL Via 680R
and With 30pF caps
JP5
JP6
D-/RB11
MCLR
JP2
AVSS
22
1
VSS
JP3
MINI –
USB
For
power &
bootloa
ding
VBUS
AVDD
15
VDD
23
VUSB
20
VCAP
8 19 27
VSS
28
13
ICSP to PICKIT3
Programmer
SPI – or use PPS to set them to UART1
Supplies temperature as
Voltage, MCP9701A
Sensor, 3 pin TO92
Diagram of DP32
board, see full
schematic for details!
Note, using PPS you can wire U1RX to RA4,RB13 or RB2. U1TX to RA0,RB3,RB4,RB7,U2RX to RB5,RB8,RA1,RB1 and U2TX to RB9 or RB14
You should now understand
the code on the left…
ANSELB…
TRISB….
PORTB….
Etc.,
Microchip also supply a peripheral
library – <plib.h>
mPORTBSetPinsDigitalOut(BIT_0|BIT_1|BIT_2);
mPORTBSetPinsDigitalOut(7);
mPORTBSetPinsDigitalIn(BIT_6|BIT_7|BIT_13);
mPORTBSetPins(0x20C0);
mPORTBClearBits(BIT_0);
mPORTBClearBits(1);
mPORTBToggleBits(BIT_7);
mPORTBToggleBits(0x80);
mPORTBReadBits(BIT_2);
mPORTBReadBits(4);
mPORTBWrite(0x00FF);
Value=mPORTBRead();
Frankly this is a lot of typing, a lot of reading needed to get the correct function.
Microchip say it makes for portable code, I doubt that. It might make for more readable
code to a programmer using Plib every day of the year but for occasional coding I prefer
to access the registers “manually” – you need only read the datasheet and do not also
need to read the plib documentation
Exercises
• Plugin PICKIT3 or 2
• Start MPLABX – new project
• Try out code from website –
simple_blinker_timer1.c (load into wordpad
then copy and paste into MPLAB
• Modify … chevrons 0001 0011 0111 1111
1111 1110 1100 1000 0000 0000 …
Download