Tech
Tech
Tech
Exercise session 2
PIC16F Basics
Documentation:
https://www.microchip.com/en-us/product/PIC16F1789
PIC16F1788/9
28-Pin 8-Bit Advanced Analog Flash Microcontroller
I/O Features:
• Up to 36 I/O Pins and 1 Input-only Pin:
• High Current Sink/Source for LED Drivers
• Individually Programmable Interrupt-on-Change
Pins
• Individually Programmable Weak Pull-Ups
• Individual Input Level Selection
• Individually Programmable Slew Rate Control
• Individually Programmable Open-Drain Outputs
Oscillator Features:
• Operate up to 32 MHz from Precision Internal
Oscillator:
- Factory calibrated to ±1%, typical
- Software selectable frequency range from
• Operate up to 32 MHz from Precision Internal
Oscillator:
- Factory calibrated to ±1%, typical
- Software selectable frequency range from
32 MHz to 31 kHz
• 31 kHz Low-Power Internal Oscillator
• 32.768 kHz Timer1 Oscillator:
- Available as system clock
- Low-power RTC
• External Oscillator Block with:
- 4 crystal/resonator modes up to 32 MHz
using 4x PLL
- 3 external clock modes up to 32 MHz
• 4x Phase-Locked Loop (PLL)
• Fail-Safe Clock Monitor:
- Detect and recover from external oscillator
failure
• Two-Speed Start-up:
- Minimize latency between code execution
and external oscillator start-up
PC<14:0>
CALL, CALLW 15
RETURN, RETLW
Interrupt, RETFIE
Stack Level 0
Stack Level 1
Stack Level 15
Page 7
3FFFh
Rollover to Page 0 4000h
Rollover to Page 7
7FFFh
FIGURE 3-3: LOADING OF PC IN
DIFFERENT SITUATIONS
7 8
6 0
PCLATH ALU Result
14 PCH PCL 0
PC GOTO, CALL
6 4 0 11
PCLATH OPCODE <10:0>
14 PCH PCL 0
PC CALLW
6 7 0 8
PCLATH W
14 PCH PCL 0
PC BRW
15
PC + W
14 PCH PCL 0
PC BRA
15
PC + OPCODE <8:0>
Notes:
If you manage to fit your program in the first 2K page, you do not
need them!
Data memory organization
FIGURE 3-2: BANKED MEMORY
PARTITIONING
PIC16(L)F1788/9
The memory maps for Bank 0 through Bank 31 are shown in the tables in this section.
PROCESSOR 16F1789
#include <xc.inc>
PSECT udata_bank0
counter_l:
DS 1
counter_h:
DS 1
counter_hh:
DS 1
end reset_vec
PIC16(L)F1788/9
6.6 Register Definitions: Oscillator Control
Legend:
R = Readable bit W = Writable bit U = Unimplemented bit, read as ‘0’
u = Bit is unchanged x = Bit is unknown -n/n = Value at POR and BOR/Value at all other Resets
‘1’ = Bit is set ‘0’ = Bit is cleared
% /opt/microchip/xc8/v2.32/pic-as/bin/pic-as -mcpu=16F1789 \
-xassembler-with-cpp -Wa,-a -Wl,-preset_vec=0h \
-Wl,-pisr_vec=4h blink.asm
Memory Summary:
Program space used 1Ch ( 28) of 4000h words ( 0.2%)
Data space used 3h ( 3) of 800h bytes ( 0.1%)
EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)
Configuration bits used 2h ( 2) of 2h words (100.0%)
ID Location space used 0h ( 0) of 4h bytes ( 0.0%)
blink.lst:
38 psect udata_bank0
39 0020 counter_l:
40 0020 ds 1
41 0021 counter_h:
42 0021 ds 1
43 0022 counter_hh:
44 0022 ds 1
45
46 psect code
47 0005 start:
48 0005 0021 banksel 153
49 0006 30F8 movlw 248 ; PLL enable, 32MHz HF
50 0007 0099 movwf 153
51 0008 0023 banksel 396
52 0009 018C clrf 396
53 000A 0021 banksel 140
54 000B 018C clrf 140
55 000C 0188 clrf 8
56 000D loop:
57 000D 30FF movlw 255
58 000E 008C movwf 12
59 000F 2014 call delay
60 0010 3000 movlw 0
61 0011 008C movwf 12
62 0012 2014 call delay
63 0013 280D goto loop
Programming
Always double-check connections and power supply voltage before
power-on!
% java -jar /opt/microchip/mplabx/v5.50/mplab_platform/mplab_ipe/\
ipecmd.jar -TPPK4 -E -M -P16F1789 -F"blink.hex" -OL
*****************************************************
Connecting to MPLAB PICkit 4...
Currently loaded versions:
Application version............00.06.87
Boot version...................01.00.00
Script version.................00.04.48
Script build number............7acb7c9d66
Tool pack version .............1.8.1120
Target voltage detected
Target device PIC16F1789 found.
Device Revision Id = 0x2041
*****************************************************
Erasing...
Erase successful
*****************************************************
Calculating memory ranges for operation...
Erasing...
The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x1f
configuration memory
Programming/Verify complete
Program Report
Simple Makefile:
CPU = 16F1789
PROGRAMMER = PPK4
SRCS = blink.asm
EXEC = blink
AS-DIR = /opt/microchip/xc8/v2.32/pic-as/bin/
AS = $(AS-DIR)/pic-as
IPE_DIR = /opt/microchip/mplabx/v5.50/mplab_platform/mplab_ipe/
FLASH_CMD = java -jar $(IPE_DIR)/ipecmd.jar
all: $(EXEC)
$(EXEC):
$(AS) $(AS_FLAGS) $(SRCS) -o $(EXEC)
clean:
-@rm -f $(EXEC).{elf,hex,hxl,cmf,sym,lst,rlf,d,i} MPLABXLog.xml
flash: $(EXEC)
$(FLASH_CMD) $(FLASH_ARGS)
References: