EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / looping problem in ATmega16

looping problem in ATmega16

|

Microcontroller › AVR › looping problem in ATmega16

  • This topic has 3 replies, 2 voices, and was last updated 12 years, 1 month ago by AJISH ALFRED.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • June 18, 2013 at 10:02 am #2489
    Shashank Rao.C
    Participant

    when i use bello progrm usart_getch function repeats only 3 times. why?

     

     

    #define F_CPU 16000000     // set CPU freq.
    #define USART_BAUDRATE 9600
    #define BAUD_PRESCALE F_CPU/16/USART_BAUDRATE-1
    #define PUMP_CONTROL PORTC

    #include<avr/io.h>
    #include<util/delay.h>
    #include<string.h>

     

    void USART_Init();
    void usart_putch(unsigned char send);
    unsigned int usart_getch();

     

     

    int main(void)
    {
     unsigned int c=5;
    USART_Init();

     

    while(c)
    {

    usart_putch(usart_getch());
    c–;
    }

    }

     

     

     

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

    /* Enable receiver and transmitter */

    UCSRB = (1<<RXEN)|(1<<TXEN);

    UCSRC = 0x86;  // Set frame format: 8data, 1stop bit, no parity
    }

    void usart_putch(unsigned char send)    
     {
    unsigned char ucsrc;
    while ( !( UCSRA & (1<<UDRE)))
    ;

    UDR = send; // Send the byte
    }

    unsigned int usart_getch()         //FUNCTION TO RECIVE A CHARATER.
    {

    while ( !(UCSRA & (1<<RXC)) );

    return UDR; // return the byte

    }

     

     

     

     

     

    insted of while loop i used for, do-while also….

    June 18, 2013 at 1:23 pm #9979
    AJISH ALFRED
    Participant

    Hi Shashank,

    For all microcontroller codes there should be an infinite loop at the end of the main function like the following

     

    int main(void)
    {
      unsigned int c=5;
      USART_Init();

     while(c)
     {
         usart_putch(usart_getch());
         c–;
     }

     

     while (1)

     {

        ;

     }

    }

     

    Which all keys in the keyboard are you pressing to send data to the microcontroller? If you press keys like ENTER key, they will send two characters at a time and hence you will feel that you missed one loop.

    June 19, 2013 at 5:03 am #9985
    Shashank Rao.C
    Participant

    I am using cutecom for hyperterminal and to send data i press enter key.

    other than enter key which key i can use to send data to mcu?

    June 19, 2013 at 1:06 pm #9986
    AJISH ALFRED
    Participant

    Hi,

    Normally in cutecom you don’t have to press the ENTER key to send data. As soon as you press any key that data will be send to through the serial port. Check your cutecome settings.

    Or you can try with some other software.

    Another solution is that in your code try to compensate for the two extra characters due to ENTER key

  • Author
    Posts
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Fun with AI and swordfish basic July 19, 2025
  • using a RTC in SF basic July 19, 2025
  • Does US electric code allow branching ? July 19, 2025
  • Faulty heat air gun (dc motor) - problem to locate fault due to Intermittent fault July 19, 2025
  • Sump pit water alarm - Kicad 9 July 19, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise