sending char from microcontroller to another one

Status
Not open for further replies.

foxbrain

Full Member level 2
Joined
Feb 1, 2010
Messages
142
Helped
5
Reputation
10
Reaction score
4
Trophy points
1,298
Location
Damas
Visit site
Activity points
2,322
hi
how can i send a char array from atmega168 to atmega16?
do i need a protocol?
is there a ready c function for it?
i'm using avr studio 5.1
thanks
 

With this device you have 4 choices including SPI, I2C, 8 GPIOs, or the UART. If both are on the same board (or mounted in the same case) I would look at SPI first but it really depends on the application. If you can spare the 8 GPIOs on each device and assuming the devices are on the same PCB that may be the best approach. If it is two separate cases with a cable between then the UART only way to go. There is example code for each of these interfaces that would do the job.
 

I think UART is the best and easier option than any other protocols....

Go for UART programming.
 

Use SPI,

you can use inbuilt functions which are developed by processor manufacturer already.
go check library functions first.
 

SPI is the best choice if the two devices are on the same board or the two boards are close to each other. Its only 4 wires and ground. It is fast at 6Mbps or so, and it is bidirectional.
 

i liked the usart so after i had understood it i wrote these codes :
the sender: atmega168
Code:
#define F_CPU 7372800 // Clock Speed
# define USART_BAUDRATE 9600
# define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void init_usart(void)
{
	UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
	UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8- bit character sizes
	UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8- bits of the baud rate value into the high byte of the UBRR register
	UBRRL = BAUD_PRESCALE; // Load lower 8- bits of the baud rate value into the low byte of the UBRR register
}
void main()
{
init_usart();
char number[26];
while ((UCSRA & (1 << UDRE)) == 0) {}; // Do nothing until UDR is ready for more data to be written to it
UDR = number; // Echo back the received byte back to the computer
while ((UCSRA & (1 << UDRE)) == 0) {};
}
the receiver: atmega16A
Code:
# include <avr/io.h>
# define USART_BAUDRATE 9600
# define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
void init_usart(void)
{
	UCSRB |= (1 << RXEN) | (1 << TXEN); // Turn on the transmission and reception circuitry
	UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8- bit character sizes
	UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8- bits of the baud rate value into the high byte of the UBRR register
	UBRRL = BAUD_PRESCALE; // Load lower 8- bits of the baud rate value into the low byte of the UBRR register
}
int main ( void )
{
	DDRC=0xFF;
	char rec;
	init_usart();
	for (;;) // Loop forever
	{
		while ((UCSRA & (1 << RXC)) == 0) {}; // Do nothing until data have been received and is ready to be read from UDR
		rec= UDR; // Fetch the received byte value into the variable " ByteReceived "
		if(rec[0]==3){do something}
	}
	return(0);
}
will this code works???
do i get at the receiver the same array char number[26] ?
 

i'm getting an error here:
Code:
UDR = number;
since number is an array of char and UDR must be uint8_t.
so how can i transfer this array of char ?
 

Try to send your array of characters by looping into UDR and by same technique try to receive that array into buffer array of same size.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…