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.

Help me with coding for PIC12F675 in PIC Siren

Status
Not open for further replies.
pic variable frequency tone

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.

Now only I get what u mean. Any formula that I can calculate the required freq?
Can we control the internal oscillator?
for example, run it at 2Mhz etc.....so this will effect the output right?

Thanks!

Added after 5 hours 29 minutes:

One more thing.
Can this coding work with 10F200?
 

programming 12f675 tone

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.
 

10f200 gp3 mclr

VVV 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.
ts wrong with my c
I try to run it on 10f200 but nothing came out whaode?
Code:
	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
 

siren generator program

Anybody can explain how to calculate the desire frequency & timing?

The constant will vary the output but I need to calculate whats the frequency of the tone & its timing.

Pls help.
 

pic basic siren

The output frequency will be:
fo=fosc/4/4/N_freq/2, where fosc is the oscillator frequency
 

12f675 sounds

VVV said:
The output frequency will be:
fo=fosc/4/4/N_freq/2, where fosc is the oscillator frequency

Thanks VVV.

if my freq=4MHz & N_freq=150, so the calculation is:

4/4/4/150/2 right?

whats the unit I'll get?

Hz,KHz,MHz etc...
 

generateur numero siret

If you enter fosc in Hz, the result will be in Hz. If you enter it in MHz, then the result will also be in MHz.
 

generator sound siren used pic

Hi VVV,

Thanks for yr reply.

ok since 4MHz = 4000000 Hz. So:
40000/4/4/255/2 = 490Hz. am I right?

Another thing is how do I control timing & use tmr0 to flash LED on GP1 while GP0 output the sound?

Any idea?
 

16f84 siren

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.
 

siren pic

Hi VVV,

Thanks for yr reply.
Since the siren sound from low to high, so 490Hz was the low or high freq? I'm now using 10f200.

I need to flash an LED at GP1 with 20% on, 80% off at 1 Hz.

I never use tmr0 before so have no idea how to insert mentioned rolling over in the loop.
 

12f675 making tone

I don't know where you got the 150 number. N_freq is initalized to 50H=80 dec.
And that is the lowest frequency, because then it rises.

Well, you will have to read up on TMR0 and set it up so that it rolls over every 0.2s. Then use another conter, initialized to 4, to tell you when the 80% of the period has elapsed. For the other 20% the counter can be initialized to 1 or not used. Reading TMR0 is no different than any other file register.
 

siren project using pic

Hi VVV,
I changed the 50H(80dec) to 150dec.

If I use 150 in decimal, it means that it will rises from 150 to 255 dec or 150 to 0dec?

as calculated the smaller the figure the higher the freq.

for example:
4000000/4/4/150/2=833.3Hz start or end?
4000000/4/4/255/2=490Hz end or start?

Is this calculation correct?
 

schematic sirene

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.

Hi VVV,

I use TMR0 to create this LED flash. its ok if stand alone.
I tried to insert into repeat or before goto repeat but don't work.

here are my LED flash code:
Code:
		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
 

piezo sirene schematic

You cannot create delays like that, as they consume too much processor time.
Here is what I came up with. This is the code you had before and I assume it worked. I added a few things to it. Note the option instruction, used to set up the option register.
Code:
   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
 

generate siren with pic

Hi VVV,

Thanks for pointing me the right TMR0 code for output.

I connected a NPN transistor on GP1 for the LED to run from 12V with 6 LEDs. The LED only lit up after 2 round of siren. LED lit up immediately if I connect directly from GP1 with a 270R resistor.
Do you have any idea whats going wrong?

TQ!
 

pic sirene generator

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      ;
 

siren generator

VVV 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      ;
OK
Code:
TMR0_RLD   EQU   .256-.195   ;TMR0 value for 50ms intervals

The EQU can be subtract?
 

+pic +driving +speaker

Its the same delay after TMR0 initiate.
 

16f84 piezo

It runs OK in the simulator... I can't say why it doesn't work for you.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top