Skip to content
Commits on Source (2)
#include <Arduino.h>
//ver se os 15 fazem interferencia
#define MASTER_ADDR 15
#define SLAVE1_ADDR 13
......@@ -15,8 +13,6 @@ int lastButtonState2 = 0;
uint8_t getData = 0;
uint8_t *data =0;
#define LED_PIN 13
#define ADDR3_PIN 11
#define ADDR2_PIN 10
......@@ -27,22 +23,23 @@ uint8_t *data =0;
#define WREN_PIN 2
#define FOSC 16000000
void asynch9_init(long BAUD) {
// put your code here, to setup asynchronous serial communication using 9 bits:
unsigned int ubrr = (FOSC/(16*BAUD))-1;
/*Set baud rate */
UBRR0H = (unsigned char)(ubrr>>8); //shift para por os bits mais signicativos
UBRR0L = (unsigned char)ubrr; // 8 bits menos significativos
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr; // 8 LSB
/*Set transmitter*/
if(ADDR == MASTER_ADDR){
UCSR0B |= (1<<TXEN0);
digitalWrite(WREN_PIN,HIGH); // to allow master transciever to send
}
else /*Set Receiver*/{
UCSR0B |= (1<<RXEN0);
UCSR0A |= (1<<MPCM0); // multi processor communication mode- permite receber só os addr
UCSR0A |= (1<<MPCM0); // multi processor communication mode- allows recieving addr
digitalWrite(WREN_PIN,LOW); //so that slaves transciever can recieve
}
/*frame format 9 bits of data*/
UCSR0C |= (3<<UCSZ01);
......@@ -76,6 +73,7 @@ void send_data(uint8_t data) {
uint8_t get_data(uint8_t *data) {
// put your code here, to receive a data byte using multi processor communication mode:
unsigned char resh;
/* Wait for data to be received */
while (!(UCSR0A & (1<<RXC0)));
......@@ -87,6 +85,12 @@ uint8_t get_data(uint8_t *data) {
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
if(resh == 1 && ADDR == *data){ // if it is the respective address
UCSR0A &= ~(1<<MPCM0); //to start "listening" data frames
}else if(resh == 1 && ADDR != *data){
UCSR0A |= (1<<MPCM0); //if it's not the respective address keep listening "addr"
}
return resh;
}
......@@ -106,16 +110,7 @@ void setup() {
pinMode(WREN_PIN, OUTPUT);
digitalWrite(LED_PIN,LOW);
ADDR = (digitalRead(ADDR3_PIN)<<3) | (digitalRead(ADDR2_PIN)<<2) | (digitalRead(ADDR1_PIN)<<1) | (digitalRead(ADDR0_PIN)) ;
if(ADDR == MASTER_ADDR){
digitalWrite(WREN_PIN,HIGH); // para o transciever do master possa enviar
}else
digitalWrite(WREN_PIN,LOW); //para os slaves receberem
asynch9_init(BAUD);
}
......@@ -161,6 +156,7 @@ void loop() {
}
}
else if(buttonState2 == 1){
if(last_ADDR == SLAVE2_ADDR){
send_data(0);
} else {
......@@ -175,15 +171,7 @@ void loop() {
}
//slaves
if(ADDR == SLAVE1_ADDR || ADDR == SLAVE2_ADDR){
digitalWrite(WREN_PIN,LOW); // Para que os transceivers (os slaves) possam receber
getData = get_data(data);
if(getData == 1 && ADDR == *data){// se for o endereço
UCSR0A &= ~(1<<MPCM0); //para comecar a "ouvir" data framees
}else if(getData == 1 && ADDR != *data){
UCSR0A |= (1<<MPCM0); //senao for o seu endereco continuar a ouvir "addr"
}
if(getData==0)
digitalWrite(LED_PIN,*data);
}
......