30
Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6 th ring Email: [email protected] Web site: http://www.eej.ulst.ac.uk

Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: [email protected]

Embed Size (px)

Citation preview

Page 1: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Embedded Systems

Lecture 3: Using MPLAB, C and the Computer Hardware

Ian McCrum Room 5B18, Tel: 90 366364 voice mail on 6th ringEmail: [email protected] Web site: http://www.eej.ulst.ac.uk

Page 2: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.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

Page 3: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Development boards• Microchips’s own starter kits• Fubarino modules

• Digilent boards (including arduino form factor)

Page 4: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 5: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

RB0/PGDRB1/PGC

RB2RB3

RB4

RB7

RB15(AN9)

RB13(AN11)

LED 3LED 2LED 1LED 0

LEDs all via4k7 to base of transistors‘1’ lights LED

BTN2

BTN3

10k seriesResistors and 10k pullups,Switches to ground

VR1

10k Variable resistor 3v3 to 0v, feed to slider via 1k

IC3Supplies temperature as Voltage, MCP9701ASensor, 3 pin TO92

PGCPGDMCLR 3V3 0V

ICSP to PICKIT3Programmer

Pin 1

RB5/USBID (for USB OTG)

VBUS

D-/RB11

D+/RB10

VCAP

VUSB

VDD

AVDD

VSS

VSS

AVSS

RA2/OSC1

RA3/OSC2To 8MHz XTAL Via 680R and With 30pF caps

JP3

JP2

JP1

MINI –USBFor power & bootloading

PIC32MX250F128B28 PIN DIL PACKAGE

3V3 REG

(SDA1)RB9

(SCL1)RB8

(SCK1)RB14

(SD0)RA4

(SDI)RA1

(CS)RA0

10k

2k22k2

JP5JP6

JP7

DC IN J6

NB, link only One of these

2320

13 28 8 19 27 1

MCLR

18 17 25 12 3 2

24

26

16

11

7654

15

22

21

14

9

10

I2C – or use PPS to set them to UART2 SPI – or use PPS to set them to UART1

Diagram of DP32 board, see full schematic for details!

Page 6: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 7: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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.

Page 8: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Screendumps of the 7 steps…

Page 9: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 10: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Look at the project navigator and project properties windows

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!

Rightclick here to change project properties

Page 11: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

If powering the board from the PICKIT3Rightclick on the project name and

select properties

Page 12: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

A more completeC Source file Will have all theseRather 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…

Page 13: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

The HardwareThe PIC32MX1xx/2xx familyThe 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)

Page 14: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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.

Page 15: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 16: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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)

Page 17: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Clues to functions of pins

Page 4 of the Datasheet (above) and pages 20-26 - part of which is shown below

Page 18: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 19: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

• 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.

Page 20: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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)

Page 21: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk
Page 22: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 23: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

E.G to map UART1 RX to a PIN

Page 24: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

So U1RX can only map to bits RA2,4 or RB2, or 13 in the PIC32MX256F128BOn 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.

Page 25: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

For U1TX

So U1TX can only be output from RA0, RB3,4,7 or 15

Page 26: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

Similarly for UART2

Page 27: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

RB0/PGDRB1/PGC

RB2RB3

RB4

RB7

RB15(AN9)

RB13(AN11)

LED 3LED 2LED 1LED 0

LEDs all via4k7 to base of transistors‘1’ lights LED

BTN2

BTN3

10k seriesResistors and 10k pullups,Switches to ground

VR1

10k Variable resistor 3v3 to 0v, feed to slider via 1k

IC3Supplies temperature as Voltage, MCP9701ASensor, 3 pin TO92

PGCPGDMCLR 3V3 0V

ICSP to PICKIT3Programmer

Pin 1

RB5/USBID (for USB OTG)

VBUS

D-/RB11

D+/RB10

VCAP

VUSB

VDD

AVDD

VSS

VSS

AVSS

RA2/OSC1

RA3/OSC2To 8MHz XTAL Via 680R and With 30pF caps

JP3

JP2

JP1

MINI –USBFor power & bootloading

PIC32MX250F128B28 PIN DIL PACKAGE

3V3 REG

(SDA1)RB9

(SCL1)RB8

(SCK1)RB14

(SD0)RA4

(SDI)RA1

(CS)RA0

10k

2k22k2

JP5JP6

JP7

DC IN J6

NB, link only One of these

2320

13 28 8 19 27 1

MCLR

18 17 25 12 3 2

24

26

16

11

7654

15

22

21

14

9

10

I2C – or use PPS to set them to UART2 SPI – or use PPS to set them to UART1

Diagram of DP32 board, see full schematic for details!

Further restraints – the DP32 schematic

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

Page 28: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

You should now understand the code on the left…

ANSELB…

TRISB….

PORTB….

Etc.,

Page 29: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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

Page 30: Embedded Systems Lecture 3: Using MPLAB, C and the Computer Hardware Ian McCrumRoom 5B18, Tel: 90 366364 voice mail on 6 th ring Email: IJ.McCrum@Ulster.ac.uk

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 …