;=============================================================================== ; Filename: freq2.asm ; Date: 12/11/2002 ; File Version: 1.0 ; Author: Vila ; Company: WVR-2000 ;================================================================================ ; Files required: ;================================================================================ ; Notes: ; 14/11/2002 - Version:1.02 ; 15/11/2002 - Version:1.03 ; 16/11/2002 - Version:1.04 ; 17/11/2002 - Version:1.05 ; 19/11/2002 - Version:1.06 ; 06/01/2003 - Version:1.07 ; 10/02/2003 - Version:1.08 ; 16/02/2003 - Version:1.09 ; 17/02/2003 - Version:1.10 ; 13/06/2003 - Version:1.20 ; 14/06/2003 - Version:1.30 ;================================================================================ ;================================================================================ ;DIRETIVAS DE COMPILAÇÃO ;================================================================================ LIST P=16F84A ;list directive to define processor #INCLUDE ;processor specific variable definitions __IDLOCS 2002 ;dados a serem gravados na EEPROM ERRORLEVEL -302 ;suppress bank selection messages RADIX DEC ;set to decimal __CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_HS_OSC ;================================================================================= ;VARIABLE DEFINITIONS ;================================================================================= CBLOCK 0x0C ;endereço inicial p/ variáveis diguni ;digito LSB digdez digcent digmil digdezmil ;digito MSB tmr1 ;2o byte contador de pulsos (1o->TMR0) H_byte ;número de 16 bits a ser convertido L_byte R0 ;MSB R1 R2 ;LSB count temp t1 ;variáveis de controle da t2 ;rotina usDelay ovf ;flag de overflow w_temp ;variable used for context saving status_temp ;variable used for context saving tmpLcdRegister ;devem permanecer no final msDelayCounter ;pois ocupam 4 bytes ENDC ;================================================================================= ;PAGINAÇÃO DE MEMÓRIA ;================================================================================= #DEFINE BANK0 BCF STATUS,RP0 ;seta bank0 de memória #DEFINE BANK1 BSF STATUS,RP0 ;seta bank1 de memória ;===================================================================== ;CONSTANTS DEFINITIONS ;===================================================================== ;LCD Control lines LCD_RS equ 2 ;Register Select LCD_E equ 3 ;Enable ;LCD data line bus LCD_DB4 equ 4 ;LCD data line DB4 LCD_DB5 equ 5 ;LCD data line DB5 LCD_DB6 equ 6 ;LCD data line DB6 LCD_DB7 equ 7 ;LCD data line DB7 ;================================================================================= ;VETOR DE RESET ;================================================================================= ORG 00H goto Start ;================================================================================= ;VETOR DE INTERRUPÇÃO ;================================================================================= ORG 0x04 ;interrupt vector location MOVWF w_temp ;save off current W register contents SWAPF STATUS,w ;move status register into W register MOVWF status_temp ;save off contents of STATUS register bcf INTCON,T0IF ;limpa flag int clrf TMR0 ;limpa TMR0 incfsz tmr1,f ;incrementa tmr1 goto FimInt ;encerra int se tmr1>255 clrf tmr1 ;limpa tmr1 clrf ovf ;reseta flag overflow ;================================================================================== ;avisa overflow movlw 10H ;reposiciona cursor call LcdLocate movlw 'O' call LcdSendData movlw 'v' call LcdSendData movlw 'e' call LcdSendData movlw 'r' call LcdSendData movlw 'F' call LcdSendData movlw 'l' call LcdSendData movlw 'o' call LcdSendData movlw 'w' call LcdSendData movlw '!' call LcdSendData movlw ' ' call LcdSendData ;================================================================================== FimInt SWAPF status_temp,w ;retrieve copy of STATUS register MOVWF STATUS ;restore pre-isr STATUS register contents SWAPF w_temp,f SWAPF w_temp,w ;restore pre-isr W register contents RETFIE ;return from interrupt ;================================================================================= ;INICIALIZAÇÃO DOS REGISTROS E VARIÁVEIS ;================================================================================= Start BANK1 ;Swap to register bank 1 movlw 00011111B ;Set PORTA lines as inputs movwf TRISA movlw 00000011B ;Set PORTB RB2..RB7 lines as outputs movwf TRISB BANK0 ;Swap to register bank 0 call ClearCount ;limpa contadores goto Main ;================================================================================= ;Rotina de retardo, onde w=tempo em ms - clock=20MHz ;================================================================================= msDelay movwf msDelayCounter+1 ;duas variáveis em uma clrf msDelayCounter+0 ;return ;retorno p/ debug ***************** msDelayLoop goto $+1 ;1 ms (about) internal loop goto $+1 ;equivale a 2xnop goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 nop decfsz msDelayCounter+0,F ;dec. o prim. cont. goto msDelayLoop decfsz msDelayCounter+1,F ;dec. o seg. cont. goto msDelayLoop return ;================================================================================= ;Rotina de retardo, onde w=tempo - clock=20MHz (500us p/ cada unidade em w) ;================================================================================= usDelay movlw 201 movwf t2 loop1 movlw 191 goto $+1 goto $+1 goto $+1 movwf t1 loop2 goto $+1 goto $+1 goto $+1 goto $+1 goto $+1 decfsz t1,f goto loop2 decfsz t2,f goto loop1 return ;================================================================================= ;Rotinas de controle do LCD ;================================================================================= LcdInit movlw 30 ;Wait 30 ms call msDelay bcf PORTB,LCD_RS ;Set LCD command mode bsf PORTB,LCD_DB4 ;0011->48 bsf PORTB,LCD_DB5 bcf PORTB,LCD_DB6 bcf PORTB,LCD_DB7 call LcdPulse_E ;sequência de reset call LcdPulse_E call LcdPulse_E bcf PORTB,LCD_DB4 ;0010->32 bsf PORTB,LCD_DB5 bcf PORTB,LCD_DB6 bcf PORTB,LCD_DB7 call LcdPulse_E movlw 28H ;Set 4 bit data bus length call LcdSendCommand movlw 06H ;Entry mode set, increment, no shift call LcdSendCommand movlw 0CH ;Display ON, Curson OFF, Blink OFF call LcdSendCommand call LcdClear ;clear display return LcdClear movlw 01H call LcdSendCommand movlw 2 ;Wait 2 ms call msDelay return LcdLocate movwf tmpLcdRegister+0 ;Locate cursor on LCD movlw 80H ;W = D7-D4 row, D3-D0 col movwf tmpLcdRegister+1 ;00h=linha0/10h=linha1 movf tmpLcdRegister+0,W andlw 0FH iorwf tmpLcdRegister+1,F btfsc tmpLcdRegister+0,4 bsf tmpLcdRegister+1,6 movf tmpLcdRegister+1,W call LcdSendCommand return LcdSendData bsf PORTB,LCD_RS call LcdSendByte return LcdSendCommand bcf PORTB,LCD_RS call LcdSendByte return LcdSendByte movwf tmpLcdRegister ;Send a byte to LCD by 4 bit data bus bcf PORTB,LCD_DB4 ;Send highter four bits bcf PORTB,LCD_DB5 bcf PORTB,LCD_DB6 bcf PORTB,LCD_DB7 btfsc tmpLcdRegister,4 bsf PORTB,LCD_DB4 btfsc tmpLcdRegister,5 bsf PORTB,LCD_DB5 btfsc tmpLcdRegister,6 bsf PORTB,LCD_DB6 btfsc tmpLcdRegister,7 bsf PORTB,LCD_DB7 call LcdPulse_E bcf PORTB,LCD_DB4 ;Send lower four bits bcf PORTB,LCD_DB5 bcf PORTB,LCD_DB6 bcf PORTB,LCD_DB7 btfsc tmpLcdRegister,0 bsf PORTB,LCD_DB4 btfsc tmpLcdRegister,1 bsf PORTB,LCD_DB5 btfsc tmpLcdRegister,2 bsf PORTB,LCD_DB6 btfsc tmpLcdRegister,3 bsf PORTB,LCD_DB7 call LcdPulse_E return LcdShiftL movlw 18h ;Shift Mensagem call LcdSendCommand movlw 150 call msDelay return LcdPulse_E bsf PORTB,LCD_E ;Enables LCD movlw 1 ;Wait 1ms call msDelay bcf PORTB,LCD_E ;Disabled LCD movlw 1 ;Wait 1ms call msDelay return ;=============================================================================== ;Rotinas de conversão p/ BCD ;=============================================================================== B2_BCD bcf STATUS,0 ;limpa carry movlw 16 movwf count clrf R0 clrf R1 clrf R2 loop16 rlf L_byte, F rlf H_byte, F rlf R2, F rlf R1, F rlf R0, F decfsz count, F goto adjDEC RETLW 0 adjDEC movlw R2 movwf FSR call adjBCD movlw R1 movwf FSR call adjBCD movlw R0 movwf FSR call adjBCD goto loop16 adjBCD movlw 3 addwf 0,W movwf temp btfsc temp,3 ;testa se result > 7 movwf 0 movlw 48 addwf 0,W movwf temp btfsc temp,7 ;testa se result > 7 movwf 0 ;salva como MSB RETLW 0 ;=============================================================================== ;Rotinas de conversão p/ ASC ;=============================================================================== ;digitos da unidade e dezena ConvASC movf R2,w ;copia R2->w movwf diguni ;copia w->diguni movwf digdez ;copia w->digdez movlw B'00001111' andwf diguni,f ;zera 4 bits superiores rrf digdez,f rrf digdez,f rrf digdez,f rrf digdez,f movlw B'00001111' andwf digdez,f ;zera 4 bits superiores ;digitos da centena e milhar movf R1,w ;copia R1->w movwf digcent ;copia w->digcent movwf digmil ;copia w->digmil movlw B'00001111' andwf digcent,f ;zera 4 bits superiores rrf digmil,f rrf digmil,f rrf digmil,f rrf digmil,f movlw B'00001111' andwf digmil,f ;zera 4 bits superiores ;digitos da dezena de milhar movf R0,w ;copia R0->w movwf digdezmil ;copia w->digdezmil movlw B'00001111' andwf digdezmil,f ;zera 4 bits superiores CorVal movlw 48 ;w=48 addwf diguni,f ;transf. valor em caracter addwf digdez,f addwf digcent,f addwf digmil,f addwf digdezmil,f movf digdezmil,w ;mostra digdezmil call LcdSendData movf digmil,w ;mostra digmil call LcdSendData movf digcent,w ;mostra digcent call LcdSendData movlw '.' ;*************************************** call LcdSendData ;*************************************** movf digdez,w ;mostra digdez call LcdSendData movf diguni,w ;mostra diguni call LcdSendData movlw ' ' call LcdSendData movlw 'k' call LcdSendData movlw 'H' call LcdSendData movlw 'z' call LcdSendData movlw ' ' call LcdSendData movlw ' ' call LcdSendData movlw ' ' call LcdSendData return ;=============================================================================== ;Rotinas genéricas ;=============================================================================== HabIntTMR0 BANK1 movlw 10100000B ;Habilita interrupção TMR0 movwf INTCON BANK0 return DesIntTMR0 BANK1 movlw 00000000B ;Desabilita interrupção TMR0 movwf INTCON BANK0 return ClearCount clrf diguni ;init contatores clrf digdez clrf digcent clrf digmil clrf digdezmil return Amostra clrf TMR0 ;zera TMR0 clrf tmr1 ;zera tmr1 clrf ovf incf ovf,f ;seta flag de overflow call HabIntTMR0 call usDelay ;+- 100000 us ;movlw 210 ;ajuste fino do tempo ;call msDelay ;amostra durante +/- 1s a freq ;movlw 255 ;call msDelay ;movlw 255 ;call msDelay ;movlw 255 ;amostra p/ 1 décimo de sgundo ;call msDelay call DesIntTMR0 decfsz ovf,f ;se ovf=1 dec e salta goto Amostra ;se ovf=0 dec e refaz amostra movf TMR0,w ;copia TMR0->w ;movlw 255 ;valor para debug ****************** movwf L_byte ;copia w->L_byte movf tmr1,w ;copia tmr1->w ;movlw 255 ;valor para debug ****************** movwf H_byte ;copia w->H_byte call B2_BCD return ;=========================================================================== ;Rotinas de apresentação ;=========================================================================== ShowLoad movlw 00H ;Locate LCD cursor on row 0, col 0 call LcdLocate movlw 'L' ;Shows "Loading..." string on LCD call LcdSendData movlw 'o' call LcdSendData movlw 'a' call LcdSendData movlw 'd' call LcdSendData movlw 'i' call LcdSendData movlw 'n' call LcdSendData movlw 'g' call LcdSendData movlw '.' call LcdSendData movlw '.' call LcdSendData movlw '.' call LcdSendData movlw 255 ;retardo p/ efeito call msDelay movlw 10H ;Locate LCD cursor on row 1, col 0 call LcdLocate movlw 13 ;número de vezes que o caracter vai movwf temp ;se repetir PutChar movlw '#' ;carácter a ser impresso call LcdSendData movlw 255 call msDelay decfsz temp,f goto PutChar movlw 255 ;retardo p/ efeito call msDelay call LcdClear ;limpa LCD return ShowName movlw 00H ;Locate LCD cursor on row 0, col 0 call LcdLocate movlw 'W' ;Shows "WVR-2002" string on LCD call LcdSendData movlw 'V' call LcdSendData movlw 'R' call LcdSendData movlw '-' call LcdSendData movlw '2' call LcdSendData movlw '0' call LcdSendData movlw '0' call LcdSendData movlw '2' call LcdSendData movlw 255 ;retardo p/ efeito call msDelay movlw 255 call msDelay return ShowHello movlw 10H ;Locate LCD cursor on row 1, col 0 call LcdLocate movlw 'H' ;Shows "HELLO WORLD" string on LCD call LcdSendData movlw 'E' call LcdSendData movlw 'L' call LcdSendData movlw 'L' call LcdSendData movlw 'O' call LcdSendData movlw ' ' call LcdSendData movlw 'W' call LcdSendData movlw 'O' call LcdSendData movlw 'R' call LcdSendData movlw 'L' call LcdSendData movlw 'D' call LcdSendData movlw ' ' call LcdSendData movlw '!' call LcdSendData movlw 255 ;retardo p/ efeito call msDelay movlw 255 call msDelay movlw 255 call msDelay movlw 255 call msDelay return ;================================================================================= ;ROTINA PRINCIPAL ;================================================================================= Main ;call usDelay ;******************linha p/ debug call LcdInit ;LCD inizialization call ShowLoad call ShowName call ShowHello call LcdClear call ShowName foreverLoop ;call LcdShiftL ;rola mens. p/ esq. call Amostra ;efetua amostra e contagem de pulsos movlw 10H ;reposiciona cursor call LcdLocate call ConvASC goto foreverLoop End ;fim do programa