Controlling a hobby servo using ATmega32 UART

Discussion in 'Homework Help' started by yaijl, Dec 30, 2013.

  1. yaijl

    Thread Starter New Member

    Dec 30, 2013
    2
    0
    I am trying to control a hobby servo motor using two ATmega32 microcontrollers. I am using all the default configurations, with the internal default 1 MHz clock and TIMER2 to generate a fast PWM control signal for the servo. The corresponding UART pins TX and RX of both microcontrollers are connected on different breadboards, but are very close to each other. The servo is controlled using a potentiometer, whose amount of rotation also proportionately rotates the servo. The potentiometer is connected to the ADC0 pin. Baud rate is 9600 bps. The PWM output is from OC0A pin.

    My problem is, whenever I turn the potentiometer, there is about 0.5 second delay in the servo rotation. So, if I keep on rotating the potentiometer from one direction to the other, the servo does not turn as smoothly as it should, and instead turns in 1 second intervals.

    I tried the program on a single ATmega32, without the UART communication, and the servo reciprocated the movement of the potentiometer instantly.

    So, why is it not doing through UART? Do I need to mess around with the fuse bits to increase the clock speed to 8 MHz? Or do I need to add an external crystal - something I am trying to avoid due to additional cost.

    Please advice...
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    It would appear that there is some processing delay in the code of the microcontrollers.
    You will have to post the code for both transmitter and receiver mcu.
     
  3. yaijl

    Thread Starter New Member

    Dec 30, 2013
    2
    0
    Okay, I will post the code as soon as my school re-opens in january. it is in my drive.

    However, what I can suspect that is causing the said processing delay, is that I am using interrupts for UART communciation. So, in the main loop, I am waiting for the UART TX register to be empty and then I feed it the position of the potentiomter. At the RX end too, I am waiting for the RX register to be empty and then feeding it the incoming 8 bit UART signal. Then, I am using that information to drive the servo by producing the appropriate length of PWM.

    Is this a possible culprit?
     
    Last edited: Dec 30, 2013
  4. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,542
    2,369
    One USART word at 9600 baud takes around 1ms to transmit, so it should not be that delay, unless you have a repeated interrupt condition?
    Max.
     
  5. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,361
    As max says, it takes only 1ms to transmit one byte at 9600 baud.

    You are experiencing delays of 500ms.
    Did you write the codes yourself? Then ask yourself where and how can a delay of 500ms be introduced? I cannot think of too many possibilities.

    1) software delay in the code
    2) hardware timer delay
    3) interrupts locking out polling
    4) watch dog timer kicking in
    5) power supply drop out
     
Loading...