V1

parent 512965d2
......@@ -5,6 +5,13 @@
#define SLAVE2_ADDR 14
uint8_t ADDR;
uint8_t last_ADDR;
int buttonState1 = 0;
int buttonState2 = 0;
int lastbuttonState1 = 0;
int lastbuttonState2 = 0;
uint8_t getData = 0;
#define LED_PIN 13
#define ADDR3_PIN 11
......@@ -14,29 +21,147 @@ uint8_t ADDR;
#define BUT1_PIN 7
#define BUT2_PIN 6
#define WREN_PIN 2
#define FOSC 16000000
void asynch9_init(long BAUD) {
// put your code here, to setup asynchronous serial communication using 9 bits:
delay(500);
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
/*Set transmitter*/
if(ADDR & MASTER_ADDR){
UCSR0B |= (1<<TXEN0);
}
else /*Set Receiver*/{
UCSR0B |= (1<<RXEN0);
UCSR0A |= (1<<MPCM0);
}
/*frame format 7 bits de data*/
UCSR0C |= (2<<UCSZ00);
}
void send_addr(uint8_t addr) {
// put your code here, to send an address:
/* Wait for empty transmit buffer */
while (!(UCSR0A & (1<<UDRE0)));
UCSR0B |= (1<<TXB80);
/* Put addr into buffer, sends the addr */
UDR0 = addr;
}
void send_data(uint8_t data) {
// put your code here, to send a data byte:
/* Wait for empty transmit buffer */
while (!(UCSR0A & (1<<UDRE0)));
/* Copy 9th bit to TXB8 */
UCSR0B &= ~(1<<TXB80);
/* 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;
/* Wait for data to be received */
while (!(UCSR0A & (1<<RXC0)));
/* Get status and 9th bit, then data */
/* from buffer */
resh = UCSR0B;
*data = UDR0;
/* 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
*/
}
void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:~
int ADDR_PINS [4] = {0};
pinMode(ADDR3_PIN, INPUT_PULLUP);
pinMode(ADDR2_PIN, INPUT_PULLUP);
pinMode(ADDR1_PIN, INPUT_PULLUP);
pinMode(ADDR0_PIN, INPUT_PULLUP);
pinMode(BUT1_PIN, INPUT_PULLUP);
pinMode(BUT2_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
pinMode(WREN_PIN, OUTPUT);
ADDR_PINS[0] = digitalRead(ADDR0_PIN);
ADDR_PINS[1] = digitalRead(ADDR1_PIN);
ADDR_PINS[2] = digitalRead(ADDR2_PIN);
ADDR_PINS[3] = digitalRead(ADDR3_PIN);
ADDR = ADDR_PINS[3]<<3 | ADDR_PINS[2]<<2 | ADDR_PINS[1]<<1 | ADDR_PINS[0] ;
asynch9_init(BAUD);
}
void loop() {
// put your main code here, to run repeatedly:
// Master
if(ADDR & MASTER_ADDR){
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?
} else {
send_addr(SLAVE1_ADDR);
send_data(1);//nao sei se isso é necessário?
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?
} else {
send_addr(SLAVE2_ADDR);
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
}
}
\ 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