Code:
'***************************************************************************
'* Name : 18F24K22 encoder.pbp *
'* Author : Demon *
'* Date : Sep 5 2012 *
'* Version : 1.0 *
'* Notes : Test program *
'* Hardware : PIC 18F24K22, internal oscillator, 8 MHz *
'* : ICSP *
'* : MeLabs U2 Programmer v4.32 *
'* Software : PIC Basic Pro v2.60C *
'* : MicroCode Studio Plus v2.2.1.1 *
'* : MPASM v5.46 *
'* CONFIG : - if you use these, you must comment the ones in the *
' .INC file in PBP folder *
' - available options at the bottom of P*.INC file in *
' MPLAB TOOLS/MPASM SUITE folder *
'***************************************************************************
asm
__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_OFF_1H & _PRICLKEN_ON_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
__CONFIG _CONFIG2H, _WDTEN_OFF_2H
__CONFIG _CONFIG3H, _PBADEN_ON_3H & _HFOFST_OFF_3H & _MCLRE_EXTMCLR_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
;--- Oscillator speed ------------------------------------------------------
DEFINE OSC 8
CLEAR
;--- Setup Interrupts ------------------------------------------------------
DEFINE USE_LOWPRIORITY 1
INCLUDE "DT_INTS-18.bas" ; Base Interrupt System
INCLUDE "ReEnterPBP-18.bas" ; PBP Re-entry for external interrupt
INCLUDE "ReEnterPBP-18LP.bas" ; PBP Re-entry for low priority external interrupt
ASM
;----[High Priority Interrupts] --------------------------------------------
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT0_INT, _ExternalInterrupt0, PBP, yes
endm
INT_CREATE ; Creates the interrupt processor
;----[Low Priority Interrupts] ---------------------------------------------
INT_LIST_L macro ; IntSource, Label, Type, ResetFlag?
INT_Handler INT1_INT, _ExternalInterrupt1, PBP, yes
endm
INT_CREATE_L ; Creates the Low Priority interrupt processor
;----[Enable Interrupts] ---------------------------------------------------
INT_ENABLE INT0_INT ; Enable (INT0) external interrupt
INT_ENABLE INT1_INT ; Enable (INT1) external interrupt
ENDASM
INTCON2.7=1
' bit 7 RBPU: PORTB Pull-up Enable bit
' 1 = All PORTB pull-ups are disabled
' 0 = PORTB pull-ups are enabled provided that the pin is an input and the
' corresponding WPUB bit is set.
INTCON2.6=0
' bit 6 INTEDG0: External Interrupt 0 Edge Select bit
' 1 = Interrupt on rising edge
' 0 = Interrupt on falling edge
INTCON2.5=1
' bit 5 INTEDG1: External Interrupt 1 Edge Select bit
' 1 = Interrupt on rising edge
' 0 = Interrupt on falling edge
INTCON2.4=0
' bit 4 INTEDG2: External Interrupt 2 Edge Select bit
' 1 = Interrupt on rising edge
' 0 = Interrupt on falling edge
;--- Setup Registers -------------------------------------------------------
OSCCON = %01100110 ' OSCILLATOR CONTROL REGISTER
' bit 7 IDLEN: Idle Enable bit
' 1 = Device enters Idle mode on SLEEP instruction
' 0 = Device enters Sleep mode on SLEEP instruction
' bit 6-4 IRCF<2:0>: Internal RC Oscillator Frequency Select bits(2)
' 111 = HFINTOSC – (16 MHz)
' 110 = HFINTOSC/2 – (8 MHz)
' 101 = HFINTOSC/4 – (4 MHz)
' 100 = HFINTOSC/8 – (2 MHz)
' 011 = HFINTOSC/16 – (1 MHz)(3)
' If INTSRC = 0 and MFIOSEL = 0:
' 010 = HFINTOSC/32 – (500 kHz)
' 001 = HFINTOSC/64 – (250 kHz)
' 000 = LFINTOSC – (31.25 kHz)
' If INTSRC = 1 and MFIOSEL = 0:
' 010 = HFINTOSC/32 – (500 kHz)
' 001 = HFINTOSC/64 – (250 kHz)
' 000 = HFINTOSC/512 – (31.25 kHz)
' If INTSRC = 0 and MFIOSEL = 1:
' 010 = MFINTOSC – (500 kHz)
' 001 = MFINTOSC/2 – (250 kHz)
' 000 = LFINTOSC – (31.25 kHz)
' If INTSRC = 1 and MFIOSEL = 1:
' 010 = MFINTOSC – (500 kHz)
' 001 = MFINTOSC/2 – (250 kHz)
' 000 = MFINTOSC/16 – (31.25 kHz)
' bit 3 OSTS: Oscillator Start-up Time-out Status bit
' 1 = Device is running from the clock defined by FOSC<3:0> of the CONFIG1H register
' 0 = Device is running from the internal oscillator (HFINTOSC, MFINTOSC or LFINTOSC)
' bit 2 HFIOFS: HFINTOSC Frequency Stable bit
' 1 = HFINTOSC frequency is stable
' 0 = HFINTOSC frequency is not stable
' bit 1-0 SCS<1:0>: System Clock Select bit
' 1x = Internal oscillator block
' 01 = Secondary (SOSC) oscillator
' 00 = Primary clock (determined by FOSC<3:0> in CONFIG1H).
OSCCON2 = %00000100 ' OSCILLATOR CONTROL REGISTER 2
' bit 7 PLLRDY: PLL Run Status bit
' 1 = System clock comes from 4xPLL
' 0 = System clock comes from an oscillator, other than 4xPLL
' bit 6 SOSCRUN: SOSC Run Status bit
' 1 = System clock comes from secondary SOSC
' 0 = System clock comes from an oscillator, other than SOSC
' bit 5 Unimplemented: Read as ‘0’.
' bit 4 MFIOSEL: MFINTOSC Select bit
' 1 = MFINTOSC is used in place of HFINTOSC frequencies of 500 kHz, 250 kHz and 31.25 kHz
' 0 = MFINTOSC is not used
' bit 3 SOSCGO(1): Secondary Oscillator Start Control bit
' 1 = Secondary oscillator is enabled.
' 0 = Secondary oscillator is shut off if no other sources are requesting it.
' bit 2 PRISD: Primary Oscillator Drive Circuit Shutdown bit
' 1 = Oscillator drive circuit on
' 0 = Oscillator drive circuit off (zero power)
' bit 1 MFIOFS: MFINTOSC Frequency Stable bit
' 1 = MFINTOSC is stable
' 0 = MFINTOSC is not stable
' bit 0 LFIOFS: LFINTOSC Frequency Stable bit
' 1 = LFINTOSC is stable
' 0 = LFINTOSC is not stable
OSCTUNE = %00000000 ' OSCILLATOR TUNING REGISTER
' bit 7 INTSRC: Internal Oscillator Low-Frequency Source Select bit
' 1 = 31.25 kHz device clock derived from the MFINTOSC or HFINTOSC source
' 0 = 31.25 kHz device clock derived directly from LFINTOSC internal oscillator
' bit 6 PLLEN: Frequency Multiplier 4xPLL for HFINTOSC Enable bit(1)
' 1 = PLL enabled
' 0 = PLL disabled
' bit 5-0 TUN<5:0>: Frequency Tuning bits – use to adjust MFINTOSC and HFINTOSC frequencies
' 011111 = Maximum frequency
' 011110 =
' • • •
' 000001 =
' 000000 = Oscillator module (HFINTOSC and MFINTOSC) are running at the factory
' calibrated frequency.
' 111111 =
' • • •
' 100000 = Minimum frequency
PMD0 = %11111111 ' PERIPHERAL MODULE DISABLE REGISTER 0
' bit 7 UART2MD: UART2 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 6 UART1MD: UART1 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 5 TMR6MD: Timer6 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 4 TMR5MD: Timer5 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 3 TMR4MD: Timer4 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 2 TMR3MD: Timer3 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 1 TMR2MD: Timer2 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 0 TMR1MD: Timer1 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
PMD1 = %11111111 ' PERIPHERAL MODULE DISABLE REGISTER 1
' bit 7 MSSP2MD: MSSP2 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 6 MSSP1MD: MSSP1 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 5 Unimplemented: Read as ‘0’
' bit 4 CCP5MD: CCP5 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 3 CCP4MD: CCP4 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 2 CCP3MD: CCP3 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 1 CCP2MD: CCP2 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 0 CCP1MD: CCP1 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
PMD2 = %00001111 ' PERIPHERAL MODULE DISABLE REGISTER 2
' bit 7-4 Unimplemented: Read as ‘0’
' bit 3 CTMUMD: CTMU Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 2 CMP2MD: Comparator C2 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 1 CMP1MD: Comparator C1 Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
' bit 0 ADCMD: ADC Peripheral Module Disable Control bit
' 1 = Module is disabled, Clock Source is disconnected, module does not draw digital power
' 0 = Module is enabled, Clock Source is connected, module draws digital power
ANSELA = 0 ' Digital I/O
ANSELB = 0 ' Digital I/O
ANSELC = 0 ' Digital I/O
;--- Setup Port directions -------------------------------------------------
TRISA = %00000000 ' Set port A pins to output
TRISB = %00000011 ' Set port B pins 0,1 to input, others output
TRISC = %00000100 ' Set port C pin 2 to input, others output
;--- Pins ------------------------------------------------------------------
WiperA VAR PortB.0 ' Input, Interrupt 0
WiperB VAR PortC.2 ' Input
Switch VAR PortB.1 ' Input, Interrupt 1 low priority
LedPower VAR PortB.5 ' Output
Leds VAR PortA ' Output, 8 Leds connected on Port A
;--- Variables -------------------------------------------------------------
;--- Constants -------------------------------------------------------------
StateOff con 0 ' Off
StateOn con 1 ' On
ShiftOne con 1 ' One
DebounceTime con 250 ' Debounce time
StabilizeTime con 200 ' Stabilize time
;--- Initialize pins -------------------------------------------------------
Ledpower = StateOff ' Turn OFF switch Led
Leds = %00000001 ' Turn ON first Led
;--- Initialize variables --------------------------------------------------
;--- Subroutines -----------------------------------------------------------
goto Start ' Jump over sub-routines
ExternalInterrupt0: ' [INT0 - interrupt handler]
if wiperb = StateOn then ' Check if turning counter-clockwise
if leds.7 = StateOff then ' Check if reached last LED
LEDS = LEDs << ShiftOne ' Blink Led
endif
else ' Check if turning clockwise
if leds.0 = StateOff then ' Check if reached first LED
leds = leds >> ShiftOne ' Blink Led
endif
endif
@ INT_RETURN
ExternalInterrupt1: ' [INT1 - interrupt handler]
Ledpower = StateOn ' Turn ON switch Led
pause DebounceTime ' Basic debounce
@ INT_RETURN
;--- Program Start ---------------------------------------------------------
Start:
Pause StabilizeTime ' Let PIC stabilize
;--- The Main Loop ---------------------------------------------------------
mainloop:
Ledpower = StateOff ' Turn switch Led back OFF
Goto mainloop
end
Bookmarks