Hallo
Nachdem der "Bug" entfernt ist sieht es nun richtig klasse aus:
Bild hier
http://www.youtube.com/watch?v=KKtTXcCFqGo
Somit würde ich sagen: Ziel erreicht. Das sollte auch mit einem Mega8 funktionieren :)
Hier noch der finale Code:
Code:
'WII-Nunchuk mit Bascom auslesen. Grundgerüst des Codes von Linux_80 7.8.08 mic
'https://www.roboternetz.de/phpBB2/viewtopic.php?p=389309
$regfile = "M32def.dat" ' RP6 mit Mega32
$crystal = 8000000 ' taktet mit 8MHz
$baud = 38400 ' Loader-Baud
$lib "i2c_twi.lbx" ' Für Hardware TWI
Config Scl = Portc.0 ' Ports fuer IIC-Bus
Config Sda = Portc.1
Config Twi = 400000 ' Init TWBR und TWSR
I2cinit
' TWI gleich einschalten, das macht Bascom ansonsten erst beim I2CStart !
Twcr = &B00000100 ' nur TWEN setzen
Const Nunchuck_write = &HA4 ' Slaveadresse
Const Nunchuck_read = &HA5
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portb.7 , Db7 = Portb.1 , E = Portb.0 , Rs = Portc.6
Config Lcd = 20 * 4
Deflcdchar 0 , 63 , 63 , 63 , 63 , 63 , 63 , 63 , 63
Cls 'clear the LCD display
Cursor Off Noblink
Lcd " RP6 ROBOT SYSTEM" 'display this at the top line
Config Servos = 2 , Servo1 = Porta.0 , Servo2 = Porta.1 , Reload = 10
Servo(1) = 90
Servo(2) = 85
Config Porta.0 = Output
Config Porta.1 = Output
Enable Interrupts
Dim A As Byte
Dim Tmp As Byte
Dim Pos_x As Byte
Dim Pos_y As Byte
Dim Acc_x As Byte
Dim Acc_y As Byte
Dim Acc_z As Byte
Dim Buttons As Byte
Dim Tasten As Byte
Dim Stick As Byte
' Startausgabe
Print
Print "I2C-TWI Demo mit Wii Nunchuck"
Print "Ausgabe auf dem LCD beachten"
Gosub Nunchuck_init
Waitms 1000
Gosub Nunchuk_read
Cls
'Lcd " WII-Nunchuk am RP6"
Stick = 1
Do
'(
Gosub Getkeys
Locate 1 , 1
Lcd Bin(tasten)
')
Gosub Nunchuk_read
'Print Hex(pos_x) ; " " ; Hex(pos_y)
Locate 2 , 1
Lcd Hex(pos_x)
Lcd " "
Lcd Hex(pos_y)
Locate 2 , 19
If Buttons.1 = 0 Then
Lcd "C"
Else
Lcd "c"
End If
If Buttons.0 = 0 Then
Lcd "Z"
Else
Lcd "z"
End If
Locate 3 , 1
Lcd "Accs X: " ; Hex(acc_x) ; " y: " ; Acc_y;
Locate 4 , 6
Lcd "Z: " ; Acc_z
'(
Locate 2 , 1
Lcd "X: " ; Bin(acc_x) ; " " ; Acc_x ; " "
Locate 3 , 1
Lcd "Y: " ; Bin(acc_y) ; " " ; Acc_y ; " "
Locate 4 , 1
Lcd "Z: " ; Bin(acc_z) ; " " ; Acc_z ; " "
')
If Buttons.0 = 0 Then Stick = 0
If Buttons.1 = 0 Then Stick = 1
If Stick = 0 Then
Servo(1) = Acc_y - 40
Servo(2) = Acc_x - 40
Else
Servo(1) = Pos_y - 40
Servo(2) = Pos_x - 40
End If
'Waitms 10
Loop
End
Nunchuck_init:
I2cstart
I2cwbyte Nunchuck_write
I2cwbyte &H40 ' sends memory address
I2cwbyte &H00 ' sends sent a zero.
I2cstop
Return
Nunchuk_read:
Dim Buffer(6) As Byte
I2cstart
I2cwbyte Nunchuck_write
I2cwbyte &H00 ' sends one byte
I2cstop
Waitms 1
Buffer(1) = 0
I2creceive Nunchuck_read , Buffer(1) , 0 , 6
Tmp = Buffer(1) Xor &H17
Tmp = Tmp + &H17
Pos_x = Tmp
Tmp = Buffer(2) Xor &H17
Tmp = Tmp + &H17
Pos_y = Tmp
Tmp = Buffer(3) Xor &H17
Tmp = Tmp + &H17
Acc_x = Tmp
Tmp = Buffer(4) Xor &H17
Tmp = Tmp + &H17
Acc_y = Tmp
Tmp = Buffer(5) Xor &H17
Tmp = Tmp + &H17
Acc_z = Tmp
Tmp = Buffer(6) Xor &H17
Tmp = Tmp + &H17
Buttons = Tmp
'(
If Buttons.2 = 1 Then Set Acc_x.0 ' Die acc-LSB einlesen
If Buttons.3 = 1 Then Set Acc_x.1
If Buttons.4 = 1 Then Set Acc_y.0
If Buttons.5 = 1 Then Set Acc_y.1
If Buttons.6 = 1 Then Set Acc_z.0
If Buttons.7 = 1 Then Set Acc_z.1
')
Return
Getkeys:
' Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portb.7 , Db7 = Portb.1 , E = Portb.0 , Rs = Portc.6
Tasten = 0
Config Pinc.4 = Input
Config Pinc.5 = Input
Config Pinb.7 = Input
Config Pinb.1 = Input
Config Pinc.6 = Input
Set Portc.4
Set Portc.5
Set Portb.7
Set Portb.1
Set Portc.6
If Pinc.4 = 0 Then Set Tasten.3
If Pinc.5 = 0 Then Set Tasten.2
If Pinb.7 = 0 Then Set Tasten.1
If Pinb.1 = 0 Then Set Tasten.0
If Pinc.6 = 0 Then Set Tasten.4
Reset Portc.4
Reset Portc.5
Reset Portb.7
Reset Portb.1
Reset Portc.6
Config Pinc.4 = Output
Config Pinc.5 = Output
Config Pinb.7 = Output
Config Pinb.1 = Output
Config Pinc.6 = Output
Return
Damit beende ich mein kleines Bascom-Gastspiel und verkrümmel mich wieder in meine C-Ecke. Viel Spass und Erfolg beim Nachbau.
Gruß
mic
Lesezeichen