Actions

Difference between revisions of "Fake Cricket"

From Just in Time

m
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
A fake cricket is not a really useful application, but it is very simple to create and can be fun to drive people crazy. Or to hide and seek with children.
 
A fake cricket is not a really useful application, but it is very simple to create and can be fun to drive people crazy. Or to hide and seek with children.
  
If you use a high-impedance speaker, it can be driven directly from the output pins of your microcontroller. A piezo element should also work, but might not give much output power with low voltages.
+
If you use a high-impedance speaker, it can be driven directly from the output pins of your microcontroller. A piezo element should also work, but might not give much output power with low voltages / low frequencies.
  
 
For a low-impedance speaker a simple 'amplifier' (transistor) should work.
 
For a low-impedance speaker a simple 'amplifier' (transistor) should work.
  
Tip: Instead of connecting your output speaker with one pin to VDD/VSS, more output can be generated by connecting with two output pins and swapping them. This trick seems to work, and I think it works because a speaker is current-driven. For a piezo, it doesn't matter, since a piezo is voltage-driven.
+
Here is the Fake Cricket built in a cheap scan radio.
 +
 
 +
[[Image:cricket.jpg]]
 +
 
 +
Tip: Instead of connecting your output speaker with one pin to VDD/VSS, more output can be generated by connecting with two output pins and alternating them. This trick seems to work, and I think it works because a speaker is current-driven. For a piezo, it doesn't matter, since a piezo is voltage-driven.
  
 
  ; File cricket.asm
 
  ; File cricket.asm
Line 32: Line 36:
 
  _IntRC_OSC          EQU        0FFEh       
 
  _IntRC_OSC          EQU        0FFEh       
 
  _ExtRC_OSC          EQU        0FFFh         
 
  _ExtRC_OSC          EQU        0FFFh         
 
+
 
   
 
   
 
  ; 509 defs  
 
  ; 509 defs  
Line 59: Line 63:
 
          
 
          
 
  start:   
 
  start:   
movwf OSCCAL ; calibration bits
+
movwf OSCCAL ; calibration bits
movlw 0 ; set tris read except for aupin
+
movlw 0 ; set tris output
tris GPIO
+
tris GPIO
 
   
 
   
 
  mainloop:
 
  mainloop:
 
   
 
   
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
call freq4500
+
call freq4500
call freq4200
+
call freq4200
+
 
 
  none:
 
  none:
call secdelay
+
call secdelay ; which is in fact 8 secs
 
   
 
   
goto mainloop
+
goto mainloop
 
   
 
   
 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 91: Line 95:
 
    
 
    
 
  freq4200: ; freq loop should be 238 cycles to get 4200Hz  
 
  freq4200: ; freq loop should be 238 cycles to get 4200Hz  
movlw 0040h ; counter set for 11msec
+
movlw 0040h ; counter set for 11msec
movwf CounterD
+
movwf CounterD
+
 
 
  loop42:
 
  loop42:
bcf GPIO,aupin
+
bcf GPIO,aupin
bsf GPIO,aupin2
+
bsf GPIO,aupin2
call del42
+
call del42
bcf GPIO,aupin2
+
bcf GPIO,aupin2
bsf GPIO,aupin
+
bsf GPIO,aupin
call del42
+
call del42
+
 
decfsz CounterD,1
+
decfsz CounterD,1
goto loop42
+
goto loop42
 
   
 
   
retlw 0
+
retlw 0
 
   
 
   
 
  freq4500: ; freq loop should be 222 cycles to get 4500Hz
 
  freq4500: ; freq loop should be 222 cycles to get 4500Hz
movlw 002Dh ; counter set for 14msec
+
movlw 002Dh ; counter set for 14msec
movwf CounterD
+
movwf CounterD
 
  loop45:
 
  loop45:
bcf GPIO,aupin
+
bcf GPIO,aupin
bsf GPIO,aupin2
+
bsf GPIO,aupin2
call del45
+
call del45
bcf GPIO,aupin2
+
bcf GPIO,aupin2
bsf GPIO,aupin
+
bsf GPIO,aupin
call del45
+
call del45
 +
 
 +
decfsz CounterD,1
 +
goto loop45
 
   
 
   
decfsz CounterD,1
+
retlw 0
goto loop45
 
 
retlw 0
 
 
   
 
   
 
  ;;;;;;;;;;
 
  ;;;;;;;;;;
 
  del42
 
  del42
movlw 0023h
+
movlw 0023h
movwf CounterA
+
movwf CounterA
+
 
 
  loop4 decfsz CounterA,1
 
  loop4 decfsz CounterA,1
goto loop4
+
goto loop4
+
 
retlw 0
+
retlw 0
 
   
 
   
 
  ;;;;;;;;;;;
 
  ;;;;;;;;;;;
 
  del45
 
  del45
movlw 0026h
+
movlw 0026h
movwf CounterA
+
movwf CounterA
 +
 
 +
loop5 decfsz CounterA,1
 +
goto loop5
 
   
 
   
loop5 decfsz CounterA,1
+
retlw 0
goto loop5
 
 
 
retlw 0
 
 
   
 
   
 
   
 
   
Line 147: Line 151:
 
  ; insert picloops code here
 
  ; insert picloops code here
 
  ;PIC Time Delay = 8,0000040 s with Osc = 4,000000 MHz
 
  ;PIC Time Delay = 8,0000040 s with Osc = 4,000000 MHz
movlw D'41'
+
movlw D'41'
movwf CounterC
+
movwf CounterC
movlw D'190'
+
movlw D'190'
movwf CounterB
+
movwf CounterB
movlw D'87'
+
movlw D'87'
movwf CounterA
+
movwf CounterA
 
  sloop decfsz CounterA,1
 
  sloop decfsz CounterA,1
goto sloop
+
goto sloop
decfsz CounterB,1
+
decfsz CounterB,1
goto sloop
+
goto sloop
decfsz CounterC,1
+
decfsz CounterC,1
goto sloop
+
goto sloop
retlw 0
+
retlw 0
 
   
 
   
 
   
 
   
 
         end
 
         end
 +
[[Category:PIC]]
 +
{{Comments}}

Latest revision as of 00:20, 26 January 2011

A fake cricket is not a really useful application, but it is very simple to create and can be fun to drive people crazy. Or to hide and seek with children.

If you use a high-impedance speaker, it can be driven directly from the output pins of your microcontroller. A piezo element should also work, but might not give much output power with low voltages / low frequencies.

For a low-impedance speaker a simple 'amplifier' (transistor) should work.

Here is the Fake Cricket built in a cheap scan radio.

Cricket.jpg

Tip: Instead of connecting your output speaker with one pin to VDD/VSS, more output can be generated by connecting with two output pins and alternating them. This trick seems to work, and I think it works because a speaker is current-driven. For a piezo, it doesn't matter, since a piezo is voltage-driven.

; File cricket.asm
; Assembly code for a 4MHz PIC12F509 microcontroller
;
; Description: Fake cricket sound for a 12F509 
; Author: Vincent
; Last Modified: 17 / 8 / 06
 
; 
; CPU configuration
        processor 12f509
         __config  _IntRC_OSC & _WDT_OFF & _MCLRE_OFF   

;=======================================
;  Configuration (FUSE) BIT Definitions 
;=======================================
_MCLRE_ON            EQU        0FFFh       
_MCLRE_OFF           EQU        0FEFh       
_CP_ON               EQU        0FF7h       
_CP_OFF              EQU        0FFFh       
_WDT_ON              EQU        0FFFh       
_WDT_OFF             EQU        0FFBh       
_LP_OSC              EQU        0FFCh       
_XT_OSC              EQU        0FFDh       
_IntRC_OSC           EQU        0FFEh       
_ExtRC_OSC           EQU        0FFFh         


; 509 defs 

PCL		equ	H'02'
STATUS		equ	H'03'
OSCCAL		equ	H'05'
GPIO		equ	H'06'
Z		equ	H'02' 

; variables
        
CounterA	equ	H'1F'		; delay counter
CounterB	equ	H'1E'		; delay counter
CounterC	equ	H'1D'		; delay counter
CounterD	equ	H'1C'		; delay counter 

; statics

aupin	equ	H'00'
aupin2  equ H'01' 

; Program
 
        org     0         	; start at address 0
        
start:   
	movwf	OSCCAL		; calibration bits
	movlw	0		; set tris output
	tris	GPIO		

mainloop:

	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
	call freq4500
	call freq4200
 
none:	
	call secdelay 	 	; which is in fact 8 secs

	goto mainloop

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; subs
 
freq4200:			; freq loop should be 238 cycles to get 4200Hz 
	movlw	0040h		; counter set for 11msec
	movwf	CounterD
 
loop42:
	bcf		GPIO,aupin
	bsf		GPIO,aupin2
	call del42
	bcf		GPIO,aupin2
	bsf		GPIO,aupin
	call del42
 
	decfsz	CounterD,1
	goto loop42

	retlw 0

freq4500:			; freq loop should be 222 cycles to get 4500Hz
	movlw	002Dh		; counter set for 14msec
	movwf	CounterD	
loop45:
	bcf		GPIO,aupin
	bsf		GPIO,aupin2
	call del45
	bcf		GPIO,aupin2
	bsf		GPIO,aupin
	call del45
 
	decfsz	CounterD,1
	goto loop45

	retlw 0

;;;;;;;;;;
del42
		movlw	0023h
		movwf	CounterA
 
loop4	decfsz	CounterA,1
		goto loop4
 
		retlw 0

;;;;;;;;;;;
del45
		movlw	0026h
		movwf	CounterA
 
loop5	decfsz	CounterA,1
		goto loop5

		retlw 0


secdelay
; insert picloops code here	
;PIC Time Delay = 8,0000040 s with Osc = 4,000000 MHz
		movlw	D'41'
		movwf	CounterC
		movlw	D'190'
		movwf	CounterB
		movlw	D'87'
		movwf	CounterA
sloop	decfsz	CounterA,1
		goto	sloop
		decfsz	CounterB,1
		goto	sloop
		decfsz	CounterC,1
		goto	sloop
		retlw 0


        end

Template:Comments