list p=18f4580 list r=dec include "p18f4580.inc" ; Include file, change directory if needed ERRORLEVEL -302 ; SUPPRESS BANK SELECTION MESSAGES ;Configuration bits CONFIG OSC = HS ; HS 20 MHz CONFIG PWRT = ON ; power up timer on CONFIG BOREN = OFF ; brown out detect off CONFIG WDT = OFF ; watchdog off CONFIG LVP = OFF ; lvp off CONFIG PBADEN = OFF cblock 0x0000 sleep_counter endc org 0x0000 goto init org 0x0008 goto intHigh org 0x0018 goto intLow ;************************************************************ ; Interrupt ;************************************************************ intHigh retfie intLow ;CAN und Timer btfss PIR3, RXB0IF goto Timer_int bcf RXB0CON, RXFUL ;This will allow CAN Module to load new messages bcf PIR3, RXB0IF clrf sleep_counter movlw 0x02 xorwf PORTD, f ;LED Blinken lassen Timer_int btfss PIR1, TMR1IF goto Wakeup_Int bcf PIR1, TMR1IF movlw 0x01 xorwf PORTD incf sleep_counter, f btfss sleep_counter, 3 ;2 sec kein can interrupt goto exit clrf sleep_counter movlw 0x20 ;CAN Modul in Sleep mode schalten movwf CANCON movf CANSTAT, w andlw 0xE0 ;only OPMODE2:OPMODE0 xorlw 0x20 bnz $-6 sleep ; bcf PORTD, 2 goto exit Wakeup_Int bcf PIR3, WAKIF clrf CANCON ;CAN Modul wieder in Normal mode schalten movf CANSTAT, w ;Request Normal mode andlw 0xE0 ;only OPMODE2:OPMODE0 xorlw 0x00 bnz $-6 bsf PORTD, 2 exit retfie FAST ;************************************************************ ; Initialisierung ;************************************************************ init clrf PORTA clrf PORTB clrf PORTC clrf PORTD clrf PORTE clrf TRISD ;PORTD als Ausgang movlw 0x0F movwf ADCON1 movlw 0x07 movwf CMCON ;************************ ; can ;************************ bsf TRISB, RB3 ;CANRX bcf TRISB, RB2 ;CANTX ;Configuration Mode movlw 0x80 movwf CANCON ;Request Configuration mode movf CANSTAT, w andlw 0xE0 ;only OPMODE2:OPMODE0 xorlw 0x80 bnz $-6 movlw 0x01 movwf BRGCON1 movlw 0xBA movwf BRGCON2 movlw 0x07 ;Microchip CAN Bit Timing Calculator movwf BRGCON3 ;100kbit, 8Mhz bcf BRGCON3, WAKDIS ;Enable CAN bus activity wake-up feature bsf BRGCON3, WAKFIL ;Use CAN bus line filter for wake-up ; clrf ECANCON ;Legacy mode (Mode 0, default) ; bsf CIOCON, ENDRHI ;1 = CANTX pin will drive VDD when recessive ;RECEIVE ACCEPTANCE MASK BANKSEL RXM0SIDH clrf RXM0SIDH clrf RXM0SIDL clrf BSR ;Bank 0 clrf CANCON ;Request Normal mode movf CANSTAT, w andlw 0xE0 ;only OPMODE2:OPMODE0 xorlw 0x00 bnz $-6 ; call loop_wait ;Timer 1 bsf T1CON, T1CKPS0 ;Vorteiler 1:8 bsf T1CON, T1CKPS1 bsf T1CON, TMR1ON ;Timer1 on bsf RCON, IPEN ;Enable priority levels on interrupts bsf INTCON, GIE bsf INTCON, PEIE bcf IPR1, TMR1IP ;Timer1 low priority Interrupt bcf IPR3, RXB0IP ;Receive Buffer 0 Low priority bcf IPR3, WAKIP ;Bus activity wake-up interrupt low priority bsf PIE1, TMR1IE ;Enable Timer1 Interrupt bsf PIE3, RXB0IE ;Enable Receive Buffer 0 interrupt bsf PIE3, WAKIE ;Enable bus activity wake-up interrupt clrf sleep_counter bsf PORTD, 2 ;************************************************************ main goto main ;************************************************************ loop_wait movlw 0x40 movwf CANCON movf CANSTAT, w andlw 0xE0 xorlw 0x40 bnz $-6 return end