Author Topic: Atmega16 USART works incorect Help Please.  (Read 1094 times)

0 Members and 1 Guest are viewing this topic.

Offline Karlis.RasisTopic starter

  • Jr. Member
  • **
  • Posts: 14
  • Helpful? 0
Atmega16 USART works incorect Help Please.
« on: March 19, 2012, 10:21:51 AM »
Sorry for my English  and re posting, I' am from Latvia and Working on my bachelor's work.

Project is Control Robot via Bluetooth.
Months was spent before I get some results, but they are far from necessary

I use Atmega16 and connected Bluetooth module "linvor" to receive and send characters via Hyper-terminal on integrated Bluetooth adapter in my laptop. Bluetooth is connected tx-rx, rx-tx, gnd-gnd directly to MCU.
I found code that sends received character back. I modificate code and flashed to Atmega16.
When i send Characters via hyper-terminal it sends it back but, not all symbols.
To test all this for next steps I connected 3 leds on ports PB0-PB2 and want to get this- when i send characters- 'l' first diode light up, when 'n'- second, when 'p' third, when "r" all power down.

Problem is  when i send those characters 'l, n, p' or 'r' nothing happened. But first diode light up when i press 'j' on keyboard second when 'k' and third when 'l' ,to switch all off  'm'.
That means MCU understands l as j, n as k, p as l,  and r as m...
how it can be?
Maybe there is problems with BoudRate or clock speed, or protocol at all?
I used external oscilator- 14.7456 Mhz

I readed many websites, and MCU Atmega16, Atmega8 Datasheets, but can not find information, about my problem. Hope someone will help.

Waiting for posts, thank You!
Code: [Select]
#define F_CPU 7372800UL //Defines clock speed
#include <avr/io.h>
#include <avr/interrupt.h>
#define USART_BAUDRATE 115200 //Baudrate for serial comm.
//Serial com. Interrupt Service Routine (runs each time a byte is received)
    char ReceivedByte;
    ReceivedByte = UDR; // Retrieves byte from serial port (bluetooth module)
    UDR = ReceivedByte; // Echoes it back for fun
    switch (ReceivedByte) //Which ASCII character was received?
        case 'p':   PORTB =  0b00000100;//l
        case 'n':   PORTB =  0b00000010;//k
        case 'r':   PORTB =  0;//m
        case 'l':   PORTB =  0b00000001;//j
        default:    break; //Character unknown to my routine, discard character
int main(void)
    //I/O Initialization
    DDRB |= (1<<PIN0) | (1<<PIN1) | (1<<PIN2); //DIR1, DIR2 and Enable pins as outputs
    PORTB=0; //All initialized to 0
    UCSRB |= (1 << RXEN) | (1 << TXEN); //Enable Tx and Rx
    UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
    UBRRL = BAUD_PRESCALE;          //Sets
    UBRRH = (BAUD_PRESCALE >> 8); //baudrate registers
    UCSRB |= (1 << RXCIE); //Enable USART-interrupt
    sei(); //Enable global interrupt
    /* This program is completely interrupt driven, so nothing goes on in while loop*/
    while(1); //Never gets out from here!
    return 0; //Never reaches this point!
« Last Edit: March 20, 2012, 06:52:45 AM by Karlis.Rasis »

Offline newInRobotics

  • Supreme Robot
  • *****
  • Posts: 1,015
  • Helpful? 48
  • N.I.R.
Re: Atmega16 USART works incorect Help Please.
« Reply #1 on: March 19, 2012, 01:59:53 PM »
No need to double-post.
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W


Get Your Ad Here