-1
\$\begingroup\$

I am learning Atmega328p USART peripheral. I wrote a simple code to send and receive data from a computer through a FTDI chip. In my code I actually use two led for indication of working. 1st LED which is connected PINB0 will toggle after a BYTE transmission . The 2nd LED will toggle if a successful reception of 1 BYTE from computer. Now I burned the code.And things were happening as they should happen. The 1st LED is toggling indicating data transmission . And the 2nd LED is not TOGGLING as I did not connect FTDI chip till now. Then I unplugged my usbasp and powered microcontroller with a cellphoine charger of constant 5v which i measured with my oscilloscope(with a 1uf capacitor in supply rail). Now interesting thing happen. The 2nd LED is blinking randomly indicating that microcontroller is receving BYTE through its RX pin. But I haven`t connected the FTDI driver till now and the RX pin is without any connection. What is happening here?

Here is my code:

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#define F_CPU 1000000ul
volatile unsigned char ReceivedByte;

int main(void)
{
DDRB |=(1<<PINB0)|(1<<PINB1);
int reg_value=12;
UBRR0L= (unsigned char) reg_value;
UBRR0H= (unsigned char) (reg_value>>8);
UCSR0B |=(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
UCSR0C |=(1<<USBS0)|(1<<UCSZ00)|(1<<UCSZ01); //2 stopbit,8data bit
sei();
    while (1) 
    {
        while(!(UCSR0A &(1<<UDRE0)) ) //polling
        UDR0='a';
         PORTB ^=(1<<PINB0);
        _delay_ms(500);

    }
}
ISR(USART_RX_vect)
{
    ReceivedByte=UDR0;
    PORTB ^=(1<<PINB1);
}
\$\endgroup\$
  • \$\begingroup\$ idownvotedbecau.se/imageofcode \$\endgroup\$ – Marcus Müller Mar 16 '18 at 21:23
  • \$\begingroup\$ I can not add code as text.It looks horrible when I am trying to paste the code in the editor \$\endgroup\$ – Zarzisur Mar 16 '18 at 21:30
  • \$\begingroup\$ You then select it and press Ctrl-K \$\endgroup\$ – Eugene Sh. Mar 16 '18 at 21:31
  • \$\begingroup\$ @Eugene Sh .That worked like a charm!!! \$\endgroup\$ – Zarzisur Mar 16 '18 at 21:38
1
\$\begingroup\$

the RX pin is without any connection.

A floating pin can easily pickup noise from all kind of sources. I would only be worried if you receive data whilst the pin is pulled high or low.

Just to add: that it seems idle in one condition and receiving on another may just be a matter of noise level or DC level at which the noise comes in.

\$\endgroup\$
  • \$\begingroup\$ Oldfart Thank you! I am wondering what is the noise source!! Btw when the pin is pulled high or low problem solves. \$\endgroup\$ – Zarzisur Mar 17 '18 at 0:30
  • \$\begingroup\$ Oldfart I am also having another problem. I have checked there is no pblm with FTDI cause I have chacked by connecting it`s TX and RX pin together and it can echo back sent data from my terminal software. Now I have connected the FTDI chip with PC and Microcontroller.When I send data from my computer to microcontroller it works fine. But I am also sending letter 'a' from my microcontroller.It should be showed in my terminal software.But it shows nothing. \$\endgroup\$ – Zarzisur Mar 17 '18 at 0:36
  • 1
    \$\begingroup\$ For a different problem it is best you start a new thread. (Also check you voltage and baudrate.) \$\endgroup\$ – Oldfart Mar 17 '18 at 7:27
0
\$\begingroup\$

I have found out the problem. It is silly I haven`t put a comma after polling.Now I am wondering why that happened.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.