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.

noob - problem with 16f87 delay loop

Status
Not open for further replies.

doctorbunhead

Newbie level 2
Newbie level 2
Joined
Oct 27, 2010
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,300
Hi all
I have been banging my head against the desk all day on this one.
My ultimate goal is to create a 50Hz PWM to control a servo but for now I am just trying to get some loops working as they will form the core of this.

I have generated loop code using the tool at piclist and using picloops but both with the same results.
The listing I have below should be screaming along but it is visibly pulsing an LED at around 10Hz ish. Ignore some of the comment lines. It is running at 8MHz (at least I think so). Messing with the OSCCON bits does seem to slow it down.


Code:
	list      p=16f87           ; list directive to define processor
	#include <p16F87.inc>        ; processor specific variable definitions

	errorlevel  -302              ; suppress message 302 from list file

	__CONFIG    _CONFIG1, _CP_OFF & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO
	__CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF


; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

;this is running at 8MHz. each instruction takes 4 clocks so 1ms is 20,000 cycles. 20ms (50Hz) is 400,000 cycles




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x71        ; variable used for context saving 
status_temp   EQU     0x72        ; variable used for context saving
pclath_temp   EQU     0x73	  ; variable used for context saving
;my variable for ff
varFF		  EQU	  0xff







;**********************************************************************
	ORG     0x000             ; processor reset vector
	goto    main              ; go to beginning of program
	

main

;set clock
	bsf STATUS, RP0 ; BANK 1
	
	movlw b'01110000' ; Set internal osc to 8Mhz
	movwf OSCCON & 0x7F

	bcf STATUS, RP0 ; BANK 0 


;setup the ports
;porta used for the output
	movlw b'00000000'
	banksel	PORTA
	movwf PORTA

;set port a for output
	movlw b'00000000'
	banksel TRISA
	movwf TRISA


CounterA	EQU	0x20
CounterB	EQU	0x21
CounterC	EQU 0x22

;the main program loop

BeginMainLoop	


	movlw	b'00000001'
	banksel PORTA
	movwf	PORTA

VariableDelay

	call Delay

	movlw	b'00000000'
	banksel PORTA
	movwf	PORTA

	call	Delay

;return to the start
	goto BeginMainLoop

Delay
;PIC Time Delay = 0.02000040 s with Osc = 20000000 Hz
		movlw	D'130'
		movwf	CounterB
		movlw	D'221'
		movwf	CounterA
loop		decfsz	CounterA,1
		goto	loop
		decfsz	CounterB,1
		goto	loop
		retlw	0
	

; initialize eeprom locations

	ORG	0x2100
	DE	0x00, 0x01, 0x02, 0x03

	


	END                       ; directive 'end of program'

If I run this with a proper loop designed for 50Hz (which has 3 cascaded loops) then the LED comes on and stays on. At 50Hz, shaking the LED around should give me a nice string of red dots as the LED travels through the air (not very scientific I know :? I haven't invested in a scope yet)

Please help.

I only have 3 weeks experience programming PIC's so please be gentle with me :smile:

Thanks
 

As your code, it needs 20,000 cycles/delay for 50Hz if fOSC is 8MHz.
So the 'Delay' has to be adjusted as like as:
Code:
		movlw	D'26'
		movwf	CounterB
		movlw	D'247'
		movwf	CounterA
 
Aha - You put me on the right track but I had to make changes to the loop structure to make it work like this -
Code:
Delay
		movlw	D'26'
		movwf	CounterB
loop2
		movlw	D'247'
		movwf	CounterA
loop		decfsz	CounterA,1
		goto	loop
		decfsz	CounterB,1
		goto	loop2
		retlw	0

If I don't put the extra loop label in then CounterA gets decremented from 0x00 to 0xFF and the loop takes forever. I must be doing something wrong as all the loop calculators on the web only use one label and the decfsw is supposed to skip on zero. Is this something to do with the Z register? Am I missing a vital config directive?
The method above works but I have to take into account the extra clock cycles of reloading CounterA.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top