Elevador
LIST p=16F84 ; PIC16F844 is the target processor
#include "P16F84.INC" ; Include header file
CBLOCK 0x10 ; Temporary storage
tempo
tptrl
tptrh
note
length
pitch
tempdl1
dl2
ENDC
ORG 0
entrypoint goto initialise
ORG 4
intvector goto toggle
initialise ; Register set up:
clrw ; Zero.
movwf PORTA ; Ensure PORTB is zero before we enable it.
movwf PORTB ; Ensure PORTB is zerobefore we enable it.
bsf STATUS,RP0 ; Select Bank 1
clrf TRISB ; Port B is outputs
; Set up timer 0 for tone generation
movlw 0x02 ; Prescaler on, internal clocking, divide by 4
movwf OPTION_REG
bcf STATUS,RP0 ; Back to bank 0; Poll for a button
wait clrf PORTB
wloop btfss PORTA,0
goto playtune0
btfss PORTA,1
goto playtune1
goto wloop
;Routines to play the tunes
playtune0 movlw 0x13
movwf PORTB
movlw 0x50
movwf tempomovlw tune0 / 0x100
movwf tptrh
movlw tune0 % 0x100
movwf tptrl
goto playtune
playtune1 movlw 0x23
movwf PORTB
movlw 0xA0
movwf tempo
movlw tune1 / 0x100
movwf tptrh
movlw tune1 % 0x100
movwf tptrl
goto playtune
;Subroutine to play a tuneplaytune call gettunedata ; Lookup note from tune table
movwf note ; Store the note code
btfsc note,7 ; Bit 7 set => end of tune
goto wait ; Back to waiting for a button press
incf tptrl,F ; Increment the tune pointer
call gettunedata ; Get the note length
movwf length ; Store it; Play the stored note
; The tune table supports two octaves and 8 note lengths.
playnote btfsc note,6 ; Bit 6 set => rest
goto playwait ; Silence ensues...
movf note,W ; Retrieve the note
andlw 0x3F ; Mask off the pitch indexcall pitchtable ; and look it up in the pitchtable
movwf pitch ; Transfer the value to Timer 0
movwf TMR0
bcf INTCON,T0IF ; Clear Timer Interrupt
bsf INTCON,T0IE ; Enable interrupts for Timer 0
bsf INTCON,GIE
playwait movf length,W ; Retrieve the note length
movwf dl1; and store it in delay counter 1
loop1 movf tempo,W ; This value sets the timing resolution
movwf dl2
loop2 nop ; Inner delay loop delay
nop
decfsz dl2,F
goto loop2
decfsz dl1,F ; Outer delay loop
goto loop1
bcfINTCON,T0IE ; Timer interrupts off
;Fetch next note/length pair:
incf tptrl,F ; Increment tune ptr
btfsc STATUS,Z ; Test for low byte rollover
incf tptrh,F ; Inc high byte
goto playtune ; Loop for next note
; Routine to fetch data from the tune tables
gettunedata movf...
Regístrate para leer el documento completo.