NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Stepper motor code question

May 31, 2011
by missle3944
missle3944's Avatar

Hi, I just got my stepper motor working. But I am experiencing one problem, it is that for some reason when I change the delay in my code the stepper doesnt have the delay that I want. It seems very weird. Currently I have the delay set to 10ms but the stepper has about 1 second in between each step. Heres my code, Sorry for all of the #includes

#define F_CPU 14745600

#include <stdio.h>

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>

#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <inttypes.h>
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"#include "../libnerdkits/delay.h"
#include "../libnerdkits/lcd.h"
#include "../libnerdkits/uart.h"

// PIN DEFINITIONS:
//
// PC4 -- LED anode
int x;

int main() {

  lcd_init();
  FILE lcd_stream = FDEV_SETUP_STREAM(lcd_putchar, 0, _FDEV_SETUP_WRITE);
  lcd_home();

  // start up the serial port
  uart_init();
  FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  stdin = stdout = &uart_stream;

  while(1) {
   lcd_home();

    lcd_line_two();
   fprintf_P(&lcd_stream, PSTR("Stepper POS %d"), x);
   // LED as output
  PORTC |= (1<<PC5);
    x = 1;
    delay_ms(10);

    // turn off LED
    PORTC &= ~(1<<PC5);
//////////////////////////////////1
  PORTC |= (1<<PC4);
    x = 2;

    delay_ms(10);

    // turn off LED
    PORTC &= ~(1<<PC4);
    ////////////////////////////2
    PORTC |= (1<<PC3);
    x = 3;

    delay_ms(10);
    // turn off LED
    PORTC &= ~(1<<PC3);
    ///////////////////////////////////////////////////3
    PORTC |= (1<<PC2);
    x = 4;

    delay_ms(10);

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

    ///////////////////////////////////////////////////4
  }

  return 0;
}

Post a Reply

Please log in to post a reply.

Did you know that you can input numbers in binary via a DIP switch, and output them to the LCD? Learn more...