?? 減速電機上控制.txt
字號:
用可變電阻器+減速電機做伺服電機
$regfile = "m8def.dat" '目標單片機為ATmega48
$baud = 9600
$crystal = 8000000 '使用內部8M晶振
' Config Serialout = Buffered , Size = 20
$prog &HFF , &HE2 , &HDF , &HFF '單片機熔絲位參數設置:禁止系統時鐘8分頻
Config Adc = Single , Prescaler = Auto , Reference = Avcc
'設置ADC為單次轉換模式,采樣頻率有編譯器自動選擇,參考電壓使用AVcc
'下降觸發
Config Timer1 = Timer , Prescale = 1024 , Capture Edge = Falling , Noice Cancel = 1
'計時器1計時
Dim A As Byte
Dim W As Integer
Dim X As Integer
Dim M As Integer
Dim S As Word
Dim R As Integer
Dim Q As Long
Dim F As Long
Dim I As Long
Dim C As Byte
'下降沿中斷開
'計時器1中斷開
On Icp1 Icp_isr
'使能
Enable Capture1
Enable Interrupts
Timer1 = 0
'裝入計時器初值
Start Timer1
Ddrb = &B00000010
Ddrd.6 = 1
Ddrd.7 = 1
Portd = Portd And &B00000000
Start Adc '啟動ADC轉換,adc電阻與一個直流減速電機同軸
Q = 0
F = 0'可為預先設頻率355
W = 0
S = 0
Do
Q = Getadc(0)
Print Q
If W = 0 Then '測得兩次的pwm值,當小于 65536(timer1溢出)時,測得值+65536-舊值,否則新值-舊值
s=w
Else
If S = W Then Goto Lp
If S > W Then
S = 65536 - S
F = S + W
Else
F = S - W
End If
W = 0
end if
lp:
If Q <> F Then
If Q > F Then
Reset Portd.6
Set Portd.7
End If
If Q < F Then '設置PD6,7口為輸出驅動電機,伺服電定位于指定角度
Reset Portd.7
Set Portd.6
End If
End If
Loop
End
Icp_isr:
S = Capture1
Return
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -