Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Serial Data Communication for Multiple Microcontrollers ATmega88PA and PC

Status
Not open for further replies.

kmdineshece

Member level 1
Joined
Dec 17, 2013
Messages
36
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
384
I'm working with ATmega88PA, ATMEL Studio6.2.............
I have written a code for serial data transmission between one controller and PC but i want to communicate with multiple controllers, how to assign a ID for different controllers,
controller 1: ID A
controller 2: ID B
controller 3: ID C
If i send a data with ID: A, micro controller '1 'only receive those data......
I have written a code for only one controller!....



Code C++ - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <avr/io.h>
#include <inttypes.h>
 
#define FOSC 20000000 // Clock Speed
#define BAUD 19200
#define MYUBRR FOSC/16/BAUD-1
void main( void )
{
    
    USART_Init(MYUBRR);
    while(1)
    {
        USART_puts("Dinesh");
    }
    
}
void USART_Init( unsigned int ubrr)
{
    /*Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)(ubrr);
    //Enable receiver and transmitter */
    UCSR0B = (1<<TXEN0);
    ////=UCSRC |= (1 << URSEL) | (1<<USBS) | (1 << UCSZ0) | (1 << UCSZ1); 
    /* Set frame format: 8data, 1stop bit */
    UCSR0C = (1<<UMSEL00)|(1<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00);
    }
 
 
 
    void USART_Transmit( unsigned char data )
    {
    /* Wait for empty transmit buffer */
    ////=while ((UCSRA & (1 << RXC)) == 0);
     while(!(UCSR0A & (1 << UDRE0)));
    
    /* Put data into buffer, sends the data */
    UDR0 = data;
    }
    
    
    void USART_puts(unsigned char *s)
    {
        while(*s)
        {
            USART_Transmit(*s);
            s++;
        }
    }

 
Last edited by a moderator:

You can use serial receive interrupt in each microcontroller and you can append the device ID to the beginning of the data you send so that the first byte will always be the ID. If a AVR received this data then it has to check if received 1st byte is the ID of that device. If yes, then it should do further processing of the received data else discard it.
 

You can use serial receive interrupt in each microcontroller and you can append the device ID to the beginning of the data you send so that the first byte will always be the ID. If a AVR received this data then it has to check if received 1st byte is the ID of that device. If yes, then it should do further processing of the received data else discard it.

Pls share some example links
 

I have not seen such examples but that is a method which you can try. Have you used serial receive interrupt ? If yes, then it is not a difficult task. You read the data into a buffer till \r or \n is received. Then you set a flag like received in the ISR. Then you check if this flag is true in the while(1) loop and if it is true then you check if 1st byte matches with the ID of that device. If yes then you do what you want based on the data received. I don't use AVR much and also I don't use Atmel Studio + GCC for writing the code. I use other compiler(s).

This is the code I wrote. Say, one AVR sends some string with 'A' as the first character of the string then this code for another AVR with ID A will receive it and takes required action on the data.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define F_CPU 20000000UL
 
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <string.h>
 
char uartBuffer[50]
unsigned int baudrate = 0, baudPrescale = 0, index;
unsigned char receivedFlag = 0;
 
void UART1_Init(unsigned int baudrate);
void UART1_Write(char text);
void UART1_Write_Text(char *text);
 
 
ISR(USART_RXC_vect){
   uartBuffer[index++] = UDR0;
   uartBuffer[index] = '\0';
  
   if((uartBuffer[index - 1] == '\r') || (uartBuffer[index - 1] == '\n')) {
    receivedFlag = 1;
   }        
}
 
void UART1_Init(unsigned int baudrate) {
   baudPrescale = (((F_CPU / (baudrate * 16UL))) - 1);
   UBRR0L = baudPrescale;
   UBRR0H = (baudPrescale >> 8);
   UCSR0B = ((1<<TXEN0)|(1<<RXEN0) | (1<<RXCIE0));
} 
 
void UART1_Write(char text) {
   while((UCSR0A &(1<<UDRE0)) == 0);
   UDR0 = text;
}
 
void UART1_Write_Text(char *text) {
   while(*text)
     UART1_Write(*text++);
}
 
int main(void) {
 
    DDRB = 0xFF;
    DDRC = 0xFF;
    DDRD = 0b11111010;
 
    PINB = 0x00;
    PINC = 0x00;
    PIND = 0x00;
 
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
 
    _delay_ms(200);
 
    UART1_Init(19200);
    _delay_ms(200);
        
    sei();
 
    while(1) {
 
            if(receivedFlag) {
                  if(uartBuffer[0] = 'A') {
            //Do what you want here like extracting the received data and displaying it on LCD
          } 
            
          memset(uartBuffer, '\0', sizeof(uartBuffer));
                  
                  receivedFlag = 0;                 
            }
    }
}

 
Last edited:
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top