Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Delay problem in pic12f508

Status
Not open for further replies.

Aditya070390

Newbie level 6
Joined
Dec 12, 2012
Messages
14
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,384
hi,
can anybody help me out wit dis??
am using 12f508 n require 10sec delay in the code,am using internal oscillator, i hav used the code shown below but the pic is not executing the
decsfz instruction properly.it stays in the loop for ever. i used mplab ide n simulated it in mplab simulator,i hav tried several other alternatives lik using decf n btfsc instructions together!!


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
; Delay = 10 seconds
; Clock frequency = 4 MHz
 
; Actual delay = 10 seconds = 10000000 cycles
; Error = 0 %
 
        cblock
        d1
        d2
        d3
        endc
 
Delay
                        ;9999995 cycles
        movlw   0x5A
        movwf   d1
        movlw   0xCD
        movwf   d2
        movlw   0x16
        movwf   d3
Delay_0
        decfsz  d1, f
        goto    $+2
        decfsz  d2, f
        goto    $+2
        decfsz  d3, f
        goto    Delay_0
 
                        ;1 cycle
        nop
 
                        ;4 cycles (including call)
        return

 
Last edited by a moderator:

**PLEASE** dont use "goto $+2" or any other number, use a label instead and jump to it. You code will be much easier to read and debug.
What you are doing is jumping from one 'goto' to the next then back to the start again. What you should be doing is jumping back to Delay_0 each time if the variable hasn't reached zero.

Brian.
 

Besides the good advice betwixt has given, you might also find the following lesson of interest as it covers software delays loops using both the PIC12F508 and Assembly Language:



The lesson concerning Delay Loops begins on page 11.


BigDog
 

Hi,

As with whats already been mentioned, but you should also start off with the 12F508 Template file to set up the system.

Attached is a basic example with your delay code, its also shown running with Mplabs Debug SIMulator to check the timing.

Use this code and follow it up with the Gooligum tutorial as mentioned by BigdogG
 

Attachments

  • delay.rar
    66.9 KB · Views: 66

Use timer will more simple and accurate.
 

thank u all very much for attending my post,i tried all the given solutions but it din workout. actually my application is to maintain water level in a tank, the logic i require is all working except the tank low condition, in tank low condition the motor shud turn on and wait for 10 secs n check for another sensor input, if the input becomes zero,it shud continue wit motor on,else it shud turn off the motor forever.
below is the code i hav written considering all the solutions given to me, but wen i simulate it,it stays in the delay loop for ever,i dont know y..pls simulate it in mplab sim n tel me if its working properly.

Code:
 list      p=12F508            ; list directive to define processor
	#include <p12F508.inc>        ; processor specific variable definitions

	__CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

;***** VARIABLE DEFINITIONS
temp	EQU     0x07        ;example variable definition
currinput   res 1
level       res 1
dry         res 1
		;or
		cblock		0x08
        d1					; delay work files
        d2
        d3
		endc

;**********************************************************************
	ORG     0x1FF             ; processor reset vector

	ORG     0x000             ; coding begins here
	movwf   OSCCAL            ; update register with factory cal value 

start
	 movlw    b'00000000'
     movwf    GPIO

    movlw    b'00010011' ;setting GP 0,1,4 as i/p's and remaining as o/p's
    tris     GPIO

mainloop
    movf     GPIO,w       ;tis functions reads the values of sensors at gp0 and gp1
    andlw    b'00010011'  ; and jumps to corresponding level of the tank
    andlw    b'00000011'
    movwf    currinput       ; 00--tank high
    btfsc    STATUS,2        ; 11--tank low
	goto     tank_high       ; 10--tank middle
    BTFSC    currinput,0
    goto     tank_low
    goto     tank_midd

tank_low                     ; in dis function the water level is low and so the output is
	bcf level,0              ; turned ON.
    movlw b'00000100'
    movwf GPIO
    nop
    call	Delay         ;calling delay
	nop
	movf GPIO,w           ;reading GP4 input
    andlw b'00010000'
    movwf dry
    btfsc dry,4           ; if the bit is high, it must stop condition
    goto stop             ; if its loe it goes to mainloop
    goto mainloop
    
tank_high              ; tis is tank full situation and the output is turnd OFF
	bsf level,0
    movlw b'00000000'
    movwf GPIO
    nop
    goto mainloop
tank_midd               ;tis is tank middle condition and the output is kept on or off depending on the level register
	btfsc level,0
	goto tank_high
	goto tank_low
stop                      ;tis is a stop condition.the motor is kept off forever
     movlw b'00000000'
   	movwf GPIO
    goto stop

	
	

; Delay = 10 seconds
; Clock frequency = 4 MHz
 
; Actual delay = 10 seconds = 10000000 cycles
; Error = 0 %
 

 
Delay
                        ;9999995 cycles
        movlw   0x5A
        movwf   d1
        movlw   0xCD
        movwf   d2
        movlw   0x16
        movwf   d3
Delay_0
        decfsz  d1, f
        goto    jump1
        decfsz  d2, f
jump1   goto    jump2
        decfsz  d3, f
jump2   goto    Delay_0
 
        nop				; 1 cycle
 
                        ;4 cycles (including call)
        retlw 00


		END                       ; directive 'end of program'
 

Follow the code in your delay routine carefully, use the MPLAB simulator to follow the instructions as they are executed.

The 'decfsz' instruction means "decrement the named file and if it has reached zero, skip over the next instruction, otherwise execute the next instruction" so your program flow goes:

1. set up variables with start values (incidentally, try to avoid names which could also be interpreted as hex values to avoid confusion)
2. decrement d1
3. decrement d2
4. decrement d3
5. nop (not sure why that's there)
6. return from the routine.

So your delay is only 11 instructions long, no matter what you set the variables to.

What you should be doing is this:
1. set up the variables.
2. decrement d1
3. if it isn't zero, goto "Delay_0"
4. (because d1 is zero) decrement d2
5. if it isn't zero, goto "Delay_0"
6. (because d2 is zero) decrement d3
7. if it isn't zero, goto "Delay_0"
8. return

So you loop d1 counting down several times, counting down d2 on each time it reaches zero and do the same with d3. One loop inside another inside another. The routine returns when all the variables are zero.

Brian.
 

ohk i got it..so i tried the code shown below and now am getting an error. "stack overflow error"
Code:
Delay
                        ;9999995 cycles
        movlw   0x5A
        movwf   reg1
        movlw   0xCD
        movwf   reg2
        movlw   0x16
        movwf   reg3
Delay_0
        decfsz  reg1, f
        goto    Delay_0
        decfsz  reg2, f
        goto    Delay_0
        decfsz  reg3, f
        goto    Delay_0
        retlw 00
 

ohk i got it..so i tried the code shown below and now am getting an error. "stack overflow error"
Code:
Delay
                        ;9999995 cycles
        movlw   0x5A
        movwf   reg1
        movlw   0xCD
        movwf   reg2
        movlw   0x16
        movwf   reg3
Delay_0
        decfsz  reg1, f
        goto    Delay_0
        decfsz  reg2, f
        goto    Delay_0
        decfsz  reg3, f
        goto    Delay_0
        retlw 00


Hi,

The complete code you posted gives errors when its built.
Use the following one method only to specify your user registers.

I also supsect you are bulding your code in Relocatable mode.
Look in your Project parameters and change it to Absolute mode.

The delay routine worked fine for me last night.

Have not run you code but think the above will get you going.

Code:
		cblock		0x07
                 d1					; delay work files
                 d2
                 d3
		currinput   
		level       
		dry         

		endc
 

am using mplab v8.10 and i dint find those project paramets,relocatable mode and absolute mode.. can u please tell me how to change it exactly??
the actual problem is wen i simulate and step into each instruction,the declared register doesnt decrease or something,but it continues to loop around and never skips wen its zero..can i see the user registers anywer wen i am simulating the code?
 

Hi,

Mplab 8.10 should work ok.

With your project open - Project, Build Options, Project,folder MPASM/C18 Suit - select - generalte Absolute code - Apply

Here is your code, I have put the delay call as the first intruction and it runs ok.
Using Mplabs Simulator the rest of the code runs around your main loop and does not call the delay - unless you are using inputs or another simulator ?


Edit - rather than single step though the delay, use the breakpoints as I have shown ( right click on the line ) then you can Run over the delay and stop at the end to continue with single step, You can do that on any section of code, but limited to typically 3 breakpoints .

Code:
 list      p=12F508            ; list directive to define processor
	#include <p12F508.inc>        ; processor specific variable definitions

	__CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF & _IntRC_OSC

;***** VARIABLE DEFINITIONS

		cblock		0x07
        d1					; delay work files
        d2
        d3
		currinput   
		level       
		dry         

		endc

;**********************************************************************
	ORG     0x1FF             ; processor reset vector

	ORG     0x000             ; coding begins here
	movwf   OSCCAL            ; update register with factory cal value 

start
	call	delay
	 movlw    b'00000000'
     movwf    GPIO

    movlw    b'00010011' ;setting GP 0,1,4 as i/p's and remaining as o/p's
    tris     GPIO

mainloop
    movf     GPIO,w       ;tis functions reads the values of sensors at gp0 and gp1
    andlw    b'00010011'  ; and jumps to corresponding level of the tank
    andlw    b'00000011'
    movwf    currinput       ; 00--tank high
    btfsc    STATUS,2        ; 11--tank low
	goto     tank_high       ; 10--tank middle
    BTFSC    currinput,0
    goto     tank_low
    goto     tank_midd

tank_low                     ; in dis function the water level is low and so the output is
	bcf level,0              ; turned ON.
    movlw b'00000100'
    movwf GPIO
    nop
    call	Delay         ;calling delay
	nop
	movf GPIO,w           ;reading GP4 input
    andlw b'00010000'
    movwf dry
    btfsc dry,4           ; if the bit is high, it must stop condition
    goto stop             ; if its loe it goes to mainloop
    goto mainloop
    
tank_high              ; tis is tank full situation and the output is turnd OFF
	bsf level,0
    movlw b'00000000'
    movwf GPIO
    nop
    goto mainloop
tank_midd               ;tis is tank middle condition and the output is kept on or off depending on the level register
	btfsc level,0
	goto tank_high
	goto tank_low
stop                      ;tis is a stop condition.the motor is kept off forever
     movlw b'00000000'
   	movwf GPIO
    goto stop

	
	

; Delay = 10 seconds
; Clock frequency = 4 MHz
 
; Actual delay = 10 seconds = 10000000 cycles
; Error = 0 %
 

 
Delay
                        ;9999995 cycles
        movlw   0x5A
        movwf   d1
        movlw   0xCD
        movwf   d2
        movlw   0x16
        movwf   d3
Delay_0
        decfsz  d1, f
        goto    jump1
        decfsz  d2, f
jump1   goto    jump2
        decfsz  d3, f
jump2   goto    Delay_0
 
        nop				; 1 cycle
 
                        ;4 cycles (including call)
        retlw 00


		END                       ; directive 'end of program'
 

Attachments

  • ScreenShot003.jpg
    ScreenShot003.jpg
    117.5 KB · Views: 62
hi,
in the build options i din get dos absolute code. below is the image wat i got and by the way The problem is solved. Thanku all..

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top