Note that there are some explanatory texts on larger screens.

plurals
  1. POUSART embedded C. trigger character to store array
    primarykey
    data
    text
    <p>USART embedded c for atmega328p. trying to store an array of 10 characters of whatever user inputs after a certain character is received(in my case char $). This compiles for me but only outputs dollar signs when I input a string of chars using hercules utility reader. any help appreciated</p> <p>the following is a copy of the code I am using</p> <pre><code>#define FOSC 16000000 // Clock Speed #define BAUD 9600 #define MYUBRR FOSC/16/BAUD-1 #include &lt;avr/io.h&gt; //#include &lt;stdio.h&gt; char trig='$'; char arr[10]; //This function is used to initialize the USART //at a given UBRR value void USARTInit(unsigned int ubrr) { //Set Baud rate UBRR0H = (ubrr&gt;&gt;8); UBRR0L = ubrr; //Enable The receiver and transmitter UCSR0B = (1&lt;&lt;RXEN0)|(1&lt;&lt;TXEN0); // Set fram format: 8data 2stopBit UCSR0C = (1&lt;&lt;USBS0)|(3&lt;&lt;UCSZ00); } //This function is used to read the available data //from USART. This function will wait untill data is //available. unsigned char USARTReadChar( void ) { //Wait untill a data is available while(!(UCSR0A &amp; (1&lt;&lt;RXC0))) { //Do nothing } //Now USART has got data from host //and is available is buffer return UDR0; } //This function writes the given "data" to //the USART which then transmit it via TX line void USARTWriteChar(unsigned char data) { //Wait untill the transmitter is ready while(!(UCSR0A &amp; (1&lt;&lt;UDRE0))) { //Do nothing PORTD ^= 1 &lt;&lt; PINB2; } //Now write the data to USART buffer UDR0 = data; } int main(void) { DDRB |= 1 &lt;&lt; PINB2; //Varriable Declaration char data; USARTInit(MYUBRR); //Loop forever while(1) { //Read data data = USARTReadChar(); int i =0; //if incoming data is a dollar sign(trig), if(data==trig) { //start a loop to collect data from buffer for(i=0;i&lt;10;i++) { //array has 10 elements, will fill up the ith element as per for loop arr[i]=data; // printf("arrayoutput %c\n",arr[i]); USARTWriteChar(data); } } } } </code></pre> <p>I edited the while loop as suggested by oleg but still cannot get it to return the array .the entire code is as follows:</p> <pre><code>#define FOSC 16000000 // Clock Speed #define BAUD 9600 #define MYUBRR FOSC/16/BAUD-1 #include &lt;avr/io.h&gt; #include &lt;stdio.h&gt; char trig='$'; char arr[10]; //This function is used to initialize the USART //at a given UBRR value void USARTInit(unsigned int ubrr) { //Set Baud rate UBRR0H = (ubrr&gt;&gt;8); UBRR0L = ubrr; //Enable The receiver and transmitter UCSR0B = (1&lt;&lt;RXEN0)|(1&lt;&lt;TXEN0); // Set fram format: 8data 2stopBit UCSR0C = (1&lt;&lt;USBS0)|(3&lt;&lt;UCSZ00); } //This function is used to read the available data //from USART. This function will wait untill data is //available. unsigned char USARTReadChar( void ) { //Wait untill a data is available while(!(UCSR0A &amp; (1&lt;&lt;RXC0))) { //Do nothing } //Now USART has got data from host //and is available is buffer return UDR0; } //This function writes the given "data" to //the USART which then transmit it via TX line void USARTWriteChar(unsigned char data) { //Wait untill the transmitter is ready while(!(UCSR0A &amp; (1&lt;&lt;UDRE0))) { //Do nothing PORTD ^= 1 &lt;&lt; PINB2; } //Now write the data to USART buffer UDR0 = data; } int main(void) { DDRB |= 1 &lt;&lt; PINB2; //Varriable Declaration char data; USARTInit(MYUBRR); //Loop forever //Read data char input[10]; while(1){ data = USARTReadChar(); if(data == trig){ for(int i = 0; i &lt; 10; i++){ //here we're saving 10 characters to input array input[i] = USARTReadChar(); USARTWriteChar(input[i]);//tested without this also } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload