Guest User

Untitled

a guest
Jul 1st, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;******************************************************************************
  2. ;   ;******************************************************************************
  3. ;   This file is a basic template for assembly code for a PIC18F4550. Copy    *
  4. ;   this file into your project directory and modify or add to it as needed.  *
  5. ;                                                                             *
  6. ;   The PIC18FXXXX architecture allows two interrupt configurations. This     *
  7. ;   template code is written for priority interrupt levels and the IPEN bit   *
  8. ;   in the RCON register must be set to enable priority levels. If IPEN is    *
  9. ;   left in its default zero state, only the interrupt vector at 0x008 will   *
  10. ;   be used and the WREG_TEMP, BSR_TEMP and STATUS_TEMP variables will not    *
  11. ;   be needed.                                                                *
  12. ;                                                                             *
  13. ;   Refer to the MPASM User's Guide for additional information on the         *
  14. ;   features of the assembler.                                                *
  15. ;                                                                             *
  16. ;   Refer to the PIC18FXX50/XX55 Data Sheet for additional                    *
  17. ;   information on the architecture and instruction set.                      *
  18. ;                                                                             *
  19. ;******************************************************************************
  20. ;                                                                             *
  21. ;    Filename:                                                                *
  22. ;    Date:                                                                    *
  23. ;    File Version:                                                            *
  24. ;                                                                             *
  25. ;    Author:                                                                  *
  26. ;    Company:                                                                 *
  27. ;                                                                             *
  28. ;******************************************************************************
  29. ;                                                                             *
  30. ;    Files required: P18F4550.INC                                             *
  31. ;                                                                             *
  32. ;******************************************************************************
  33.  
  34.     LIST P=18F4550, F=INHX32    ;directive to define processor
  35.     #include <P18F4550.INC> ;processor specific variable definitions
  36.  
  37. ;******************************************************************************
  38. ;Configuration bits
  39. ;Microchip has changed the format for defining the configuration bits, please
  40. ;see the .inc file for futher details on notation.  Below are a few examples.
  41.  
  42.  
  43.  
  44. ;   Oscillator Selection:
  45.     CONFIG  FOSC = XT_XT         ;XT oscillator, XT used by USB
  46.  
  47. ;******************************************************************************
  48. ;Variable definitions
  49. ; These variables are only needed if low priority interrupts are used.
  50. ; More variables may be needed to store other special function registers used
  51. ; in the interrupt routines.
  52.  
  53. #define RX_BUF_LEN 32
  54. ADDRESS equ 0x22
  55.         UDATA
  56. FSRsave RES 1
  57. PCLATHsave RES 1
  58. Index RES 1
  59. Temp  RES 1
  60. RXBuffer RES 1
  61.  
  62.     ;example of a variable in access RAM
  63.  
  64. ;******************************************************************************
  65. ;EEPROM data
  66. ; Data to be programmed into the Data EEPROM is defined here
  67.  
  68.  
  69. DATA_EEPROM CODE    0xf00000
  70.  
  71. ;       DE  "Test Data",0,1,2,3,4,5
  72.  
  73. memset macro Buf_addr,Value,Length
  74.     movlw Length
  75.     movwf Temp
  76.     movlw Buf_addr
  77.     movwf FSR0L
  78.  
  79. SetNext
  80.     movlw Value
  81.     movwf INDF0
  82.     incf FSR0L,F
  83.     decfsz Temp,F
  84.     goto SetNext
  85. endm
  86.  
  87. load macro Address,Offset
  88.     movlw Address
  89.     movwf FSR0L
  90.     movf Offset,W
  91.     addwf FSR0L,F
  92. endm
  93.  
  94.  
  95. PRG CODE 0x00
  96. goto Start
  97. INT1 CODE 0x08
  98. goto Int
  99. INT2 CODE 0x18
  100. goto Int
  101. MAIN CODE 0x30
  102. ;---------------------------------------------------------------------
  103. ; Main Code
  104. ;---------------------------------------------------------------------
  105. Start
  106.     clrf Index
  107.     clrf Temp
  108.     clrf RXBuffer
  109.     call Setup
  110. Main
  111.     goto Main
  112. Setup
  113.     bsf TRISC,3
  114.     bsf TRISC,4
  115.     clrf FSR0L
  116.     clrf FSR0H
  117.     movlw ADDRESS
  118.     movwf SSPADD
  119.     movlw 0x36
  120.     movwf SSPCON1
  121.     clrf SSPSTAT
  122.     clrf SSPCON2
  123.     bsf SSPCON2,SEN
  124.     bcf PIR1,SSPIF
  125.     bsf PIE1,SSPIE
  126.     movlw 0xC0
  127.     movwf INTCON
  128. return
  129.  
  130. Int
  131. movf FSR0L,W;
  132. movwf FSRsave
  133. btfss PIR1,SSPIF
  134. goto $
  135. bcf PIR1,SSPIF
  136. call SSP_Handler
  137.    
  138. movf FSRsave,W
  139. movwf FSR0L
  140. bsf SSPCON1,CKP
  141. retfie FAST
  142.  
  143. SSP_Handler
  144.     movf SSPSTAT,W
  145.     andlw b'00101101'
  146.     movwf Temp
  147.    
  148. State1:
  149.     movlw b'00001001'
  150.     xorwf Temp,W
  151.     btfss STATUS,Z
  152.     goto State2
  153.     memset RXBuffer,0,RX_BUF_LEN
  154.     clrf Index
  155.     movf SSPBUF,W
  156. return
  157.  
  158. State2:
  159.     movlw b'00101001'
  160.     xorwf Temp,W
  161.     btfss STATUS,Z
  162.     goto State3
  163.     load RXBuffer,Index
  164.     movf SSPBUF,W
  165.     movwf INDF0
  166.     incf Index,F
  167.     movf Index,W
  168.     sublw RX_BUF_LEN
  169.     btfsc STATUS,Z
  170.     clrf Index
  171. return
  172. State3:
  173.     movf Temp,W ;
  174.     andlw b'00101100'
  175.     xorlw b'00001100'
  176.     btfss STATUS,Z
  177.     goto State4
  178.     movf SSPBUF,W
  179.     clrf Index
  180.     load RXBuffer,Index
  181.     movf INDF0,W
  182.     call WriteI2C
  183.     incf Index,F
  184. return    
  185.  
  186. State4
  187.     movlw b'00101100'
  188.     xorwf Temp,W
  189.     movf Index,W
  190.     sublw RX_BUF_LEN
  191.     btfsc STATUS,Z
  192.     clrf Index
  193.     load RXBuffer,Index
  194.     movf INDF0,W
  195.     call WriteI2C
  196.     incf Index,F
  197. return
  198.  
  199. WriteI2C  
  200.     btfsc SSPSTAT,BF
  201.     goto WriteI2C
  202.  
  203. DoI2CWrite
  204.     bcf SSPCON1,WCOL
  205.     movwf SSPBUF
  206.     btfsc SSPCON1,WCOL
  207.     goto DoI2CWrite
  208.     return
  209. END
Add Comment
Please, Sign In to add comment