Test Version

parent 13344b9f
......@@ -38,20 +38,22 @@ void asynch9_init(long BAUD) {
}
else /*Set Receiver*/{
UCSR0B |= (1<<RXEN0);
UCSR0A |= (1<<MPCM0);
UCSR0A |= (1<<MPCM0); // multi processor communication mode- permite receber só os addr
}
/*frame format 7 bits de data*/
UCSR0C |= (2<<UCSZ00);
/*frame format 9 bits of data*/
UCSR0C |= (3<<UCSZ01);
UCSR0B |= (1<<UCSZ02)
}
void send_addr(uint8_t addr) {
// put your code here, to send an address:
/* Wait for empty transmit buffer */
// Wait for empty transmit buffer
while (!(UCSR0A & (1<<UDRE0)));
//Copy the 9th bit to TXB8 =1
UCSR0B |= (1<<TXB80);
/* Put addr into buffer, sends the addr */
// Put addr into buffer, sends the addr
UDR0 = addr;
}
......@@ -59,18 +61,18 @@ void send_addr(uint8_t addr) {
void send_data(uint8_t data) {
// put your code here, to send a data byte:
/* Wait for empty transmit buffer */
// Wait for empty transmit buffer
while (!(UCSR0A & (1<<UDRE0)));
/* Copy 9th bit to TXB8 */
// Copy 9th bit to TXB8 = 0
UCSR0B &= ~(1<<TXB80);
/* Put data into buffer, sends the data */
// Put data into buffer, sends the data
UDR0 = 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, resl;
unsigned char resh;
/* Wait for data to be received */
while (!(UCSR0A & (1<<RXC0)));
......@@ -81,17 +83,8 @@ uint8_t get_data(uint8_t *data) {
/* Filter the 9th bit, then return */
resh = (resh >> 1) & 0x01;
/*
//se for um address
if(((resh<<8) | resl) & ){ //mudar isso!!!
if(UDR0 == ADDR)
return 1;
else return 0;
} // se for data
else if()
//enviar
*/
return resh;
}
void setup() {
......@@ -111,9 +104,9 @@ void setup() {
ADDR_PINS[1] = digitalRead(ADDR1_PIN);
ADDR_PINS[2] = digitalRead(ADDR2_PIN);
ADDR_PINS[3] = digitalRead(ADDR3_PIN);
digitalWrite(LED_PIN,LOW);
ADDR = ADDR_PINS[3]<<3 | ADDR_PINS[2]<<2 | ADDR_PINS[1]<<1 | ADDR_PINS[0] ;
asynch9_init(BAUD);
......@@ -124,44 +117,67 @@ void loop() {
// put your main code here, to run repeatedly:
// Master
if(ADDR & MASTER_ADDR){
buttonState1 = digitalRead(BUT1_PIN);
buttonState2 = digitalRead(BUT2_PIN);
digitalWrite(WREN_PIN,HIGH); // para o transciever do master possa enviar
buttonState1 = digitalRead(BUT1_PIN);
buttonState2 = digitalRead(BUT2_PIN);
if(buttonState1 != lastButtonState1){
if(buttonState1 == LOW){
if(last_ADDR & SLAVE1_ADDR){
send_data(1);//o que é que ele envia para o led ficar a 1?
send_data(15);
} else {
send_addr(SLAVE1_ADDR);
send_data(0);
last_ADDR = SLAVE1_ADDR;
}
} lastbuttonState1 = buttonState1;
}else if(buttonState1 != lastButtonState1){
if(buttonState1 == HIGH){
if(last_ADDR & SLAVE1_ADDR){
send_data(0);
} else {
send_addr(SLAVE1_ADDR);
send_data(1);//nao sei se isso é necessário?
send_data(15);
last_ADDR = SLAVE1_ADDR;
}
}
}
lastbuttonState1 = buttonState1;
if(buttonState2 != lastButtonState2){
if(buttonState2 == LOW){
if(last_ADDR & SLAVE2_ADDR){
send_data(1);//o que é que ele envia para o led ficar a 1?
send_data(15);
} else {
send_addr(SLAVE2_ADDR);
send_data(15);
last_ADDR = SLAVE2_ADDR;
}
}
lastbuttonState2 = buttonState2;
}else if(buttonState2 != lastButtonState2){
if(buttonState2 == HIGH){
if(last_ADDR & SLAVE2_ADDR){
send_data(0);//o que é que ele envia para o led ficar a 1?
} else {
send_addr(SLAVE2_ADDR);
send_data(0);//nao sei se isso é necessário?
last_ADDR = SLAVE2_ADDR;
}
}
}
lastbuttonState2 = buttonState2;
}
if(ADDR & SLAVE1_ADDR){
getData = get_data(&data);
if(getData == 1)// se for o endereço
}
if(ADDR & SLAVE2_ADDR){
getData = get_data(&data);
if(getData == 1)// se for o endereço
//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(*data == 15 && getData == 0)
digitalWrite(LED_PIN, HIGH);
if(*data == 0 && getData == 0)
digitalWrite(LED_PIN, LOW);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment