Академический Документы
Профессиональный Документы
Культура Документы
Elaborado por:
Arzabala Contreras Ernesto Alonso
Salazar Lopez Luis Ricardo
Vallejo Gonzlez Mario Alberto
Contenido
Unidad I Arquitectura interna de los Microcontroladores.................................................................. 8
Practica 1.1 Conexiones basicas y encendido de leds ..................................................................... 8
Descripcin P1.1: ......................................................................................................................... 8
Cdigo P1.1: ................................................................................................................................ 8
Circuito P1.1: ............................................................................................................................... 9
Practica 1.2 Oscilador interno y ciclos de repeticin ...................................................................... 9
Descripcin P1.2: ......................................................................................................................... 9
Cdigo P1.2: .............................................................................................................................. 10
Circuito P1.2: ............................................................................................................................. 12
Practica 1.3 Ciclos anidados .......................................................................................................... 13
Descripcin P1.3: ....................................................................................................................... 13
Cdigo P1.3: .............................................................................................................................. 13
Circuito P1.3: ............................................................................................................................. 15
Practica 1.4 Temporizador TMR0 e interrupciones ...................................................................... 15
Descripcin P1.4: ....................................................................................................................... 15
Cdigo P1.4: .............................................................................................................................. 16
Circuito P1.4: ............................................................................................................................. 19
Practica 1.5 Uso de botones y subrutinas ..................................................................................... 19
Descripcin P1.5: ....................................................................................................................... 19
Cdigo P1.5: .............................................................................................................................. 20
Circuito P1.5: ............................................................................................................................. 22
Practica 1.6 Temporizador TMR0 como contador, variables y relevadores ................................. 22
Descripcin P1.6: ....................................................................................................................... 22
Cdigo P1.6: .............................................................................................................................. 23
Circuito P1.6: ............................................................................................................................. 25
Practica 1.7 Macros y rutinas para evitar el rebote de botones ................................................... 25
Descripcin P1.7: ....................................................................................................................... 25
Cdigo P1.7: .............................................................................................................................. 25
Circuito P1.7: ............................................................................................................................. 31
Practica 1.8 Temporizador TMR1 e interrupciones ...................................................................... 31
Descripcin P1.8: ....................................................................................................................... 31
Practica 3.4 Medicin de temperatura con un sensor tipo IC y calibracin de un termistor. .... 122
Descripcion P3.4: ..................................................................................................................... 122
Circuito P3.4: ........................................................................................................................... 122
Cdigo P3.4: ............................................................................................................................ 122
Practica 3.5 Control de temperatura tipo ON/OFF ..................................................................... 123
Descripcion P3.5: ..................................................................................................................... 123
Circuito P3.5: ........................................................................................................................... 123
Cdigo P3.5: ............................................................................................................................ 123
Practica 3.6 Modulo remoto de medicin y control. 3 sensores, 2 actuadores, comunicacin
mediante RS232, modo de control remoto mediante PC, modo de control local mediante
botones. ...................................................................................................................................... 123
Descripcion P3.6: ..................................................................................................................... 123
Circuito P3.6: ........................................................................................................................... 124
Cdigo P3.6: ............................................................................................................................ 124
Proyecto final .............................................................................................................................. 124
Descripcion PF: ........................................................................................................................ 124
Circuito PF: .............................................................................................................................. 124
Cdigo PF:................................................................................................................................ 124
Formato de reportes ................................................................................................................... 125
Criterios y ponderaciones de evaluacin .................................................................................... 126
Unidad I ................................................................................................................................... 126
Unidad II .................................................................................................................................. 126
Unidad III ................................................................................................................................. 126
Links de ayuda ............................................................................................................................. 127
Nombre
P1_1.asm
Fecha
Enero 10,2013
Alumno
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG _CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_ON & _IESO_ON &
_FCMEN_ON & _LVP_OFF
ORG
0x0000
Banksel
TRISB
clrf
TRISB
banksel
PORTB
movlw
B'01010101'
movwf
PORTB
end
Circuito P1.1:
1.
2.
3.
4.
5.
Para poder observar los cambios de combinacin binaria en el puerto B, apreciar el efecto del
parpadeo, el proceso mencionado anteriormente se necesita llevar a cabo de una manera lenta.
Para lograr esto se utiliza el oscilador interno LFINTOSC del microcontrolador el cual tiene una
frecuencia de 31 kHz mucho ms lenta que la del oscilador externo el cual tiene conectado el
cristal de 8 MHz.
La fuente de la seal de reloj es cambiada durante la ejecucin del programa, on the fly. Para
verificar que esto realmente est sucediendo, antes de energizar el uC desconecta el cristal. Qu
paso? El uC no debe de hacer nada ya que la configuracin de este requiere el uso del cristal al
momento de iniciar a ejecutar el programa. Si el cristal se desconecta despus de que el uC ya
inicio la ejecucin del programa esto no debe de afectar el funcionamiento del uC de ninguna
manera.
Cdigo P1.2:
;************************************************
;
Nombre :
P1_2.asm
Fecha
Enero 10,2013
Alumno :
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG
_CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF &
_BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG
;************************************************************************
;************* DEFINING VARIABLES ***************************************
cblock 0x20
counter1
endc
;************************************************************************
org
0x0000
banksel OSCCON
OSCCON,6
bcf
OSCCON,5
bcf
OSCCON,4
bsf
OSCCON,0
banksel TRISB
clrf TRISB
banksel PORTB
loop
movlw B'01010101'
movwf PORTB
movlw h'FF'
movwf counter1
loop1
decfsz counter1
goto loop1
movlw B'10101010'
movwf PORTB
movlw h'FF'
movwf counter1
loop2
decfsz counter1
goto loop2
Circuito P1.2:
Nombre :
P1_3.asm
Fecha
Enero 10,2013
Alumno :
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG
_CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF &
_BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG
0x20
counter1
counter2
endc
;**************************************************************************
org
banksel
clrf
banksel
0x0000
TRISB
TRISB
; Clears TRISB
PORTB
loop
movlw
movwf
PORTB
movlw
h'FF'
movwf
counter2
loop2
movlw
h'FF'
movwf
counter1
loop1
decfsz
counter1
goto
loop1
decfsz
counter2
goto
loop2
movlw
movwf
PORTB
movlw
h'FF'
movwf
counter2
loop4
movlw
h'FF'
movwf
counter1
loop3
decfsz
counter1
loop3
decfsz
counter2
goto
loop4
goto
loop
end
; End of program
Circuito P1.3:
Igual al de la prctica P1.2
Cdigo P1.4:
;************************************************
;
Nombre :
P1_4.asm
Fecha
Enero 10,2013
Alumno :
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG
_CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF &
_BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG
cblock
0x20
w_temp
pclath_temp
status_temp
endc
0x0000
goto
main
; Go to label "main"
0x0004
; Interrupt vector
w_temp
STATUS
status_temp
PCLATH
movwf
pclath_temp
banksel
PORTB
incf
banksel
bcf
movf
movwf
movf
PORTB
INTCON
movwf
STATUS
swapf
w_temp,f
swapf
w_temp,w
bsf
INTCON,GIE
retfie
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
banksel
clrf
banksel
TRISB
TRISB
OPTION_REG
bcf
bcf
bsf
bsf
OPTION_REG,PS1
bsf
OPTION_REG,PS2
banksel
INTCON
bsf
bsf
INTCON,GIE
banksel
clrf
PORTB
PORTB
; Clears port B
loop
; Remain here
loop
goto
end
; End of program
Circuito P1.4:
Cdigo P1.5:
;************************************************
;
Nombre :
P1_5.asm
Fecha
Enero 10,2013
Alumno :
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG
_CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF &
_BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG
cblock
0x20
counter1
counter2
endc
org
banksel
0x0000
ANSEL
clrf
ANSEL
clrf
ANSELH
banksel
clrf
TRISB
TRISB
movlw
B'00000010'
movwf
TRISA
banksel
PORTB
movlw
B'00000001'
movwf
PORTB
; Writes 1 to register W
; Number is moved to PORTB
loop
rlf
PORTB
call
DELAY
loop3
btfss
PORTA,1
goto
loop3
goto
loop
counter2
counter1
loop1
clrf
loop2
decfsz
counter1
goto
loop2
decfsz
counter2
goto
loop1
return
end
; End of program
Circuito P1.5:
Cdigo P1.6:
;************************************************
;
Nombre :
P1_6.asm
Fecha
Enero 10,2013
Alumno :
Grupo
MT00X
;************************************************
;
Descripcion
;************************************************
list p=16f887
#include <p16f887.inc>
errorlevel -302
__CONFIG
_CONFIG1, _HS_OSC & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF &
_BOR_ON & _IESO_ON & _FCMEN_ON & _LVP_OFF
__CONFIG
TEST
#define
banksel
0x0000
TRISB
clrf
TRISB
clrf
TRISD
movlw
B'00010000'
movwf
TRISA
banksel
OPTION_REG
bsf
bsf
banksel
PORTB
clrf
TMR0
bcf
PORTD,3
loop
movfw
TMR0
movwf
PORTB
xorlw
TEST
btfsc
bsf
goto
end
STATUS,Z
PORTD,3
loop
; End of program
Circuito P1.6:
cblock
HIcnt
0x20
LOcnt
LOOPcnt
cnt
endc
;**********************************************************************
ORG
0x000
; Reset vector
nop
goto
main
;**********************************************************************
include "pause.inc"
include "button.inc"
;**********************************************************************
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
banksel
TRISB
bsf
TRISA, 0
bsf
TRISA, 1
clrf
TRISB
banksel
clrf
PORTB
cnt
Loop
button
PORT,0,0,Increment
button
PORT,1,0,Decrement
goto
Loop
Increment
incf
movf
movwf
goto
cnt, f
cnt, w
PORTB
Loop
Decrement
decf
movf
movwf
goto
cnt, f
cnt, w
PORTB
Loop
end
; End of program
Macro pausems
ARCHIVO: pause.inc
Loop1
local
dechi
local
Delay1ms
local
Loop2
local
End
movlw
High(arg1)
; to HIcnt
movwf
HIcnt
movlw
Low(arg1)
; to LOcnt
movwf
LOcnt
Loop1
movf
LOcnt, f
btfsc
STATUS, Z
goto
dechi
call
Delay1ms
decf
LOcnt, f
goto
Loop1
dechi
movf
HIcnt, f
btfsc
STATUS, Z
goto
End
call
Delay1ms
decf
HIcnt, f
decf
LOcnt, f
goto
Loop1
Delay1ms:
movlw
.100
movwf
LOOPcnt
; 100*10us=1ms
; LOOPcnt<-100
Loop2:
nop
nop
nop
nop
nop
nop
nop
decfsz
LOOPcnt, f
goto
Loop2
return
End
ENDM
;**********************************************************************
Macro button
ARCHIVO: button.inc
Pressed1
local
Pressed2
local
Exit1
local
Exit2
IFNDEF
; in main program
#define
debouncedelay .10
ENDIF
IF (hilo == 0)
btfsc
port, pin
goto
Exit1
pausems
; If pull-up used
; If "1", push-button is pressed
Pressed1
btfss
port, pin
goto
Pressed1
pausems
goto
Exit1
ELSE
; If pull-down used
btfss
port, pin
goto
Exit2
pausems
Pressed2
btfsc
port, pin
goto
Pressed2
pausems
goto
Exit2
ENDIF
ENDM
;**********************************************************************
Circuito P1.7:
cblock
0x20
w_temp
pclath_temp
status_temp
endc
0x0000
goto
main
org
movwf
movf
movwf
movf
0x0004
; Interrupt vector
w_temp
STATUS
; Save register W
status_temp
PCLATH
movwf
pclath_temp
banksel
PORTB
incf
movf
movwf
movf
PORTB
movwf
STATUS
swapf
w_temp,f
swapf
w_temp,w
banksel
PIR1
bcf
PIR1,TMR1IF
bsf
INTCON,GIE
retfie
main
banksel
clrf
ANSEL
clrf
ANSELH
banksel
clrf
banksel
TRISB
TRISB
T1CON
bcf
bsf
bsf
T1CON,T1CKPS1
bsf
banksel
PIE1
bsf
PIE1,TMR1IE
bsf
INTCON,PEIE
banksel
clrf
INTCON,GIE
PORTB
PORTB
; Clears port B
loop
; Remain here
loop
goto
end
; End of program
Circuito P1.8:
cblock
0x20
w_temp
pclath_temp
status_temp
endc
0x0000
goto
main
org
movwf
movf
movwf
movf
0x0004
; Interrupt vector
w_temp
STATUS
; Save register W
status_temp
PCLATH
movwf
pclath_temp
banksel
PORTB
incf
PORTB
banksel
decf
movf
movwf
movf
PR2
PR2
; PR2 is decremented by 1
movwf
STATUS
swapf
w_temp,f
swapf
w_temp,w
banksel
PIR1
bcf
PIR1,TMR2IF
bsf
INTCON,GIE
retfie
bcf
OSCCON,6
bsf
OSCCON,5
; frequency of 500KHz
bsf
OSCCON,4
bsf
OSCCON,0
ANSEL
banksel
clrf
ANSEL
clrf
banksel
ANSELH
TRISB
clrf
TRISB
clrf
PR2
banksel
T2CON
movlw
H'FF'
movwf
T2CON
clrf
banksel
PORTB
PIE1
bsf
PIE1,TMR2IE
bsf
NTCON,PEIE
INTCON,GIE
loop
goto
end
loop
; Remain here
; End of program
Circuito P1.9:
0x20
w_temp
pclath_temp
status_temp
endc
#define
; to PORTB,0 pin
#define
org
0x0000
goto
main
org
movwf
movf
movwf
movf
0x0004
; Interrupt vector
w_temp
STATUS
; Save register W
status_temp
PCLATH
movwf
pclath_temp
banksel
CCPR1L
btfss
DARK
decf
CCPR1L
btfss
BRIGHT
incf
CCPR1L
movf
movwf
movf
movwf
STATUS
swapf
w_temp,f
swapf
w_temp,w
banksel
PIR1
bcf
PIR1,TMR1IF
bsf
TMR1H,7
bsf
TMR1H,6
bsf
INTCON,GIE
retfie
main
banksel
clrf
ANSEL
clrf
ANSELH
banksel
OPTION_REG
bcf
bsf
WPUB,0
bsf
WPUB,1
banksel
clrf
banksel
TRISC
TRISC
T1CON
bcf
bcf
bcf
T1CON,T1CKPS1
bsf
banksel
PIE1
bsf
bsf
bsf
INTCON,GIE
movlw
banksel
T2CON
movwf
T2CON
movlw
banksel
PR2
movwf
PR2
banksel
CCP1CON
movlw
movwf
CCP1CON
loop
goto
loop
end
; Remain here
; End of program
Circuito P1.10:
Cdigo P1.11:
;*********************** Header *********************************************
;************************ PROGRAM START *************************************
org
banksel
clrf
0x0000
TRISB
TRISB
movlw
B'00001100'
movwf
TRISA
banksel
ANSEL
movlw
B'00001100'
movwf
ANSEL
clrf
banksel
ANSELH
ADCON1
bsf
ADCON1,ADFM
bcf
bsf
banksel
ADCON0
movlw
B'00001001'
movwf
ADCON0
; AD converter is enabled
loop
banksel
ADCON0
btfsc
ADCON0,1
goto
loop
; loop
banksel
movf
ADRESL
ADRESL,w
; copied to W
banksel
PORTB
movwf
PORTB
bsf
goto
ADCON0,1
loop
end
Circuito P1.11:
; End of program
0x20
HIcnt
LOcnt
LOOPcnt
cnt
endc
; End of block
;****************************************************************************
ORG
0x000
; Reset vector
nop
goto
main
;****************************************************************************
include
"pause.inc"
include
"button.inc"
;****************************************************************************
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
banksel
TRISB
bsf
TRISA, 0
; Input pin
bsf
TRISA, 1
; Input pin
bsf
TRISA, 2
; Input pin
clrf
TRISB
clrf
TRISD
banksel
PORTB
clrf
PORTB
; PORTB=0
clrf
PORTD
; PORTD=0
clrf
cnt
; cnt=0
Loop
banksel
PORTA
button
PORTA,0,0,Increment
button
PORTA,1,0,Decrement
button
PORTA,2,0,Save
banksel
EEADR
movlw
.5
movwf
EEADR
banksel
EECON1
bcf
EECON1,EEPGD
bsf
EECON1,RD
banksel
EEDATA
movfw
EEDATA
banksel
PORTD
; Moves data to W
movwf
goto
PORTD
Loop
Increment
incf
movf
movwf
goto
Decrement
decf
movf
movwf
goto
Save
banksel
EEADR
movlw
.5
movwf
EEADR
banksel
PORTB
movfw
PORTB
banksel
EEDAT
movwf
EEDAT
banksel
EECON1
; Writes address
bcf
EECON1,EEPGD
bsf
EECON1,WREN
bcf
INTCON,GIE
btfsc
INTCON,GIE
; Write enabled
goto
$-2
movlw
55h
movwf
EECON2
movlw
H'AA'
movwf
EECON2
bsf
EECON1,WR
btfsc
EECON1,WR
goto
$-1
bsf
INTCON,GIE
bcf
EECON1,WREN
goto
end
Loop
; Interrupt enabled
Circuito P1.12:
EQU 0x7D
CBLOCK
0x20
Digtemp
Dig0
Dig1
Dig2
Dig3
Dval
; Counter value
One
ENDC
poc_vr
EQU .99
include
"Digbyte.inc"
;****************************************************************************
ORG
0x0000
goto
main
;****************************************************************************
ORG
0x0004
movwf
movf
movwf
w_temp
STATUS,w
status_temp
movf
PCLATH,w
movwf
pclath_temp
; register
BANKSEL
TMR0
movlw
.100
movwf
TMR0
bcf
INTCON, T0IF
bcf
PORTA, 0
bcf
PORTA, 1
btfsc
One, 0
goto
Lsdon
goto
Msdon
Lsdon
incf
One, f
movlw
HIGH (Bcdto7seg)
movwf
PCLATH
digbyte
Dval
movf
Dig1, w
call
movwf
bsf
goto
Bcdto7seg
PORTD
PORTA, 1
ISR_end
Msdon
incf
One, f
movlw
HIGH (Bcdto7seg)
movwf
PCLATH
digbyte
Dval
movf
Dig0, w
call
movwf
bsf
goto
Bcdto7seg
PORTD
PORTA, 0
ISR_end
ISR_end
movf
movwf
PCLATH
movf
; state
movwf
STATUS
swapf
w_temp,f
; state
; state
swapf
w_temp,w
retfie
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
BANKSEL
movlw
TRISA
b'11111100'
movwf
clrf
TRISA
TRISD
BANKSEL
OPTION_REG
movlw
b'10000110'
movwf
OPTION_REG
BANKSEL
PORTA
movlw
poc_vr
movwf
Dval
movlw
b'00000001'
movwf
One
movwf
PORTA
movlw
.100
movwf
TMR0
; to switch on
bsf
INTCON, GIE
bsf
bcf
INTCON, T0IF
Loop
btfss
One, 3
goto Dec
btfss
PORTA, 2
goto
Reset
goto
Loop
One, 3
goto
Dec
movf
Dval, f
btfsc
STATUS, Z
; Is Dval equal to 0?
goto
Loop
decf
Dval, f
goto
Loop
Reset
btfss
PORTA, 2
goto
Reset
movlw
poc_vr
movwf
Dval
goto
Loop
;****************************************************************************
ORG
0x0300
PCL, f
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f
;****************************************************************************
END
; End of program
Macro digbyte
Archivo: Digbyte.inc
LOCAL
Exit0
LOCAL
Exit1
LOCAL
Exit2
clrf
Dig0
clrf
Dig1
clrf
Dig2
clrf
Dig3
movf
arg0, w
movwf
Digtemp
movlw
.100
Exit2
incf
subwf
Dig2, f
Digtemp, f
btfsc
STATUS, C
goto
Exit2
decf
Dig2, f
addwf
Digtemp, f
Exit1
movlw
incf
subwf
.10
Dig1, f
Digtemp, f
btfsc
STATUS, C
goto
Exit1
decf
Dig1, f
addwf
Digtemp, f
Exit0
movf
movwf
Digtemp, w
Dig0
ENDM
Circuito P1.13:
0x20
; Auxiliary variables for macro pausems
LOcnt
LOOPcnt
PRESCwait
Beep_TEMP1
Beep_TEMP2
Beep_TEMP3
endc
; Speaker pin
expand
;**********************************************************************
ORG
0x0000
goto
main
;**********************************************************************
; remaining code goes here
include
"pause.inc"
include
"button.inc"
include
"beep.inc"
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
banksel
TRISD
movlw
b'11111011'
movwf
TRISD
banksel
PORTD
BEEPinit
; PORTA D initialization
; Macro "Beep"
Loop
button
PORTD,0,0,Play1 ; Push-button 1
button
PORTD,1,0,Play2 ; Push-button 2
goto
Loop
Play1
; First tone
BEEP
0xFF, 0x02
BEEP
0x90, 0x05
BEEP
0xC0, 0x03
BEEP
0xFF, 0x03
goto
Loop
Play2
; Second tone
BEEP
0xBB, 0x02
BEEP
0x87, 0x05
BEEP
0xA2, 0x03
BEEP
0x98, 0x03
goto
Loop
;**********************************************************************
END
; End of program
Macro beep
BEEPinit MACRO
bcf
STATUS, RP0
bcf
STATUS, RP1
bcf
BEEPport
bsf
STATUS, RP0
bcf
STATUS, RP1
bcf
BEEPtris
movlw
b'00000111'
movwf
OPTION_REG
bcf
STATUS, RP0
bcf
STATUS, RP1
ENDM
BEEP MACRO
freq, duration
bcf
STATUS, RP0
bcf
STATUS, RP1
movlw
freq
movwf
Beep_TEMP1
movlw
duration
movwf
Beep_TEMP2
call
BEEPsub
ENDM
;**********************************************************************
; Subroutines
BEEPsub
clrf
TMR0
; Counter initialization
bcf
INTCON, T0IF
bcf
BEEPport
BEEPa
bcf
BEEPb
bsf
BEEPport
call
B_Wait
bcf
BEEPport
call
B_Wait
btfss
goto
BEEPb
decfsz
Beep_TEMP2, f ; Is Beep_TEMP2 = 0 ?
goto BEEPa
; Go to BEEPa again
return
B_Wait
movf Beep_TEMP1, w
movwf Beep_TEMP3
B_Waita
decfsz Beep_TEMP3, f
goto B_Waita
return
Circuito P1.14:
CBLOCK
HIcnt
LOcnt
0x20
LOOPcnt
LCDbuf
LCDtemp
LCDportBuf
Digtemp
Dig0
Dig1
Dig2
Dig3
temp
ENDC
; End of block
EQU 4
EN
EQU 5
;**********************************************************************
ORG
0x0000
nop
goto
main
;**********************************************************************
include "LCD.inc"
include "digbyte.inc"
include "pause.inc"
;**********************************************************************
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
bcf
STATUS,RP0
bcf
STATUS,RP1
movlw
.23
movwf
temp
; is to be displayed on LCD
lcdinit
; LCD initialization
Loop
lcdcmd
lcdtext
0x01
lcdtext
pausems
lcdcmd
.2000
0x01
; 2 sec. delay
; Instruction to clear LCD
temp
.2000
; 2 sec. delay
Loop
;**********************************************************************
end
; End of program
Archivo LCD.inc
; Esto va en un archivo que lleva el nombre de LCD.inc
;**********************************************************************
; Initialization must be done by using macro lcdinit before access
; ing LCD
;**********************************************************************
lcdinit MACRO
bcf
STATUS, RP0
bcf
STATUS, RP1
clrf
LCDportBuf
movf
movwf
; Bank0
LCDportBuf, w
LCDport
bsf
STATUS, RP0
bcf
STATUS, RP1
clrf
TRISB
bcf
STATUS, RP0
bcf
STATUS, RP1
; Bank1
b'00100000'
movwf
LCDbuf
swapf
LCDbuf, w
movwf
bcf
movf
movwf
bsf
movf
movwf
bcf
movf
movwf
call
LCDportBuf
LCDportBuf, RS
LCDportBuf, w
LCDport
LCDportBuf, EN
LCDportBuf, w
LCDport
LCDportBuf, EN
LCDportBuf, w
LCDport
Delay1ms
; 1 ms delay
b'00101100'
Delay1ms
; 1 ms delay
b'00001100'
Delay1ms
; 1 ms delay
b'00000110'
Delay1ms
; 1 ms delay
; Display Clear
lcdcmd
b'00000001'
pausems
.40
; 40 ms delay
b'00100000'
movwf
LCDbuf
swapf
LCDbuf, w
movwf
bcf
movf
movwf
bsf
movf
movwf
bcf
movf
movwf
call
LCDportBuf
LCDportBuf, RS
LCDportBuf, w
LCDport
LCDportBuf, EN
LCDportBuf, w
LCDport
LCDportBuf, EN
LCDportBuf, w
LCDport
Delay1ms
; 1 ms delay
b'00101100'
Delay1ms
; 1 ms delay
b'00001100'
call
Delay1ms
; 1 ms delay
b'00000110'
Delay1ms
; 1 ms delay
; Display Clear
lcdcmd
b'00000001'
pausems
.40
; 40 ms delay
ENDM
;**********************************************************************
; lcdcmd sends command to LCD (see the table on the previous page)
; lcdclr is the same as lcdcmd 0x01
;**********************************************************************
lcdcmd MACRO LCDcommand
movlw
call
LCDcommand
LCDcomd
ENDM
LCDcomd
movwf
bcf
movf
movwf
LCDbuf
LCDportBuf, RS
LCDportBuf, w
LCDport
goto
LCDwr
LCDdata
movwf
bsf
movf
movwf
goto
LCDbuf
LCDportBuf, RS
LCDportBuf, w
LCDport
LCDwr
LCDwr
swapf
call
movf
call
LCDbuf, w
SendW
LCDbuf, w
SendW
return
SendW
andlw
0x0F
movwf
LCDtemp
movlw
0xF0
andwf
LCDportBuf, f
movf
LCDtemp, w
iorwf
LCDportBuf, f
movf
LCDportBuf, w
movwf
LCDport
call
Delay1ms
bsf
LCDportBuf, EN
movf
LCDportBuf, w
movwf
bcf
LCDport
LCDportBuf, EN
movf
LCDportBuf, w
movwf
call
LCDport
Delay1ms
return
;**********************************************************************
; lcdtext writes text containing 16 characters which represents a
; macro argument. The first argument select selects the line in which
; text writing is to start. If select is 0, text writing starts from
; cursor current position.
;**********************************************************************
lcdtext MACRO select, text
Message
local
Start
local
Exit
local
i=0
goto
Start
Message
DT
DT text
Start
IF (select == 1)
lcdcmd b'10000000'
ELSE
IF (select == 2)
lcdcmd b'11000000'
ENDIF
ENDIF
WHILE (i<16)
call Message+i
addlw 0
bz Exit
; until 0 is read
call LCDdata
i=i+1
ENDW
Exit
ENDM
;**********************************************************************
; This macro writes value in size of 1 byte on LCD
; excluding leading zeros
;**********************************************************************
lcdbyte MACRO arg0
digbyte
arg0
; A hundred is in Dig2,
; A ten is in Dig1 and one in Dig0
movf
Dig2, w
addlw
0x30
call
LCDdata
movf
Dig1, w
addlw
0x30
call
LCDdata
movf
Dig0, w
addlw
0x30
call
LCDdata
ENDM
;**********************************************************************
; 1ms Delay
Delay1ms:
movlw
.200
movwf
LOOPcnt
Delay10us:
nop
;1us
nop
;1us
nop
;1us
nop
;1us
nop
;1us
nop
;1us
nop
;1us
decfsz
LOOPcnt, f
;1us
goto
Delay10us
;2us
return
Circuito P1.15:
w_temp
EQU 0x7D
cblock
0x20
Port_A
; Variable at address 20 h
Port_B
; Variable at address 21 h
RS232temp
; Variable at address 22 h
RXchr
; Variable at address 23 h
endc
;**********************************************************************
ORG
0x0000
; Reset vector
nop
goto
main
;**********************************************************************
ORG
movwf
movf
movwf
movf
movwf
0x0004
w_temp
STATUS,w
status_temp
PCLATH,w
pclath_temp
;**********************************************************************
; This part of the program is executed in interrupt routine
banksel
PIE1
btfss
PIE1, RCIE
goto
ISR_Not_RX232int
banksel
btfsc
call
PIE1
PIR1, RCIF
RX232_int_proc
ISR_Not_RX232int
movf
pclath_temp,w
movwf
movf
PCLATH
status_temp,w
movwf
STATUS
swapf
w_temp,f
swapf
w_temp,w
retfie
;**********************************************************************
RX232_int_proc
banksel
movf
movwf
RCSTA
RCSTA, w
RS232temp
btfsc
RS232temp, FERR
goto
RX232_int_proc_FERR
btfsc
RS232temp, OERR
goto
RX232_int_proc_OERR
goto
RX232_int_proc_Cont
RX232_int_proc_FERR
bcf
RCSTA, CREN
; Delay ...
nop
bsf
movf
RCSTA, CREN
RCREG, w
bsf
movf
movwf
goto
Port_A, 0
Port_A, w
PORTA
RS232_exit
RX232_int_proc_OERR
bcf
RCSTA, CREN
nop
; Delay ...
nop
bsf
movf
bsf
movf
movwf
goto
RCSTA, CREN
RCREG, w
Port_A, 1
Port_A, w
PORTA
RS232_exit
RX232_int_proc_Cont
movf
RCREG, W
movwf
RXchr
movwf
PORTB
movwf
TXREG
RS232_exit
return
;**********************************************************************
; Main program
main
banksel
ANSEL
clrf
ANSEL
clrf
ANSELH
TRISA
movlw
b'11111100'
movwf
TRISA
movlw
b'00000000'
movwf
TRISB
PORTA
movlw
b'11111100'
movwf
PORTA
movwf
Port_A
movlw
b'00000000'
movwf
PORTB
movwf
Port_B
;--------------------------------------banksel
TRISC
bcf
TRISC, 6
; RC6/TX/CK = output
bsf
TRISC, 7
; RC7/RX/DT = input
banksel
bsf
BAUDCTL
BAUDCTL, BRG16
banksel
SPBRG
movlw
.51
movwf
clrf
banksel
SPBRG
SPBRGH
TXSTA
bcf
TXSTA, TX9
bsf
TXSTA, TXEN
bcf
TXSTA, SYNC
; Asynchronous mode
bsf
TXSTA, BRGH
banksel
RCSTA
bsf
RCSTA, SPEN
bcf
RCSTA, RX9
bsf
RCSTA, CREN
bcf
RCSTA, W
movf
RCREG, W
PIE1
bsf
PIE1, RCIE
bsf
bsf
INTCON, GIE
end
; End of program
Circuito P1.16:
Cdigo P2.1:
/*
*************************************************************
* Este es el Header
* Nombre del programa : p2_1.c
* Descripcion: Programa simple para demostrar el funcionamiento de un uC
* encendiendo y apagando leds conectados al puerto B.
* Configuracion:
*
Microcontroladores: PIC16F887
void main() {
ANSEL = 0;
ANSELH = 0;
PORTB = 0XFF;
TRISB 0 0;
Delay_ms(1000);
PORTB = 0;
switch(PORTB) {
case 0x00:
PORTB = 0xFF;
Delay_ms(100);
break;
case 0XFF:
PORTB = 0x00;
Delay_ms(500);
}
}
PORTB = 0b01010101;
while(1) {
PORTB = ~PORTB;
Delay_ms(200);
}
}
Cdigo P2.2:
/* Header *********************************************/
int k = 0;
char saveBank;
void main() {
ANSEL = 0;
ANSELH = 0;
PORTB = 0;
TRISB = 0;
do {
PORTB = ~PORTB;
Delay_ms(100);
k++;
// Increment k by 1
}
while(k<20);
k=0;
// Reset variable k
saveBank = STATUS & 0b01100000; // Save the state of bits RP0 and RP1
// (bits 5 and 6 of the STATUS register)
asm {
bsf STATUS,RP0
bcf STATUS,RP1
// register
bcf OSCCON,6
bcf OSCCON,5
// of 31KHz frequency
bcf OSCCON,4
bsf OSCCON,0
}
STATUS |= saveBank;
do {
PORTB = ~PORTB;
Delay_ms(10);
k++;
// Increment k by 1
}
while(k<20);
}
Cdigo P2.3:
/*Header******************************************************/
void main() {
char TEST = 5;
// Constant TEST = 5
ANSEL = 0;
ANSELH = 0;
PORTA = 0;
// Reset port A
TRISA = 0xFF;
PORTD = 0;
// Reset port D
TRISD = 0b11110111;
// configured as inputs
OPTION_REG.F5 = 1;
OPTION_REG.F3 = 1;
TMR0 = 0;
do {
if (TMR0 == TEST)
(PORTD.RELAY = 1); // Numbers match. Set the RD3 bit (output RELAY)
}
while (1);
}
Cdigo P2.4:
/*Header*****************************************************
Interrupt occurs on every timer register TMR0 overflow.
*/
unsigned cnt;
void interrupt() {
cnt++;
TMR0 = 96;
INTCON = 0x20;
void main() {
OPTION_REG = 0x84;
ANSEL = 0;
ANSELH = 0;
TRISB = 0;
PORTB = 0x0;
// Reset port B
TMR0 = 96;
INTCON = 0xA0;
cnt = 0;
do {
// Endless loop
if (cnt == 400) {
}
} while(1);
}
/*Header*****************************************************
In this case, an interrupt is enabled after the timer register TMR1 (TMR1H, TMR1L) overflow occurs
*/
void interrupt() {
cnt++ ;
PIR1.TMR1IF = 0;
TMR1H = 0x80;
TMR1L = 0x00;
void main() {
ANSEL = 0;
ANSELH = 0;
PORTB = 0xF0;
TRISB = 0;
T1CON = 1;
PIR1.TMR1IF = 0;
TMR1H = 0x80;
TMR1L = 0x00;
PIE1.TMR1IE = 1;
cnt = 0;
INTCON = 0xC0;
do {
// Endless loop
if (cnt == 76) {
PORTB = ~PORTB;
cnt = 0;
}
} while (1);
/*Header*****************************************************
This time, an interrupt occurs after timer register TMR2 overflow occurs.
*/
void Replace() {
PORTB = ~PORTB;
}
void interrupt() {
if (PIR1.TMR2IF) { // If bit TMR2IF = 1,
cnt++ ;
}
}
// main
void main() {
cnt = 0;
ANSEL = 0;
ANSELH = 0;
PORTB = 0b10101010; // Logic state on port B pins
TRISB = 0;
T2CON = 0xFF;
// Set timer T2
TMR2 = 0;
while (1) {
// Endless loop
Cdigo P2.5:
/*Header*****************************************************
In order to make this example work properly, it is necessary to enable the watchdog
Timer Watchdog Timer - Enabled option in mE programmer.
*/
void main() {
OPTION_REG = 0x0E; // Prescaler is assigned to timer WDT (1:64)
asm CLRWDT;
PORTB = 0x0F;
TRISB = 0;
Delay_ms(300);
// 30mS delay
PORTB = 0xF0;
while (1);
Cdigo P2.6:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the following
Libraries in the Library Manager prior to compiling:
PWM
Button
*/
void initMain() {
ANSEL = 0;
ANSELH = 0;
PORTA = 255;
TRISA = 255;
PORTB = 0;
TRISB = 0;
PORTC = 0;
TRISC = 0;
PWM1_Init(5000);
void main() {
initMain();
current_duty = 16;
old_duty = 0;
PWM1_Start();
while (1) {
// Endless loop
if (Button(&PORTA, 0,1,1))
current_duty++ ;
if (Button(&PORTA, 1,1,1))
current_duty-- ;
Delay_ms(200);
}
}
// 200mS delay
Cdigo P2.7:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the ADC library
in the Library Manager prior to compiling:
ADC
*/
void main() {
ANSEL = 0x0C;
TRISA = 0xFF;
ANSELH = 0;
TRISB = 0x3F;
TRISD = 0;
ADCON1.F4 = 1 ;
do {
temp_res = ADC_Read(2); // Result of A/D conversion is copied to temp_res
PORTD = temp_res;
PORTB = temp_res >> 2; // 2 MSBs are moved to bits RB6 and RB7
} while(1);
}
// Endless loop
Cdigo P2.8:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the EEPROM
EEPROM
*/
void main() {
ANSEL = 0;
ANSELH = 0;
PORTB = 0;
TRISB = 0;
PORTD = 0;
TRISD = 0;
TRISA = 0xFF;
PORTD = EEPROM_Read(5);
do {
PORTB=PORTB++;
Delay_ms(100);
// Increment port B by 1
// 100mS delay
if (PORTA.F2){
EEPROM_Write(5,PORTB); // If MEMO is pressed, save PORTB
PORTD = EEPROM_Read(5); // Read written data
do;
while (PORTA.F2);
// pressed
}
}
while(1);
// Endless loop
Cdigo P2.9:
/*Header******************************************************/
void interrupt() {
if (digit_no==0) {
PORTA = 0;
PORTD = digit1;
PORTA = 1;
digit_no = 1;
} else {
PORTA = 0;
PORTD = digit10;
PORTA = 2;
digit_no = 0;
}
TMR0 = 0;
INTCON = 0x20;
void main() {
OPTION_REG = 0x80;
TMR0 = 0;
INTCON = 0xA0;
PORTA = 0;
TRISA = 0;
PORTD = 0;
TRISD = 0;
do {
for (i = 0; i<=99; i++) { // Count from 0 to 99
digit = i % 10u;
digit1 = mask(digit); // Prepare mask for displaying ones
digit = (char)(i / 10u) % 10u;
digit10 = mask(digit); // Prepare mask for displaying tens
Delay_ms(1000);
}
} while (1);
// Endless loop
}
Archivo : mask.c
/*Header*****************************************************
In order to make this example work properly, it is necessary to include document
mask.c into the project prior to compiling:
Sources - Add File To Project
*/
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
Cdigo P2.10:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the following
libraries in the Library Manager prior to compiling:
ADC
LCD
*/
//
// Declare variables
char *text;
//
long tlong;
//
void main() {
INTCON = 0;
ANSEL = 0x04;
TRISA = 0x04;
ANSELH = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
text = "mikroElektronika";
Lcd_Out(1,1,text);
Lcd_Out(2,1,text);
ADCON1 = 0x82;
TRISA = 0xFF;
Delay_ms(2000);
text = "voltage:";
while (1) {
adc_rd = ADC_Read(2);
Lcd_Out(2,1,text);
ch = tlong / 1000;
// from result
Lcd_Chr(2,9,48+ch);
Lcd_Chr_CP('.');
ch = (tlong / 100) % 10;
Lcd_Chr_CP(48+ch);
Delay_ms(1);
}
}
Cdigo P2.11:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the
UART library in the Library Manager prior to compiling:
UART
*/
unsigned short i;
void main() {
UART1_Init(19200);
}
}
}
i = UART1_Read();
// read it
UART1_Write(i);
Cdigo P2.12:
/*Header*****************************************************
In order to make this example work properly, it is necessary to tick off the following
libraries in the Library Manager prior to compiling:
Button
Sound
*/
void Tone1() {
Sound_Play(659, 250);
void Tone2() {
Sound_Play(698, 250);
void Tone3() {
Sound_Play(784, 250);
void Melody1() {
void ToneA() {
// Tone A
Sound_Play(880, 50);
}
void ToneC() {
// Tone C
Sound_Play(1046, 50);
}
void ToneE() {
// Tone E
Sound_Play(1318, 50);
}
void Melody2() {
unsigned short i;
for (i = 9; i > 0; i--) {
ToneA(); ToneC(); ToneE();
}
}
void main() {
ANSEL = 0;
ANSELH = 0;
TRISB = 0xF0;
Sound_Init(&PORTB, 3);
Sound_Play(1000, 500);
while (1) {
if (Button(&PORTB,7,1,1)) // RB7 generates Tone1
Tone1();
while (PORTB & 0x80) ; // Wait for push button release
Control remoto por medio del puerto series (protocolo RS232), por medio de las siguientes
secuencias de bytes:
Circuito P3.1:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Driver: 4011 CMOS Quad NAND Gate IC, alimentado con +12 V DC.
4049 CMOS Hex Inverting Buffer IC.
NOTA: Los transistores MOSFET canal N se activa con un voltaje positivo en la terminal de Gate.
Cdigo P3.1:
[Escriba aqu el cdigo que implemento]
Control remoto por medio del puerto series (protocolo RS232), por medio de las siguientes
secuencias de bytes:
ATCMx, modo continuo, donde si x=0 avanzar CW, x=1 avanzad CCW.
ATSTP, detener motor.
ATMxyz, El motor se mueve en la direccin (x=0 CW ,x=1 CCW) un numero de pasos yz,
donde yz es un numero en el rango de 0-99.
Circuito P3.2:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Step
wire 1
wire 2
low
high
high
high
high
low
low
low
Step
wire
1
wire
2
wire
3
wire
4
High
low
high
low
low
high
high
low
low
high
low
high
high
low
low
high
Cdigo P3.2:
[Escriba aqu el cdigo que implemento]
Movimiento en direccin de las manecillas del reloj (ClockWise, CW), el servo motor debe
de avanzar solo un grado, o la minima resolucin posible.
Movimiento en direccin contraria a las manecillas del reloj (Counter ClockWise , CCW), el
servo motor debe de avanzar solo un grado, o la minima resolucin posible.
Volver a la posicin central o neutral. NOTA: Suponer que el movimiento del servo motor
esta limitado a 180 grados, 90 grados en sentido positivo y 90 grados en sentido negativo.
NOTA: Verificar esto, cada servo es diferente el servo que utilizen puede estar limitado a
solo 120 grados, 60 grados en ambos sentidos.
Control remoto por medio del puerto series (protocolo RS232), por medio de las siguientes
secuencias de bytes:
El siguiente diagrama de tiempos de una seal tipo PWM para el control de un servo motor es solo
ilustrativa. Las seales de control de los servos motores no siguen un estndar verdadero. Lo nico
estandarizado es que el circuito de control del servo motor espera recibir un pulso cada 20 ms
(periodo). La duracin del pulso debe de ser entre 0.7 ms y 2.0 ms. Si la duracin del pulso esta
entre 0.7 y 1.0 ms el servo se mover hacia la derecha (CW). Si la duracin del pulso esta entre 1.7
y 2.0 ms el servo se mover hacia la izquierda (CCW). Si la duracin del pulso esta entre 1.3 y 1.5
ms el servo se mover a su posicin central o neutral.
http://www.ermicro.com/blog/?p=771
Circuito P3.3:
http://web.cecs.pdx.edu/~eas199/B/howto/thermistorCalibration/thermistorResistanceCalibratio
n.pdf
http://web.cecs.pdx.edu/~eas199/B/howto/thermistorCalibration/thermistorResistanceCalibratio
n.m
http://web.cecs.pdx.edu/~eas199/B/howto/thermistorProbe/
http://web.cecs.pdx.edu/~eas199/B/howto/arduino/horner.html
http://www.daycounter.com/Calculators/Steinhart-Hart-Thermistor-Calculator.phtml
Circuito P3.4:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Cdigo P3.4:
[Escriba aqu el cdigo que implemento]
Se debe de usar un sensor de temperatura para realizar las mediciones, un foco incandescente
como fuente de calor y un ventilador que servir para disminuir la temperatura de la caja.
Circuito P3.5:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Cdigo P3.5:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Circuito P3.6:
[Dibuje aqu el circuito esquemtico. El circuito debe de estar completo]
Cdigo P3.6:
[Escriba aqu el cdigo que implemento]
Proyecto final
Descripcion PF:
Modificar tanto el cdigo como el hardware necesario para realizar este proyecto con un
PIC16F887 .
La construccin del pndulo invertido y del hardware de control debe de ser construido,
modificado o alterado a su conveniencia.
Software PID Control of an Inverted Pendulum Using the PIC16F684
The purpose of this application note is to describe how a PIC16F684 can be used to implement a positional
Proportional-Integral-Derivative (PID) feedback control in an inherently unstable system. An inverted pendulum
is used to demonstrate this type of control.
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en
021807
http://ww1.microchip.com/downloads/en/AppNotes/00964A.pdf
Circuito PF:
http://ww1.microchip.com/downloads/en/AppNotes/00964A.pdf
Cdigo PF:
http://ww1.microchip.com/downloads/en/AppNotes/INV_PEN_CODE.zip
http://ww1.microchip.com/downloads/en/AppNotes/AN964.zip
Formato de reportes
Todas las prcticas deben de ir acompaadas de un reporte.El reporte debe de entregarse escrito
a mano (diagramas y cdigo fuente tambin). Cada reporte dede de contener al menos los
siguientes componentes:
Links de ayuda
http://ww1.microchip.com/downloads/en/devicedoc/33023a.pdf
http://www.microchip.com/pagehandler/en-us/family/mplabx/#downloads
http://www.mikroe.com/products/view/11/book-pic-microcontrollers/
http://www.mikroe.com/products/view/285/book-pic-microcontrollers-programming-in-c/
http://tutorial.cytron.com.my/2012/07/02/mplab-x-ide-with-mplab-xc8/
http://tutorial.cytron.com.my/category/projects/page/2/
https://forum.sparkfun.com/viewtopic.php?f=32&t=31170
http://tutorial.cytron.com.my/2011/08/08/project-0-%E2%80%93-my-1st-pic-project-load-asimple-led-blink/
http://www.microchipc.com/
http://www.microchipc.com/sourcecode/
http://www.picmicros.org.uk/
http://www.mikroe.com/forum/viewtopic.php?t=19530