Help with Usart

I have to write a C program that cyclically send ASCII characters between 0x00 and 0xff on asynchronous serial line. Posted a character, it won't send other until it will receive one back or if it was spent more time to transmit two characters . Each received character must be compared with the last sent and, depending of character received if it is equal to or different from the one previously sent must be changed the state of two LEDs to report the incident. Can anyone help me to do this exercise?

I have to write a C program that cyclically send ASCII characters between 0x00 and 0xff on asynchronous serial line.

Not likely, since the valid range of values for a char is -128 to 127.

Can anyone help me to do this exercise?

Almost anyone could. You will only learn something if YOU do it, though.

Try something. If it doesn't work, post the code (both the PC code and the Arduino code) here, and we'll help you figure out why it doesn't do what you expect.

The requirements as you have expressed them are too vague to implement, so I'll assume that you have simply paraphrased them, and that the actual requirements are less vague.

What can I study to start to do it?

How good is your knowledge of C/C++ ? Have you tried and understood the examples in the IDE ?

rambo19997: What can I study to start to do it?

Without a frame of reference, as UKHeliBob suggests, all I can suggest is "Everything you need to to get the job done".

I know very well c++ but c not good.

I know very well c++

Good, because that is what the Arduino environment uses.

rambo19997: What can I study to start to do it?

Look at the serial examples that come with the Arduino IDE. Look at the Threads that are "stickied" at the top of the different sections of the Forum - for example this and this

It's all out there waiting for you to explore.

...R

rambo19997: I know very well c++ but c not good.

C++ is a superset of C, so that statement makes no sense.

Ascii is 7 bit. How can I set the frame format? In the datasheet is write UCSR0C = (1<<USBS0)|(3<<UCSZ00) that sets 8data and 2stop bit. How can I set 7 bit data?

rambo19997:
Ascii is 7 bit. How can I set the frame format? In the datasheet is write UCSR0C = (1<<USBS0)|(3<<UCSZ00) that sets 8data and 2stop bit. How can I set 7 bit data?

Perhaps if you were to describe in plain language what you are trying to do we could give you some useful advice.

I can’t understand what you mean by “set 7 bit data” when all I need to do to send an ascii value is Serial.print(‘a’);

…R

rambo19997:
Ascii is 7 bit. How can I set the frame format? In the datasheet is write UCSR0C = (1<<USBS0)|(3<<UCSZ00) that sets 8data and 2stop bit. How can I set 7 bit data?

I am not sure exactly what you want to do but does this help ?
Serial.begin()
If you want/need more understanding of how it works, then HardwareSerial.h and HardwareSerial.cpp are part of your Arduino installation.

This is an echo program that I’ve written for study

#define F_CPU 16000000UL			// Setto il clock
#define BAUDRATE 9600				// Baud rate
#define BAUD_PRESCALLER (((F_CPU / (BAUDRATE * 16UL))) - 1)    //Questa è una formula che mi serve per non so cosa
#include <avr/io.h>					
#include <util/delay.h>            //Questa libreria è se ci dovesse mettere del tempo a lampeggiare il led
#include <avr/interrupt.h>
#include <avr/io.h>
#include <util/atomic.h>
 
int main(void){        
	ATOMIC_BLOCK(ATOMIC_FORCEON)
	{
		init();								//Inizializzo d'ogni cosa
	}  									
	UCSR0B |= (1 << RXCIE0);                 // Questo mi serve per abiliare l'interrupt in ricezione del dato
	for(;;)
	{
											//Aspetto in pace che qualcuno mi invii qualcosa per far si che gle lo rispedisca
	}                            
return 0;                
}
void init(void)
{
UBRR0H = (unsigned char)(BAUD_PRESCALLER>>8);		// Setto il baud rate
UBRR0L = (unsigned char)(BAUD_PRESCALLER);			// Setto il baud rate
UCSR0B = (1<<RXEN0)|(1<<TXEN0);			// Abilito la trasmissione e la ricezione
UCSR0C = (1<<USBS0)|(1<<UCSZ01);		// Setto il formato del frame in questo caso: 7 data, 2 stop bits
DDRB=0x20;								 //Setto la porta B in uscita
PORTB=0x00;								 //Spengo il led  
}
void USART_send( unsigned char data){  //Questa è la funzione per inviare tutto, basta che gli dai un char e lui invia
 
 while(!(UCSR0A & (1<<UDRE0)));			//Aspetto che il buffer sia libero per caricarci il dato
 UDR0 = data;							//Carico il dato nel buffer
 
}
unsigned char USART_receive(void){    //Questa è la funzione che riceve tutto (fa da schiavetto che te lo mette nel registro)
 
 while(!(UCSR0A & (1<<RXC0)));			//Aspetto che il receiver buffer sia pieno per poter scaricare il dato
 return UDR0;							//Ritorno il dato ricevuto
 
}
void USART_putstring(char* StringPtr){    // Questa è una funzione che mi permette di inviare una stringa ma non serve a niente L'ho presa da internet
 
while(*StringPtr != 0x00){				 //Here we check if there is still more chars to send, this is done checking the actual char and see if it is different from the null char
 USART_send(*StringPtr);				 //Using the simple send function we send one char at a time
 StringPtr++;}							 //We increment the pointer so we can read the next char
 
}
ISR(USART_RXC_vect)       //questo è quello che succede quando viene riempito il buffer in ricezione
{
	char ReceivedByte;    //dichiato una variabile d'appoggio
	ReceivedByte = UDR0;  //ci infilo il dato ricevuto
	UDR0 = ReceivedByte;  //nel registro di andata dell'usart ci infilo il dato ricevuto che si trova nella variabile di appoggio
	if(PORTB==0x00)       //se il led è accesso lo spengo altrimenti lo accendo
	{
		PORTB=0x20;
	}
	else
		PORTB=0x00;
}						  
//tadàààà rispedisco indietro il dato ricevuto

Sorry but comments are in italian

Sounds like there are two "terminals". Is this Arduino to a PC terminal program, or Arduino to Arduino? Or is this an Arduino in loopback? If this is Arduino to terminal (PC), do you have to write the terminal code too? If you are sending/receiving 0x00 - 0xff, you may have to take steps to handle control codes. Please specify the tasks at each end of the line (who does what) and what is talking to what.

Look at the documentation for Serial. You can create your data with a counter variable, use Serial.write( ) to send binary. You might have to mask an int with 0xff. Read data back with Serial.parseInt( ). Compare your current counter value with what you get back. I don't see how you could ever get anything different back. The only way to "take too long" is to pull your loopback wire. If you know C++ very well, you can see how to do this in C extremely easily. This LEDs should be a walk in the park.