Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Design Assignment #6

Code Stepper:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
float i;

void delay_ms(uint16_t count)
{
while(count--)
{
_delay_ms(1);
}
}
ISR(TIMER1_COMPA_vect)
{
ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
i = ADC*(-.0879) + 100;
}

int main(void)
{

DDRB = 0xFF; //make PORTD output
ADCSRA = 0x86; //turn on ADC select clock 1/64
TCCR1B = 0x0D; //set timer1 CTC mode and prescaler 1/1024
OCR1A = 3000; //make timer1 count up to 3000, raise compare on OC1A
interrupt every .38 s which starts A2D conversion
TIMSK1 = 0x02; //enable OCR1A compare interrupt

ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
i = ADC*(-.0879) + 100;

sei();

while(1) //turn Stepper using sequence

{
PORTB = 0X66;
delay_ms((int)i);
PORTB = 0xCC;
delay_ms((int)i);
PORTB = 0x99;
delay_ms((int)i);
PORTB = 0x33;
delay_ms((int)i);
}
}

Code Flowchart:


Set timer1 to CTC mode.
Set OCR1A to 3000 to generate a
match every .38 seconds.
Enable compare on OCRA match
interrupt.
Make stepper turn using
sequence. Delay/speed based on
A2D value.











































Stepper Schematic
Do A2D conversion to update delay.
OCR1A match
interrupt.






















Code Servo:
#define F_CPU 8000000UL
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>


float i;
ISR(TIMER1_COMPA_vect)
{
ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
i = ADC*(2.1) + 400;
}
int main()
{
ADCSRA = 0x86; //turn on ADC select clock
1/64

DDRB = 0xFF; //make PORTB output

//TOP = ICR1;
//output compare OC1B 8 bit non inverted PWM
//Clear OC1B on Compare Match, set OC1B at TOP
//Fast PWM
//ICR1 = 19999 defines 50Hz pwm
ICR1 = 19999;
TCCR1A = 0x22;
TCCR1B = 0x1A;
TIMSK1 = 0x02;
//start timer with prescaler 8
OCR1A = 10000; //on OCR1A match raise
interrupt to start new A2D conversion


ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA & (1<<ADIF)) == 0); //wait till conversion is done
i = ADC*(2.1) + 400;

sei();

while(1)
{
OCR1B =(int)i;
}

}








Code flowchart

























Make timer1 generate a PWM at 50 Hz
on OC1B, duty cycle is set by changing
OC1B.
Enable interrupt on OCR1A match.
Get initial A2D value.
Turn stepper based on A2D
value/duty cycle.
Do A2D conversion to update duty
cycle.
OCR1A match
interrupt
Servo Schem:

You might also like