Hi,
I am completely new to PIC programming (I did COBOL some years ago and did some Assembly at college)
I want to design and build a small project with two PIC's
PIC 1 - Monitors 12 Digital Inputs and sends the data to PIC 2
PIC 2 - Is remote and will light a LED or LED(s) depending on the status of the 12 Digital inputs the interface has sent to PIC 1
PIC 2 meanwhile is looking for the operator pressing a button
It then detects the button press and sends the data to PIC 1
PIC 1 I guess would then perform a quick interupt routine where it sets the appropriate pin of the port corresponding to the button pressed to Ground (As the interface is looking for a Ground) before returning to the main program, monitoring the 12 Digital Inputs
So that is the grand plan
However, first I need to walk before I can run and need to learn PIC programming for dummies
I have been spending lots of time reading very helpful tutorials and I know a lot of people write in "C" or "Basic" but I want to stick to assembler because for me (at the moment) it is easier to follow
The gist of what I understand so far is this
1) The PIC has several banks of memory (depending on which one you use)
2)On powerup the PIC defaults to BANK 0
3)In order to write to the correct BANK, you must set the RP0 or RP1 flag
so far so good
4)Memory is like a set of drawers and several of them are available to store stuff in
5)In order to store something you must first put it into temporary storage AKA the W register (I think also called the accumulator)
You can then move it to another draw which will overwrite the current content of that drawer
Now it gets really confusing for me
A lot of tutorials say that memory starts at 20h (20 Hex)
In the EXAMPLE A (I assume written in Assembly) It looks like you are able to assign meaningful names to the areas of memory (I hope)
So for example it looks like they equate timera to memory location 20(hex)
Hex 20 = 32 in Decimal or 00100000 Binary
Next they equate timerb to 21(hex)
Hex 21 = 33 in Decimal or 00100001 Binary
In decimal the difference between 32 and 33 is 1 and it should also be the same in Binary eg 0000 = 0 0001 = 1
For a PIC16F873A (The one I am using) BANK 0 has 96 BYTES of GPR from address 20h to 7Fh
So from 32 to 127 in decimal (The difference is 95 although as it states 96 BYTES I guess maybe 20h starts at 0 then 21h is 1, 22h is 2 and so on up to 7F)
In EXAMPLE B it looks like again some variable names have been set up J & K
Part way through the program it looks like they move Decimal 50 to the W Register
As 50 Decimal equates to 32 HEX and 00110010 Binary this is more than just a single BIT because further down the program they then move 50 to J
My assumption at first was that as the memory locations were sequential 20h,21h,22h and that this equated to 32,33,34 decimal, that they must be 1 BIT.
But this does not make sense given that there is no way decimal 50 will fit into 1 BIT
Also the fact that the data sheet says 96 BYTES implies that each memory location (or drawer) consists of 8 BITS (as there are 8 BITS in a BYTE)
I just want to confirm my suspicion that this is true and if so ask the following
A)Is it possible to define areas of memory in advance e.g Fred is at 20h (which occupies 8 BITS), Joe is at 21h(which occupies 8 BITS) etc
B) Say you are storing some data at 20h Fred which is 8 BITS long, can you pick 1 BIT of this data out to either send somewhere else or compare against another BIT in another memory location, and if so how?
c)Lastly I read somewhere that the BANKS shared memory and that address 20h in BANK0 is the same as say 80h in BANK1
Does that mean that I only still have 96 BYTES of available storage
I ssumed it might be like this to save instructions so that if you are in BANK 1 and want to read data from memory in BANK 0, instead of having so first switch back to BANK 0 then read the data, you could say read data at address 80h
I had the idea of making up an EXCEL spreadsheet from 32 - 127 x 8 and then I have a visual idea of what is being stored where as I can use colours as an easy reference KEY
Anyway hopefully some kind person can advise and I can make a start
Thanks in anticipation
------------------------------------------------------------------
EXAMPLE A
You can then send some data to 20h
;Files for F628 start at 20h but to make programs easily
; convertible from F84A to F628, we start at 20h.
timera equ 0x20 ;general purpose timer
timerb equ 0x21 ;general purpose timer
timerc equ 0x22 ;general purpose timer
timerd equ 0x23 ;general purpose timer
-------------------------------------------------------------
EXAMPLE B
; File CHASER.ASM
; Blinks LEDs on outputs (Port B) in a rotating pattern.
; Reverses direction if port A, Bit 0, is high.
processor 16f84
include <p16f84.inc>
__config _RC_OSC & _WDT_OFF & _PWRTE_ON
; Give names to 2 memory locations (registers)
J equ H1F ; J = address hex 1F
K equ H1E ; K = address hex 1E
; Program
org 0 ; start at address 0
; Set Port B to output and initialize it.
movlw B00000000 ; w := binary 00000000
tris PORTB ; copy w to port B control reg
movlw B00000001 ; w := binary 00000001
movwf PORTB ; copy w to port B itself
bcf STATUS,C ; clear the carry bit
; Main loop. Check Port A, Bit 0, and rotate either left
; or right through the carry register.
mloop:
btfss PORTA,0 ; skip next instruction if bit=1
goto m1
rlf PORTB,f ; rotate port B bits to left
goto m2
m1:
rrf PORTB,f ; rotate port B bits to right
m2:
; Waste some time by executing nested loops
movlw D50 ; w := 50 decimal
movwf J ; J := w
jloop: movwf K ; K := w
kloop: decfsz K,f ; K := K-1, skip next if zero
goto kloop
decfsz J,f ; J := J-1, skip next if zero
goto jloop
goto mloop ; do it all again
end ; program ends here
I am completely new to PIC programming (I did COBOL some years ago and did some Assembly at college)
I want to design and build a small project with two PIC's
PIC 1 - Monitors 12 Digital Inputs and sends the data to PIC 2
PIC 2 - Is remote and will light a LED or LED(s) depending on the status of the 12 Digital inputs the interface has sent to PIC 1
PIC 2 meanwhile is looking for the operator pressing a button
It then detects the button press and sends the data to PIC 1
PIC 1 I guess would then perform a quick interupt routine where it sets the appropriate pin of the port corresponding to the button pressed to Ground (As the interface is looking for a Ground) before returning to the main program, monitoring the 12 Digital Inputs
So that is the grand plan
However, first I need to walk before I can run and need to learn PIC programming for dummies
I have been spending lots of time reading very helpful tutorials and I know a lot of people write in "C" or "Basic" but I want to stick to assembler because for me (at the moment) it is easier to follow
The gist of what I understand so far is this
1) The PIC has several banks of memory (depending on which one you use)
2)On powerup the PIC defaults to BANK 0
3)In order to write to the correct BANK, you must set the RP0 or RP1 flag
so far so good
4)Memory is like a set of drawers and several of them are available to store stuff in
5)In order to store something you must first put it into temporary storage AKA the W register (I think also called the accumulator)
You can then move it to another draw which will overwrite the current content of that drawer
Now it gets really confusing for me
A lot of tutorials say that memory starts at 20h (20 Hex)
In the EXAMPLE A (I assume written in Assembly) It looks like you are able to assign meaningful names to the areas of memory (I hope)
So for example it looks like they equate timera to memory location 20(hex)
Hex 20 = 32 in Decimal or 00100000 Binary
Next they equate timerb to 21(hex)
Hex 21 = 33 in Decimal or 00100001 Binary
In decimal the difference between 32 and 33 is 1 and it should also be the same in Binary eg 0000 = 0 0001 = 1
For a PIC16F873A (The one I am using) BANK 0 has 96 BYTES of GPR from address 20h to 7Fh
So from 32 to 127 in decimal (The difference is 95 although as it states 96 BYTES I guess maybe 20h starts at 0 then 21h is 1, 22h is 2 and so on up to 7F)
In EXAMPLE B it looks like again some variable names have been set up J & K
Part way through the program it looks like they move Decimal 50 to the W Register
As 50 Decimal equates to 32 HEX and 00110010 Binary this is more than just a single BIT because further down the program they then move 50 to J
My assumption at first was that as the memory locations were sequential 20h,21h,22h and that this equated to 32,33,34 decimal, that they must be 1 BIT.
But this does not make sense given that there is no way decimal 50 will fit into 1 BIT
Also the fact that the data sheet says 96 BYTES implies that each memory location (or drawer) consists of 8 BITS (as there are 8 BITS in a BYTE)
I just want to confirm my suspicion that this is true and if so ask the following
A)Is it possible to define areas of memory in advance e.g Fred is at 20h (which occupies 8 BITS), Joe is at 21h(which occupies 8 BITS) etc
B) Say you are storing some data at 20h Fred which is 8 BITS long, can you pick 1 BIT of this data out to either send somewhere else or compare against another BIT in another memory location, and if so how?
c)Lastly I read somewhere that the BANKS shared memory and that address 20h in BANK0 is the same as say 80h in BANK1
Does that mean that I only still have 96 BYTES of available storage
I ssumed it might be like this to save instructions so that if you are in BANK 1 and want to read data from memory in BANK 0, instead of having so first switch back to BANK 0 then read the data, you could say read data at address 80h
I had the idea of making up an EXCEL spreadsheet from 32 - 127 x 8 and then I have a visual idea of what is being stored where as I can use colours as an easy reference KEY
Anyway hopefully some kind person can advise and I can make a start
Thanks in anticipation
------------------------------------------------------------------
EXAMPLE A
You can then send some data to 20h
;Files for F628 start at 20h but to make programs easily
; convertible from F84A to F628, we start at 20h.
timera equ 0x20 ;general purpose timer
timerb equ 0x21 ;general purpose timer
timerc equ 0x22 ;general purpose timer
timerd equ 0x23 ;general purpose timer
-------------------------------------------------------------
EXAMPLE B
; File CHASER.ASM
; Blinks LEDs on outputs (Port B) in a rotating pattern.
; Reverses direction if port A, Bit 0, is high.
processor 16f84
include <p16f84.inc>
__config _RC_OSC & _WDT_OFF & _PWRTE_ON
; Give names to 2 memory locations (registers)
J equ H1F ; J = address hex 1F
K equ H1E ; K = address hex 1E
; Program
org 0 ; start at address 0
; Set Port B to output and initialize it.
movlw B00000000 ; w := binary 00000000
tris PORTB ; copy w to port B control reg
movlw B00000001 ; w := binary 00000001
movwf PORTB ; copy w to port B itself
bcf STATUS,C ; clear the carry bit
; Main loop. Check Port A, Bit 0, and rotate either left
; or right through the carry register.
mloop:
btfss PORTA,0 ; skip next instruction if bit=1
goto m1
rlf PORTB,f ; rotate port B bits to left
goto m2
m1:
rrf PORTB,f ; rotate port B bits to right
m2:
; Waste some time by executing nested loops
movlw D50 ; w := 50 decimal
movwf J ; J := w
jloop: movwf K ; K := w
kloop: decfsz K,f ; K := K-1, skip next if zero
goto kloop
decfsz J,f ; J := J-1, skip next if zero
goto jloop
goto mloop ; do it all again
end ; program ends here