久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2692|回復: 0
打印 上一主題 下一主題
收起左側

bascom avr利用can通信控制伺服電機上使能

[復制鏈接]
跳轉到指定樓層
樓主
ID:941018 發表于 2021-6-20 18:36 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
本帖最后由 紅日888 于 2021-6-20 21:34 編輯

'此列子為串口轉can通信控制伺服電機上使能的列子
$regfile = "m128def.dat"                                    '單片機型號頭文件
$crystal = 8000000                                          '晶振頻率
$baud = 19200
$hwstack = 256
$swstack = 256
$framesize = 256

Declare Sub Send(byval Str_code As String)

Declare Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
                           Byval Func_code As String , Byval Node_id As String , _
                           Byval Str_data As String) As String
Declare Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)

Config Portd = Output
Const Std_frame = 1                                         '標準幀
Const Ext_frame = 0                                         '擴展幀
Const Position_mode = 0                                     '位置模式
Const Speed_mode = 1                                        '速度模式

Dim Can_code As String * 26

Portd = &HFF
Wait 1
'1號伺服位置模式上使能
Set_enable "01" , Position_mode , 1
Portd.3 = 0
Wait 1
'2號伺服速度模式上使能
Set_enable "02" , Speed_mode , 1
Portd.4 = 0
Wait 10

'1號伺服掉使能
Set_enable "01" , Position_mode , 0
Portd.3 = 1
Wait 1
'2號伺服掉使能
Set_enable "02" , Speed_mode , 0
Portd.4 = 1
Wait 1

End

'按值發送
Sub Send(byval Str_code As String )
    Local I1 As Integer , Mystr As String * 4 , Mybyte As Byte , Length As Integer
    Length = Len(str_code)
    For I1 = 1 To Length Step 2
        Mystr = Mid(str_code , I1 , 2)
        Mybyte = Hexval(mystr)
        Printbin Mybyte
    Next
End Sub

'Can幀數據Std_mode=1為標準幀=0為擴展幀,
'Data_length為數據區長度最大8字節,
'Func_code為功能碼一個字節,
'Node_id為節點ID一個字節,
'Str_data數據區十六進制字符串,最大8個字節
Function Can_data(byval Std_mode As Integer , Byval Data_length As Integer , _
                  Byval Func_code As String , Byval Node_id As String , _
                  Byval Str_data As String) As String

   Local Count As Integer , Str_code As String * 26 , I As Integer

   Select Case Std_mode
   Case Is = Std_frame
      Str_code = "0" + Str(data_length)                     '標準幀的第一個字符為0第二個字符為數據區的字節數(最大8字節)
   Case Is = Ext_frame
      Str_code = "8" + Str(data_length)                     '擴展幀的第一個字符為8第二個字符為數據區的字節數(最大8字節)
   End Select

   Str_code = Str_code + "0000"

   If Len(func_code) = 1 Then
      Str_code = Str_code + "0"
   End If
   Str_code = Str_code + Func_code

   If Len(node_id) = 1 Then Str_code = Str_code + "0"
   Str_code = Str_code + Node_id

   Count = Len(str_data)
   Count = 16 - Count
   Str_code = Str_code + Str_data
   For I = 1 To Count
      Str_code = Str_code + "0"
   Next I
   Can_data = Str_code
End Function

'伺服驅動器使能Node_id為驅動器節點ID,
'Ctrl_mode有兩種模式,位置模式Position_mode = 0和速度模式 Speed_mode = 1
'Value值設置非0為上使能,設置為0為掉使能
Sub Set_enable(byval Node_id As String , Byval Ctrl_mode As Integer , Byval Value As Integer)
   If Value <> 0 Then
      '上使能
      '02 00 00 02 0A 00 00 00 00 00 00 00 00   '控制字清0
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0000" )       '獲得完整can報文
      Send Can_code                                         '發數據
      Waitms 5
      Select Case Ctrl_mode
      Case Is = Position_mode
        '01 00 00 03 0A 01 00 00 00 00 00 00 00    '速度模式
        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "01" )       '獲得完整can報文
        Send Can_code                                       '發數據
        Waitms 5
      Case Is = Speed_mode
        '01 00 00 03 0A 03 00 00 00 00 00 00 00    '位置模式
        Can_code = Can_data(std_frame , 1 , "03" , Node_id , "03" )       '獲得完整can報文
        Send Can_code                                       '發數據
        Waitms 5
      End Select
      '02 00 00 02 0A 06 00 00 00 00 00 00 00   '始能第一步
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0600" )       '獲得完整can報文
      Send Can_code                                         '發數據
      Waitms 5
      '02 00 00 02 0A 07 00 00 00 00 00 00 00   '始能第二步
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0700" )       '獲得完整can報文
      Send Can_code                                         '發數據
      Waitms 5
      '02 00 00 02 0A 0F 00 00 00 00 00 00 00   '始能第三步
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0F00" )       '獲得完整can報文
      Send Can_code                                         '發數據
      Waitms 5
   Else
      '掉使能02 00 00 02 0A 05 00 00 00 00 00 00 00
      Can_code = Can_data(std_frame , 2 , "02" , Node_id , "0500" )       '獲得完整can報文
      Send Can_code                                         '發數據
   End If
End Sub

評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 色综合成人网 | 国产精品不卡视频 | 欧美freesex黑人又粗又大 | 国产乱码精品一区二区三区中文 | 狠狠爱视频 | 中文字幕一区二区三区四区五区 | 国产精品毛片在线 | 欧美精品一区在线发布 | 久久影院一区 | 色伊人久久 | 国产一区二区三区在线免费 | 精品福利在线 | 精品免费国产视频 | 国产精品国产精品国产专区不卡 | 一级免费看片 | 午夜网| 欧美一级毛片在线播放 | 欧美日韩视频在线第一区 | 毛片免费在线 | 密室大逃脱第六季大神版在线观看 | 天堂资源 | 欧美日韩免费 | 欧美一区二区三区久久精品 | 欧美精品一区在线发布 | 伊人网站 | 免费同性女女aaa免费网站 | 久久久久中文字幕 | 国产精品久久久久久久岛一牛影视 | 成人免费共享视频 | 拍真实国产伦偷精品 | 91丨九色丨国产在线 | 亚洲一区| 亚洲区一区二区 | 久久av一区二区三区 | 欧美在线a | 99re在线视频观看 | 午夜影院网站 | 成人亚洲| 婷婷二区| 精品自拍视频在线观看 | 亚洲国产成人在线观看 |