	LIST P=18F258		    ;directive to define processor
	#include <P18F258.INC>	;processor specific variable definitions
;******************************************************************************
;Configuration bits
   CONFIG OSC = ECIO           ;HS Oscillator Selection
   config OSCS = OFF           ;Osc. Switch Enable:
   CONFIG PWRT = ON            ;Enabled ;Power-up Timer:
   CONFIG BOR = OFF            ;Disabled ; Brown out Reset
   CONFIG WDT = OFF            ;Disabled Watchdog Timer
   CONFIG LVP = OFF            ;Disabled Low Voltage ICSP 
;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
		CBLOCK	0x060
		WREG_TEMP	;variable used for context saving 
		STATUS_TEMP	;variable used for context saving
		BSR_TEMP	;variable used for context saving
		ENDC

		CBLOCK	0x000
		EXAMPLE		;example of a variable in access RAM
		ENDC
;******************************************************************************
;Reset vector
        ORG	0x0000
        goto	Main		;go to start of main code
;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.
		ORG	0x0008
		bra	HighInt		;go to high priority interrupt routine
;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.
		ORG	0x0018
		movff	STATUS,STATUS_TEMP	;save STATUS register
		movff	WREG,WREG_TEMP		;save working register
		movff	BSR,BSR_TEMP		;save BSR register
;	*** low priority interrupt code goes here ***
    	movff	BSR_TEMP,BSR		;restore BSR register
		movff	WREG_TEMP,WREG		;restore working register
		movff	STATUS_TEMP,STATUS	;restore STATUS register
		retfie
;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here to avoid conflicting with
; the low priority interrupt vector.
HighInt:
;	*** high priority interrupt code goes here ***
		retfie	FAST
;******************************************************************************
;Start of main program

Main:
Start
        ;Intialisierung Port A auf ausgang
        movlw   B'00000000'     ; PortA alle Output
        movwf   TRISA
        clrf    PORTA           ; alle  ausschalten
       
        MOVLW 07h
        MOVWF ADCON1             ;Wichtig nur 07h macht alle pins Digital siehe Tabelle in PIC Lastenheft

                    
        ;Intialisierung Port B auf ausgang 
        movlw   B'00000000'     ; PortB alle output
        movwf   TRISB
        clrf    PORTB           ; alle ausschalten
        
        
        ;Intialisierung Port C auf ausgang
        movlw   B'00000000'     ; PortC alle output
        movwf   TRISC
        clrf    PORTC



; Unterprogramm Verteiler

       call CAN_CONFIG
       call init_Normal_mode
       Call Sender
      

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

CAN_CONFIG

       BSF TRISB,3  ;RX    PIN =1
       BCF TRISB,2  ;TX    PIN =0


; Request Configuration mode.
       MOVLW B'10000000' ; Set to Configuration Mode.
       MOVWF CANCON

ConfigWait
       MOVF CANSTAT, W   ; Read current mode state.
       ANDLW B'11100000' ; Interested in OPMODE bits only.
       BTFSS WREG,7      ; Is it Configuration mode yet?
       BRA ConfigWait    ; No. Continue to wait...

;Bautraten Register Bits:

       MOVLW 01h         ; 01  Oszi 10mhz, 100kbit CAN
       MOVWF BRGCON1     ; 
       
       MOVLW 3Fh         ; 3f  Oszi 10mhz, 100kbit CAN
       MOVWF BRGCON2     ;        
       
       MOVLW 07h         ; 07  Oszi 10mhz, 100kbit CAN
       MOVWF BRGCON3     ;        
 
       MOVLW 20h             
       MOVWF CIOCON      ; Ausgänge Aktivieren     
    
       return	         ; Zurück zum Unterprogramm Verteiler
    
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


init_Normal_mode
      
      MOVLW B'00001000' ; Set to Normal_mode.
      MOVWF CANCON
      
	        
NormalWait               ;Warten bis Normal_mode erreicht...
       MOVF CANSTAT, W   ; Read current mode state.
       ANDLW B'11100000' ; Interested in OPMODE bits only.
       BTFSC WREG,7      ; Is it Normal mode yet? Bit7=0
       BRA NormalWait    ; No. Continue to wait...
             
       MOVF CANSTAT, W   ; Read current mode state.
       ANDLW B'11100000' ; Interested in OPMODE bits only.
       BTFSC WREG,6      ; Is it Normal mode yet? Bit6=0
       BRA NormalWait    ; No. Continue to wait...
      
       MOVF CANSTAT, W   ; Read current mode state.
       ANDLW B'11100000' ; Interested in OPMODE bits only.
       BTFSC WREG,5      ; Is it Configuration mode yet? Bit5=0
       BRA NormalWait    ; No. Continue to wait...
       
       return            ;Normal Mode ereicht, zurück zum Unterprogramm Verteiler
 ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

;Senden Puffer 0
Sender
  
     
       MOVLW 0Fh ;Auf Bank 15 ,die Bank Änderung für CAN Register 
       MOVWF BSR 
    

; Sende ID Festlegen z.b. 130h
; Senden von Daten Sendepuffer 0  TXB0SIDH (High)
; Id Adresse bit    10, 9, 8, 7, 6, 5, 4 ,3 
; Bit in Register B` 7  6  5  4  3  2  1  0'

	  movlw B'00100110'
	  movwf TXB0SIDH


; Sende ID Festlegen
; Senden von Daten Sendepuffer 0  TXB0SIDL (Low) 
; Id Adresse bit     X  X  X                2,1,0 
; Unbenutzt 4,2               X     X       TBD=0
; Bit3                           X          1=Extended ID  0=Standard Id
; BIT 1, 0                             X  X Extended ID Bit 17 und 16
; Bit in Register B` 7  6  5  4  3  2  1  0'

	movlw B'00000000' 
	movwf TXB0SIDL


;TXB0DLC Transmit Buffer DLC und TXRTR(Remote) Bit

;7 6 5 4 3 2 1 
;X   X X         TBD=0
;  X             TXRTR Bit in CAN Botschaft= Remote Botschaft, Default=0
;        x x x x DLC in Bin, max 8 byte '1000'

	movlw b'00000101'  ; DLC=5 kein Remote
     movwf TXB0DLC


;Nutzlast CAN Botschaft 05 00 00 00 00
;TXB0D0 =Nutzinformation
;   X   =Sendepuffer 0 bis 2
;     X =Welches nutzbyte 0 bis 7


	movlw 05h
	movwf TXB0D0

	movlw 00h
	movwf TXB0D1

	movlw 00h
	movwf TXB0D2

	movlw 00h
	movwf TXB0D3

	movlw 00h
	movwf TXB0D4

	movlw 00h
	movwf TXB0D5



;Das Senden auslösen
  
     ;BSF TXB0CON,3 ;Bit fürs senden setzen +Hohe Sende Prio
	MOVLW 0bh
	MOVWF TXB0CON   



     MOVLW 00h ;Auf Bank 0 Umschalten für Warteschleife
     MOVWF BSR

 	CALL Wait; 10ms Warten (10* 1ms)
 	CALL Wait
 	CALL Wait
     CALL Wait 
     CALL Wait
  	CALL Wait
 	CALL Wait
 	CALL Wait
     CALL Wait 
     CALL Wait
 
     goto Sender ;Neuer Sendecyle



;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
; Warteschleife 1 ms 
; @10 MHZ
Wait
      movlw   .249           ; Zeitkonstante für 1ms gleich 249 Runden
      movwf   0x21
Wai2  nop
      nop
      nop
      nop
      nop
      nop 
      nop
      decfsz  0x21, F        ; 1 ms vorbei? Bei Null sprung zu retlw 0
      goto    Wai2           ; 
      retlw   0              ; das Warten hat ein Ende
;******************************************************************************
;End of program
                END 
