Forum Archive

Go Back   3D Realms Forums > General Topics > Programming Forum
Blogs FAQ Members List Social Groups Calendar Mark Forums Read

Notices

 
 
Thread Tools
Old 04-19-2006, 08:25 AM   #1
Samji

Samji's Avatar
PIC16F84 ASM Delay program
For an assignment at college, I have to write an ASM program for the PIC16F84 microcontroller which:

Quote:
Outputs number 2h to port B,
delays for 0.5 second and then outputs 3h to port B.
I don't think I have done this task correctly, so I have posted here to ask for anyone who understands PIC16F84 ASM to evaluate this code and tell me what needs changed.

Code:
; Written by      SAMUEL SAINT-PETTERSEN
; Date            19-04-2006
; File saved      delay.asm
; Device          PIC16F84
; Oscillator      XT (4 MHz)
;
; Function        Output number 2h to port B,
;                 delay for 0.5 second and then output
;                 3h to port B.
;
; ------------------------EQUATES--------------------------
  portb      EQU       0x06       ; portb equates to 0x06
  counter    EQU       0x01       ; countera equates to 0x01
; ---------------------------------------------------------
             ORG       0x00       ; origin is 0x00
start       clrw                 ; reset W register
             movlw     0x02       ; move 2h into W
             tris      portb      ; copy W tristate, portb outputs
             clrw                 ; reset W register

             movlw     0x05       ; move 5h into W
delay      movwf     counter    ; move 5h into countera
             goto      decrm      ; loop to decrm
             
decrm     decfsz    counter, 1 ; decrement countera if > 0
             goto      delay      ; loop to delay and loop if so

             clrw                 ; reset W register
             movlw     0x03       ; move 3h into W
             tris      portb      ; copy w trisate, portb outputs
             goto      start      ; loop back to start
             end                  ; end the program
Thanks in advance.
__________________
Sam's Py
Samji is offline  
Old 04-20-2006, 08:08 PM   #2
Wooloomooloo

Wooloomooloo's Avatar
Goofy Re: PIC16F84 ASM Delay program
I don't think you program is correct. The way it is now, I think it'll just loop forever, because you're always overwriting the value in counter with whatever is in W (I'm guessing 5h) at the beginning of the loop. Try putting the decremented value in W instead of placing it back into counter.
Also, with a 4MHz clock, you'll need 0.5 million non-branching instructions (each taking 4 clock cycles or 1 microsecond to execute) to get a 0.5 second delay. You need to count to way more than 5 .
__________________
"I'm an engineer, not a normal person." -Dilbert
Wooloomooloo is offline  
Old 04-20-2006, 09:36 PM   #3
phreak
Re: PIC16F84 ASM Delay program
edit: nevermind... I can't read
phreak is offline  
Old 04-21-2006, 07:12 AM   #4
Samji

Samji's Avatar
Re: PIC16F84 ASM Delay program
Quote:
Originally Posted by Wooloomooloo
I don't think you program is correct. The way it is now, I think it'll just loop forever, because you're always overwriting the value in counter with whatever is in W (I'm guessing 5h) at the beginning of the loop. Try putting the decremented value in W instead of placing it back into counter.
Also, with a 4MHz clock, you'll need 0.5 million non-branching instructions (each taking 4 clock cycles or 1 microsecond to execute) to get a 0.5 second delay. You need to count to way more than 5 .
Thanks for your help. So decrementing a value by one each time would take 1 microsecond each time, so therefore decrementing 5 to 0 would take 5 microseconds.

So if a microsecond is 10^-6 of a second?
__________________
Sam's Py
Samji is offline  
Old 04-21-2006, 10:50 AM   #5
Wooloomooloo

Wooloomooloo's Avatar
Re: PIC16F84 ASM Delay program
1 microsecond is 10e-6 seconds. As it is now, your loop has 4 instructions:
Code:
delay    movwf    counter
         goto     decrm
decrm    decfsz   counter, 1
         goto     delay
Instructions 1 and 3 take 1 microsecond each; instructions 2 and 4 (goto) takes 2 microseconds, so one iteration of the loop takes 6 microseconds to execute. You're counting to 5 which gives you a delay of about 30 microseconds (the last iteration will be a bit shorter since the branch will be taken, skipping "goto delay").
__________________
"I'm an engineer, not a normal person." -Dilbert
Wooloomooloo is offline  
Old 04-22-2006, 10:19 AM   #6
Samji

Samji's Avatar
Re: PIC16F84 ASM Delay program
Thanks for your help. Our tutor said that we had to have a delay of 0.5 second, but he didn't explain this.
__________________
Sam's Py
Samji is offline  
Old 04-22-2006, 10:36 AM   #7
Wooloomooloo

Wooloomooloo's Avatar
Re: PIC16F84 ASM Delay program
No problem. You should really take a look at the datasheet for the PIC if you haven't already, especially the chapter about the instruction set.
__________________
"I'm an engineer, not a normal person." -Dilbert
Wooloomooloo is offline  
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 07:56 AM.

Page generated in 0.13618588 seconds (100.00% PHP - 0% MySQL) with 18 queries

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.

Website is 1987-2014 Apogee Software, Ltd.
Ideas and messages posted here become property of Apogee Software Ltd.