Átírtam Bascomra a belinkelt programot!
De kipróbálni nem tudom, mert nincsen HM55B digitális iránytűm
Megjegyzésben ott hagytam az eredeti, hogy legyen mivel összehasonlítani.
Pi-t MÉG pontosítani kel!
Kód: Egész kijelölése
'Hitachi_HM55B.bas
'( /*
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
Htachi Hm55b Compass
Parallax(#)
Author : Kiilo Kiilo@kiilo.org
License : Http : / / Creativecommons.org / Licenses / By -nc -sa / 2.5 / Ch/
Http : / / Parallax.com / Store / Microcontrollers / Basicstampmodules / Tabid / 134 / Txtsearch / Hm55b / List / 1 / Productid / 98 / Default.aspxPrint Sortfield = Productname%2cproductname
Http : / / Sage.medienkunst.ch / Tiki -index.phpPrint Page = Howto_arduino_parallax_hm55b_kompass
Http : / / Arduino.cc / Playground / Hm55b
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
* /
#include < Math.h > / /(no Semicolon)
/ / / / Vars
')
$regfile = "m16def.dat"
$crystal = 14745600
$baud = 115200
$hwstack = 32
$swstack = 32
$framesize = 32
'$sim
'(
Void Setup() {
Serial.begin(115200);
Pinmode(en_pin , Output);
Pinmode(clk_pin , Output);
Pinmode(dio_pin , Input);
')
Config Portd.4 = Output 'byte CLK_pin = 8;
Config Portd.5 = Output 'byte EN_pin = 9;
Config Portd.6 = Input 'byte DIO_pin = 10;
Clk_pin Alias Portd.4
En_pin Alias Portd.5
Dio_pin Alias Portd.6
Dim X_data As Single ' int X_Data = 0;
Dim Y_data As Single ' int Y_Data = 0;
Dim Angle As Word 'Single ' int angle;
Dim Angle_t As Single 'Single
Dim Result As Single
Result = 0
'//// FUNCTIONS
Declare Sub Shiftout_f(byval Value As Single , Byval Bitscount As Word)
Declare Function Shiftin_f(byval Bitscount As Word) As Single
Declare Sub Hm55b_reset()
Declare Sub Hm55b_startmeascommand()
Declare Function Hm55b_readcommand() As Single
Call Hm55b_reset()
Angle_t = Hm55b_readcommand()
Print Angle_t
Do
Call Hm55b_startmeascommand()
Waitms 40
Angle_t = Hm55b_readcommand()
Print Angle_t ;
Print " ";
X_data = Shiftin_f(11)
Y_data = Shiftin_f(11)
Print X_data ; ' // print X strength
Print " " ;
Print Y_data '// print Y strength
En_pin = 1 ' HIGH); // ok deselect chip
' var = ATN2( y, x )
'Angle = 180 *(atan2( -1 * Y_data , X_data) / M_pi) '; // angle is atan( -y/x) !!!
Angle_t = -1 * Y_data
Angle_t = Atn2(angle_t , X_data)
Angle_t = 180 * Angle_t
Angle = Angle_t / 3.14 'Pi-t MÉG pontosítani kel!
Print Angle
Loop
'(
}
Void Loop() {
Hm55b_startmeasurementcommand() ; / / Necessary!!
Delay(40) ; / / The Data Is 40ms Later Ready
Serial.print(hm55b_readcommand()) ; / / Read Data And Print Status
Serial.print( " ");
X_data = Shiftin(11) ; / / Field Strength In X
Y_data = Shiftin(11) ; / / And Y Direction
Serial.print(x_data) ; / / Print X Strength
Serial.print( " ");
Serial.print(y_data) ; / / Print Y Strength
Serial.print( " ");
Digitalwrite(en_pin , High) ; / / Ok Deselect Chip
Angle = 180 *(atan2( -1 * Y_data , X_data) / M_pi) ; / / Angle Is Atan( -y / X) !!!
Serial.print(angle) ; / / Print Angle
Serial.println( "");
}
')
Sub Shiftout_f(byval Value As Single , Byval Bitscount As Word)
Config Dio_pin = Output
En_pin = 0 'Low
Waitus 1
'SHIFTOUT pin , pclock , var , option [, bits , delay ]
Shiftout Dio_pin , Clk_pin , Value , 0 , Bitscount , 1
Waitus 1
En_pin = 1
End Sub
'(
Void Shiftout(int Value , Int Bitscount) {
For(int I = Bitscount ; I > ; = 0 ; I - -) {
Digitalwrite(clk_pin , Low);
If((value & ; 1 < ; < ; I) ==(1 < ; < ; I)) {
Digitalwrite(dio_pin , High);
/ / Serial.print(&Quot ; 1&Quot ; );
}
Else {
Digitalwrite(dio_pin , Low);
/ / Serial.print(&Quot ; 0&Quot ; );
}
Digitalwrite(clk_pin , High);
Delaymicroseconds(1);
}
/ / Serial.print(&Quot ; " ; );
}
')
Function Shiftin_f(byval Bitscount As Word) As Single
Result = 0
Config Dio_pin = Input
'SHIFTIN pin , pclock , var , option [, bits , delay ]
En_pin = 0 'Low
Waitus 1
Shiftin Dio_pin , Clk_pin , Result , 0 , Bitscount , 1
Waitus 1
En_pin = 1
Shiftin_f = Result
End Function
'(
Int {
Int Shiftin_result;
Shiftin_result = 0;
Pinmode(dio_pin , Input);
For(int I = Bitscount ; I >= 0 ; I - -) {
Digitalwrite(clk_pin , High);
Delaymicroseconds(1);
If(digitalread(dio_pin) == High) {
Shiftin_result =(shiftin_result << 1) + 1;
/ / Serial.print( "x");
}
Else {
Shiftin_result =(shiftin_result << 1) + 0;
/ / Serial.print( "_");
}
Digitalwrite(clk_pin , Low);
Delaymicroseconds(1);
}
/ / Serial.print( ":");
/ / Below Is Difficult To Understand:
/ / If Bit 11 Is Set The Value Is Negative
/ / The Representation Of Negative Values You
/ / Have To Add B11111000 In The Upper Byte Of
/ / The Integer.
/ / See : Http : / / En.wikipedia.org / Wiki / Two%27s_complement
If((shiftin_result & 1 << 11) == 1 << 11) {
Shiftin_result =(b11111000 << 8) | Shiftin_result;
}
Return Shiftin_result;
}
')
Sub Hm55b_reset()
Call Shiftout_f(&B0000 , 4)
End Sub
'(
Void Hm55b_reset() {
Pinmode(dio_pin , Output);
Digitalwrite(en_pin , Low);
Shiftout(b0000 , 3);
Digitalwrite(en_pin , High);
}
')
Sub Hm55b_startmeascommand()
Call Shiftout_f(&B1000 , 4)
End Sub
'(
Void Hm55b_startmeasurementcommand() {
Pinmode(dio_pin , Output);
Digitalwrite(en_pin , Low);
Shiftout(b1000 , 3);
Digitalwrite(en_pin , High);
}
')
Function Hm55b_readcommand() As Single
Call Shiftout_f(&B1100 , 4)
Hm55b_readcommand = Shiftin_f(4)
End Function
'(
Int Hm55b_readcommand() {
Int Result = 0;
Pinmode(dio_pin , Output);
Digitalwrite(en_pin , Low);
Shiftout(b1100 , 3);
Result = Shiftin(3);
Return Result;
}
')
End
Valaki átnézhetné működőképes e?
