Das Forum für den durchgeknallten Bastler
https://www.fingers-welt.de/phpBB/
Da müsste ich doch wen dich die interne referenz messe und und an der externen unterschiedliche Spannungen anlege unterschiedliches messen können, oder?ATmega1284P features an internal bandgap refer
ence. This reference is used for Brown-out
Detection, and it can be used as an input to the Analog Comparator or the ADC
Code: Alles auswählen
inline static void HW_InitIoPins(void)
{
DDRB = 0xFF;
}
Code: Alles auswählen
inline static void HW_InitIoPins(void)
{
41c: cf 93 push r28
41e: df 93 push r29
420: cd b7 in r28, 0x3d ; 61
422: de b7 in r29, 0x3e ; 62
DDRB = 0xFF;
424: 87 e3 ldi r24, 0x37 ; 55
426: 90 e0 ldi r25, 0x00 ; 0
428: 2f ef ldi r18, 0xFF ; 255
42a: fc 01 movw r30, r24
42c: 20 83 st Z, r18
}
42e: df 91 pop r29
430: cf 91 pop r28
432: 08 95 ret
Code: Alles auswählen
int main (void)
{
DDRB = 0xFF;
}
Code: Alles auswählen
424: 87 e3 ldi r24, 0x37 ; 55 <- 0x37 in RegisterPAAR Laden (lowbyte)
426: 90 e0 ldi r25, 0x00 ; 0 <- 0x0 in RegisterPAAR Laden (highbyte)
428: 2f ef ldi r18, 0xFF ; 255 <- deine 255 in ein Register packen
42a: fc 01 movw r30, r24 <- Registerpaare kopieren (r24 nach r30 und r25 nach r31)
42c: 20 83 st Z, r18 <- Z Pointerregister ist r31/r30 und die 255 dort hinschreiben
Code: Alles auswählen
inline static void HW_InitIoPins(void)
{
DDRB = (1<<PB4) | (1<<PB1);
PORTB = (1<<PB2); //Pull-Up
}
Code: Alles auswählen
PORTB |= (1<<PB4);
Code: Alles auswählen
*(volatile uint8_t *)(0x17)
Code: Alles auswählen
inline static void HW_InitTimer1ForPwm(void)
{
TCCR1 = (1<<CS11) | (1<<CS10); //Prescaler PCK/2
GTCCR = (1<<PWM1B) //Pulse Width Modulator B Enable
| (1<<COM1B1); //Clear the OC1B output line.
}
Code: Alles auswählen
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(6, 9, 5, 2, 3, 4);
//Define the LED Pin
#define PIN_OUT 13
//Define unit length in ms
#define UNIT_LENGTH 100
//Build a struct with the morse code mapping
static const struct {const char letter, *code;} MorseMap[] =
{
{ 'A', ".-" },
{ 'B', "-..." },
{ 'C', "-.-." },
{ 'D', "-.." },
{ 'E', "." },
{ 'F', "..-." },
{ 'G', "--." },
{ 'H', "...." },
{ 'I', ".." },
{ 'J', ".---" },
{ 'K', ".-.-" },
{ 'L', ".-.." },
{ 'M', "--" },
{ 'N', "-." },
{ 'O', "---" },
{ 'P', ".--." },
{ 'Q', "--.-" },
{ 'R', ".-." },
{ 'S', "..." },
{ 'T', "-" },
{ 'U', "..-" },
{ 'V', "...-" },
{ 'W', ".--" },
{ 'X', "-..-" },
{ 'Y', "-.--" },
{ 'Z', "--.." },
{ ' ', " " }, //Gap between word, seven units
{ '1', ".----" },
{ '2', "..---" },
{ '3', "...--" },
{ '4', "....-" },
{ '5', "....." },
{ '6', "-...." },
{ '7', "--..." },
{ '8', "---.." },
{ '9', "----." },
{ '0', "-----" },
{ '/', "–..-." },
{ '.', ".–.–.–" },
{ ',', "--..--" },
{ '?', "..--.." },
{ '!', "-.-.--" },
{ ':', "---..." },
{ ';', "-.-.-." },
{ '(', "-.--." },
{ ')', "-.--.-" },
{ '"', ".-..-." },
{ '@', ".--.-." },
{ '&', ".-..." },
};
void setup()
{
pinMode( PIN_OUT, OUTPUT );
digitalWrite( PIN_OUT, LOW );
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
}
void loop()
{
// set the cursor to (16,1):
lcd.setCursor(16, 1);
// set the display to automatically scroll:
lcd.autoscroll();
String morseWord = encode( "MORSEBAKE TEST " );
for(int i=0; i<=morseWord.length(); i++)
{
switch( morseWord[i] )
{
case '.': //dit
digitalWrite( PIN_OUT, HIGH );
// tone(8,2500);
delay( UNIT_LENGTH );
// noTone(8);
digitalWrite( PIN_OUT, LOW );
delay( UNIT_LENGTH );
break;
case '-': //dah
digitalWrite( PIN_OUT, HIGH );
// tone(8,2500);
delay( UNIT_LENGTH*3 );
digitalWrite( PIN_OUT, LOW );
// noTone(8);
delay( UNIT_LENGTH );
break;
case ' ': //gap
delay( UNIT_LENGTH );
}
}
}
String encode(const char *string)
{
size_t i, j;
String morseWord = "";
for( i = 0; string[i]; ++i )
{
for( j = 0; j < sizeof MorseMap / sizeof *MorseMap; ++j )
{
if( toupper(string[i]) == MorseMap[j].letter )
{
morseWord += MorseMap[j].code;
break;
}
}
morseWord += " "; //Add tailing space to seperate the chars
}
return morseWord;
}
String decode(String morse)
{
String msg = "";
int lastPos = 0;
int pos = morse.indexOf(' ');
while( lastPos <= morse.lastIndexOf(' ') )
{
for( int i = 0; i < sizeof MorseMap / sizeof *MorseMap; ++i )
{
if( morse.substring(lastPos, pos) == MorseMap[i].code )
{
msg += MorseMap[i].letter;
}
}
lastPos = pos+1;
pos = morse.indexOf(' ', lastPos);
// Handle white-spaces between words (7 spaces)
while( morse[lastPos] == ' ' && morse[pos+1] == ' ' )
{
pos ++;
}
}
// turn off automatic scrolling
lcd.noAutoscroll();
// clear screen for the next loop:
// lcd.clear();
return msg;
}
Das wird relativ schwierig, da du erst den Text in Morsecode umwandelst und dann mit der for schleife ausgibst. Man müsste das so umbauen, dass mit encode immer nur der aktuelle Buchstabe in einer for-Schleife umgewandelt wird. Habe das mal grob skizziert, hoffentlich einigermaßen verständlich. Word ist dein eingabetext. Ob der Code jetzt genauso funktioniert, keine AhnungTDI hat geschrieben:
Leider bin ich prog-technisch noch zu grün hinter den Ohren um im Code erkennen zu können, an welcher Stelle ich wie den Buchstaben abgreife, der gerade in Morsecode ausgegeben wird.
TDI
Code: Alles auswählen
for(int i=0; i<=word.length(); i++ )
{
String morseCharacter = encode(word[i])
for(int j=0; j<=morseCharacter.length(); j++ )
{
switch( morseCharacter[j] )
{
case '.': //dit
digitalWrite( PIN_OUT, HIGH );
// tone(8,2500);
delay( UNIT_LENGTH );
// noTone(8);
digitalWrite( PIN_OUT, LOW );
delay( UNIT_LENGTH );
break;
case '-': //dah
digitalWrite( PIN_OUT, HIGH );
// tone(8,2500);
delay( UNIT_LENGTH*3 );
digitalWrite( PIN_OUT, LOW );
// noTone(8);
delay( UNIT_LENGTH );
break;
case ' ': //gap
delay( UNIT_LENGTH );
}
}
Könnte das an dem "Sample and Hold" liegen, was auf einem Kondensator beruht, der an der zu messenden Spannung anliegt, und bei der Messung davon abgeklemmt wird, um während der Digitalisierung einen stabilen Wert zu haben? Wenn dann noch ein Analogmuliplexer vorgeschaltet ist, um ggf. an mehreren Eingänge zu messen, ist es notwendig, diesen Kondensator auf die entsprechende Spannung aufzuladen, bevor die Messung beginnt. Die Auf/Entladung des Kondensators dauert aufgrund des Quellwiderstands natürlich ein wenig... -> Wenn du nur eine (sich sehr langsam ändernde) Spannung messen willst, ist der hochohmige Wert ok, aber wenn du möchtest, daß ein paar µs nach der Umschaltung des Eingangs die korrekte Spannung ansteht, brauchst du natürlich "etwas mehr Dampf" aus der QuelleProfipruckel hat geschrieben:Anderweitig wird geschrieben, man solle diese mit Quellwiderständen nicht über 10 kOhm betreiben.
Danke für Deine (einzige) Antwort - ich habe das im Datenblatt gefunden, auf Seite 244 von 650:xoexlepox hat geschrieben:Könnte das an dem "Sample and Hold" liegen, was auf einem Kondensator beruht, ..Profipruckel hat geschrieben:Anderweitig wird geschrieben, man solle diese mit Quellwiderständen nicht über 10 kOhm betreiben.
Ich weiß nicht, wie sich die Arduino-Umgebung da verhält bzw. was hinter "analogRead" stattfindet. Einen Zugriff auf die Wandlungszeit oder das Ready-Bit sehe ich dort nicht.Wenn du nur eine (sich sehr langsam ändernde) Spannung messen willst, ist der hochohmige Wert ok, aber wenn du möchtest, daß ein paar µs nach der Umschaltung des Eingangs die korrekte Spannung ansteht,
Hmmm, ich habe mich bisher nur recht oberflächlich mit den ATMELs beschäftigt (und die Arduino-Libs sofort "abgeklemmt") , gehe aber mal davon aus, daß du die Funtion des "AnalogRead" auch "zu Fuß" (mit Zugriffen auf die Registerbits) realisieren kannst. Beim PIC gibt es nur ein Bit, welches beim Start der A/D-Wandlung gesetzt wird, und beim Ende der Wandlung wieder gelöscht wird (und ggf. einen Interrupt auslöst). Wie lange es dauert, den S&H-Kondensator zu laden, "weiss" die Hardware natürlich nichtProfipruckel hat geschrieben:Einen Zugriff auf die Wandlungszeit oder das Ready-Bit sehe ich dort nicht.
Das könnte bei der Umschaltung von A/D-Eingängen nützlich sein, aber der externe Kondensator muss natürlich auch von der hochohmigen Quelle auf die korrekte Spannung gebracht werdenBei hochohmigem Teiler einen Kondensator drauf, damit der AVR zügig messen kann.
Wenn du keine anderen Analogeingänge verwendest, und somit den Analogmultiplexer nicht verwendest, kannst du es m.E. durchaus hochohmig belassen.Ich will alle x Minuten die Spannung eines Akkus messen, ohne deutlich zu dessen Entladung beizutragen, habe also Zeit.
In meinem ursprünglichen Entwurf habe ich das so. Es ist eine Rechenaufgabe: mA nur für die Messung oder µA dauerhaft.Fritzler hat geschrieben:Da hilft aber nicht hochohmig, sondern den Spannungsteiler über nen PFET abklemmen wenn der nicht gebraucht wird.
"Arduino analogread" habe ich nicht wirklich verstanden. In meiner Lötstation werfe ich den ersten Meßwert weg, weil der sporadisch grob falsch ist. Dann hole ich mir zehn weitere im Abstand von je einer ms und rechne den Mittelwert - passt.Zu dem Arduino analogread Mumpitz sag ich jetz mal nix... (natürlich haben die daran nicht gedacht)
Na ja, auch das Roboternetz macht mich nicht erheblich schlauer. Mit Deinem Beitrag 21.07.2016, 16:29 bringst Du den großen Kondensator ins Spiel, eine klare Aussage zum Meßfehler sehe ich aber auch dort nicht.BMS hat geschrieben:Oder hochohmig + Kondensator vom ADC Eingang nach GND. Oder CD4066 als Analogschalter.
Vor kurzem im Roboternetz
(bin dort auch als BMS unterwegs)
Grüße, Bernhard
Code: Alles auswählen
//Sitzheizung_rechts
if (digitalRead(sitz_rechts_on)){
val = analogRead (sitz_rechts_out);
analogWrite (sitz_rechts, val /4);
}else{
digitalWrite(sitz_rechts, LOW);
Code: Alles auswählen
val=1034-val;
Code: Alles auswählen
//Sitzheizung_rechts
if (digitalRead(sitz_rechts_on)){
val = analogRead (sitz_rechts_out);
analogWrite (sitz_rechts, val=1023-val/4+25);
}else{
digitalWrite(sitz_rechts, LOW);
Code: Alles auswählen
/*============================ EG LABS ===================================//
Demonstration on how to use PS2 MOUSE with an arduino board
The circuit:
LCD:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 7
* LCD D5 pin to digital pin 6
* LCD D6 pin to digital pin 5
* LCD D7 pin to digital pin 4
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD pin 3
* LED anode attached to digital output 9
* LED cathode attached to ground through a 1K resistor
MOUSE:
DATA PIN TO PIN NUMBER 8
CLOCK PIN TO PIN NUMBER 3
============================== EG LABS ===================================*/
#include "PS2Mouse.h"
#define MOUSE_DATA 8
#define MOUSE_CLOCK 3
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM); // initializing the PS2 library
int data[2];
int led = 9;
void setup()
{
pinMode(led, OUTPUT);
lcd.begin(16, 2);
lcd.print("ENGINEERS GARAGE");
lcd.setCursor(0, 1);
lcd.print(" PS2 MOUSE ");
delay(2000);
lcd.clear();
Serial.begin(38400);
mouse.initialize(); // initializing the PS2 mouse connected with the Arduino
digitalWrite(led, HIGH);
}
void loop()
//--------------------------------------------------------------------------------------------------------------------------------
//----------------------------------------Hier meine bescheidenen und sinnlose Versuche----------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
{
mouse.report(data); // get data from the mouse
int wertx; //zusätzliche variable für data1 definiert
int werty; //zusätzliche variable für data2 definiert
if((data[1] != 0) || (data[2] != 0) || (data[0] != 8))
{
lcd.clear();
Serial.print("X = "); //ab 38000baud darstellung richtig
lcd.print("X=");
Serial.print( data[1]); // X Movement Data
lcd.print( wertx + data[1]); //einfach + wertx geht nicht ??
lcd.setCursor(0, 1);
Serial.print(", Y = ");
lcd.print("Y=");
Serial.print(data[2]); // Y Movement Data
lcd.print(data[2]);
if(data[0] == 9)
Serial.print(", LEFT BUTTON"); // Status Byte
else if (data[0] == 10)
Serial.print(", RIGHT BUTTON"); // Status Byte
else if (data[0] == 12)
Serial.print(", CENTRE BUTTON"); // Status Byte
else;
Serial.println();
}else;
wertx = data[1] ; //neue variable für data, aber wie baue ich die hier um damit die "neue data"
werty = data[2] ; //dazugezählt oder abgezogen wird ?
}
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
//******** PS2.h*******//
//#include "WConstants.h"
#include "HardwareSerial.h"
#include "PS2Mouse.h"
#include "Arduino.h"
PS2Mouse::PS2Mouse(int clock_pin, int data_pin, int mode) {
_clock_pin = clock_pin;
_data_pin = data_pin;
_mode = mode;
_initialized = false;
_disabled = true;
_enabled = false;
}
int PS2Mouse::clock_pin() {
return _clock_pin;
}
int PS2Mouse::data_pin() {
return _data_pin;
}
void PS2Mouse::initialize() {
pull_high(_clock_pin);
pull_high(_data_pin);
delay(20);
write(0xff); // Send Reset to the mouse
read_byte(); // Read ack byte
delay(20); // Not sure why this needs the delay
read_byte(); // blank
read_byte(); // blank
delay(20); // Not sure why this needs the delay
if (_mode == REMOTE) {
set_remote_mode();
} else {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
_initialized = 1;
}
void PS2Mouse::set_mode(int data) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(data); // Send Set Mode
read_byte(); // Read Ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
if (_initialized) {
delayMicroseconds(100);
}
}
void PS2Mouse::set_remote_mode() {
set_mode(0xf0);
_mode = REMOTE;
}
void PS2Mouse::set_stream_mode() {
set_mode(0xea);
_mode = STREAM;
}
void PS2Mouse::set_sample_rate(int rate) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(rate); // Send Set Sample Rate
read_byte(); // Read ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
}
void PS2Mouse::set_scaling_2_1() {
set_mode(0xe7); // Set the scaling to 2:1
}
void PS2Mouse::set_scaling_1_1() {
set_mode(0xe6); // set the scaling to 1:1
}
// This only effects data reporting in Stream mode.
void PS2Mouse::enable_data_reporting() {
if (!_enabled) {
write(0xf4); // Send enable data reporting
read_byte(); // Read Ack Byte
_enabled = true;
}
}
// Disabling data reporting in Stream Mode will make it behave like Remote Mode
void PS2Mouse::disable_data_reporting() {
if (!_disabled) {
write(0xf5); // Send disable data reporting
read_byte(); // Read Ack Byte
_disabled = true;
}
}
void PS2Mouse::set_resolution(int resolution) {
if (_mode == STREAM) {
enable_data_reporting();
}
write(0xe8); // Send Set Resolution
read_byte(); // Read ack Byte
write(resolution); // Send resolution setting
read_byte(); // Read ack Byte
if (_mode == STREAM) {
disable_data_reporting();
}
delayMicroseconds(100);
}
void PS2Mouse::write(int data) {
char i;
char parity = 1;
pull_high(_data_pin);
pull_high(_clock_pin);
delayMicroseconds(300);
pull_low(_clock_pin);
delayMicroseconds(300);
pull_low(_data_pin);
delayMicroseconds(10);
pull_high(_clock_pin); // Start Bit
while (digitalRead(_clock_pin)) {;} // wait for mouse to take control of clock)
// clock is low, and we are clear to send data
for (i=0; i < 8; i++) {
if (data & 0x01) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
// wait for clock cycle
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity
if (parity) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
while ((!digitalRead(_clock_pin)) || (!digitalRead(_data_pin))) {;} // wait for mouse to switch modes
pull_low(_clock_pin); // put a hold on the incoming data.
}
//--------------------------------------------------------------------------------------------------------------------------------
//------------------------------Von hier kommt wohl data1 und 2 ? --------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
int * PS2Mouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
return data;
}
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
int PS2Mouse::read() {
return read_byte();
}
int PS2Mouse::read_byte() {
int data = 0;
pull_high(_clock_pin);
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
delayMicroseconds(5); // not sure why.
while (!digitalRead(_clock_pin)) {;} // eat start bit
for (int i = 0; i < 8; i++) {
bitWrite(data, i, read_bit());
}
read_bit(); // Partiy Bit
read_bit(); // Stop bit should be 1
pull_low(_clock_pin);
return data;
}
int PS2Mouse::read_bit() {
while (digitalRead(_clock_pin)) {;}
int bit = digitalRead(_data_pin);
while (!digitalRead(_clock_pin)) {;}
return bit;
}
int PS2Mouse::read_movement_x(int status) {
int x = read();
if (bitRead(status, 4)) {
for(int i = 8; i < 16; ++i) {
x |= (1<<i);
}
}
return x;
}
int PS2Mouse::read_movement_y(int status) {
int y = read();
if (bitRead(status, 5)) {
for(int i = 8; i < 16; ++i) {
y |= (1<<i);
}
}
return y;
}
void PS2Mouse::pull_low(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void PS2Mouse::pull_high(int pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
Code: Alles auswählen
#ifndef PS2Mouse_h
#define PS2Mouse_h
#define REMOTE 1
#define STREAM 2
class PS2Mouse
{
private:
int _clock_pin;
int _data_pin;
int _mode;
int _initialized;
int _enabled;
int _disabled;
int read_byte();
int read_bit();
int read_movement_x(int);
int read_movement_y(int);
void pull_high(int);
void pull_low(int);
void set_mode(int);
public:
PS2Mouse(int, int, int mode = REMOTE);
void initialize();
int clock_pin();
int data_pin();
int read();
int* report(int data[]);
void write(int);
void enable_data_reporting();
void disable_data_reporting();
void set_remote_mode();
void set_stream_mode();
void set_resolution(int);
void set_scaling_2_1();
void set_scaling_1_1();
void set_sample_rate(int);
};
#endif
// http://www.engineersgarage.com/embedded/arduino/how-to-interface-ps2-mouse-with-arduino
//Da ich weiß welche Arbeit hinter dem bisherigen code steckt, gibt’s hier auch die Quelle.
Code: Alles auswählen
/*============================ EG LABS ===================================//
Demonstration on how to use PS2 MOUSE with an arduino board
The circuit:
LCD:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 7
* LCD D5 pin to digital pin 6
* LCD D6 pin to digital pin 5
* LCD D7 pin to digital pin 4
* LCD R/W pin to ground
* 10K resistor:
* ends to +5V and ground
* wiper to LCD pin 3
* LED anode attached to digital output 9
* LED cathode attached to ground through a 1K resistor
MOUSE:
DATA PIN TO PIN NUMBER 8
CLOCK PIN TO PIN NUMBER 3
============================== EG LABS ===================================*/
#include "PS2Mouse.h"
#define MOUSE_DATA 8
#define MOUSE_CLOCK 3
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
PS2Mouse mouse(MOUSE_CLOCK, MOUSE_DATA, STREAM); // initializing the PS2 library
int data[2];
//int data[1];waehre ja auch zu einfach gewesen
int led = 9;
int wertx; //zusätzliche variable für data1 definiert
int werty; //zusätzliche variable für data2 definiert
void setup()
{
pinMode(led, OUTPUT);
lcd.begin(16, 2);
lcd.print("ENGINEERS GARAGE");
lcd.setCursor(0, 1);
lcd.print(" PS2 MOUSE ");
delay(2000);
lcd.clear();
Serial.begin(38400);
mouse.initialize(); // initializing the PS2 mouse connected with the Arduino
digitalWrite(led, HIGH);
}
void loop()
//----------------------------------------------------------------------------------------------------------------------------
//-----------------------------------Hier meine bescheidenen und sinnlose Versuche---------------------------
//----------------------------------------------------------------------------------------------------------------------------
mouse.report(data); // get data from the mouse
if((data[1] != 0) || (data[2] != 0) || (data[0] != 8))
{
lcd.clear();
Serial.print("X = "); //ab 38000baud darstellung richtig
lcd.print("X=");
Serial.print( data[1]); // X Movement Data
lcd.print( wertx); //einfach wertx geht nicht , gibt immer noch nur
// die Unterschiede aber nicht den Gesamtwert aus
lcd.setCursor(0, 1);
Serial.print(", Y = ");
lcd.print("Y=");
Serial.print(data[2]); // Y Movement Data
lcd.print(werty); //funktioniert !
if(data[0] == 9)
Serial.print(", LEFT BUTTON"); // Status Byte
else if (data[0] == 10)
Serial.print(", RIGHT BUTTON"); // Status Byte
else if (data[0] == 12)
Serial.print(", CENTRE BUTTON"); // Status Byte
else;
Serial.println();
}else;
wertx = wertx+data[1] ; //neue variable für data,
werty = werty+data[2] ; //funktioniert aber leider nur mit data2 und nicht mit data1
//data[1] wird ja weiter unten(markiert) generiert komme
//ich dort irgendwie an den wert ?
}
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------------------------------------------
//******** PS2.h*******//
//#include "WConstants.h"
#include "HardwareSerial.h"
#include "PS2Mouse.h"
#include "Arduino.h"
PS2Mouse::PS2Mouse(int clock_pin, int data_pin, int mode) {
_clock_pin = clock_pin;
_data_pin = data_pin;
_mode = mode;
_initialized = false;
_disabled = true;
_enabled = false;
}
int PS2Mouse::clock_pin() {
return _clock_pin;
}
int PS2Mouse::data_pin() {
return _data_pin;
}
void PS2Mouse::initialize() {
pull_high(_clock_pin);
pull_high(_data_pin);
delay(20);
write(0xff); // Send Reset to the mouse
read_byte(); // Read ack byte
delay(20); // Not sure why this needs the delay
read_byte(); // blank
read_byte(); // blank
delay(20); // Not sure why this needs the delay
if (_mode == REMOTE) {
set_remote_mode();
} else {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
_initialized = 1;
}
void PS2Mouse::set_mode(int data) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(data); // Send Set Mode
read_byte(); // Read Ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
if (_initialized) {
delayMicroseconds(100);
}
}
void PS2Mouse::set_remote_mode() {
set_mode(0xf0);
_mode = REMOTE;
}
void PS2Mouse::set_stream_mode() {
set_mode(0xea);
_mode = STREAM;
}
void PS2Mouse::set_sample_rate(int rate) {
if (_mode == STREAM) {
disable_data_reporting(); // Tell the mouse to stop sending data.
}
write(0xf3); // Tell the mouse we are going to set the sample rate.
read_byte(); // Read Ack Byte
write(rate); // Send Set Sample Rate
read_byte(); // Read ack byte
if (_mode == STREAM) {
enable_data_reporting(); // Tell the mouse to start sending data again
}
delayMicroseconds(100);
}
void PS2Mouse::set_scaling_2_1() {
set_mode(0xe7); // Set the scaling to 2:1
}
void PS2Mouse::set_scaling_1_1() {
set_mode(0xe6); // set the scaling to 1:1
}
// This only effects data reporting in Stream mode.
void PS2Mouse::enable_data_reporting() {
if (!_enabled) {
write(0xf4); // Send enable data reporting
read_byte(); // Read Ack Byte
_enabled = true;
}
}
// Disabling data reporting in Stream Mode will make it behave like Remote Mode
void PS2Mouse::disable_data_reporting() {
if (!_disabled) {
write(0xf5); // Send disable data reporting
read_byte(); // Read Ack Byte
_disabled = true;
}
}
void PS2Mouse::set_resolution(int resolution) {
if (_mode == STREAM) {
enable_data_reporting();
}
write(0xe8); // Send Set Resolution
read_byte(); // Read ack Byte
write(resolution); // Send resolution setting
read_byte(); // Read ack Byte
if (_mode == STREAM) {
disable_data_reporting();
}
delayMicroseconds(100);
}
void PS2Mouse::write(int data) {
char i;
char parity = 1;
pull_high(_data_pin);
pull_high(_clock_pin);
delayMicroseconds(300);
pull_low(_clock_pin);
delayMicroseconds(300);
pull_low(_data_pin);
delayMicroseconds(10);
pull_high(_clock_pin); // Start Bit
while (digitalRead(_clock_pin)) {;} // wait for mouse to take control of clock)
// clock is low, and we are clear to send data
for (i=0; i < 8; i++) {
if (data & 0x01) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
// wait for clock cycle
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
parity = parity ^ (data & 0x01);
data = data >> 1;
}
// parity
if (parity) {
pull_high(_data_pin);
} else {
pull_low(_data_pin);
}
while (!digitalRead(_clock_pin)) {;}
while (digitalRead(_clock_pin)) {;}
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
while ((!digitalRead(_clock_pin)) || (!digitalRead(_data_pin))) {;} // wait for mouse to switch modes
pull_low(_clock_pin); // put a hold on the incoming data.
}
//--------------------------------------------------------------------------------------------------------------------------------
//------------------------------Von hier kommt wohl data1 und 2 ? ----------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
int * PS2Mouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
return data;
}
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------
int PS2Mouse::read() {
return read_byte();
}
int PS2Mouse::read_byte() {
int data = 0;
pull_high(_clock_pin);
pull_high(_data_pin);
delayMicroseconds(50);
while (digitalRead(_clock_pin)) {;}
delayMicroseconds(5); // not sure why.
while (!digitalRead(_clock_pin)) {;} // eat start bit
for (int i = 0; i < 8; i++) {
bitWrite(data, i, read_bit());
}
read_bit(); // Partiy Bit
read_bit(); // Stop bit should be 1
pull_low(_clock_pin);
return data;
}
int PS2Mouse::read_bit() {
while (digitalRead(_clock_pin)) {;}
int bit = digitalRead(_data_pin);
while (!digitalRead(_clock_pin)) {;}
return bit;
}
int PS2Mouse::read_movement_x(int status) {
int x = read();
if (bitRead(status, 4)) {
for(int i = 8; i < 16; ++i) {
x |= (1<<i);
}
}
return x;
}
int PS2Mouse::read_movement_y(int status) {
int y = read();
if (bitRead(status, 5)) {
for(int i = 8; i < 16; ++i) {
y |= (1<<i);
}
}
return y;
}
void PS2Mouse::pull_low(int pin) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void PS2Mouse::pull_high(int pin) {
pinMode(pin, INPUT);
digitalWrite(pin, HIGH);
}
Code: Alles auswählen
int data[2];
Code: Alles auswählen
int * PS2Mouse::report(int data[]) {
write(0xeb); // Send Read Data
read_byte(); // Read Ack Byte
data[0] = read(); // Status bit
data[1] = read_movement_x(data[0]); // X Movement Packet
data[2] = read_movement_y(data[0]); // Y Movement Packet
return data;
}
Code: Alles auswählen
int data[3];
Also sollte da oben
int data[3];
stehen.
Das ist mir als erstes aufgefallen. Kann also noch etwas mehr nicht stimmen.
Anse
Compiler für andere Sprachen hätte diesen Fehler gefunden. C-Compilern ist so was relativ egal.Ich werd verrückt, genau das war das Problem !
Hätte ich selbst wohl nie gefunden,
Code: Alles auswählen
result = (ENCdata & 0x8000 );
result = (ENCdata & 0x4000) ^ (result>>1);
result = (ENCdata & 0x2000) ^ (result>>1);
result = (ENCdata & 0x1000) ^ (result>>1);
result = (ENCdata & 0x0800) ^ (result>>1);
result = (ENCdata & 0x0400) ^ (result>>1);
result = (ENCdata & 0x0200) ^ (result>>1);
result = (ENCdata & 0x0100) ^ (result>>1);
result = (ENCdata & 0x0080) ^ (result>>1);
result = (ENCdata & 0x0040) ^ (result>>1);
result = (ENCdata & 0x0020) ^ (result>>1);
result = (ENCdata & 0x0010) ^ (result>>1);
result = (ENCdata & 0x0008) ^ (result>>1);
result = (ENCdata & 0x0004) ^ (result>>1);
result = (ENCdata & 0x0002) ^ (result>>1);
result = (ENCdata & 0x0001) ^ (result>>1);
Code: Alles auswählen
int gray_to_bin(int ENCdata){
int result = ENCdata & 0x8000;
result |= (ENCdata ^ (result >> 1)) & 0x4000;
result |= (ENCdata ^ (result >> 1)) & 0x2000;
result |= (ENCdata ^ (result >> 1)) & 0x1000;
result |= (ENCdata ^ (result >> 1)) & 0x0800;
result |= (ENCdata ^ (result >> 1)) & 0x0400;
result |= (ENCdata ^ (result >> 1)) & 0x0200;
result |= (ENCdata ^ (result >> 1)) & 0x0100;
result |= (ENCdata ^ (result >> 1)) & 0x0080;
result |= (ENCdata ^ (result >> 1)) & 0x0040;
result |= (ENCdata ^ (result >> 1)) & 0x0020;
result |= (ENCdata ^ (result >> 1)) & 0x0010;
result |= (ENCdata ^ (result >> 1)) & 0x0008;
result |= (ENCdata ^ (result >> 1)) & 0x0004;
result |= (ENCdata ^ (result >> 1)) & 0x0002;
result |= (ENCdata ^ (result >> 1)) & 0x0001;
return result;
}
Code: Alles auswählen
#define GRAYBITS 16
int gray_to_bin(int ENCdata){
int i, result = ENCdata & (1 << (GRAYBITS-1));
for (i = GRAYBITS - 2; i >= 0; i--){
result |= (ENCdata ^ (result >> 1)) & (1 << i);
}
return result;
}
Code: Alles auswählen
#define GRAYBITS 25
uint32_t gray_to_bin(uint32_t ENCdata){
uint32_t i, result = ENCdata & (1 << (GRAYBITS-1));
for (i = GRAYBITS - 2; i >= 0; i--){
result |= (ENCdata ^ (result >> 1)) & (((uint32_t)1) << i);
}
return result;
}
Stimmt, aber der GCC bietet die Option "-mint8" an, mit der wird ein "Int" 8 Bits lang. long ist dann 16 Bits und long long 32Bit.Fritzler hat geschrieben:Ein int ist übrigens immer mind 16bit breit, 8bit int geht laut c standard nicht.
Code: Alles auswählen
(uint32_t)1 << 30
Code: Alles auswählen
1UL << 30
Code: Alles auswählen
void bin_to_bcd(uint32_t value, unsigned char bcd[10])
{
bcd[0] = value % 10;
value=value/10;
bcd[1] = value % 10;
value=value/10;
bcd[2] = value % 10;
value=value/10;
bcd[3] = value % 10;
value=value/10;
bcd[4] = value % 10;
value=value/10;
bcd[5] = value % 10;
value=value/10;
bcd[6] = value % 10;
value=value/10;
bcd[7] = value % 10;
value=value/10;
bcd[8] = value % 10;
value=value/10;
bcd[9] = value % 10;
value=value/10;
}