Code:
'--------------------------------------------------------------------'
' Author: Demon '
' Date: Dec 29th, 2014 '
' Project: RESIDENTIAL ALARM SYSTEM '
' Circuit: Master '
' '
' PIC: 18F44K22 @ 64MHz '
' Language: PIC Basic Pro v2.60c '
' Assembler: MPASM v6.49 '
' IDE: MicroCode Studio Plus from Mecanique v2.1.0.7 '
' Programmer: MeLabs USB Programmer v4.32 '
' LCD: CASIL CCM-2040CSL v2.0 (HD44780 parallel interface)'
' '
' All rights reserved. '
' '
' Use of parts of code is permitted as long as credit is given '
' to the author. Code may or may not work, use at your own risk '
' including but not limitted to financial loss, heartbreak and '
' loss of an eye. '
'--------------------------------------------------------------------'
'DEFINE LOADER_USED 1
clear
' ---------------------------- Configuration Fuses -----------------------------
asm
__CONFIG _CONFIG1H, _FOSC_INTIO67_1H & _PLLCFG_ON_1H & _PRICLKEN_OFF_1H & _FCMEN_ON_1H & _IESO_OFF_1H
__CONFIG _CONFIG2L, _PWRTEN_ON_2L & _BOREN_SBORDIS_2L & _BORV_285_2L
__CONFIG _CONFIG2H, _WDTEN_OFF_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC1_3H & _PBADEN_OFF_3H & _CCP3MX_PORTE0_3H & _HFOFST_OFF_3H & _T3CMX_PORTB5_3H & _P2BMX_PORTC0_3H & _MCLRE_EXTMCLR_3H
__CONFIG _CONFIG4L, _STVREN_OFF_4L & _LVP_OFF_4L & _XINST_OFF_4L & _DEBUG_OFF_4L
endasm
' -------------------------------- Oscillator ----------------------------------
DEFINE OSC 64
' --------------------------------- Includes -----------------------------------
'INCLUDE "DT_INTS-18.bas"
'INCLUDE "ReEnterPBP-18.bas"
'INCLUDE "ReEnterPBP-18LP.bas" ; PBP Re-entry for low priority external interrupt
'INCLUDE "Elapsed_INT-18_64MHz.bas"
' -------------------------------- Interrupts ----------------------------------
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' RX must be done manually on Master x
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;ASM
;----[High Priority Interrupts] ------------------------------------------------
;INT_LIST macro ; IntSource, Label, Type, ResetFlag?
; INT_Handler TMR1_INT, _ClockCount, PBP, yes
; endm
; INT_CREATE ; Creates the High Priority interrupt processor
;----[Low Priority Interrupts] -------------------------------------------------
;INT_LIST_L macro ; IntSource, Label, Type, ResetFlag?
; INT_Handler TMR3_INT, _T3handler, PBP, yes
; endm
; INT_CREATE_L ; Creates the Low Priority interrupt processor
;ENDASM
;DEFINE USE_LOWPRIORITY 1 ' Low priority interrupts
' ------------------------------------ USB -------------------------------------
' --------------------------------- Registers ----------------------------------
OSCCON = %01110000 ' OSCILLATOR CONTROL REGISTER
' bit 7 IDLEN: Idle Enable bit
' R/W 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)
' R/W 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
' R 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
' R 1 = HFINTOSC frequency is stable
' 0 = HFINTOSC frequency is not stable
' bit 1-0 SCS<1:0>: System Clock Select bit
' R/W 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
' R 1 = System clock comes from 4xPLL
' 0 = System clock comes from an oscillator, other than 4xPLL
' bit 6 SOSCRUN: SOSC Run Status bit
' R 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
' R/W=0 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
' R/W=0 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
' R/W=1 1 = Oscillator drive circuit on
' 0 = Oscillator drive circuit off (zero power)
' bit 1 MFIOFS: MFINTOSC Frequency Stable bit
' R 1 = MFINTOSC is stable
' 0 = MFINTOSC is not stable
' bit 0 LFIOFS: LFINTOSC Frequency Stable bit
' R 1 = LFINTOSC is stable
' 0 = LFINTOSC is not stable
OSCTUNE = %11000000 ' OSCILLATOR TUNING REGISTER
' bit 7 INTSRC: Internal Oscillator Low-Frequency Source Select bit
' R/W 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)
' R/W 1 = PLL enabled
' 0 = PLL disabled
' bit 5-0 TUN<5:0>: Frequency Tuning bits – use to adjust MFINTOSC and HFINTOSC frequencies
' R/W 011111 = Maximum frequency
' 011110 =
' • • •
' 000001 =
' 000000 = Oscillator module (HFINTOSC and MFINTOSC) are running at the factory calibrated frequency.
' 111111 =
' • • •
' 100000 = Minimum frequency
PMD0 = %10111111 ' 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 = %11011111 ' 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 = %00000000 ' Set ports to digital
ANSELB = %00000000
ANSELC = %00000000
ANSELD = %00000000
ANSELE = %00000000
ADCON0 = %00000000 ' Disable ADC
' ---------------------------------- EUSART ------------------------------------
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' For MULTIPLE Slaves x
' x
' Do not use USART TX pin for HSEROUT on Slave x
' Use SEROUT at 115200 baud open collector x
' x
' - or - x
' x
' Enable USART TX pin for HSEROUT on Slave x
' Transmit to Master x
' Disable TX pin x
' (my preferred method) x
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
'DEFINE CHAR_PACING 1000
DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
DEFINE HSER_SPBRG 138 ' 115200 Baud @ 64MHz, -0,08%
RCSTA = $90 ' Enable serial port & continuous receive
TXSTA = $24 ' Enable transmit, BRGH = 1
SPBRG = 138 ' 115200 Baud @ 64MHz, -0.08%
SPBRGH = 0 ' Baud Rate Generator high byte
BAUDCON.3 = 1 ' Enable 16 bit Baud Rate Generator
' Override switches
'RCSTA1.4 = 0 ' Disable USART RX1 function
'RCSTA2.4 = 0 ' Disable USART RX2 function
'TXSTA1.5 = 0 ' Disable USART TX1 function
'TXSTA2.5 = 0 ' Disable USART TX2 function
' ------------------------------------ LCD -------------------------------------
DEFINE LCD_DREG PORTA
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTE ' Register Select
DEFINE LCD_RSBIT 0
DEFINE LCD_EREG PORTE ' Enable
DEFINE LCD_EBIT 1
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
'DEFINE LCD_COMMANDUS 1500 ' Set command delay time in microseconds
'DEFINE LCD_DATAUS 50 ' Set data delay time in microseconds
' ------------------------------ Port directions -------------------------------
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
' - From Microchip PIC datasheet in EUSART section - x
' For all modes of EUSART operation, the TRIS control bits corresponding to x
' the RXx/DTx and TXx/CKx pins should be set to ‘1’. The EUSART control will x
' automatically reconfigure the pin from input to output, as needed. x
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TRISA = %00000000 ' 0 = output
TRISB = %00000000 ' 1 = input
TRISC = %11000000
TRISD = %00000000
TRISE = %00000000
' ----------------------------------- Ports ------------------------------------
PORTA = %00000000 ' 0 = low (GND)
PORTB = %00000000 ' 1 = high (5V)
PORTC = %00000000
PORTD = %00000000
PORTE = %00000000
' --------------------------------- Constants ----------------------------------
conSlave1 CON 1 ' Slave address
conSlave2 CON 2
conEcho con 69 ' Slave Echo command (Dec 69 = ASCII "E")
' --------------------------------- Variables ----------------------------------
pinTX var PortC.4 ' This pin is used for debugging only
bytLoop var byte ' Loop control
bytAddress var byte ' Slave address
bytCommand var byte ' Slave command
wrdCS var word ' CheckSum
bytBuffer1 var byte[8] ' Data buffers sent to Slave
bytBuffer2 var byte[8]
' ------------------------------- Program start --------------------------------
for bytLoop = 0 to 7 ' Initialize arrays with "fake data"
bytBuffer1[bytLoop] = bytLoop
bytBuffer2[bytLoop] = bytLoop
next bytLoop
pause 500
'@ INT_ENABLE TMR1_INT
goto MainProgram
' -------------------------------- Subroutines ---------------------------------
'T1handler:
' T1CON.0 = 0 ' Stop timer
'@ INT_RETURN
' -------------------------------- Main Program --------------------------------
MainProgram:
Lcdout $fe, 1, "USART sample program"
bytAddress = conSlave1 ' Slave system address
bytCommand = conEcho ' Slave echo command
wrdCS = bytAddress + bytCommand ' Checksum = Address + Command
for bytLoop = 0 to 7 ' Checksum = Checksum + Buffer1 + Buffer2
wrdCS = wrdCS + bytBuffer1[bytLoop]
wrdCS = wrdCS + bytBuffer2[bytLoop]
next bytLoop
pinTX = 1 ' Signal the start of Transmit to Slave PIC
hserout [dec3 bytAddress, dec2 bytCommand, _
str bytBuffer1\8, str bytBuffer2\8, _
dec5 wrdCS.byte1, dec5 wrdCS.byte0]
pinTX = 0 ' Signal the end
Lcdout $fe, $c0, "Addr:", dec3 bytAddress, " Cmd:", dec2 bytCommand
Lcdout $fe, $94, "Buf:", dec1 bytBuffer1[0], dec1 bytBuffer1[1], dec1 bytBuffer1[2], _
dec1 bytBuffer1[3], dec1 bytBuffer1[4], dec1 bytBuffer1[5], _
dec1 bytBuffer1[6], dec1 bytBuffer1[7], _
dec1 bytBuffer2[0], dec1 bytBuffer2[1], dec1 bytBuffer2[2], _
dec1 bytBuffer2[3], dec1 bytBuffer2[4], dec1 bytBuffer2[5], _
dec1 bytBuffer2[6], dec1 bytBuffer2[7]
Lcdout $fe, $D4, "Checksum:", dec5 wrdCS.byte1, dec5 wrdCS.byte0
' goto MainProgram
end
You can comment out the LCD stuff if you don't need it. I use a Saleae probe to see the data on the TX pin. The normal method is to connect to a PC using MAX232 chip (search forum for circuit).
Bookmarks