Sending a txt file to Arduino via UART interface

My program objective is to count the number of "1", if total is even, the LED will turn on and vice versa. I will send a txt file, containing these bits, each line has 8 bits of "0" and "1" to the Arduino board using a software call Terminal.exe:
image
This is my code so far (not complete):

#ifndef F_CPU
#define F_CPU 16000000UL  // Set 16 MHz clock speed
#endif

#include <avr/io.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((( F_CPU / 16) + ( USART_BAUDRATE / 2)) / ( USART_BAUDRATE )) - 1)
#include <avr/interrupt.h>

 int main ( void )
 {
  //Setup the Baud Rate
   UBRR0H = ( BAUD_PRESCALE >> 8); // Load upper 8- bits of the baud rate value into the high byte of the UBRR0H register
   UBRR0L = BAUD_PRESCALE ; // Load lower 8- bits of the baud rate value into the low byte of the UBRR0L register
   //Configure data format for transimission
   UCSR0C = (1 << UCSZ00 ) | (1 << UCSZ01 ); // Use 8- bit character sizes
   UCSR0B = (1 << RXEN0 ) | (1 << TXEN0 ); // Turn on the transmission and recep-tion circuitry
   UCSR0B |= (1 << RXCIE0);
   DDRB = 0xFF;
   sei();
   char ReceivedByte ; //Variable to store the data (1 byte) read from the register
    while(1)
  {
     while (( UCSR0A & (1 << RXC0 )) == 0) {}; // Wait till data received and ready to be read from UDR0
      ReceivedByte = UDR0 ; // Read the received byte value
      if ((UDR0 %2) == 0){
        PORTB |= (1 << PORTB5);  // Turn on LED
        sei();
      }
      else if ((UDR0 %2) == 1) {
        PORTB &= ~ (1 << PORTB5);  // Turn off LED
      }
  }
 } 
ISR (USART_RX_vect)
{
  char ReceivedByte;
  ReceivedByte = UDR0;
  UDR0 = ReceivedByte;
}

With this code, it simply just recognizes the input number in decimal, not binary, so I don't know what I really need to do now. I see the task might need to use Parity bit, is that correct? Or do I need to use something else?

this is not an Installation & Troubleshooting question.

➜ moved to a better location

Do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation for your ask).

what do you expect from this ?

also the fact that the byte is even or odd does not mean you have a even or odd number of 1. this is just the LSb that is 0 (even) or 1 (odd)

eg;
0b11111111 ➜ 8 bits at 1 but the number is odd (255)
0b10000000 ➜ 1 bit at 1 but the number is even (128)

When is our homework due?

I don't see any counting in your code.

Any particular reason you are not using the serial library?

A few days

Just because I'm not allowed to use Arduino Library

You're only examining the LSB of the received character.

Sorry, the code is incomplete and I'm still working on it.
What I mean is, for example, the txt file has 8 lines, each line has 8 bits that includes "0" and "1", and the total of bit "1" in that file is 10, so that's even numbers of "1". If that file contains 9 bit "1" then it's odd.

1. Does that mean that you are required to write only register level codes?
2. Are you allowed to use the setup() and loop() functions of the IDE?

Just send this bit pattern: 10101010101 from the InputBox of the Serial Monitor (with Line ending option at Newline) to the sketch of your post #1, count for the even number of 1s and then ignite LED connected at PORTB5-pin (the built-in LED of UNO).

So, what is(are) the line ending character(s)?

  1. Yes.
  2. IDE is allowed

Check if the following sketch is useful for you (tested on UNO). Select Newline option of the Line end tab of the Serial Monitor (Fig-1). This sketch counts for even number of 1s from Newline-terminated 1010... -type string coming from the Serial Monitor.

#ifndef F_CPU
#define F_CPU 16000000UL  // Set 16 MHz clock speed
#endif

#include <avr/io.h>
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((( F_CPU / 16) + ( USART_BAUDRATE / 2)) / ( USART_BAUDRATE )) - 1)
#include <avr/interrupt.h>
char ReceivedByte ; //Variable to store the data (1 byte) read from the register
volatile byte counter;

void setup()//int main ( void )
{
  //Setup the Baud Rate
  UBRR0H = ( BAUD_PRESCALE >> 8); // Load upper 8- bits of the baud rate value into the high byte of the UBRR0H register
  UBRR0L = BAUD_PRESCALE ; // Load lower 8- bits of the baud rate value into the low byte of the UBRR0L register
  //Configure data format for transmission
  UCSR0C = (1 << UCSZ00 ) | (1 << UCSZ01 ); // Use 8- bit character sizes
  UCSR0B = (1 << RXEN0 ) | (1 << TXEN0 ); // Turn on the transmission and reception circuitry
  UCSR0B |= (1 << RXCIE0);
  DDRB = 0xFF;
  PORTB &= ~ (1 << PORTB5);  // Turn off LED
  sei();
}

void loop(){}
 
ISR (USART_RX_vect)
{
  ReceivedByte = UDR0;
  UDR0 = ReceivedByte; //00110011
  if (ReceivedByte != 0x0A)   //More bits are yet to arrive from Serial Monitor
  {
    if (ReceivedByte == 0x31)  //1 has arrived from Serial Monitor
    {
      counter++;
    }
  }
  else
  {
    if (counter % 2 == 0) //counter contains even number of 1s
    {
      PORTB |= (1 << PORTB5);  // Turn on LED
      counter = 0;
    }
    else
    {
      PORTB &= ~ (1 << PORTB5);  // Turn off LED
      counter = 0;
    }
  }
}


Figure-1:

It seems to run fine and count exact number of 1s, however, it can only count each line at a time, if I send in a file containing multiple lines, it doesn't work. But thank you for your help!

This is the building block and now you can add other blocks to make it accepting and processing multilines.

Hins:
Using a software UART port of your UNO-1, acquire the following data structure from another UNO-2 and then count the number of 1s.

char myData[3][9] =
{
  {"10101010"},
  {"11110100"},
  {"00110001"}
};

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.