Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Delay 16f88 assembly language

Status
Not open for further replies.
hi guys, here's my program sample.
It is just to set RA0 (pin 17) to HIGH for 1.5ms and then LOW for 18.5ms.

HTML:
list p=16f88
#include <pic16f88.inc>

osccon	equ 8fh
status	equ	03h
porta	equ	05h
portb	equ	06h
ansel	equ	9bh
trisa	equ	85h
trisb	equ	86h

cblock	20h
d1
d2
d3
endc

org 0x000

main ;initialisiing ports and clock
;********setting the internal RC clock frequency

call 	banksel1
movlw 	01101110 ;freq = 4MHz, bit 1 and 0 are 10 because internal RC is used for the system clock
movwf	osccon

clrf	ansel ;setting all pins to digital pins
clrf	trisa ;setting all port A into output
clrf	trisb ;setting all port B into output

call	banksel0
clrf	porta;set all port A into LOW
clrf	portb;set all port B into LOW

mainloop ;the main loop

bsf		porta,0 ;set pin 17 HIGH
call 	delay1.5ms
bcf		porta,0 ;set pin 17 LOW
call	delay18.5ms

goto	mainloop

;***bank selections***

banksel0
bcf		status,5
bcf		status,6
return

banksel1
bsf		status,5
bcf		status,6
return

;***delay routine***
;*delay variables
delay1.5ms ;total of 1,500,004 cycles
movlw	odh
movwf	d1
movlw	46h
movwf	d2
movlw	04h
movwf	d3
goto	delay
return

delay18.5ms ;total of 18,500,003 cycles
movlw	a8h
movwf	d1
movlw	54h
movwf	d2
movlw	29h
movwf	d3
goto	delay
return

;*actual delay routine
delay
decfsz	d1,f
goto	$+2
decfsz	d2,f
goto	$+2
decfsz	d3,f
goto	delay
return

Is my sample program correct?
I saw some sample programs in the internet, they include something like this after include<pic16f88.inc>
HTML:
__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _INTRC_OSC_CLKOUT  & _MCLRE_OFF & _LVP_OFF


If there is anything wrong in the program, please let me know.
In addition, if I want this program to run just for 1 minute, what should be modified or in what way the code should be written?
Oh, and also, I can write using hexadecimal, binary, and decimal in 1 program, can't I?
Thank you.



Regards.
 
Last edited:
guys, I just realise that I am studying from 2 different websites and they have different theory about decfsz.
one website says that decfsz count,1 means decreasing the value of memory register count by 1.
The other website says that the value after the comma should be a destination address for storing the result of decrementing.
Which of them is right?
Thanks.


Regards.
 
Both. DECFSZ decrease the register by 1 and jumps if result is 0. DECFSZ REG1,w will store decremented register in w. DECFSZ REG1,f or DECFSZ REG1 (default f ) will store it in itself, what you want for your delay.
 
ah, so that means the 2nd website is correct... the first one says that the number after comma is number of decrementing. for example if the count = 50 and the program is decfsz count,2.... the calculation will goes like (50 - 2). but now I have understood it completely. thank you.

HTML:
Loop1          decfsz              COUNT1,1 
                     goto                  Loop1   
                     decfsz             COUNT2,1
                     goto                 Loop1

erm... is the sample code above a nested loop?
what I am confused with in the sample program is that after count1 reduced to 0, it will jump to the next decfsz count2,1, won't it?
Then if count2 is not reduced to 0, it will go to loop1 again. What will happen to the first decfsz count1,1? will the count1 goes back to the previous value or 0?
Will it skip the first delay or will it run the first delay again?
Thanks.



Regards.
 
The second one is correct. The writer of the first one likely misinterpeted it.

A value of 1(default) writes the result back to the register it was read from while a value of 0 or writes the result to the working register, keeping the original register unchanged.

In MPLAB those values can be replaced with f and w respectively.
 
Last edited:
It is important to note for the delay that if you decfsz a register with a value of 0, it will now hold 255 (loop around not reset to original).

o.o is that true? like in the sample delay that I wrote above, after count1 goes to 0, it will jump and run to decfsz count2 and if count2 is not 0 yet, count1 will go to 255?
thanks.


Regards.
 
haha. alright, thanks a lot.

would you mind to take a look at my sample program above? I am still new and I don't know what I am doing, especially at the starter part (list, include, etc).
Thanks.


Regards.
 
1) this won't assemble because the assembler likes all capitals for registers etc

2)
osccon equ 8fh
status equ 03h
porta equ 05h
portb equ 06h
ansel equ 9bh
trisa equ 85h
trisb equ 86h

doesnt need to be here because you are using the include file.

3)
1.5 M instructions = 1.5 seconds, not 1.5ms

Besides these minor details looks alright I guess!
 
1) this won't assemble because the assembler likes all capitals for registers etc

2)
osccon equ 8fh
status equ 03h
porta equ 05h
portb equ 06h
ansel equ 9bh
trisa equ 85h
trisb equ 86h

doesnt need to be here because you are using the include file.

3)
1.5 M instructions = 1.5 seconds, not 1.5ms

Besides these minor details looks alright I guess!

you mean for all registers and values that I put in there have to be in capitals? x.x

and about the delay, I can only say 'oh, crap.'
I will post another one with correction real soon. thank you very much for your help.

Regards.
 
alright, here's my corrected program:

HTML:
list p=16f88
#include <pic16f88.inc>

osccon	equ 8Fh
status	equ	03h
porta	equ	05h
portb	equ	06h
ansel	equ	9Bh
trisa	equ	85h
trisb	equ	86h

cblock	20h
d1
d2
d3
endc

org 0x000

main	;main loop
;********setting the internal RC clock frequency

call 	banksel1
movlw 	01101110 ;freq = 4MHz, bit 1 and 0 are 10 because internal RC is used for the system clock
movwf	osccon

clrf	ansel ;setting all pins to digital pins
clrf	trisa ;setting all port A into output
clrf	trisb ;setting all port B into output

call	banksel0
clrf	porta;set all port A into LOW
clrf	portb;set all port B into LOW

bsf		porta,0 ;set pin 17 HIGH
call 	delay1.5ms
bcf		porta,0 ;set pin 17 LOW
call	delay18.5ms

goto	main

;***bank selections***

banksel0
bcf		status,5
bcf		status,6
return

banksel1
bsf		status,5
bcf		status,6
return

;***delay routine***
;*delay variables
delay1.5ms ;total of 1,500 cycles
movlw	2Bh
movwf	d1
movlw	02h
movwf	d2
goto	delay_m
return

delay18.5ms ;total of 18,500 cycles
movlw	73h
movwf	d1
movlw	0Fh
movwf	d2
goto	delay_m
return

;*actual delay routine
delay_m
decfsz	d1,f
goto	$+2
decfsz	d2,f
goto	delay_m
return

to mr. birdman, what about the commands? do they have to be in capital letters too? e.g: EQU, MOVLW, DECFSZ, etc.

Thank you.


Regards.
 
Last edited:
guys, I just realise that I am studying from 2 different websites and they have different theory about decfsz.
one website says that decfsz count,1 means decreasing the value of memory register count by 1.
The other website says that the value after the comma should be a destination address for storing the result of decrementing.
Which of them is right?
Thanks.


Regards.

Hi,
The decrement or increment of a variable value can be loaded into either the register where its located by using , F or placed in the working register W.
The symbol F or W can be used OR you can use 0 or 1, its explained in the Instruction for dec/inc in the datasheet.

Regarding the the servo, if the servo output arm is driving against an external force, say a model aircrafts elevator, if you remove the 20mS refresh from the servo's position the wind would blow the elevator back into a neutral position.
It really depends upon the 'load' the servo is trying to hold in position....OK.?
 

Attachments

  • AAesp01.gif
    AAesp01.gif
    23 KB · Views: 170
Last edited:
Hi,
The decrement or increment of a variable value can be loaded into either the register where its located by using , F or placed in the working register W.
The symbol F or W can be used OR you can use 0 or 1, its explained in the Instruction for dec/inc in the datasheet.

Regarding the the servo, if the servo output arm is driving against an external force, say a model aircrafts elevator, if you remove the 20mS refresh from the servo's position the wind would blow the elevator back into a neutral position.
It really depends upon the 'load' the servo is trying to hold in position....OK.?

yes, I have understood about that....
but I have to do ADC as well which take time. doing parallel processing using microcontroller is a little bit tough, hence I'm trying to remove the refresh. In any case, I am trying to reduce the load torque as much as possible.
Thank you, mr eric.

But just in case if I want to get the 20ms refresh, do you have any method to achieve that? say, refreshing every 20ms for about a min?
Thanks.



Regards.
 
hi,
Ref your program header.
As you are declaring #include <pic16f88.inc>, you do not have to use EQU for the SFR's [special function registers]


Code:
list p=16f88
#include <pic16f88.inc>


[B]; none of this is required, its taken care of by #include <pic16f88.inc>[/B]
osccon	equ 8Fh
status	equ	03h
porta	equ	05h
portb	equ	06h
ansel	equ	9Bh
trisa	equ	85h
trisb	equ	86h
 
yes, I have understood about that....
but I have to do ADC as well which take time. doing parallel processing using microcontroller is a little bit tough, hence I'm trying to remove the refresh. In any case, I am trying to reduce the load torque as much as possible.
Thank you, mr eric.

But just in case if I want to get the 20ms refresh, do you have any method to achieve that? say, refreshing every 20ms for about a min?
Thanks.

Regards.

hi,
Keeping the servo arm/load in position really depends on the application.
If its critical that external forces do not move the servo arm then you should refresh every 20mS.
What is the application.

How often are you sampling the adc and why.??
 
Mr. eric, I am doing the equ just for self reassurance... are the commands have to be in capitals? or just the numbers? and is it FFH or FFh?

I am actually designing a solar tracker and I use servo motor as the actuator...

I am planning to do the sampling every 5 mins.

could you please tell me how to repeat 20ms signal repeatedly for some period of time?
Thank you so much.



Regards.
 
Mr. eric, I am doing the equ just for self reassurance... are the commands have to be in capitals? or just the numbers? and is it FFH or FFh?

I am actually designing a solar tracker and I use servo motor as the actuator...

I am planning to do the sampling every 5 mins.

could you please tell me how to repeat 20ms signal repeatedly for some period of time?
Thank you so much.

Regards.

hi,
Some assemblers will accept upper or lower case text, some have to be set to accept upper or lower.

I would recommend that you use lower case for the PIC instructions and upper case for the SFR names, this IMO makes it easier to read.

If you have a spare output pin, you could use it to drive a 'brake/holding' solenoid, so that the solar panel can resist the wind trying to move it.
In that way you could remove the brake and drive the servo every 5 minutes and apply the hold.

Its possible to count a 5min delay period and at the same time activate the servo.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top