Friday, May 27, 2011

Stepper motor Control with Atmega16

Bascom Code

$regfile = "m16def.dat"
$crystal = 1000000

Config Kbd = Portb
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , Rs = Portc.0 , E = Portc.1
Config Portd = Output
Config Porta = Input

Dim A As Byte , B As Byte , Key As Byte , C As Byte , D As Byte , E As Byte

Cls
Cursor Off

B = 0
D = 1

Q:
Locate 1 , 1
Lcd "Direction:"
Locate 2 , 1
Lcd "Speed:"
Do
'************Read Keyboard
A = Getkbd()
If A > 15 Then
Goto Q
Else
Key = Lookup(a , Dta)
If Key <= 9 Then
B = B * 10
B = B + Key
'*************Direction
If D = 0 Then Lcd B
Waitms 270
Elseif Key = 10 Then
Locate 1 , 11
Lcd "Left"
C = 1
D = 0
B = 0

Elseif Key = 11 Then
Locate 1 , 11
Lcd "Right"
C = 2
D = 0
B = 0
'****************reset
Elseif Key = 13 Then
Cls
D = 1
B = 0
Portd.0 = 0
Portd.1 = 0
Waitms 50
Goto Q
'****************Enter key
Elseif Key = 14 Then
If D = 0 Then
Select Case C
Case 1 : Gosub R
Case 2 : Gosub T
End Select

End If
End If

End If

Wait 1

Loop

End

Dta:
Data 7 , 8 , 9 , 10 , 4 , 5 , 6 , 11 , 1 , 2 , 3 , 12 , 13 , 0 , 14 , 15

R:
Do
Portd = &B00000001 : Waitms B
Portd = &B00000011 : Waitms B
Portd = &B00000010 : Waitms B
Portd = &B00000110 : Waitms B
Portd = &B00000100 : Waitms B
Portd = &B00001100 : Waitms B
Portd = &B00001000 : Waitms B
Portd = &B00001001 : Waitms B
If Pina.0 = 1 Then
Locate 2 , 1
Lcd "Speed:"
B = 0
Lcd B ; " "
Return
End If

Loop

T:
Do
Portd = &B00001001 : Waitms B
Portd = &B00001000 : Waitms B
Portd = &B00001100 : Waitms B
Portd = &B00000100 : Waitms B
Portd = &B00000110 : Waitms B
Portd = &B00000010 : Waitms B
Portd = &B00000011 : Waitms B
Portd = &B00000001 : Waitms B
If Pina.0 = 1 Then
Locate 2 , 1
Lcd "Speed:"
B = 0
Lcd B ; " "
Return
End If

Loop

No comments:

Post a Comment

Terima kasih atas komentar yang anda sampaikan , sehingga dapat menambah wawasan saya sebagai penulis dan membuat blog ini semakin berguna banyak orang