Hello
I am fairly new to PIC programming, but have a fair bit of experience form
school involving the Intel 8031. I was wondering if someone could write a
quick program (2 minute job) to do the fallowing on a 16f84, so that I
could learn about these functions by doing them:
-push W onto a stack, and pop W off of the stack
- input from port A to W when there is an interrupt
- have a 10ms delay using the timer
- output to pb.0 through pb.4 after the interrupt
- have a 1 second timer using loops, and have the output (port B) blink on
and off every second thereafter.
oh, and could someone tell em the difference between portA and trisA?
thanks for your help, this should pretty well be what I need to get
everything straightened out.
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
> could learn about these functions by doing them:
> -push W onto a stack, and pop W off of the stack
> - input from port A to W when there is an interrupt
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
>
> oh, and could someone tell em the difference between portA and trisA?
>
> thanks for your help, this should pretty well be what I need to get
> everything straightened out.
>
> Steve Horvath
>
There are only 35 instructions to learn on this PIC. The easiest way to learn
how to use the PIC is to read the data sheets, and jump in. There are some
books which can help you do so, and many web sites which will also help.
Steve Horvath wrote:
>
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
> could learn about these functions by doing them:
First, the architecture is very different between these two mcus. You'll find
yourself referring to the datasheet as you program.
> -push W onto a stack, and pop W off of the stack
There really is no direct analogy in the PIC for your stack. You have no access
to the stack in the '84. If you want to program this way, then you'll need to
create a software stack (which, for most things, is inefficient. It's just a
different architechture, and a different way of thinking)
> - input from port A to W when there is an interrupt
org 4
(Save status registers if needed here (no point in saving W ;-) )
movf PORTA, W
(restore status registers if needed here)
RETFIE
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
> oh, and could someone tell em the difference between portA and trisA?
Each i/o pin on the PIC can be an input, or an output. TRISA defines whether
each pin in portA is input(1) or output(0). PORTA represents the actual status
of PORTA. If you are driving all pins low, but an external circuit is driving
them all high, then PORTA will read all high (ie, the real state, not the state
the PIC is trying to keep it it)
There are other examples out there which do all of what you are asking. Check
my links above, and then start asking more specific questions.
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
Let me get this straight...
You want to LEARN by having someone write the code for you?
And you want it done in 2 Minutes?
What wrong with this picture?
> could learn about these functions by doing them:
> -push W onto a stack, and pop W off of the stack
> - input from port A to W when there is an interrupt
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
> oh, and could someone tell em the difference between portA and trisA?
>
> thanks for your help, this should pretty well be what I need to get
> everything straightened out.
>
> Steve Horvath
--
*
| __O Thomas C. Sefranek spam_OUTtcsTakeThisOuTcmcorp.com
|_-\<,_ Amateur Radio Operator: WA1RHP
(*)/ (*) Bicycle mobile on 145.41, 448.625 Mhz
ARRL Instructor, Technical Specialist, VE Contact.
hamradio.cmcorp.com/inventory/Inventory.html http://www.harvardrepeater.org
Steve,
If you are interested, I can send you a free book "Understanding &
Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown). It4s
about 25 A4 pages, now in HTML format, including commented code examples. I
found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
> could learn about these functions by doing them:
> -push W onto a stack, and pop W off of the stack
> - input from port A to W when there is an interrupt
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
>
> oh, and could someone tell em the difference between portA and trisA?
>
> thanks for your help, this should pretty well be what I need to get
> everything straightened out.
>
> Steve Horvath
>
> > school involving the Intel 8031. I was wondering if someone could
write a {Quote hidden}
> > quick program (2 minute job) to do the fallowing on a 16f84, so that
I
>
> Let me get this straight...
> You want to LEARN by having someone write the code for you?
> And you want it done in 2 Minutes?
> What wrong with this picture?
> > -push W onto a stack, and pop W off of the stack
> > - input from port A to W when there is an interrupt
> > - have a 10ms delay using the timer
> > - output to pb.0 through pb.4 after the interrupt
> > - have a 1 second timer using loops, and have the output (port B)
blink on
> > and off every second thereafter.
And doesn't that look *exactly* like a specification handed out for a
school project?
> > Steve Horvath wrote:
>
> > > school involving the Intel 8031. I was wondering if someone could
> write a
> > > quick program (2 minute job) to do the fallowing on a 16f84, so that
> I
> >
> > Let me get this straight...
> > You want to LEARN by having someone write the code for you?
> > And you want it done in 2 Minutes?
> > What wrong with this picture?
>
HAHAH!! Actually, this is how I got my first project off the ground! Its
much quicker and more useful than one might imagine. I learned more by
someone writing it for me and me troubleshooting the code than I EVER would
have learned alone given twice the time.. But, OTOH, I paid the guy fairly
well to do it ($200) to spend an hour coding and maybe another more to be my
coder/teacher.
There are only 35 instructions to learn on this PIC. The easiest
way to learn
how to use the PIC is to read the data sheets, and jump in. There
are some
books which can help you do so, and many web sites which will also
help.
Steve Horvath wrote:
>
> Hello
> I am fairly new to PIC programming, but have a fair bit of
experience form
> school involving the Intel 8031. I was wondering if someone could
write a
> quick program (2 minute job) to do the fallowing on a 16f84, so
that I
> could learn about these functions by doing them:
First, the architecture is very different between these two mcus.
You'll find
yourself referring to the datasheet as you program.
> -push W onto a stack, and pop W off of the stack
There really is no direct analogy in the PIC for your stack. You
have no access
to the stack in the '84. If you want to program this way, then
you'll need to
create a software stack (which, for most things, is inefficient.
It's just a
different architechture, and a different way of thinking)
> - input from port A to W when there is an interrupt
org 4
(Save status registers if needed here (no point in saving W
;-) )
movf PORTA, W
(restore status registers if needed here)
RETFIE
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B)
blink on
> and off every second thereafter.
>
> oh, and could someone tell em the difference between portA and
trisA?
Each i/o pin on the PIC can be an input, or an output. TRISA
defines whether
each pin in portA is input(1) or output(0). PORTA represents the
actual status
of PORTA. If you are driving all pins low, but an external circuit
is driving
them all high, then PORTA will read all high (ie, the real state,
not the state
the PIC is trying to keep it it)
There are other examples out there which do all of what you are
asking. Check
my links above, and then start asking more specific questions.
I hope this helps!
-Adam
A couple of explainations of the above explainations for a beginner
-
What is org 4 ?
Program location 4 is the instruction that will be jumped to when an
interrupt occurs. Normally you put a goto statement here that jumps to your
interrupt handler routine. When you are finished handling the interrupt the
retie statement goes to the program location stored in the stack and the
program resumes from where the interrupt occurred . . .
If you haven't turned interrupts off at the start of your handler
and stored the state of certain registers before manipulating them you will
get strange results when you turn interrupts back on and attempt to retie -
beware!
org 0 should have a goto statement that points to the start of your
startup code. Your program code must NOT use locations 0, 1, 2, 3 or 4 as
these are all reserved for special uses.
It is safe to start your program from memory location 5, but I
prefer to use org 6, so my programs usually have this structure -
The difference between portA and trisA?
Trisa is where you set up port a. It has (almost) the same address
as the I/O port - it is actually on the second page of memory at 0x80
higher, but you can't add 0x80 to the port a address, you have to swap to
the second page and use the same address as port a. (Don't forget to swap
back!)
Port a is actually 2 separate sets of circuits - 8 input circuits
and 8 output circuits. If you set port a to output then output a 1 and then
read the port you will not see your output, you will see what was last read
in from the input circuitry. To read the current value you have to go back
to the control page and swap the port to input and then read the input.
Moral - try to use port pins as I or O, but don't swap them if
possible. Set up everything in one go, then LEAVE THE I/O SETTING ALONE.
This is why I wrote my "getting started" package for the AVR.
It's way easier (to me anyway) to take something that's known to be
working, than to tackle, out of the gate, setting up the machine, watchdog,
timers, ints, port directions/modes, all that has to be right before you
get to blink your LED.
(Maybe a cooperative multitasker was overkill...)
-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 6.5.2 for non-commercial use <http://www.pgp.com>
Steve Horvath wrote:
>
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
> could learn about these functions by doing them:
> -push W onto a stack, and pop W off of the stack
> - input from port A to W when there is an interrupt
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
> oh, and could someone tell em the difference between portA and trisA?
>
> thanks for your help, this should pretty well be what I need to get
> everything straightened out.
>
> Steve Horvath
Here goes......
The code will be pretty rough, but you may get the idea.
If I could do all this in 2 minutes, I think I would have my own
consultancy ;-)
> -push W onto a stack, and pop W off of the stack
First off, you can't read the stack contents using a 16F84. The newer
18Cxxx series you can.
You can implement a psuedo stack in RAM however.
clrf StackPt ; initialise stack pointer
Subroutine to push onto the stack
Push movwf temp ; store W
movf StackPt,w ; get stack pointer
movwf FSR ; set ready for indirect addressing
movf temp,w ; get w data back
movwf indf ; write to current stack position
incf StackPt ; stack pointer + 1
return
Subroutine to pop from stack
Pop decf StackPt ; stack pointer - 1
movf StackPt,w ; get stack pointer
movwf FSR ; set ready for indirect addressing
movf indf,w ; get stack data into w
return
> - input from port A to W when there is an interrupt
> - have a 10ms delay using the timer
> - output to pb.0 through pb.4 after the interrupt
Assuming a 4MHz clock
org 0h ; start vector
goto start
org 4h ; interrupt vector
;
; INTERRUPT CODE
;
movwf W_Hold ; store w reg - ram page 0 or 1
swapf status,w
bcf status,rp0 ; ram page 0
movwf S_Hold ; store status in RP 0
movf porta,w ; get porta data into W reg
andlw b'00001111' ; mask of upper nibble
movwf Itemp
movf portb,w
andlw b'11110000' ; mask off lower nibble
iorwf temp,w ; merge portb data with port a data
movwf portb ; send back to portb with porta (0 - 3) added
swapf S_Hold,w ; restore Status
movwf status
swapf W_Hold ; W reg restored
swapf W_Hold,w
bcf intcon,t0if ; reset tmr0 interrupt flag
movlw 0xB2 ; reset for 10mS timeout
movwf tmr0
retfie ; finished with interrupt
start bsf status,rp0
movlw b'11010110' ; prescaler 1:32, used from internal clock
movwf option_reg
bcf status,rp0
bsf intcon,t0ie ; activate tmr0 interrupt
bsf intcon,gie ; activate global interrupts
;
; ---------
; MAIN LOOP
; ---------
;
MnLoop nop ; this loop does nothing
goto MnLoop
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
Assuming a 4MHz clock
bsf status,rp0 ; ram page 1
clrf trisb ; all portb = outputs
bcf status,rp0 ; back to ram page 0
ALoop clrf portb ; all portb = off
call Del1Sec ; wait 1 second
movlw 0xff ; all portb = on
call Del1Sec ; wait 1 second
goto ALoop ; repeat forever
> Hello
> I am fairly new to PIC programming, but have a fair bit of experience form
> school involving the Intel 8031. I was wondering if someone could write a
> quick program (2 minute job) to do the fallowing on a 16f84, so that I
> could learn about these functions by doing them:
Hi Steve;
Two minutes eh? Well, I give it a shot.
On my mark, get set.....GO!
> -push W onto a stack, and pop W off of the stack
#include"include/stack.asm"
call push
call pop
> - input from port A to W when there is an interrupt
org 0x04
goto InterruptHandler
InterruptHandler
movf PORTA,w
retfie
> - have a 10ms delay using the timer
bsf STATUS,RP0 ;go into page one
movlw OPTIONInitial ;set the prescalar and things
movwf OPTION_REG
bcf STATUS,RP0 ;back down to page zero
movlw TMR0TenMsDelayNumber
movwf TMR0
LoopDeeDoo
movwf TMR0
addwf 0x00
btfss STATUS,Z
goto LoopDeeDoo
> - output to pb.0 through pb.4 after the interrupt
InteruptHandler
andlw 0x0f ;mask off the unwanted bits
andwf PORTB,w ;write to the port
> - have a 1 second timer using loops, and have the output (port B) blink on
> and off every second thereafter.
>
#define BIT ;any bit you want
LoopDeeDoo
call OneSecondTimerLoop
btfsc PORTB,BIT ;Set yet?
goto $+3 ;Yes-> reset it
bsf PORTB,BIT ;No-> set it
goto $+2 ;skip out
bcf PORTB,BIT
goto LoopDeeDoo
>
> oh, and could someone tell em the difference between portA and trisA?
TRISA sets the direction. Remember 1 = Input
>
> thanks for your help, this should pretty well be what I need to get
> everything straightened out.
Stop!
That took three minutes. Do I get payed overtime? No doubt all sorts
of piclisters will be correcting it anyway...
Steve, I have been playing with PICS when time permits for the last
couple of years. Although my programming methods may be less then
professional, I have found the little buggers most intriguing.
jona ATLAS wrote:
>
> Steve,
> If you are interested, I can send you a free book "Understanding &
> Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown). It4s
> about 25 A4 pages, now in HTML format, including commented code examples. I
> found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
>
> JONA
I am not Steve, but I would love to have a look at that "beginner's tutorial"
for my own purposes, if you don't mind.
--
Mark Andrew Barclay spamBeGonez80spamBeGonenewsguy.com
===================================
Work like you don't need the money,
Love like you've never been hurt,
Dance like no one is watching.
> jona ATLAS wrote:
> >
> > Steve,
> > If you are interested, I can send you a free book "Understanding &
> > Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown). It4s
> > about 25 A4 pages, now in HTML format, including commented code examples. I
> > found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
> >
> > JONA
>
> I am not Steve, but I would love to have a look at that "beginner's tutorial"
> for my own purposes, if you don't mind.
>
> --
> Mark Andrew Barclay
> RemoveMEz80TakeThisOuTnewsguy.com
> ===================================
> Work like you don't need the money,
> Love like you've never been hurt,
> Dance like no one is watching.
jona ATLAS wrote:
>
> Steve,
> If you are interested, I can send you a free book "Understanding &
> Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown). It4s
> about 25 A4 pages, now in HTML format, including commented code examples. I
> found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
>
> JONA
Hi Jona,
I would like to have a copy, kindly send me one...
> Steve,
> If you are interested, I can send you a free book "Understanding &
> Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown). It4s
> about 25 A4 pages, now in HTML format, including commented code examples.
I
> found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
>
> JONA
I would like a copy if it is not too much trouble?
>jona ATLAS wrote:
>>
>> Steve,
>> If you are interested, I can send you a free book "Understanding &
>> Programming the PIC16C84-a beginner4s tutorial, writed by Jim Brown).
It4s {Quote hidden}
>> about 25 A4 pages, now in HTML format, including commented code examples.
I
>> found it very usefull. It4s approx 25Kb zipped file. Just leave an Email
>>
>> JONA
>
>Hi Jona,
>
> I would like to have a copy, kindly send me one...
>
>Thanks,
>
> Allan