Salve ho 2 sketch dove leggo lo stato dei pin di un attiny85 e lo invio via seriale.
Il problema è che il primo sketch funziona regolarmente il secondo (dove leggo i pin tutti insieme attraverso la portb) non funziona e da valori a caso, ogni tanto trova uno stato alto quando non dovrebbe e viceversa e sembra essere più lento ad inviare le informazioni via seriale.
Ovviamente il collegamento elettrico è lo stesso.
Premetto che con arduino funziona regolarmente.
Allego gli sketch.
Dove sbaglio?
Grazie
#include <SoftwareSerial.h>
#include <avr/power.h>
const byte rx=3;
const byte tx=4; //Pin 3
SoftwareSerial mySerial(rx, tx); // RX, TX
const byte x = 0;
const byte y = 1;
const byte z = 2;
void setup()
{
power_adc_disable();
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
pinMode(x, INPUT);
pinMode(y, INPUT);
pinMode(z, INPUT);
}
void loop() // run over and over
{
// definizione della struttura
union RTX_b
{
unsigned char toSendByte;
struct PinStato
{
unsigned char x:
1;
unsigned char z:
1;
unsigned char y:
1;
unsigned char Bit4:
1;
unsigned char Bit5:
1;
unsigned char Bit6:
1;
unsigned char Bit7:
1;
unsigned char Bit8:
1;
}
Bits;
}
RTX_data;
RTX_data.Bits.x = digitalRead(x);
RTX_data.Bits.z = digitalRead(z);
RTX_data.Bits.y = digitalRead(y);
mySerial.write(RTX_data.toSendByte);
}
#include <SoftwareSerial.h>
#include <avr/power.h>
const byte rx=3;
const byte tx=4; //Pin 3
SoftwareSerial mySerial(rx, tx); // RX, TX
void setup()
{
power_adc_disable();
// set the data rate for the SoftwareSerial port
mySerial.begin(57600);
DDRB = B00010000; // Pin 4 come output, il primo pin a x è l'0
}
void loop() // run over and over
{
// definizione della struttura
union RTX_b
{
unsigned char toSendByte;
struct PinStato
{
unsigned char x:
1;
unsigned char z:
1;
unsigned char y:
1;
unsigned char Bit4:
1;
unsigned char Bit5:
1;
unsigned char Bit6:
1;
unsigned char Bit7:
1;
unsigned char Bit8:
1;
}
Bits;
}
RTX_data;
RTX_data.toSendByte=0;
RTX_data.toSendByte = PINB & B00000111;
mySerial.write(RTX_data.toSendByte);
}