征得陈MM的同意,在此公布原理图和BASCOM的源程序。
源程序(BASCOM-AVR 1.11.8.1编译通过):
鄙人的作品,自我感觉程序风格还是比较清晰流畅易懂的……(在此省略N个赞美之词)看不懂请跟帖提问,哈哈!
'********************************** 编译器设置 *********************************
$regfile = "M8def.dat"
$crystal = 7372800
$hwstack = 32
$swstack = 32
$framesize = 32
'*********************************** 定义常量 **********************************
Const Car_forward = &B00000110
Const Car_back = &B00001001
Const Car_left = &B00000101
Const Car_right = &B00001010
Const Car_stop = &B00001111
'********************************* 指定端口别名 ********************************
Voc_sck Alias Portc.4
Voc_dat Alias Portc.5
'******************************** 声明自定义过程 *******************************
Declare Sub Chip_init()
Declare Sub Voc_play(byval Command As Byte)
Declare Sub Car_go(byval Type As Byte , Byval Speed As Byte)
'******************************** 声明自定义函数 *******************************
Declare Function Get_speed() As Byte
Declare Function Get_command() As Byte
'********************************* 声明全局变量 ********************************
Dim Main_speed As Byte , Car_state As Byte
'************************************ 主程序 ***********************************
Call Chip_init()
Wait 1
Call Voc_play(&H08)
Do
Main_speed = Get_speed()
Select Case Get_command()
Case &H00:
Call Voc_play(&H00)
Case &H01:
Call Car_go(1 , Main_speed)
Call Voc_play(&H01)
Car_state = 1
Case &H02:
Call Car_go(2 , Main_speed)
Call Voc_play(&H02)
Car_state = 1
Case &H03:
Main_speed = Main_speed / 2
Call Car_go(3 , Main_speed )
Call Voc_play(&H03)
Car_state = 0
Case &H04:
Main_speed = Main_speed / 2
Call Car_go(4 , Main_speed )
Call Voc_play(&H04)
Car_state = 0
Case &H05:
Call Car_go(0 , 0)
If Car_state = 1 Then
Call Voc_play(&H09)
Waitms 800
End If
Car_state = 0
Call Voc_play(&H05)
Case &H08:
Call Voc_play(&H08)
Waitms 200
Case &H09:
Call Voc_play(&H07)
Waitms 200
Case &HFF:
$asm
Nop;
$end Asm
Case Else :
Call Car_go(0 , 0)
Call Voc_play(&H06)
End Select
Loop
'********************************** 自定义过程 *********************************
Sub Voc_play(byval Command As Byte)
Local I As Byte , Temp As Byte
Command = Command + &H80
Waitms 50
For I = 1 To 8
Reset Voc_sck
Temp = Command And 1
If Temp = 1 Then
Set Voc_dat
Else
Reset Voc_dat
End If
Rotate Command , Right
Waitus 10
Set Voc_sck
Waitus 10
Next I
End Sub
Sub Chip_init()
Portb = &B11111001
Portc = &B11111111
Portd = &B11111111
Ddrb = &B00000110
Ddrc = &B00111111
Ddrd = &B00000000
Config Rc5 = Pinb.0
Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 256
Start Timer1
Enable Interrupts
End Sub
Sub Car_go(byval Type As Byte , Byval Speed As Byte)
Select Case Type
Case 0:
Portc = &B00001111 And Car_stop
Case 1:
Portc = &B00001111 And Car_forward
Case 2:
Portc = &B00001111 And Car_back
Case 3:
Portc = &B00001111 And Car_left
Case 4:
Portc = &B00001111 And Car_right
End Select
Pwm1a = Speed
Pwm1b = Speed
End Sub
'********************************** 自定义函数 *********************************
Function Get_speed() As Byte
Local Speed As Byte
Speed = Pind
Rotate Speed , Right , 4
Speed = Speed And &B00001111
Get_speed = Speed * 17
End Function
Function Get_command() As Byte
Local Address As Byte , Command As Byte
Getrc5(address , Command)
If Address = 0 Then Command = Command And &B01111111
Get_command = Command
End Function