Projects‎ > ‎

ATMega328P

Stepper Motor Controller

posted Nov 2, 2010, 3:02 PM by Bill Bai

Here's another simple program that controls a stepper motor.  

/*
Bill Bai

Stepper Motor Controller
*/

#include <avr/io.h>
#include <avr/interrupt.h>

#define CLK 20000000 //20MHz clock

//Define functions
//======================
void ioinit(void);      //Initializes IO
void stepper(void);
void init_Timer0(void);
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults
init_Timer0();
sei();
//unsigned char step[4] = {0x09,0x0C,0x06,0x03};

    while(1)
    {
    }
   
    return(0);
}

unsigned char step = 0;
void stepper(void)
{
unsigned char step_array[8] = {0x0C,0x04,0x06,0x02,0x03,0x01,0x09,0x08};
if(step==8)
step=0;
PORTC = step_array[step];
step++;
}

unsigned int count = 0;
ISR(TIMER0_OVF_vect)
{
    if( count == 100 ) //100*1ms
{
stepper();
}
else
{
count++;
}
PORTB = PORTB ^ 0xFF;
TCNT0 = 0; //reset timer 0 counter
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0b11111111; //All outputs
    DDRC = 0b11111111; //All outputs
    DDRD = 0b11111111; //PORTD (RX on PD0)
}

//Initialize Timer0
void init_Timer0(void)
{
TCNT0 = 255;
TCCR0A = 0;
TCCR0B = 5;
TIMSK0 = 1;
}

Blinky

posted Nov 2, 2010, 2:57 PM by Bill Bai   [ updated Nov 2, 2010, 3:03 PM ]

When I program, I like to get one small piece of the puzzle working and then start digging into the meaty bits.  This is just a simple program that blinks and LED on PORTD.  It's based on a 1ms Timer 0 interrupt.  Good times.

/*
Bill Bai

Blinking LED based on tutorial from Sparkfun
http://www.sparkfun.com/commerce/tutorial_info.php?tutorials_id=93
*/

#include <avr/io.h>
#include <avr/interrupt.h>

//Define functions
//======================
void ioinit(void);      //Initializes IO
void init_Timer0(void);
//======================

int main (void)
{
    ioinit(); //Setup IO pins and defaults
init_Timer0();
sei(); //enable interrupts

    while(1){}
   
    return(0);
}

unsigned int count = 1;
ISR(TIMER0_OVF_vect)
{
if(count == 500)
{
count = 1;
PORTD ^= 0xFF;
}
count++;
TCNT0 = 0xB2; //reset timer 0 counter
}

void ioinit (void)
{
    //1 = output, 0 = input
    DDRB = 0xFF; //All outputs
    DDRC = 0xFF; //All outputs
    DDRD = 0xFF; //PORTD (RX on PD0)
}

//Initialize Timer0
void init_Timer0(void)
{
TCNT0 = 0xB2;
TCCR0A = 0;
TCCR0B = 4;
TIMSK0 = 1;
}

AVR Pocket Programmer

posted Apr 5, 2010, 12:58 AM by Bill Bai

It's fairly simple to open up a notepad on the computer and start throwing down code, but the key link between software and hardware is this AVR Pocket Programmer.  

Pretty much all microcontrollers these days have In-System Programmers (ISPs) which allow new code to be programmed onto them while they are still in the system.  This seems like an obvious thing, but I've had to work with microcontrollers and programmable logic devices that did not have ISPs.  It was a pain in the butt to remove the chip from the circuit, plug it into a special programming socket, download the program, and then put the chip back into the circuit only to find a typo two seconds later and have to repeat the process.  

The chip is still installed in the system, and peripherals can be attached.

The ISP allows that entire process to be avoided by programming the chip while it's still in the circuit and attached to all of it's other peripherals.

This picture shows the 6 connections of the ISP.  The grey ribbon cable in the top left is connected to the AVR Pocket Programmer.

This is a schematic of the 6pin connector at the end of the ribbon cable that connects the programmer to the chip.

The ISP puts the program onto the microcontroller via the Serial Peripheral Interface (SPI) connection module of the ATMega328P.  The Master-Out Slave-In (MOSI), Master-In Slave-Out (MISO) and Serial ClocK (SCK) lines do the heavy lifting of getting the code from the computer into the memory of the microcontroller.  VCC provides +5V and GND is ground/common.  RST connects to the reset pin of the microcontroller.  The reset pin controls the state of the microcontroller.  Without the reset held low (the reset pin is active low), the microcontroller will go on its merry way running the program already on it.  It will just ignore the data coming down the SPI line as gibberish.  However, with the reset held low, the microcontroller enters a programming state; wherein it listens to the data coming in on the SPI interface and programs itself.

Now that the physical connection from the computer to the microcontroller is set up, we can begin to program the micro to do interesting things.

Programming a MicroController from Scratch

posted Apr 4, 2010, 1:46 PM by Bill Bai

I do quite a bit of microcontroller programming at school.  However, the microcontroller we use at school has a nice development board with preloaded bootloader software.

I saw a tutorial on microcontroller programming on Sparkfun.com and decided to try programming a micro from scratch.  To this end, I bought an ATMega328P microcontroller and Pocket AVR Programmer from Sparkfun.

1-4 of 4