Learning to use PICs
Throughout my undergraduate career I focused entirely on analog circuits. Now that I started this hobby, there was no avoiding digital and to be honest, I felt horrible that I had a bachelor’s degree in Electrical Engineering and I knew very little about the digital side of things. Because of this I set out to learn how to use PICs.
I decided to learn ASM (Assembly) language first since most people say it gives you a good foundation for you to later learn the C side of it (not to mention I am already quite familiar with C++). The first thing I did was create the “Hello World” PIC circuit: a blinking LED. Technically, the first circuit I made with a PIC was one that turned on an LED but that was too easy so I will not count it. To make an LED flash or blink was a lot harder than I thought it would be.
I already knew how to make an LED flash at a certain frequency using a 555 timer IC, but this was purely analog. Therefore, I split the breadboard in two sections: one half was the analog method of making an LED blink and the other side was the digital method of making an LED blink. My goal was to make both LED’s blink at the same rate. I used a Microchip PICkit2 programmer to program the PIC (as seen at the top of the picture). The left side of the breadboard is the PIC side and the right side is the 555 timer (analog) side.
I am proud to anounce that I was able to make it happen and I learned a lot in the process. I did change some of the code quite a bit after I got it to work just to play around with various things and see what would and wouldn’t work so if the code I am posting is not entirely correct, then it’s probably because I saved it after messing with it.
;***********************************
;
; Filename: L2-Flash_LED.asm
; Date: June 28, 2008
; File Version: 1.0
;
; Author: Hugo
; Company:
;************************************
;
; Architecture: Baseline PIC
; Processor: 12F508/509
;
;************************************
;
; Files required: P12F509.INC
;
;************************************
; Description: Lesson 2, Exercise 1
;
; Flashes a LED at approximately 1Hz.
; LED continues to flash until power is removed
;
;************************************
;
; Pin assignments:
; GP1 – flashing LED
;
;************************************
list p=12F509 ; list directive to define processor
#include <p12F509.inc> ; processor specific variable definitions
__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _IntRC_OSC
; ‘__CONFIG’ directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.
;*****Variable definitions
UDATA
sGPIO res 1 ; shadow copy of GPIO
count1 res 1 ; delay loop counters
count2 res 1
;*************************************
RESET CODE 0x3FF ; processor reset vector
res 1 ; holds movlw with factory RC cal value
; Internal RC calibration value is placed at location 0x3FF by Microchip
; as a movlw k, where the k is a literal value.
MAIN CODE 0×000
movwf OSCCAL ; update register with factory cal value
;*****Initialization
start
nop ; ‘no operation’ – wastes an instruction cycle
movlw b’111101′ ; configure GP1 (only) as an output
tris GPIO
clrf sGPIO
;*****Main Loop
blink
movf sGPIO,w ; get shadow copy of GPIO
xorlw b’000010′ ; flip bit corresponding to GP1 (bit 1)
movwf GPIO ; write to GPIO
movwf sGPIO ; and update shadow copy
;****delay of approximately 499ms (~0.5s)
movlw .244 ; move 244 to w (‘.’ makes it a decimal value)
movwf count2 ; set the outer loop counter to 244
clrf count1 ; set the inner loop counter to 0
delay1 nop ; this delay loop goes through 249,611 instruction cycles
; 244 x (256 x 4 – 1) – 1 = 249,611
decfsz count1,f ; decrement count1 by 1 and skip next instruction if zero
goto delay1 ; since count1 is zero, this will loop 256 times (255 + 1)
delay2 nop
decfsz count1,f
goto delay2
decfsz count2,f ; go through inner loop (count1) again, 244 times
goto delay1
goto blink ; repeat forever
END ; directive ‘end of program’




2 Responses to “Learning to use PICs”
August 9th, 2008 saat: 12:09 am
I am unsure if the factory internal oscillator setting remains at 03FF if the chip is erased prior to programing
August 9th, 2008 saat: 1:51 am
@Raymond: When you erase the PIC, the oscillator calibration value is also erased. Therefore, before you erase a PIC, make sure you note the value that the oscillator calibration memory location has (it is normally the last memory location on the PIC).
For a more detailed explanation, check out this website: http://feng3.cool.ne.jp/en/howto12f509.html
Leave a Reply