Difference between revisions of "Fake Cricket"
From Just in Time
Line 6: | Line 6: | ||
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. | 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. | ||
+ | |||
+ | ; 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 read except for aupin | ||
+ | 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 | ||
+ | |||
+ | 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 |
Revision as of 22:42, 27 August 2006
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.
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.
; 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 read except for aupin 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
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