Advertisement
Guest User

control 8x motor

a guest
Sep 1st, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.90 KB | None | 0 0
  1. ' Drehgeber ###################################################################
  2.  
  3. $regfile = "m88adef.dat"
  4. $crystal = 14745600
  5. $hwstack = 128
  6. $swstack = 128
  7. $framesize = 128
  8.  
  9. Dim I As Dword , A As Byte , Count As Byte , I2 As Byte , Count2 As Byte
  10. dim count_funktion as byte
  11. Dim Xor_val As Byte , Old_val As Byte
  12. Dim Encoder_val(4) As dword, m_soll(4) as dword , m_funktion(4) as byte
  13. dim m_select as byte
  14.  
  15. Dim Rx_dword As Dword , Tx_dword As Dword
  16. dim tx_dword_byte3 as byte at tx_dword + 3 overlay
  17. dim Funktion As Byte , select_m as byte
  18. Dim Adc_wert As Word
  19. Dim Ausgabe As String * 8 , Encoder_ausgabe As String * 5
  20.  
  21. ' I/O #########################################################################
  22. Config Pinb.1 = Input
  23. Portb.1 = 1
  24.  
  25. Config Portb.0 = Output
  26. Led Alias Portb.0
  27.  
  28. Txtx Alias Portc.5
  29. Rxrx Alias Pinc.5
  30.  
  31. Clk_out Alias Portc.4
  32. Clk_in Alias Pinc.4
  33.  
  34. Ddrc = &B00000110
  35. Config Portd = Input
  36. Encoder_port Alias Pind
  37.  
  38. ' LCD #########################################################################
  39. Config Lcdpin = Pin , Rs = Portc.2 , E = Portc.1 , Db4 = Portb.5 , _
  40. Db5 = Portb.4 , Db6 = Portb.3 , Db7 = Portb.2
  41.  
  42. Config Lcd = 16x2
  43.  
  44. Cursor Off Noblink
  45.  
  46. Cls
  47.  
  48. ' ADC #########################################################################
  49. Config Adc = Single , Prescaler = Auto , Reference = Avcc
  50.  
  51. Start Adc
  52.  
  53. ' startup #####################################################################
  54. Old_val = Encoder_port
  55. Led = 0
  56.  
  57. declare sub funktion_select(byval x as byte)
  58.  
  59. Wait 1
  60.  
  61. ' Main ########################################################################
  62. Do
  63.  
  64.    Debounce Pinb.1 , 0 , Send_byte , Sub
  65.  
  66.    led = 1
  67.  
  68.    If Old_val <> Encoder_port Then
  69.  
  70.       'Led = 1
  71.  
  72.      Xor_val = Old_val Xor Encoder_port
  73.      Old_val = Encoder_port
  74.  
  75.      For I2 = 0 To 7 Step 1
  76.  
  77.         If Xor_val.i2 = 1 Then
  78.  
  79.            Select Case I2
  80.               Case 0 To 1:
  81.                  Incr Encoder_val(1)
  82.  
  83.                  if m_select.0 = 1 then
  84.                     call funktion_select(1)
  85.                  end if
  86.  
  87.               Case 2 To 3:
  88.                  Incr Encoder_val(2)
  89.  
  90.                  if m_select.1 = 1 then
  91.                     call funktion_select(2)
  92.                  end if
  93.  
  94.               Case 4 To 5:
  95.                  Incr Encoder_val(3)
  96.  
  97.                  if m_select.2 = 1 then
  98.                     call funktion_select(3)
  99.                  end if
  100.  
  101.               Case 6 To 7:
  102.                  Incr Encoder_val(4)
  103.  
  104.                  if m_select.3 = 1 then
  105.                     call funktion_select(4)
  106.                  end if
  107.  
  108.            End Select
  109.  
  110.         End If
  111.  
  112.      Next
  113.  
  114.      Select Case Funktion
  115.  
  116.         case 0:
  117.  
  118.      End Select
  119.  
  120.      '      If Encoder_val(1) >= Rx_dword Then
  121.  
  122.       '         Gosub Send_byte
  123.      '         Encoder_val(1) = 0
  124.  
  125.       '      End If
  126.  
  127.      'Led = 0
  128.  
  129.    End If
  130.  
  131.    for count_funktion = 1 to 4 step 1
  132.  
  133.       if m_funktion(count_funktion) = 1 then
  134.  
  135.          count2 = count_funktion - 1
  136.          tx_dword = encoder_val(count_funktion)
  137.          tx_dword_byte3 = &b00010000
  138.          tx_dword_byte3.count2 = 1
  139.          gosub send_byte
  140.          m_funktion(count_funktion) = 0
  141.  
  142.       end if
  143.  
  144.       if m_funktion(count_funktion) > 3 then
  145.  
  146.          call funktion_select(count_funktion)
  147.  
  148.       end if
  149.  
  150.    next
  151.  
  152.    If Clk_in = 0 Then
  153.  
  154.       'led = 1
  155.  
  156.      'Ddrc = &B00100110
  157.       Config Txtx = Output
  158.       Txtx = 0
  159.       Bitwait Clk_in , Set
  160.       Txtx = 1
  161.       'Ddrc = &B00000110
  162.      Config Rxrx = Input
  163.  
  164.      For Count = 0 To 31 Step 1
  165.         Bitwait Clk_in , Reset
  166.         Rx_dword.count = Rxrx
  167.         Bitwait Clk_in , Set
  168.      Next
  169.  
  170.      For Count = 24 To 27 Step 1
  171.         Count2 = Count - 24
  172.         select_m.count2 = Rx_dword.count
  173.         Rx_dword.count = 0
  174.      Next
  175.  
  176.      For Count = 28 To 31 Step 1
  177.         Count2 = Count - 28
  178.         Funktion.count2 = Rx_dword.count
  179.         Rx_dword.count = 0
  180.      Next
  181.  
  182.      for count = 0 to 3 step 1
  183.         if select_m.count = 1 then
  184.            m_soll(count + 1) = rx_dword
  185.            m_funktion(count + 1) = funktion
  186.            m_select.count = 1
  187.  
  188.            if m_funktion(count + 1) = 0 then
  189.               m_select.count = 0
  190.            end if
  191.  
  192.         end if
  193.      next
  194.  
  195.      'led = 0
  196.  
  197.    End If
  198.  
  199.    led = 0
  200.  
  201.    Incr I
  202.  
  203.    If I = 100000 Then
  204.  
  205.       Adc_wert = Getadc(0)
  206.  
  207.       'Tx_dword = Adc_wert
  208.  
  209.      'Ausgabe = Str(tx_dword)
  210.       'Locate 1 , 1
  211.      'Lcd "TX: "
  212.       'Lcd Format(ausgabe , "000000")
  213.  
  214.      Ausgabe = Str(a)
  215.      Locate 2 , 14
  216.      Lcd Format(ausgabe , "000")
  217.  
  218.      'Ausgabe = Str(rx_dword)
  219.       'Locate 2 , 1
  220.      'Lcd "RX: "
  221.       'Lcd Format(ausgabe , "000000")
  222.  
  223.      locate 1 , 1
  224.      ausgabe = bin(m_funktion(1))
  225.      lcd ausgabe
  226.  
  227.      locate 2 , 1
  228.      ausgabe = bin(m_select)
  229.      lcd ausgabe
  230.  
  231.      Encoder_ausgabe = Str(encoder_val(1))
  232.      Locate 1 , 12
  233.      Lcd Format(encoder_ausgabe , "00000")
  234.  
  235.      Incr A
  236.  
  237.      I = 0
  238.  
  239.   End If
  240.  
  241. Loop
  242. End
  243.  
  244. Send_byte:
  245.  
  246.   If Clk_in = 1 Then
  247.  
  248.      'Ddrc = 00010110
  249.       Config Clk_out = Output
  250.       Clk_out = 0
  251.  
  252.       Bitwait Rxrx , Reset
  253.       Clk_out = 1
  254.       Bitwait Rxrx , Set
  255.  
  256.       'Ddrc = 00110110
  257.      Config Txtx = Output
  258.  
  259.      For Count = 0 To 31 Step 1
  260.         Txtx = Tx_dword.count
  261.         Clk_out = 0
  262.         Waitus 2
  263.         Clk_out = 1
  264.      Next
  265.  
  266.      'Ddrc = 00000110
  267.       Config Rxrx = Input
  268.       Config Clk_in = Input
  269.  
  270.    End If
  271.  
  272. Return
  273.  
  274. sub funktion_select(byval x as byte)
  275.  
  276.    'led = 1
  277.  
  278.   select case m_funktion(x)
  279.  
  280.      case 2:
  281.         if encoder_val(x) >= m_soll(x) then
  282.  
  283.            tx_dword = encoder_val(x)
  284.            tx_dword_byte3 = &b00100000 or m_select
  285.            gosub SEND_BYTE
  286.            encoder_val(x) = 0
  287.  
  288.         end if
  289.  
  290.      case 3:
  291.  
  292.  
  293.   end select
  294.  
  295.   'led = 0
  296.  
  297. end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement