Guest User

8-Bit Digitalpoti (DAC) - MCP413X/415X/423X/425X (microchip)

a guest
Nov 10th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3.  
  4.  
  5.  
  6. $regfile = "m88pdef.dat"
  7. $crystal = 16000000
  8. $hwstack = 40
  9. $swstack = 16
  10. $framesize = 32
  11.  
  12. 'consts ###############################################################################################################
  13. 'MCP4251 digital Poti
  14. 'n=0-256 -> 257 Positionen. Beim Lesen: Y=0=Error (bei den 6 Command Bits vorher).
  15. '
  16. 'Poti 0: W=0000_000n_nnnn_nnnn
  17. '        R= W:0000_11 R:Yn_nnnn_nnnn_1111_11
  18. 'Poti 1: W=0001_000n_nnnn_nnnn
  19. '        R= W:0001_11 R:Yn_nnnn_nnnn_1111_11
  20.  
  21. Const Poti_r0 = &B0000_1100
  22.  
  23. 'I/O ##################################################################################################################
  24. Ddrb = &B0000_0111
  25. Ddrc = &B0011_1111
  26. Ddrd = &B1000_1000
  27.  
  28. Led Alias Portd.3
  29.  
  30. Poti_cs Alias Portb.0       'chip select
  31. Poti_sdo Alias Portb.2       'serial data out
  32. Poti_clk Alias Portb.1       'clock
  33. Poti_sdi Alias Pind.6       'serial data in
  34. Poti_shdn Alias Portd.7       'shutdown
  35.  
  36. 'vars #################################################################################################################
  37. Dim String3 As String * 3
  38.  
  39. Dim Poti0 As Word
  40. Dim Positionpar As Word
  41. Dim Poti_cmd As Byte
  42. Dim Serialerr As Byte , Serialerr_count As Byte
  43.  
  44. 'lcd ##################################################################################################################
  45. Config Lcd = 16x2
  46. Config Lcdpin = Pin , Db7 = Portc.0 , Db6 = Portc.1 , Db5 = Portc.2 , Db4 = Portc.3 , E = Portc.4 , Rs = Portc.5
  47. Cursor Off Noblink
  48.  
  49. 'adc ##################################################################################################################
  50. Config Adc = Single , Prescaler = Auto , Reference = Avcc
  51.  
  52. 'subs #################################################################################################################
  53. Declare Sub Setpoti0(byval Position As Word)
  54.  
  55. 'startup ##############################################################################################################
  56. Poti_cs = 1         'inactive
  57. Poti_sdo = 0
  58. Poti_clk = 1        'idle high
  59. Poti_shdn = 1       'Poti ON
  60.  
  61. Call Setpoti0(0)
  62.  
  63. Cls
  64.  
  65. 'main #################################################################################################################
  66. Do
  67.  
  68.    Poti0 = Getadc(6)
  69.    Shift Poti0 , Right , 2
  70.  
  71.    If Poti0 = 255 Then
  72.  
  73.       Incr Poti0
  74.  
  75.    End If
  76.  
  77.    String3 = Str(poti0)
  78.    Locate 1 , 1
  79.    Lcd Format(string3 , "000")
  80.  
  81.    Call Setpoti0(poti0)
  82.  
  83.    String3 = Str(serialerr)
  84.    Locate 2 , 1
  85.    Lcd "ERR:" ; Format(string3 , "000")
  86.  
  87.    Waitms 100
  88.  
  89. Loop
  90. End
  91.  
  92. 'subs #################################################################################################################
  93. Sub Setpoti0(byval Position As Word)
  94.  
  95.    If Serialerr = 0 Then
  96.  
  97.       If Position > 256 Then
  98.  
  99.          Position = 256
  100.  
  101.       End If
  102.  
  103.       Positionpar = &HFFFF
  104.       Serialerr_count = 0
  105.  
  106.       While Position <> Positionpar
  107.  
  108.          Poti_cs = 0
  109.          Shiftout Poti_sdo , Poti_clk , Position , 1 , 16
  110.          Poti_clk = 1
  111.  
  112.          If Poti_sdi = 0 Then
  113.  
  114.             Incr Serialerr_count
  115.  
  116.             Poti_cs = 1
  117.  
  118.             If Serialerr_count = 3 Then
  119.  
  120.                Serialerr.0 = 1       'cmd ungültig (write)
  121.                Exit While
  122.  
  123.             End If
  124.  
  125.          Else
  126.  
  127.             Poti_cmd = Poti_r0
  128.             Positionpar = 0
  129.  
  130.             Poti_cs = 1
  131.             Poti_cs = 0
  132.             Shiftout Poti_sdo , Poti_clk , Poti_cmd , 1 , 6
  133.             Shiftin Poti_sdi , Poti_clk , Positionpar , 1 , 16
  134.             Poti_clk = 1
  135.             Poti_cs = 1
  136.  
  137.             If Positionpar.15 = 0 Then
  138.  
  139.                Incr Serialerr_count
  140.  
  141.                If Serialerr_count = 3 Then
  142.  
  143.                   Serialerr.1 = 1       'cmd ungültig (read)
  144.                   Exit While
  145.  
  146.                End If
  147.  
  148.             Else
  149.  
  150.                Positionpar.15 = 0
  151.                Shift Positionpar , Right , 6
  152.  
  153.                If Position <> Positionpar Then
  154.  
  155.                   Incr Serialerr_count
  156.  
  157.                   If Serialerr_count = 3 Then
  158.  
  159.                      Serialerr.2 = 1       'Daten ungültig (read)
  160.                      Exit While
  161.  
  162.                   End If
  163.  
  164.                End If
  165.  
  166.             End If
  167.  
  168.          End If
  169.  
  170.       Wend
  171.  
  172.    End If
  173.  
  174. End Sub
Add Comment
Please, Sign In to add comment