Hi I’m currently working on project to analyze file contain 1 and 0. I want to ask if I can store number in array so that it can easy to work with. Thank
Yes.
int main ( void )
{
//led output
DDRB |= (1<<5);
//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 reception circuitry
char ReceivedByte ; //Variable to store the data (1 byte) read from the register
char value[64];//array to store value in
int i=0,count=0;
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
for (i =0; i <64; i++)
{
value[i]=ReceivedByte;
}
for (i =0; i <64; i++)
if(value[i] =='1')
{
PORTB |= (1<<5);
_delay_ms(10);
}
else if(value[i] =='0')
{
PORTB &=~(1<<5);
_delay_ms(10);
}
this is code i come up with to test. when data came in it store in receivedbyte and i run loop to store in array value. It run well with less than 4 digit but more than 4 cant.
what processor and IDE are you working with? code looks like it is for a Microchip PIC or AVR processor
im using arduino uno r3 with ide arduino
Your code might run on an Uno, but it is not arduino code. I think you will find Arduino code easier to write and understand.
the code of post #1 appears to
- wait for and receive a byte from the USART
- copy that byte in the array value[64] - copy ReceivedByte 64 times??
- then process array shifting PORTB
what is the overall aim of the code and what is it not doing that you expect?
for example i want to send 10110101 to arduino board. the board will receive then count how many 1 there is. so the code i do here to put 10110101 in value[64] then i run loop to detect if value[i] =='1', the led i connect to pin13 ( portb5) will light up and if 0 it will turn off.
yes indeed but the requirement for this project is using register base. I have read arduino code but it only be able to detect 1 digit at the time not whole so im thinking the way to put the whole data in array so i can easy to work with.
at present you appear ro read a byte and copy that value 64 times into the array value[64]
should you be receiving 64 bytes and copying them into the array, e.g. something like
for (i =0; i <64; i++) {
while (( UCSR0A & (1 << RXC0 )) == 0) {}; // Wait till data received and ready to be read from UDR0
ReceivedByte = UDR0 ; // Read the received byte value
value[i]=ReceivedByte;
}
any particular reason for accessing the hardware registers directly rather than using Arduino libraries?
School/college assignment. This is the Arduino forum. Your code is AVR ATMEGA328 specific, it won't run on any other type of Arduino except Uno and others that use that chip. The AVR forum might be better suited to help you with this.
There's someone with a very similar problem here
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.