When I initialize UART, "blink" program stops blinking! Please help!

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

I graduated from Arduino to my own board using an Atmega128A running at 16 MHz. I made a blink program which works fine. My next baby step was trying to use UART. I added UART initializer to my blink program. As soon as the code uploads, my LED stop blinking. I'm not even sending UART data yet, just initializing. I can't find what's going on. Thank you SO much for taking the time to help me. The code is as follows:

 

#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

 

#define FOSC 16000000// Clock Speed
#define BAUD 9600
#define MYUBRR FOSC/16/BAUD-1

 

void USART_Init( unsigned int ubrr );
void USART_Transmit( unsigned char data );
unsigned char USART_Receive( void );

 

 

int main(void)
{
    DDRE = 0b1000000; //makes Port E Pin7 OUTPUT
    USART_Init ( MYUBRR );

    while (1) 
    {
    PORTE = 0b00000000; //writes Port E Pin7 LOW
    _delay_ms(50);
    PORTE = 0b10000000; //writes Port E Pin7 HIGH
    _delay_ms(20);
    }
}

void USART_Init( unsigned int ubrr )
{
    /* Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)ubrr;
    /* Enable receiver and transmitter */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    /* Set frame format: 8data, 2stop bit */
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

void USART_Transmit( unsigned char data )
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) )
    ;
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

Last Edited: Fri. Jul 12, 2019 - 01:54 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

M103C

 

(it does kind of raise the question as to why anyone would still bother with a 128 in 2019?? If you were trading up from Arduino why not simply continue to use the same hardware or at least use a familiar Arduino processor like a 328 or  a 2560)

Last Edited: Fri. Jul 12, 2019 - 01:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for the help! I'll looking into that.

 

I used the arduino to develop a product idea. The 2560 is way overkill for my needs and more power consumptive. The 128A seemed like a good fit, I needed 2 UARTS and at least 30 DIO pins, and better power consumption. Would you recommend a different chip? 

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

ATmega164/324/644/1284P series (40 pin DIP - should be enough IO I think?). The nice thing about these (apart from two UART) is that you have a choice of 16K, 32K, 64K or 128K so you can trade up.down the scale as your needs change. Any AVR with "P" on the end of the model name means "PicoPower", these are chips with added facilities aimed specifically at low power operation.

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

The datasheet shows only 32 programmable lines. I am using 27 pins strictly as DIOs, then 2 UARTS and a external crystal which brings me to 33 programmable lines needed. :( I think I already did look at that line of chips. Ideally, I'd like at least 3 extra IO lines unused in case of future revisions.

 

BTW, I deactivated that fuse bit and UART is working fine! THANK YOU! Do you know if the atmel ICE can be used to read outgoing UART data in a monitor? 

Last Edited: Fri. Jul 12, 2019 - 02:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jamespie wrote:
THANK YOU! Do you know if the atmel ICE can be used to read outgoing UART data in a monitor? 

Maybe, but a USB-serial_TTL dongle would be the item of choice!  https://www.ebay.com/b/Usb-To-Tt...

Look for one that is either 5v or 3.3v to match your VCC level.

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

Last Edited: Fri. Jul 12, 2019 - 02:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah I already have one, the problem is that I completely forgot to at test points on my PCB and I have no headers in line with my UART. I'll have to solder to the pads where the UART connects with another device. I was trying to avoid that, but oh well!

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

jamespie wrote:
DDRE = 0b1000000; //makes Port E Pin7 OUTPUT

Does not make!

 

DDRE = 0b1000000; //Pin6

DDRE = 0b10000000; //Pin7

 

 

Majid

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

m.majid wrote:

jamespie wrote:
DDRE = 0b1000000; //makes Port E Pin7 OUTPUT

Does not make!

 

DDRE = 0b1000000; //Pin6

DDRE = 0b10000000; //Pin7

 

This is an excellent example of why NOT to use binary constants.  So much better to write

 

DDRE = 1 << 7;

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

m.majid wrote:

DDRE = 0b1000000; //Pin6

DDRE = 0b10000000; //Pin7

 

 

Nice catch, best to get away from "magic numbers" and use symbolic names that self document your code.

 

#define LED 7

DDRE = (1<<LED);

then

PORTE |= (1<<LED); //turn on LED

PORTE &= ~(1<<LED); //turn off LED

 

 

 

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

I'm confused. If how is my PIN7 blinking the LED if that's wrong?

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

Well it must be on E6 not E7 (or those two pins are joined with solder splash or something?)

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

No I just triple checked. LED is on Pin#9 which is PORT E7. I looked under the microscope, no solder bridging. This was all soldered with solder paste in reflow oven

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

jamespie wrote:

I'm confused. If how is my PIN7 blinking the LED if that's wrong?

Toggling a PORT pin when the pin is set as an input will turn the internal pullup on and off.  That will make the pin a weak output when the pullup is enabled.  Is your LED intensity pretty low?  

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

No its pretty bright. I'm using a high efficiency LED with a 1K resistor. I changed my PORT code to the bitwise function and noticed no difference in brightness

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

I just realized my mistake. I forgot to rebuild the project after changing to the bitwise. Now the LED is even brighter. Didnt realize how bright these LEDs are with only around 2mAH!

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

jamespie wrote:

I just realized my mistake. I forgot to rebuild the project after changing to the bitwise. Now the LED is even brighter. Didnt realize how bright these LEDs are with only around 2mAH!

mAH?

 

Glad you found the problem and reported back.  Otherwise my next suggestion was that your board needed an exorcism.

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

Oops, typo. 2mA is LED current. It was around 2, I dont remember the exact number.

Haha I was confused too. So I connected the RX and TX of my 128a to the TX and RX (respectively) of an Arduino mega. Im not getting any UART data on the arduino end. No RX light blinking on the arduino. Any suggestions?

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

If you have a scope, use it to check your UART output.  That will tell you if you're getting data out, and what your baud rate REALLY is.

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

Yes I have one at home with serial decoder, just making sure my code all looks good. I'm at work right now, anxious to get home haha.

 

Just wanted to thank everyone for your help! I am always to amazed how YOU and other people on forums take your time out of your day to help people with simple, stupid questions like mine. Questions that many engineers would charge much money to answer. Seriously, thank you, guys!

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

jamespie wrote:
Questions that many engineers would charge much money to answer. Seriously, thank you, guys!

Where would that be?  smiley

 

Jim

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...