Sei sulla pagina 1di 23

EXEMPLOS DE PROGRAMAS MICROCONTROLADORES

-------------------------------------------------------------------------
EXEMPLO 1: ACENDER LED EM ASM

ESTE EXEMPLO ACENDE UM LED NA PORTA B PIN B0, AO SIMULAR GERA UM WARNING
INFINITO

;PRIMEIRO EXEMPLO DE MPLAB X


;PROFESSOR JUAN GABRIEL PAZ
;PROGRAMA PARA ACENDER UM LED

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"

org 0 ;diretiva de origem na posio 0


BSF STATUS,RP0 ;BIT SET FILE(1) ATIVA Banco 1
CLRF PORTB ;Configuro porto B como sada
BCF STATUS,RP0 ;BIT CLEAR FILE(0) ATIVA Banco 0

; A PARTIR DAQUI FAO A ROTINA

BSF PORTB,0; Ativo pino 1 da porta B


END
---------------------------------------------------------------------------

EXEMPLO 2: ACENDER LED EM ASM

Este exemplo acende um led na porta b pin b7, roda bem

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
org 0 ; diretiva de origem na posio 0
BSF STATUS,RP0 ; seleciona banco 1
BCF STATUS,RP1 ; Configuro porto B como sada
BCF TRISB,7 ; PINO 7 COMO SADA

; A PARTIR DAQUI FAO A ROTINA

BCF STATUS,RP0 ; seleciona banco 0 no RP0 do reg. STATUS


BCF STATUS,RP1 ; Configuro porto B como sada
BSF PORTB,7 ; Ativo em alto pino RB7

AQUI
GOTO AQUI
END
---------------------------------------------------------------------------

EXEMPLO 3: PISCA LED EM ASM

ESTE EXEMPLO FAZ PISCAR UM LED NA PORTA B PIN B7, SIMULA BEM

;PRIMEIRO EXEMPLO DE MPLAB XC8


;PROFESSOR JUAN GABRIEL PAZ
;PROGRAMA PARA PISCAR UM LED

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
TEMPO EQU 0x0C

org 0 ; diretiva de origem na posio 0


BSF STATUS,RP0 ; seleciona banco 1
BCF STATUS,RP1 ; Configuro porto B como sada
BCF TRISB,7 ; PINO 7 COMO SADA

; A PARTIR DAQUI FAO A ROTINA

; BCF STATUS,RP0 ; seleciona banco 1


;BCF STATUS,RP1 ; Configuro porto B como sada
;BSF PORTB,7

INICIO
BCF STATUS,RP0
BCF STATUS,RP1
BCF PORTB,7; Ativo pino 1 da porta B
CALL RETARDO ;
BSF PORTB,7 ;
CALL RETARDO ;
GOTO INICIO ;

RETARDO
MOVLW D'255';
MOVFW TEMPO ; TEMPO=255

DEC
DECFSZ TEMPO ;
GOTO DEC
RETURN

;AQUI
;GOTO AQUI
END

---------------------------------------------------------------------------

EXEMPLO 4: PISCA LED EM ASM

ESTE EXEMPLO FAZ PISCAR UM LED NA PORTA B PIN B7, GERA UM WARNING NO
PROTEUS

;PRIMEIRO EXEMPLO DE MPLAB XC8


;PROFESSOR JUAN GABRIEL PAZ
;PROGRAMA PARA PISCAR UM LED

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
TEMPO EQU 0x0C

org 0 ; diretiva de origem na posio 0


BSF STATUS,RP0 ; seleciona banco 1
CLRF PORTB; Configuro porto B como sada
BCF STATUS, RP0

; A PARTIR DAQUI FAO A ROTINA

INICIO BCF PORTB,0; Ativo pino 1 da porta B


CALL RETARDO ;
BSF PORTB,0 ;
CALL RETARDO ;
GOTO INICIO ;

RETARDO
MOVLW D'255';
MOVFW TEMPO ; TEMPO=255

DEC
DECFSZ TEMPO ;
GOTO DEC
RETURN

END

---------------------------------------------------------------------------

EXEMPLO 5: PISCA LED EM micro C

void main()
{
TRISB=0b00000000; // CONFIGURA TODOS OS PINOS DA PORTA B COMO SAIDA
PORTB=0b00000000; // TODOS AS SAIDAS COMEARAO EM NIVEL BAIXO 0 V

while (1)
{
RB0_bit=1;
delay_ms(1000);
RB0_bit=0;
delay_ms(1000);
}
}

---------------------------------------------------------------------------

EXEMPLO 6: RELOGIO DIGITAL, 4 BITS (HEXA 0-15)


ESTE RELOGIO CONTA DE 0 A 15, corrigir para hexa.

void main()
{
TRISA=0b00011111; // CONFIGURA TODOS OS PINOS DA PORTA B COMO SAIDA
TRISB=0b00000000; // CONFIGURA TODOS OS PINOS DA PORTA B COMO SAIDA
PORTB=0b00000000; // TODOS AS SAIDAS COMEARAO EM NIVEL BAIXO 0 V

// 0 SAIDA, 1 ENTRADA
while (1)
{
PORTB++;
delay_ms(1000);
}
}

---------------------------------------------------------------------------

EXEMPLO 7: L ENTRADAS DIGITAIS

Este programa detecta os interruptores nas entradas digitais. Se os


interruptores de RB0 a RB3 so pressionados, o led respectivo em RA0 a RA3
acendido.

list p=16f84A ; usar PIC 16F84A


include "P16F84A.INC"

__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC;

org 0
VAR BSF STATUS,RP0 ; ativa BANK 1
BCF STATUS,RP1
MOVLW B'00001111' ; carga 00000 en W
MOVWF TRISA ; puerto A todos salidas
MOVLW B'00000000' ; carga 11111111 en W
MOVWF TRISB ; puerto B todas entradas
BCF OPTION_REG,7 ; activa pull-up en puerto B
BCF STATUS,5 ; volvemos a la pagina 0

CLRF PORTB ; ponemos a cero el puerto A

INICIO ; etiqueta
CLRF VAR ; ponemos a cero VAR
BTFSC PORTA,0 ; si RB0 es 0 salta 1 linea
BSF VAR,0 ; pone 1 el bit 0 de VAR,
encendido
BTFSC PORTA,1 ; si RB1 es 0 salta 1 linea
BSF VAR,1 ; pone 1 el bit 1 de vAR,
encendido
BTFSC PORTA,2 ; si RB2 es 0 salta 1 linea
BSF VAR,2 ; pone 1 el bit 2 de VAR,
encendido
BTFSC PORTA,3 ; si RB3 es 0 salta 1 linea
BSF VAR,3 ; pone 1 el bit 3 de VAR,
encendido
COMF VAR,0 ; complementa VAR y carga en W (*)
MOVWF PORTB ; carga W en PORTA

GOTO INICIO ; salta a INICIO

END

---------------------------------------------------------------------------

EXEMPLO 8: L ENTRADAS DIGITAIS

Mesma coisa que o exemplo anterior

list p=16f84A ; usar PIC 16F84A


include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC

org 0
BSF STATUS,RP0 ; activa la pagina 1
MOVLW B'00001111' ; carrega 00000 en W
MOVWF TRISA ; Porta A todos os pinos como saida
MOVLW B'00000000' ; carrega 11111111 en W
MOVWF TRISB ; Porta B todos os pinos como entrada
BCF OPTION_REG,7 ; activa pull-up en puerto B
BCF STATUS,5 ; volvemos a la pagina 0

CLRF PORTB ; ponemos a cero el puerto A

INICIO
COMF PORTA,0 ; leemos el puerto B, el valor lo
; complementamos y lo guardamos en W
MOVWF PORTB ; cargamos W en el puerto A
GOTO INICIO ; salta a INICIO
END ; fin de programa

---------------------------------------------------------------------------

EXEMPLO 9: PISCA LED CADA 200ms

list p=16f84A ; usar PIC 16F84A


include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC

CBLOCK 0x0C
ENDC

#DEFINE DISPLAY PORTB,0

org 0

INICIO
BSF STATUS,RP0 ;
BCF DISPLAY ;
BCF STATUS,RP0 ;

ROTINA
BSF DISPLAY
CALL RETARDO_200ms
CALL RETARDO_200ms
BCF DISPLAY
CALL RETARDO_200ms
CALL RETARDO_100ms
GOTO ROTINA

CBLOCK
R_ContA
R_ContB
ENDC

RETARDO_200ms

MOVLW D'200'
GOTO RETARDO_ms

RETARDO_100ms

MOVLW D'100'
GOTO RETARDO_ms

RETARDO_1ms

MOVLW D'1'

RETARDO_ms

MOVWF R_ContB
R1ms_BucleExterno
MOVLW D'249'
MOVWF R_ContA

R1ms_BucleInterno

NOP
DECFSZ R_ContA,F ;
GOTO R1ms_BucleInterno
DECFSZ R_ContB,F
GOTO R1ms_BucleExterno
RETURN

END

---------------------------------------------------------------------------
EXEMPLO 10: CONTADOR 0-9 EM INTERVALO DE 1 SEGUNDO

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC

CBLOCK 0x0C
ENDC
org 0 ; diretiva de origem na posio 0
BSF STATUS,RP0 ; seleciona banco 1
CLRF PORTB
BCF STATUS,RP0

INICIO ; HGFEDCBA
; ::::::::
MOVLW B'01000000'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'11111001'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'00100100'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'10110000'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'00011001'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'10010010'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'00000010'
MOVWF PORTB
CALL RETARDO_1s

MOVLW B'10011000'
MOVWF PORTB
CALL RETARDO_1s
GOTO INICIO

;---------------------------------------------------------
CBLOCK
R_ContC
R_ContA
R_ContB

ENDC

RETARDO_1s
MOVLW D'10'
GOTO RETARDO_1decima

RETARDO_1decima
MOVWF R_ContC

R1decima_LOOPExterno2
MOVLW D'100'
MOVWF R_ContB

R1decima_LOOPExterno
MOVLW D'249'
MOVWF R_ContA

R1decima_LOOPInterno
NOP
DECFSZ R_ContA,F
GOTO R1decima_LOOPInterno
DECFSZ R_ContB,F
GOTO R1decima_LOOPExterno
DECFSZ R_ContC,F
GOTO R1decima_LOOPExterno2
RETURN

END

--------------------------------------------------------------------------

EXEMPLO 11. ROTINAS DE RETARDO

CBLOCK 0x0C
ENDC

CBLOCK
R_ContC
R_ContA
R_ContB

ENDC

;---------------------------------------------------------
; RETARDOS DE 0.5 at 20 segundos
;---------------------------------------------------------
RETARDO_0.5s
MOVLW D'5'
GOTO RETARDO_1decima

RETARDO_1s
MOVLW D'10'
GOTO RETARDO_1decima

RETARDO_2s
MOVLW D'20'
GOTO RETARDO_1decima

RETARDO_5s
MOVLW D'50'
GOTO RETARDO_1decima

RETARDO_10s
MOVLW D'100'
GOTO RETARDO_1decima

RETARDO_20s
MOVLW D'200'
GOTO RETARDO_1decima

; ------ loop do retardo para segundos --------

RETARDO_1decima
MOVWF R_ContC

R1decima_LOOPExterno2
MOVLW D'100'
MOVWF R_ContB

R1decima_LOOPExterno
MOVLW D'249'
MOVWF R_ContA

R1decima_LOOPInterno
NOP
DECFSZ R_ContA,F
GOTO R1decima_LOOPInterno
DECFSZ R_ContB,F
GOTO R1decima_LOOPExterno
DECFSZ R_ContC,F
GOTO R1decima_LOOPExterno2
RETURN
;----------------------------------------------------------------------
;---- RETARDO DE 200ms, 100ms, 50ms, 20ms, 10ms, 5ms, 2ms, 1ms --------
;----------------------------------------------------------------------
RETARDO_200ms
MOVLW D'200'
GOTO RETARDO_ms

RETARDO_100ms
MOVLW D'100'
GOTO RETARDO_ms

RETARDO_50ms
MOVLW D'50'
GOTO RETARDO_ms

RETARDO_20ms
MOVLW D'10'
GOTO RETARDO_ms

RETARDO_10ms
MOVLW D'10'
GOTO RETARDO_ms

RETARDO_5ms
MOVLW D'10'
GOTO RETARDO_ms

RETARDO_2ms
MOVLW D'2'
GOTO RETARDO_ms

RETARDO_1ms
MOVLW D'1'

;---- loops dos retardos milisegundos--------

RETARDO_ms
MOVWF R_ContB

R1ms_LOOPExterno
MOVLW D'249'
MOVWF R_ContA

R1ms_LOOPInterno
NOP
DECFSZ R_ContA,F ;
GOTO R1ms_LOOPInterno
DECFSZ R_ContB,F
GOTO R1ms_LOOPExterno
RETURN
;-------------------------------------------------
;-------------------------------------------------
END

EXEMPLO 12: INCREMENTA VALOR SE RA0 EST ALTO

ROTINA PARA INCREMENTAR UM VALOR

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
CONTA EQU 0x0C

org 0 ; diretiva de origem na posio 0


BSF STATUS,RP0 ; seleciona banco 1
BSF TRISA,0 ; seleciona banco 1
CLRF PORTB; Configuro porto B como sada
BCF STATUS, RP0
;A PARTIR DAQUI FAO A ROTINA
INICIO MOVLW PORTA;
MOVFW CONTA ;
BTFSC PORTA,0;
INCF CONTA,W
GOTO INICIO
END

---------------------------------------------------------------------------
EXEMPLO 13: SEGUIDOR DE LINHA

include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC;
#DEFINE SENSORDIREITA PORTA,0
#DEFINE SENSORESQUERDA PORTA,1
org 0
INICIO BSF STATUS,RP0
BCF STATUS,RP1
BSF SENSORDIREITA
BSF SENSORESQUERDA
CLRF PORTB
BCF STATUS,5

ROTINA MOVLW B'00001110'


BTFSS SENSORDIREITA
GOTO ATIVASAIDA

MOVLW B'00000111'
BTFSS SENSORESQUERDA
MOVLW B'00001111'

ATIVASAIDA MOVWF PORTB


GOTO ROTINA
END

---------------------------------------------------------------------------

EXEMPLO 14. PROGRAMA PARA MOSTRAR MENSAGEM EM TELA LCD


DO LIVRO DESARROLLO DE PROYECTOS, NO CONSEGUI RODAR

list p=16f84A ;INDICA O DISPOSITIVO UTILIZADO


include "P16F84A.INC"
__CONFIG _CP_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC

CBLOCK 0x0C
ENDC

CBLOCK
LCD_Dato
;LCD_BusDatos
LCD_GuardaDato
LCD_GuardaTRISB
LCD_Auxiliar1
LCD_Auxiliar2
R_ContC
R_ContA
R_ContB
ENDC

LCD_CaracteresPorLinea EQU 0x10; .16

#DEFINE LCD_PinRS PORTA,0;


#DEFINE LCD_PinRW PORTA,1;
#DEFINE LCD_PinEnable PORTA,2;
#DEFINE LCD_BusDatos PORTB;
org 0

INICIO
CALL LCD_Inicializa;
MOVLW 'H';
CALL LCD_Caracter;
MOVLW 'O';
CALL LCD_Caracter;
MOVLW 'L';
CALL LCD_Caracter;
MOVLW 'A';
CALL LCD_Caracter;

SLEEP

;************ ROTINA GERAL LCD************

LCD_Inicializa;
BSF STATUS,RP0;
BCF LCD_PinRS
BCF LCD_PinEnable;
BCF LCD_PinRW;
BCF STATUS,RP0 ;

BCF LCD_PinRW;
BCF LCD_PinEnable;
BCF LCD_PinRS;
CALL RETARDO_20ms;

MOVLW B'00110000';
CALL LCD_EscribeLCD;
CALL RETARDO_5ms
MOVLW B'00110000';
CALL RETARDO_200micros;
MOVLW B'00110000';
CALL LCD_EscribeLCD;
MOVLW B'00100000';
CALL LCD_EscribeLCD;

;******* CONFIGURAR PARMETROS *******

CALL LCD_2Lineas4Bits;
CALL LCD_Borra;
CALL LCD_CursorOFF;
CALL LCD_CursorIncr;
RETURN

LCD_EscribeLCD
ANDLW B'11110000'
MOVWF LCD_Dato;
MOVF LCD_BusDatos,W;

ANDLW B'00001111'
IORWF LCD_Dato,F;

BSF STATUS,RP0;
MOVF TRISB,W;
MOVWF LCD_GuardaTRISB;
MOVLW B'00001111';
ANDWF PORTB,F;
BCF STATUS,RP0;

MOVF LCD_Dato,W;
MOVWF LCD_BusDatos;
BSF LCD_PinEnable;
BCF LCD_PinEnable;
BSF STATUS,RP0;
MOVF LCD_GuardaTRISB,W;
MOVWF PORTB;
BCF STATUS,RP0;
RETURN

LCD_CursorIncr
MOVLW B'00000110';
GOTO LCD_Enviacomando;

LCD_Linea1
MOVLW B'10000000';
GOTO LCD_Enviacomando;

LCD_Linea2
MOVLW B'11000000';
GOTO LCD_Enviacomando;

LCD_PosicionLinea1
IORLW B'10000000';
GOTO LCD_Enviacomando;

LCD_PosicionLinea2
IORLW B'11000000';
GOTO LCD_Enviacomando;

LCD_OFF
MOVLW B'00001000';
GOTO LCD_Enviacomando;

LCD_CursorON
MOVLW B'00001110';
GOTO LCD_Enviacomando;

LCD_CursorOFF
MOVLW B'00001100';
GOTO LCD_Enviacomando;

LCD_Borra
MOVLW B'00000001';
GOTO LCD_Enviacomando;

LCD_2Lineas4Bits;
MOVLW B'00101000';
GOTO LCD_Enviacomando;

;*******
LCD_Enviacomando;
BCF LCD_PinRS;
GOTO LCD_Envia;

LCD_Caracter;
BSF LCD_PinRS;
GOTO LCD_CodigoCGROM;
LCD_Envia;
MOVWF LCD_GuardaDato;
CALL LCD_EscribeLCD;

SWAPF LCD_GuardaDato,W;
CALL LCD_EscribeLCD;
BTFSS LCD_PinRS;
CALL RETARDO_2ms;
CALL RETARDO_50micros;
RETURN
;******************************

LCD_CodigoCGROM
MOVWF LCD_Dato;

LCD_EnheMinuscula
SUBLW ''
BTFSS STATUS,Z;
GOTO LCD_EnheMaiuscula;
MOVLW B'11101110';
MOVWF LCD_Dato;
GOTO LCD_FinCGROM;

LCD_EnheMaiuscula;
MOVF LCD_Dato,W;
SUBLW ''
BTFSS STATUS,Z;
GOTO LCD_Grau;
MOVLW B'11101110';
MOVWF LCD_Dato;
GOTO LCD_FinCGROM;

LCD_Grau
MOVF LCD_Dato,W;
SUBLW ''
BTFSS STATUS,Z;
GOTO LCD_FinCGROM;
MOVLW B'11011111';
MOVWF LCD_Dato;

LCD_FinCGROM;
MOVF LCD_Dato,W;
RETURN
;***************************************

LCD_LineaEnBlanco
MOVLW LCD_CaracteresPorLinea;
GOTO LCD_EnviaBlancos;

LCD_UnEspacioBlanco
MOVLW .1;
GOTO LCD_EnviaBlancos;

LCD_DosEspaciosBlancos
MOVLW .2;
GOTO LCD_EnviaBlancos;

LCD_TresEspaciosBlancos
MOVLW .3;
LCD_EnviaBlancos;
MOVLW LCD_Auxiliar1;

LCD_EnviaOtroBlanco;
MOVLW ' ';
CALL LCD_Caracter;
DECFSZ LCD_Auxiliar1,F
GOTO LCD_EnviaOtroBlanco;
RETURN

LCD_Byte
MOVWF LCD_Auxiliar2;
ANDLW B'11110000';
BTFSS STATUS,Z;
GOTO LCD_VizualizaAlto
MOVLW ' ';
CALL LCD_Caracter
GOTO LCD_VizualizaBajo

LCD_ByteCompleto
MOVWF LCD_Auxiliar2;

LCD_VizualizaAlto
SWAPF LCD_Auxiliar2,W;
CALL LCD_Nibble;

LCD_VizualizaBajo
MOVF LCD_Auxiliar2;
CALL LCD_Nibble;
RETURN

LCD_Nibble;
ANDLW B'00001111';
MOVWF LCD_Auxiliar1;
SUBLW 0x09
BTFSS STATUS,C;
GOTO LCD_EnviaByteLetra;
MOVF LCD_Auxiliar1,W;
ADDLW '0';
GOTO LCD_FinVisualizaDigito;

LCD_EnviaByteLetra;
MOVF LCD_Auxiliar1,W;
ADDLW 'A';

LCD_FinVisualizaDigito;
GOTO LCD_Caracter;

;******************** RETARDOS ********************

RETARDO_20ms
MOVLW D'10'
GOTO RETARDO_ms

RETARDO_5ms
MOVLW D'10'
GOTO RETARDO_ms
RETARDO_2ms
MOVLW D'2'

RETARDO_ms
MOVWF R_ContB

R1ms_LOOPExterno
MOVLW D'249'
MOVWF R_ContA

R1ms_LOOPInterno
NOP
DECFSZ R_ContA,F ;
GOTO R1ms_LOOPInterno
DECFSZ R_ContB,F
GOTO R1ms_LOOPExterno
RETURN

;*************************

RETARDO_500micros
NOP
MOVLW D'164';
GOTO RetardoMicros;

RETARDO_200micros
NOP
MOVLW D'64';
GOTO RetardoMicros;

RETARDO_50micros
NOP
MOVLW D'14';

RetardoMicros;
MOVWF R_ContA

Rmicros_LOOP
NOP
DECFSZ R_ContA,F ;
GOTO Rmicros_LOOP
RETURN

END

---------------------------------------------------------------------------
EXEMPLO 15. PROGRAMA PARA MOSTRAR O TEMPO EM UMA TELA LCD
FUNCIONA QUE NEM UMA BELEZA

LIST P=16F84 ; 16F84 Runs at 4.096 MHz


INCLUDE "p16f84.inc"
__CONFIG _PWRTE_ON & _XT_OSC & _WDT_OFF
ERRORLEVEL -224 ; supress annoying message from tris

; Define Information
#DEFINE RS PORTA, 0
#DEFINE E PORTA, 1
#DEFINE TOGGLESW PORTA, 4
; Macro

EStrobe MACRO ; Strobe the "E" Bit


bsf E
bcf E
ENDM

CBLOCK 0CH
sec ; seconds digit
sec10 ; 10's of second digit
mins ; minutes digit
min10 ; 10's of minutes digit
hr ; hours digit
hr10 ; 10's of hours digit
highlim ; high limit + 1 of digit
w_temp ; holds W during interrupt
status_temp ; holds STATUS during interrupt
fsr_temp ; holds FSR during interrupt
Dlay ; 8 Bit Delay Variable
working ; working flag 0 not working, 1 working
ptr ; used in displaying message
Temp ; a temporary variable
bin ; a temporary variable
oset ; offset of time register
oldtime ; holds last value of sec
cntmsec ; used in counting milliseconds
isrcnt ; used in isr to stretch time to 1 sec
ENDC

ORG 0 ; start at location 0

goto main ; jump over to main routine

ORG 4

goto isr ; jump to interrupt routine

;----------------------------------------------------------------------;
; High limit + 1 of digits at position W ;
;----------------------------------------------------------------------;
sethi:
addwf PCL, f
dt H'A',H'6',H'A',H'6',H'A',H'A'

;----------------------------------------------------------------------;
; Data for message to be output ;
;----------------------------------------------------------------------;
shomsg ; Message to Output
addwf PCL, f ; Output the Characters
dt "J PASSARAM:", 0

;----------------------------------------------------------------------;
; ISR, increments time by one second, (BCD), every 125th time through ;
;----------------------------------------------------------------------;
isr:
movwf w_temp ; save W
swapf STATUS,W ; save status
movwf status_temp ; without changing flags
swapf FSR,W ; save FSR
movwf fsr_temp ; without changing flags
decfsz isrcnt, f ; count down to zero before incr.
goto restore
movlw D'125' ; reset isr count
movwf isrcnt
movf working, f ; check working flag
btfsc STATUS, Z ; if not zero then increment time
goto restore ; else get out of interrupt routine

movlw sec ; point at sec register


movwf FSR
newdigit: incf INDF, f ; current digit up one
movlw sec ; get difference, sec and FSR
subwf FSR, W
call sethi ; use to get high limit + 1
subwf INDF, W ; reached that number yet?
btfss STATUS, Z ; skip over if yes
goto restore ; else exit isr
clrf INDF ; set current digit to 0
incf FSR, f ; point at next digit
goto newdigit ; no, increment the next digit
restore:
swapf status_temp,W ; get original status back
movwf STATUS ; into status register
swapf fsr_temp,W ; get original fsr back
movwf FSR ; into status register
swapf w_temp,f ; old no flags trick again
swapf w_temp,W ; to restore W
bcf INTCON,T0IF ; clear the TMR0 interrupt flag
retfie ; finished reset GIE

;----------------------------------------------------------------------;
; Initialize the ports ;
;----------------------------------------------------------------------;
init:
clrf PORTA
clrf PORTB

movlw B'00010000' ; RA4 input, others outputs


tris PORTA
movlw B'00000000' ; all outputs on port B
tris PORTB
movlw B'00000011' ; pull-ups enabled
; prescaler assigned to TMR0
; prescaler set to 1:16
; rolls over each 125th second
option

movlw 0 ; zero out all registers


movwf hr10
movwf hr
movwf min10
movwf mins
movwf sec10
movwf sec
clrf working ; working flag to zero

movlw B'10100000' ; GIE set T0IE set, T0IF cleared


movwf INTCON
return

;----------------------------------------------------------------------;
; Initialize the LCD ;
;----------------------------------------------------------------------;
initlcd:
movlw D'40'
call nmsec ; Wait 40 msecs before Reset
bcf RS ; send an 8 bit instruction
movlw 0x03 ; Reset Command
call NybbleOut ; Send the Nybble
call Dlay5 ; Wait 5 msecs before Sending Again
EStrobe
call Dlay160 ; Wait 160 usecs before Sending 2nd Time
EStrobe
call Dlay160 ; Wait 160 usecs before Sending 3rd Time
bcf RS ; send an 8 bit instruction
movlw 0x02 ; Set 4 Bit Mode
call NybbleOut
call Dlay160
movlw 0x028 ; 4 bit, 2 Line, 5x7 font
call SendINS
movlw 0x010 ; display shift off
call SendINS
movlw 0x001 ; Clear the Display RAM
call SendINS
call Dlay5 ; Note, Can take up to 4.1 msecs
movlw 0x006 ; increment cursor
call SendINS
movlw 0x00C ; display on cursor off
call SendINS
return

;----------------------------------------------------------------------;
; Send the character in W out to the LCD ;
;----------------------------------------------------------------------;
SendASCII
addlw '0' ; Send nbr as ASCII character
SendCHAR ; Send the Character to the LCD
movwf Temp ; Save the Temporary Value
swapf Temp, w ; Send the High Nybble
bsf RS ; RS = 1
call NybbleOut
movf Temp, w ; Send the Low Nybble
bsf RS
call NybbleOut
return

;----------------------------------------------------------------------;
; Send an instruction in W out to the LCD ;
;----------------------------------------------------------------------;
SendINS ; Send the Instruction to the LCD
movwf Temp ; Save the Temporary Value
swapf Temp, w ; Send the High Nybble
bcf RS ; RS = 0
call NybbleOut
movf Temp, w ; Send the Low Nybble
bcf RS
call NybbleOut
return

;----------------------------------------------------------------------;
; Send the nibble in W out to the LCD ;
;----------------------------------------------------------------------;
NybbleOut ; Send a Nybble to the LCD
movwf PORTB
EStrobe ; Strobe out the LCD Data
call Dlay160 ; delay for 160 usec
return

;----------------------------------------------------------------------;
; Output the message on the LCD ;
;----------------------------------------------------------------------;
OutMessage:
movwf FSR ; Point at first letter
OutLoop:
movf FSR, w ; Get pointer into W
incf FSR, f ; Set up for next letter
call shomsg ; Get character to output
iorlw 0 ; At the End of the Message?
btfsc STATUS, Z ; Skip if not at end
return ; Yes - Equal to Zero
call SendCHAR ; Output the ASCII Character
goto OutLoop ; Get the next character

;----------------------------------------------------------------------;
; Wait until button is released ;
;----------------------------------------------------------------------;
waitup:
btfss TOGGLESW ; test toggle switch
goto $ -1 ; ck again if pressed
movlw 20 ; wait 20 msec for debounce
call nmsec
btfss TOGGLESW ; check again, still up?
goto $ -4 ; no start over
return ; yes, finished

;----------------------------------------------------------------------;
; time delay routines ;
;----------------------------------------------------------------------;

Dlay160: movlw D'41' ; delay about 160 usec


micro4 addlw H'FF' ; subtract 1 from 'W'
btfss STATUS,Z ; skip when you reach zero
goto micro4 ; more loops
return

Dlay5: movlw 5 ; delay for 5 milliseconds


goto $ + 2
msec250: movlw D'250' ; delay for 250 milliseconds
;*** N millisecond delay routine ***
nmsec: movwf cntmsec ; delay for N (in W) millisec
msecloop: movlw D'254' ; load takes .9765625 microsec
call micro4 ; by itself CALL takes ...
; 2 + 253 X 4 + 3 + 2 = 1019
; 1019 * .977 = 995 microsec
nop ; .98 microsec
decfsz cntmsec, f ; .98 skip not taken, else 1.95
goto msecloop ; 1.95 here: total ~1000 / loop
return ; final time through ~999 to here
; overhead in and out ignored

;----------------------------------------------------------------------;
; Display the Time ;
;----------------------------------------------------------------------;
DispTime
MOVLW H'C0' ; position at beginning of second line
CALL SendINS
MOVF hr10, W ; tens of hours
CALL SendASCII
MOVF hr, W ; hours
CALL SendASCII
MOVLW ":"
CALL SendCHAR
MOVF min10, W ; tens of minutes
CALL SendASCII
MOVF mins, W ; minutes
CALL SendASCII
MOVLW ":"
CALL SendCHAR
MOVF sec10, W ; tens of seconds
CALL SendASCII
MOVF sec, W ; seconds
CALL SendASCII
RETURN

;----------------------------------------------------------------------;
; Toggle Work Flag ;
;----------------------------------------------------------------------;
togglewk:
movf working, f ; set zero flag if zero
btfss STATUS, Z ; skip if working is zero
goto turnoff
incf working, f ; set working to 1
call waitup ; wait for button release
return
turnoff:
clrf working ; working set to zero
call waitup ; wait for button to be released
return

;----------------------------------------------------------------------;
; The Main routine ;
;----------------------------------------------------------------------;
main:
call init ; initialize ports, set up timer
call initlcd ; initialize the LCD
movlw 0 ; display 'Working Time:'
call OutMessage
ckbutton: ; check for a press of the TOGGLE button
btfss TOGGLESW ; skip if not pressed
call togglewk ; else change the mode of timer
movf oldtime, W ; is oldtime the same as sec?
subwf sec, W
btfsc STATUS, Z ; if not, skip over next instruction
goto ckbutton ; else continue checking
call DispTime ; sec has changed, display the time
movf sec, W ; make sec and oldsec the same
movwf oldtime
goto ckbutton ; and continue checking

end
PROGRAMA PARA MOSTRAR MENSAGEM NUMA TELA LCD 16X2 .C

// Lcd pinout settings


sbit LCD_RS at RB3_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D4 at RB4_bit;

// Pin direction
sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB7_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB4_bit;

void main() {
TRISA=0x00;
TRISB=0x00;
PORTA=0x00;
PORTB=0x00;

Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"UEMS_DOURADOS");
delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
delay_ms(100);
while(1)
{
Lcd_Out(1,1,"AC_II");
Lcd_Out(2,1,"ENG_FIS");
delay_ms(1000);
Lcd_Cmd(_LCD_TURN_OFF);
delay_ms(1000);
Lcd_Cmd(_LCD_TURN_ON);
}
}

INTERFACE DE TECLADO MATRICIAL COM LCD PIC 16F877A


unsigned short kp,cnt,oldstate = 0 ;
char txt [6];
//Keypad module connections
char keypadPort at PORTC;
//End Keypad module connections
//LCD MODULE CONNECTIONS
sbit LCD_RS at RB1_bit;
sbit LCD_EN at RB0_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB1_bit;
sbit LCD_EN_Direction at TRISB0_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
//End LCD module connections
void main() {
cnt = 0; //Reset counter
Keypad_Init(); //INICIALIZA O KEYPAD
Lcd_Init(); //INICIALIZA O LCD
Lcd_Cmd(_LCD_CLEAR); //LIMPA DISPLAY
Lcd_Cmd(_LCD_CURSOR_OFF); //CURSOR DESLIGADO

Lcd_Out(1,1,"Tecla: "); //ESCREVA MENSA-GEM NO LCD


Lcd_Out(2,1,"Contador:");

{
kp = 0;// RESETA CONTADOR
//WAIT FOR KEY TO BE PRESSED AND RE-LEASED
while(1)
{
do
kp = Keypad_Key_Press(); //Store key code in kp variable
//kp = Keypad_Key_Click(); //Store key code in kp variable
while(!kp);
//Prepare value for output, transform key to it's ACII value
switch(kp)
{
case 1:kp = 49; break; //1
case 2:kp = 50; break; //2
case 3:kp = 51; break; //3
//case 4:kp = 65; break; // A commented since 4th column is absent
for 4*3 keypad
case 5:kp = 52; break; //4
case 6:kp = 53; break; //5
case 7:kp = 54; break; //6
//case 8:kp = 66; break; // B commented since 4th column is absent
for 4*3 keypad
case 9:kp = 55; break; //7
case 10:kp = 56; break; //8
case 11:kp = 57; break; //9
//case 12:kp = 67; break; // C commented since 4th column is absent
for 4*3 keypad
case 13:kp = 42; break; //*
case 14:kp = 48; break; //0
case 15:kp = 35; break; //#
//case 16:kp = 68; break; //D
}
if (kp != oldstate)
{//Pressed key differs from previous
cnt = 1;
oldstate = kp;
}
else
{//Pressed key is same as previous
cnt++;

}
Lcd_Chr(1,10,kp); //Print key ASCII value on Lcd
if(cnt == 255)
{//If counter variable overflow
cnt = 0;
Lcd_Out(2,10," ") ;
}
WordToStr(cnt,txt); //Transform counter value to string
Lcd_Out(2,10,txt); //Display counter value on Lcd
}while(1);

}
}
LCD1
LM016L

RV1
VDD
VSS

VEE

RW
RS

D0
D1
D2
D3
D4
D5
D6
D7
E

R9 1k
1
2
3

4
5
6

7
8
9
10
11
12
13
14

10k

U2
13 33
OSC1/CLKIN RB0/INT
14 34
OSC2/CLKOUT RB1
35
RB2
2 36
RA0/AN0 RB3/PGM
3 37
RA1/AN1 RB4
4 38
RA2/AN2/VREF-/CVREF RB5
5 39
RA3/AN3/VREF+ RB6/PGC
X1 6 40
RA4/T0CKI/C1OUT RB7/PGD
7
RA5/AN4/SS/C2OUT
1 2 15
RC0/T1OSO/T1CKI
8 16
RE0/AN5/RD RC1/T1OSI/CCP2
9 17
CRYSTAL RE1/AN6/WR RC2/CCP1
10 18
RE2/AN7/CS RC3/SCK/SCL
C1 C2 RC4/SDI/SDA
23
22pF 22pF 1 24
MCLR/Vpp/THV RC5/SDO
25
RC6/TX/CK
26
RC7/RX/DT
19
RD0/PSP0
20
RD1/PSP1
21
RD2/PSP2
22
RD3/PSP3
27
RD4/PSP4
28
RD5/PSP5
29
1

RD6/PSP6
30
RD7/PSP7
PIC16F877A
A 1 2 3
B 4 5 6
C 7 8 9
D 0 #

Potrebbero piacerti anche