doctorbunhead
Newbie level 2
![Newbie level 2](/styles/images/ranks/alevel2.gif)
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.
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
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