Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. list p=18f452
  2. include "p18f452.inc"
  3.  
  4.  
  5. variable databit
  6. databit set b'000000000'
  7. potused set b'000000000'
  8. totalresmul set d'0'
  9.  
  10. UDATA_ACS
  11. DN res 1
  12. RA res 1 ;Wiper A
  13. RB res 1 ;Wiper B
  14. RAB res 2 ;Total Res
  15.  
  16. ; Declare a code section at 0x0 named 'RST'. This instruction sends the
  17. ; program to the actual start of the program.
  18. RST code 0x0000
  19. GOTO Setup
  20.  
  21. ; Declare a code section at 0x0030. This is where the actual program logic will start
  22. SRT code 0x0060
  23.  
  24.  
  25.  
  26.  
  27. Setup:
  28. totalres = 720
  29. ;Load the Total Res into an easier to locate ram loco
  30. MOVLW HIGH totalres ;We dont worry about full number, it is divided by 256
  31. MOVWF RAB
  32. RLNCF ;Save first two bits /256 = first two bits+1
  33. RLNCF
  34. MOVLW b'11'
  35. ANDWF RAB
  36. MOVLW b'1'
  37. ADDWF RAB ;Total Resistance has been setup
  38.  
  39. call SetupPortB
  40. call SetupSPI
  41. potused = b'10'
  42. databit = d'86' ;Set N a percentage of the Pot Value
  43. call convertN ;The argument N is now placed in W before the subroutine is called
  44. call VSetPotBW
  45.  
  46.  
  47. ;Converts N a percentage to a 255 bit number
  48. convertN:
  49. MOVLW databit
  50. MULLW 0xFF ; percentage * 255 = 16 bit number. First 8 bits are the new number
  51. MOVFF PRODH, W ;Move the high byte of the multiplication to the W
  52.  
  53.  
  54. VSetPotBW:
  55. ;Sets the resistance between B and W (wiper to N% of the potentionmeters value)
  56. MOVWF DN ;Store the value W in a better location
  57. potused = b'01' ;Set the Wiper A first
  58. selectP
  59. databit = DN
  60. call WriteToP
  61. potused = b'10'
  62. selectP
  63. databit = DN
  64. call WriteToP
  65.  
  66. SetupPortB:
  67. BCF PORTB, 3
  68. BSF TRISB, 3 ;
  69. RETURN
  70.  
  71. SetupSPI:
  72. BSF SSPSTAT, CKE
  73. BCF SSPSTAT, SMP ;Sampled in the middle
  74. BSF SSPCON1, SSPEN ;Enables SCK, SDO, SDI and SS
  75. MOVWF b'0001' ;Set the SPI Master mode to FOSC/16 (Pot works on 16)
  76. IORWF SSPCON1
  77.  
  78.  
  79. BSF TRISC, 4 ;Serial Data In (SDI) - Connected to
  80. BCF TRISC, 3 ;Serial Clock (SCK)
  81. BCF TRISC, 5 ;Serial Data Out (SDO)
  82. RETURN
  83.  
  84. selectP:
  85. BCF PORTB, 3 ;Set CS to LOW
  86. MOVLW b'00100000' ;Set to shutdown for selection
  87. MOVWF SSPBUF ;Send First byte (the command byte)
  88. BSF PORTB, 3
  89. call WaitforSSPConversion
  90. BCF PORTB, 3 ;Set CS to LOW
  91. MOVWF potused ;Selects Pot
  92. IORWF SSPBUF
  93. BSF PORTB, 3
  94.  
  95. WriteToP:
  96. ;SETTING UP THE COMMAND BYTE
  97. BCF PORTB, 3 ;Set CS to LOW
  98. ;Write in Values
  99. MOVLW b'00010000' ;Turn on the write byte, Select Potentiometer
  100. MOVWF SSPBUF ;Send First byte (the command byte)
  101.  
  102. BSF PORTB, 3 ;Set CS to High to execute the command
  103. call WaitforSSPConversion
  104.  
  105. ;SETTING UP THE DATA BYTE
  106. BCF PORTB, 3 ;Set CS to LOW
  107. MOVLW databit ;The Data Byte is created
  108. MOVWF SSPBUF ;Send the data to the Pot
  109. BSF PORTB, 3 ;Set CS to High to execute the command
  110. call WaitforSSPConversion
  111. RETURN
  112.  
  113.  
  114.  
  115. WaitforSSPConversion:
  116. btfss SSPSTAT, BF ; Wait for the Buffer to become full
  117. goto WaitforSSPConversion
  118. RETURN
  119.  
  120.  
  121. ;Set CS Low
  122. ;clocking in a command byte and then a data byte into the 16 bit shift regist
  123. ;;Executed when CS is raised
  124. ;Clocked in on the rising edge of the clock andf out the SO pin on the falling edge
  125. ;Command Byte - two command select bytes and two potenitometer select bits
  126. ;c1 and c0 (bits 4 and 5) - 01 write command will be executed to the 8 bits
  127. ; 10 shutdown
  128.  
  129.  
  130. ; SSPBUF = 0xFF; // Sends data in order to read from the buffer
  131. ; while ( !SSPSTATbits.BF ); // wait until cycle complete
  132. ;
  133.  
  134. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement