Help on UART interfering with I/O port pins (ATiny2313A)

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, I admit I don´t mess with code for some time and obviously I´m doing something stupid. But I spend the last 2 hours trying to understand what is wrong with pins outputs without luck. This simple code transmits some bytes over a RF module. I feed a byte CMD to be sent by the UART. I can receive this sent byte in the receiver side just fine. But I bump in a little problem. The UART, when idle, is always high and my RF module stays on the air all the time. As I just have data to transmit every 30s, this is not acceptable. I searched the forum about UART idle and looks like that´s the way it is. So I decided to implement a PTT (Push To Talk) to enable my transmit only when there is data to be sent. But something is affecting the output pin PTT. Everything starts right, but in the middle of the UART transmission the PTT pin get interfered (see attached image - UART yellow and PTT green). I turned the RF module off to check if was a RF problem but is not. I even tried to exchange pins and ports without luck. So I would like to ask:

1) There is a way to tell the UART when idle to stay low instead high? This would eliminated the need for the PTT approach;
2) Any ideas what is causing this problem on PTT output pin?

Any suggestions will be appreciated.  

JR

 

/*
 * PRN_TX.c
 *
 * Created: 15/09/2016 22:16:35
 * Author : JRGandara
 */
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define BAUDRATE 1200
#define BAUD_PRESCALE ((F_CPU/(BAUDRATE*16UL))-1)		//calculate UBRR value
#define SYNC 0XAA		// sync signal
#define CMD 0x44		// command
#define PTT_ON PORTB |= (1<<PB0)
#define PTT_OFF PORTB &= ~(1<<PB0)
#define LED_GREEN_ON PORTB |= (1<<PB1)
#define LED_GREEN_OFF PORTB &= ~(1<<PB1)

void USART_Init(void)
{
	//Set baud rate
	UBRRL = BAUD_PRESCALE;
	UBRRH = (BAUD_PRESCALE >> 8);

	//Set data frame format: asynchronous mode,no parity, 1 stop bit, 8 bit size
	UCSRC= 0<<UMSEL0|(0<<UMSEL1)|(0<<UPM1)|(0<<UPM0)|(0<<USBS)|(0<<UCSZ2)|(1<<UCSZ1)|(1<<UCSZ0);
	//Enable Transmitter
	UCSRB=(1<<TXEN);
}
void USART_vSendByte(uint8_t u8Data)
{
	// Wait if a byte is being transmitted
	while((UCSRA&(1<<UDRE)) == 0);
	// Transmit data
	UDR = u8Data;
}
void Send_Packet(uint8_t cmd)
{
	PTT_ON;
	USART_vSendByte(SYNC);			//send sync
	USART_vSendByte(cmd);			//send command
	USART_vSendByte((SYNC+cmd));	//send checksum
	PTT_OFF;
}
int main(void) {
	DDRB= 0b0000011;	//set PTT and LED as output
	PORTB=0b0000000;
	sei();
	USART_Init();
	while(1)
	{
		Send_Packet(CMD);
		_delay_ms(800);
	}
	return 0;
}

 

Attachment(s): 

Good Soldering JRGandara

Last Edited: Fri. Sep 16, 2016 - 07:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'd look at the module first.  IME it would be unusual for a module to continually "talk" unless told to.  But maybe a simple plug-and-lay device? What module are you using?

 

Every UART in the world idles high, doesn't it?

 

During idle, can't you simply clear TXEN?

 

???  But your posted code does continuous sends!?!

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for the quick reply!

I disconnected the RF module as I stated. The problem persists even without it. 

Looks like UARTs idles high by what I found in the forum. But my oscilloscope have an option for idle high or low, so I thought I could have an option in AVR. 

TXEN! Silly me! I will test that. The code is only a test and I still not implemented the timed transmission. 

JR
 

Good Soldering JRGandara

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gandara wrote:
I disconnected the RF module as I stated. The problem persists even without it.

 

???  The module keeps transmitting, even when not connected to anything?  How is that an AVR problem?

 

Are you going to share the make and model so we can look at the docs too?  And a schematic?

 

The green trace in your picture appears to be a floating input.  And PTT should be idling low.  I'd guess you aren't really looking at the pin you think you are.  Or the posted source code doesn't match the firmware actually running.

 

And yes, with your code you will be chopping off the last character or two of your "message", because you turn off PTT as soon as the last byte is put into the UDR FIFO.  At that point there are nearly two bytes still to transmit.

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your suggestion of TXEN worked perfect! Thank you!

But I stull want to understand UART is messing with the outpit pins. Let me try to be more clear. I turned the RF module off. I just disconnected it from the board. No RF is being transmitted. The problem with the UART interfering in the output pin persists having just the Attiny2313A alone being connected to a 5V DC source and measuring with the scope. I even replace the attiny2313A for another one. All that being said, I suspect would be something with the AVR configuration. What do you think?

Schematic attacked.

 

Attachment(s): 

Good Soldering JRGandara

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

According to your circuit diagram, your /Reset pin is floating (unconnected). This will cause all manner of problems I suspect, especially when RF is present. Try connecting it to your Vcc via a 10K resistor.

Ross McKenzie ValuSoft Melbourne Australia

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't understand VCC being connected to BOTH Vin and Vout of the voltage regulator, as well as VCC of the ATtiny. And then there's the 5V on the RF315?

 

I believe you should have a wait till the XMIT buffer is empty before turning PTT off:

void Send_Packet (uint8_t cmd) {
    PTT_ON;
    USART_vSendByte (SYNC);		//send sync
    USART_vSendByte (cmd);		//send command
    USART_vSendByte ((SYNC + cmd));	//send checksum
    while ((UCSRA & (1 << TXC)) == 0);	// Wait till everything shifted out
    PTT_OFF;
}

Also not sure why you are enabling interrupts (sei) when you have no interrupt routines...

(and if you did have interrupts, global interrupts should enabled AFTER everything is initialized)

David (aka frog_jr)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I did this schematic in a hurry to show here and forget to replace the right VCC of regulator and the attiny power pin to 5V. But VCC is only at the regulator input. The reset is disabled on fuses, but I will check this. You right! I did not realize the usart would still be running after the function. I will try this. The sei () came from copying this code from another code. Should not be there. It´s good to hear from real programmers. In just few lines of this code I learned a lot.

Good Soldering JRGandara

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gandara wrote:
But I stull want to understand UART is messing with the outpit pins.
theusch wrote:
The green trace in your picture appears to be a floating input. And PTT should be idling low. I'd guess you aren't really looking at the pin you think you are. Or the posted source code doesn't match the firmware actually running.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is the schematic fixed. Disabling the UART the pin 3 goes low and the transmitter is disabled. That way I don´t need the PTT output anymore. I performed a test inside my house and I was able to receive the transmitter signal from 30m without problems with several brick walls in the way. I think in open space I would be able to receive the signal as far as 100m. I wil be testing this next week.

Attachment(s): 

Good Soldering JRGandara

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You still have the reset pin floating.

 

Ross McKenzie ValuSoft Melbourne Australia