Hayley said:
Hi,
I am just starting out with learning how these pics work, and
esp how one could be used to drive some 7 segment displays
doing some counting etc.
is there some example code i could play with?
many thanks in advance for any advice
I don't know why you would want to program that horrible device, but if
you insist, here are some of my first micro (PIC16F84) learning
experiments. BTW, I quickly moved to the AVR. Never looked back.
------------------------------------------------------------------------
; exp5.asm
; This program will take 4-bit binary data in port A and decode it
; to 7-segment display, with hex digits 'A' 'b' 'C' 'd' 'E' 'F'
; in addition to the numeric digits. A lookup table is addressed
; by the data to be decoded. The table contains the 7-segment pattern
; to display the corresponding digit.
; This program uses the relative addresing concept, or "computed jump"
; A later experiment will set up the table and address it using
; program memory paging, which will allow for large tables (almost 256
; bytes). But, the table for this experiment is small.
list p=16f84
radix hex
; cpu equates (memory map)
w equ 0
f equ 1
porta equ 0x05
portb equ 0x06
pc equ 0x02 ; program counter register
org 0x000
start clrw
tris portb ; make all port B lines an output
decode movf porta,w ; get port A data
call segments ; decode into 7 segment
xorlw 0xFF ; complement all bits because the
; LED is common anode
movwf portb ; send it out port B
goto decode ; do it ad infinitum
segments addwf pc,f ; index pc into following instructions
retlw 0x3F ; '0'
retlw 0x06 ; '1'
retlw 0x5B ; '2'
retlw 0x4F ; '3'
retlw 0x66 ; '4'
retlw 0x6D ; '5'
retlw 0x7D ; '6'
retlw 0x07 ; '7'
retlw 0x7F ; '8'
retlw 0x6F ; '9'
retlw 0x77 ; 'A'
retlw 0x7C ; 'b'
retlw 0x39 ; 'C'
retlw 0x5E ; 'd'
retlw 0x79 ; 'E'
retlw 0x71 ; 'F'
end
------------------------------------------------------------------------
; exp6.asm
;
; This program takes a count input at T0CKI and displays the full
; 8-bit counter datum in hex on a 2 digit 7 segment display.
; This is the first exercise in display multiplexing.
;
; Hardware Context:
;
; Port B bits 0-7 connect through 220 ohms to cathodes a-g
; respsectively. Port A bits 0-1 enable digits 1-2 respectively.
; A logic 0 on a Port A "digit select output" line enables the digit
; by connecting its common anode to Vdd (+5V) via a p-MOSFET.
list p=16f84
radix hex
; cpu equates (memory map)
w equ 0
f equ 1
porta equ 0x05
portb equ 0x06
counter equ 0x01
pc equ 0x02 ; program counter register
count_copy equ 0x0C ; first G.P. register to store
; working copy of counter
delay equ 0x0D ; next G.P. register for delay
; iterator
org 0x000
start clrw
tris portb ; make all port B lines output
movlw b'00010000' ; All port A bits except RA4/T0CKI
tris porta ; make them outputs
clrf counter
; movlw 0xAA
; movf counter,w ; load a fixed value to test
multiplex movf counter,w ; grab the count datum
movwf count_copy ; store a copy of it
andlw 0x0F ; mask out the high nibble
call segments ; decode into 7 segment format
xorlw 0xFF ; invert the bits
bsf porta,1 ; turn off digit 2
movwf portb ; send 7 segments to port B
bcf porta,0 ; turn on digit 1
call wait_500u ; wait 500 us
swapf count_copy,w ; swap nibbles of the count datum
; and get into W
andlw 0x0F ; mask off the high nibble of W,
; which is the low nibble of the count!
call segments ; decode into 7 segment format
xorlw 0xFF ; invert the bits
bsf porta,0 ; turn off digit 1
movwf portb ; send 7 segments to port B
bcf porta,1 ; turn on digit 2
call wait_500u ; wait 500 us
goto multiplex ; ad infinitum
stop goto stop
wait_500u movlw d'166' ; this will give about 500us delay
movwf delay
d_loop decfsz delay,f
goto d_loop
return
segments addwf pc,f ; index pc into following instructions
retlw 0x3F ; '0'
retlw 0x06 ; '1'
retlw 0x5B ; '2'
retlw 0x4F ; '3'
retlw 0x66 ; '4'
retlw 0x6D ; '5'
retlw 0x7D ; '6'
retlw 0x07 ; '7'
retlw 0x7F ; '8'
retlw 0x6F ; '9'
retlw 0x77 ; 'A'
retlw 0x7C ; 'b'
retlw 0x39 ; 'C'
retlw 0x5E ; 'd'
retlw 0x79 ; 'E'
retlw 0x71 ; 'F'
end