VVV said:The code I modified for you has equates at the top. All you have to do is change those lines. For instance change:
N_Freq EQU 64H
To use an external 20MHz crystal, change the oscillator option to _HF_OSC in the config word.
But why would you do that, since the internal oscillator is stable and accurate enough for this application? And it's absolutely free, as there are no components to add.
Plus, a 20MHz clock will make the sound pitch too high.
ts wrong with my cVVV said:You have very limited control over the internal oscillator. It is meant to be a fixed-frequency oscillator. If you need variable frequency, then use the RC option.
If you need anything other than 4MHz or 8MHz, then use an external crystal oscillator
The code should work with few changes on a 10F200.
list p=10f200
#include <p10f200.inc>
errorlevel -302 ; no warning about register not in Bank 0
;***Processor Configuration*****
; MCLRE Off, Watch dog Off, Code protection Off
__CONFIG _MCLRE_ON & _WDT_OFF & _CP_OFF
#define OUT GPIO,0
;*****Veriables defination*****
Cycles EQU 10H ;holds # of cycles
Steps EQU 11H ;holds # of steps
Freq EQU 12H ;holds frequency variable
Counter EQU 13H ;variable used as counter
Counter1 EQU 14H ;variable used as counter
;***** CONSTANTS
N_cycles EQU 10H ;number of cycles
N_steps EQU 10H ;# of steps
N_freq EQU 10H ;constant that determines frequency
;***** Oscillator Calibration Value *****
;RESET CODE 0x000 ; processor reset vector
; calibtae Internal RC osccilator
;movwf OSCCAL
;movlw b'11011111' ; clear T0CS so that GP2 can be used
;option ; load into Option Register
; I/O Init
;movlw b'1100' ; configure GP0 & GP1 as an output,GP2 & GP3 as input
;tris GPIO ;
ORG 0 ;This is the start of memory for the program.
Setup: CLRF GPIO ;all outputs are zeroed
MOVLW 06H ;GPIO2:0 are digital IO
MOVWF 07H ;
;BSF STATUS,RP0 ;Go to Bank 1
;CLRF ANSEL ;all digital IO
CLRF GPIO ;Make all port output
TRIS GPIO
;BCF STATUS,RP0 ;Go to Bank 0 - the program memory area.
Siren: MOVLW N_cycles ;Number of cycles for each tone
MOVWF Cycles ;
MOVWF Counter1 ;
MOVLW N_steps ;Number of steps
MOVWF Steps ;File 0F holds the number of steps
MOVLW N_freq ;Determines frequency
MOVWF Freq ;File 0C determines the frequency
Repeat: MOVF Freq,W ;File 0C is moved to W
MOVWF Counter ;W is moved to file 0D for decrementing
On: BSF OUT ;Length of HIGH time to Piezo
DECFSZ Counter,F
GOTO On
MOVWF Counter ;W is moved to file 0D again
Off: BCF OUT ;Length of LOW time to Piezo
DECFSZ Counter,F ;
GOTO Off
DECFSZ Counter1,F ;Number of cycles for each tone
GOTO Repeat ;Go to repeat
DECF Freq,F ;HIGH and LOW is shortened -tone rises
INCF Cycles,F ;Increase the number of cycles
MOVF Cycles,W ;File 0E to W
MOVWF Counter1 ;W to file 10h
DECFSZ Steps,F ;dec Number of steps
GOTO Repeat ;Go to Repaet
GOTO Siren ;Go to Siren
end
VVV said:The output frequency will be:
fo=fosc/4/4/N_freq/2, where fosc is the oscillator frequency
VVV said:Correct.
Since you do not have interrupts on this PIC, insert a test fot TRM0 rolling over to 0 inside the loop, either at Repeat or before the goto Repeat instruction.
if TMR0 has rolled over, toggle GP1.
movlw b'111100' ; configure GP0 & GP1 as an output
tris GPIO ; GP2 & GP3 as an input
movlw b'11010011' ; clear T0CS so that GP2 can be used
option ; select Timer0: timer mode, prescale = 16
; & load into Option Register
clrf GPIO ; Make all port output
banksel dlycnt1 ;
movlw .125 ; repeat 125 times for dlycnt1
movwf dlycnt1 ; -> 125 x 0.8ms = 100ms
movlw .225 ; repeat 225 times for dlycnt2
movwf dlycnt2 ; -> 225 x 4ms = 900ms
SIREN: MOVLW N_cycles ;Number of cycles for each tone
MOVWF Cycles ;
MOVWF Counter1 ;
MOVLW N_steps ;Number of steps
MOVWF Steps ;File 0F holds the number of steps
MOVLW N_freq ;Determines frequency
MOVWF Freq ;File 0C determines the frequency
dly100 ; delay 100ms
clrf TMR0 ; clear timer0
ON: BSF LED ; turn on GP1
w_tmr0
movf TMR0,w ;
xorlw .50 ; wait for 0.8ms (50 x 16us)
btfss STATUS,Z
goto w_tmr0
decfsz dlycnt1,f ; end 100ms delay loop
goto dly100
dly900 ; delay900ms
clrf TMR0
OFF: BCF LED ; turn off GP1
w_tmr1
movf TMR0,w
xorlw .250 ; wait for 4ms (250 x 16us)
btfss STATUS,Z
goto w_tmr1
decfsz dlycnt2,f ; end 900ms delay loop
goto dly900
Repeat MOVF Freq,W ;File Freq is moved to W
MOVWF Counter ;W is moved to Counter for decrementing
On BSF OUT ;Length of HIGH time to Piezo
DECFSZ Counter,F
GOTO On
MOVWF Counter ;W is moved to file Counter again
Off BCF OUT ;Length of LOW time to Piezo
DECFSZ Counter,F ;
GOTO Off
DECFSZ Counter1,F ;Number of cycles for each tone
GOTO Repeat ;Go to repeat
DECF Freq,F ;HIGH and LOW is shortened -tone rises
INCF Cycles,F ;Increase the number of cycles
MOVF Cycles,W ;File 0E to W
MOVWF Counter1 ;W to file 10h
DECFSZ Steps,F ;dec Number of steps
GOTO Repeat
GOTO SIREN
END ; end of program
list p=10f200
#include <p10f200.inc>
errorlevel -302 ; no warning about register not in Bank 0
;***Processor Configuration*****
; MCLRE Off, Watch dog Off, Code protection Off
__CONFIG _MCLRE_ON & _WDT_OFF & _CP_OFF
#define OUT GPIO,0
#define LED GPIO,1
;*****Veriables defination*****
Cycles EQU 10H ;holds # of cycles
Steps EQU 11H ;holds # of steps
Freq EQU 12H ;holds frequency variable
Counter EQU 13H ;variable used as counter
Counter1 EQU 14H ;variable used as counter
Timer EQU 15H
Tmr_stor EQU 16H
;***** CONSTANTS
N_cycles EQU 10H ;number of cycles
N_steps EQU 10H ;# of steps
N_freq EQU 10H ;constant that determines frequency
OPTION_INI EQU B'11000111' ;prescaler on TMR0, 1:256
_200ms EQU 4H ;constant for 200ms delay
_800ms EQU 10H ;constant for 800ms delay
TMR0_RLD EQU .256-.195 ;TMR0 value for 50ms intervals
;***** Oscillator Calibration Value *****
;RESET CODE 0x000 ; processor reset vector
; calibtae Internal RC osccilator
;movwf OSCCAL
;movlw b'11011111' ; clear T0CS so that GP2 can be used
;option ; load into Option Register
; I/O Init
;movlw b'1100' ; configure GP0 & GP1 as an output,GP2 & GP3 as input
;tris GPIO ;
ORG 0 ;This is the start of memory for the program.
Setup: CLRF GPIO ;all outputs are zeroed
MOVLW 06H ;GPIO2:0 are digital IO
MOVWF 07H ;
;BSF STATUS,RP0 ;Go to Bank 1
;CLRF ANSEL ;all digital IO
CLRF GPIO ;Make all port output
TRIS GPIO
;BCF STATUS,RP0 ;Go to Bank 0 - the program memory area.
;**********************************************************
movlw OPTION_INI
option
;**********************************************************
Siren: MOVLW N_cycles ;Number of cycles for each tone
MOVWF Cycles ;
MOVWF Counter1 ;
MOVLW N_steps ;Number of steps
MOVWF Steps ;File 0F holds the number of steps
MOVLW N_freq ;Determines frequency
MOVWF Freq ;File 0C determines the frequency
Repeat: MOVF Freq,W ;File 0C is moved to W
MOVWF Counter ;W is moved to file 0D for decrementing
On: BSF OUT ;Length of HIGH time to Piezo
DECFSZ Counter,F
GOTO On
MOVWF Counter ;W is moved to file 0D again
Off: BCF OUT ;Length of LOW time to Piezo
DECFSZ Counter,F ;
GOTO Off
;*******************************************************************
movf Tmr_stor,W ;get previous timer reading
subwf TMR0,W ;check if TMR0 rolled over by comparing to previous reading
btfss STATUS,C ;if this reading is lower, TMR0 has rolled over
goto Rld ;go reload the timer
movf TMR0,W ;this reading is >= previous
movwf Tmr_stor ;store this reading
goto Dec_C ;continue with the siren
Rld: movlw TMR0_RLD ;else, reload TMR0
movwf TMR0 ;
movwf Tmr_stor ;
decfsz Timer,F ;check if time to toggle LED
goto Dec_C ;
btfss LED ;was the pin high?
goto SET_L ;if not set it now
bcf LED ;else, clear it
movlw _200ms ;set timer for 200ms
movwf Timer ;
goto Dec_C ;
SET_L: bsf LED ;set LED pin
movlw _800ms ;set timer for 800ms
movwf Timer ;
;*******************************************************************
Dec_C:
DECFSZ Counter1,F ;Number of cycles for each tone
GOTO Repeat ;Go to repeat
DECF Freq,F ;HIGH and LOW is shortened -tone rises
INCF Cycles,F ;Increase the number of cycles
MOVF Cycles,W ;File 0E to W
MOVWF Counter1 ;W to file 10h
DECFSZ Steps,F ;dec Number of steps
GOTO Repeat ;Go to Repaet
GOTO Siren ;Go to Siren
end
movlw TMR0_RLD ;init TMR0
movwf TMR0 ;
OKVVV said:Sorry, I missed two lines that initialize TMR0.
Insert these two lines right after the option instruction.
Code:movlw TMR0_RLD ;init TMR0 movwf TMR0 ;
TMR0_RLD EQU .256-.195 ;TMR0 value for 50ms intervals
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?