Download presentation
Presentation is loading. Please wait.
Published bySilas Rice Modified over 9 years ago
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 Web site: http://www.eej.ulst.ac.ukIJ.McCrum@Ulster.ac.ukhttp://www.eej.ulst.ac.uk
2
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
3
Development boards Microchips’s own starter kits Fubarino modules Digilent boards (including arduino form factor)
4
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
5
RB0/PGD RB1/PGC RB2 RB3 RB4 RB7 RB15(AN9) RB13(AN11) LED 3 LED 2 LED 1 LED 0 LEDs all via 4k7 to base of transistors ‘1’ lights LED BTN2 BTN3 10k series Resistors and 10k pullups, Switches to ground VR1 10k Variable resistor 3v3 to 0v, feed to slider via 1k IC3 Supplies temperature as Voltage, MCP9701A Sensor, 3 pin TO92 PGCPGD MCLR 3V3 0V ICSP to PICKIT3 Programmer Pin 1 RB5/USBID (for USB OTG) VBUS D-/RB11 D+/RB10 VCAP VUSB VDD AVDDVSS AVSS RA2/OSC1 RA3/OSC2 To 8MHz XTAL Via 680R and With 30pF caps JP3 JP2 JP1 MINI – USB For power & bootloa ding PIC32MX250F128B 28 PIN DIL PACKAGE 3V3 REG (SDA1)RB9 (SCL1)RB8 (SCK1)RB14 (SD0)RA4 (SDI)RA1 (CS)RA0 10k 2k2 JP5 JP6 JP7 DC IN J6 NB, link only One of these 23 20 13 28 8 1927 1 MCLR 18 17 25 12 3 2 24 26 16 11 7 6 5 4 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!
6
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, stdio.h and stdlib.h declare a prototypes for main and EXIT_SUCCESS A function prototype for my waitabit function
7
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.
8
Screendumps of the 7 steps…
9
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
10
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
11
If powering the board from the PICKIT3 Rightclick on the project name and select properties
12
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…
13
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)
14
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.
16
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)
17
Clues to functions of pins Page 4 of the Datasheet (above) and pages 20-26 - part of which is shown below
18
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
19
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.
20
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)
22
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
23
E.G to map UART1 RX to a PIN
24
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.
25
For U1TX So U1TX can only be output from RA0, RB3,4,7 or 15
26
Similarly for UART2
27
RB0/PGD RB1/PGC RB2 RB3 RB4 RB7 RB15(AN9) RB13(AN11) LED 3 LED 2 LED 1 LED 0 LEDs all via 4k7 to base of transistors ‘1’ lights LED BTN2 BTN3 10k series Resistors and 10k pullups, Switches to ground VR1 10k Variable resistor 3v3 to 0v, feed to slider via 1k IC3 Supplies temperature as Voltage, MCP9701A Sensor, 3 pin TO92 PGCPGD MCLR 3V3 0V ICSP to PICKIT3 Programmer Pin 1 RB5/USBID (for USB OTG) VBUS D-/RB11 D+/RB10 VCAP VUSB VDD AVDDVSS AVSS RA2/OSC1 RA3/OSC2 To 8MHz XTAL Via 680R and With 30pF caps JP3 JP2 JP1 MINI – USB For power & bootloa ding PIC32MX250F128B 28 PIN DIL PACKAGE 3V3 REG (SDA1)RB9 (SCL1)RB8 (SCK1)RB14 (SD0)RA4 (SDI)RA1 (CS)RA0 10k 2k2 JP5 JP6 JP7 DC IN J6 NB, link only One of these 23 20 13 28 8 1927 1 MCLR 18 17 25 12 3 2 24 26 16 11 7 6 5 4 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
28
You should now understand the code on the left… ANSELB… TRISB…. PORTB…. Etc.,
29
Microchip also supply a peripheral library – 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
30
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 …
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.