http://www.ee.ui.ac.id/eecourse
http://www.ee.ui.ac.id/eecourse
Motor Servo
Generate PWM for Motor Servo (AVR)Method #1 (use delay)
#include
#define MotorOut PORTB.0
void main(void)
Method #2 (timer interrupt)
#define MotorOut PORTB.0
int duty=100;
int period=2000;
int count=0;
interrupt [TIM1_COMPA] ...
Method #3 (timer output)
#include
#define MotorOut OCR1A
void main(void)
{
....
while (1) {
MotorOut=1;
delay_us(1000); // 1ms
MotorOut=0;
delay_us(20000-1000);
}
}
interrupt [TIM1_COMPA] ...
{ // interrupt every 10us
if (count=0) MotorOut=1;
if (count>duty) MotorOut=0;
if (count>=period) count=0;
count++;
}
void main(void)
{
....
}
{
MotorOut=100; //1ms
delay_ms(1); // 1s later
MotorOut=200; //2ms
}
AVR – Motor Servo (connection)
DC Brush
H-bridge - forwardforward
H-bridge - reverse
Current Limiting(high-side current shunt)
Real Condition Fenomenon
Driving H-bridge
Driving H-bridge
????
Driving H-bridge
100 Ω resistance
Maxon motor
Cheap motor
Driving H-bridge
100 Ω resistance
Maxon motor
Connection
Unipolar vs BipolarIf you are unfamiliar with the difference
between bipolar and unipolar stepper
motors, the following information may be
useful. Unipolar motors were at one time
much cheaper to use because they needed
less complicated circuitry. Rather than
switching the direction of current through its
coils, a unipolar motor simply has two sets coils, a unipolar motor simply has two sets
of coils dedicated to the 2 possible
directions of current flow. Bipolar motors
have only one coil. The depend on external
circuitry to switch the current through them.
Unipolar motors can only dedicate half their
coil toward motion in one direction,
whereas bipolar motors can devote all. The
end result is that bipolar motors, with
essentially the same coil mass, deliver
about 40% higher torque.
Motor DC Stepper
'1'
'0 '
'0 '
'0'
'1' '0 '
Rotor Rotor
L1
L2
L3
L4
L1
L2
L3
L4
Stepke
Full Step Half Step
1 1 0 0 0 1 0 0 0
2 0 1 0 0 1 1 0 0
3 0 0 1 0 0 1 0 0
'0 '
'0 '
Rotor Rotor L3
'1'
'0 '
'0 '
'0 '
'1'
'1'
'0 '
'0 '
Rotor Rotor
L1
L2
L3
L4
L1
L2
L3
L4
3 0 0 1 0 0 1 0 0
4 0 0 0 1 0 1 1 0
5 Berulang ke
step 1
0 0 1 0
6 0 0 1 1
7 0 0 0 1
8 1 0 0 1
Berulang ke
step 1
Unipolar
Full Step Unipolar
Full Mode Sequence
Step A B A\ B\
0 1 1 0 00 1 1 0 0
1 0 1 1 0
2 0 0 1 1
3 1 0 0 1
Half Step UnipolarHalf Mode Sequence
Step A B A\ B\
0 1 1 0 0
1 0 1 0 01 0 1 0 0
2 0 1 1 0
3 0 0 1 0
4 0 0 1 1
5 0 0 0 1
6 1 0 0 1
7 1 0 0 0
Bipolar step sequence
Step Sequence
Step A A\ B B\
0 1 0 0 00 1 0 0 0
1 0 0 1 0
2 0 1 0 0
3 0 0 0 1
Unipolar stepper dgn L293
Bipolar stepper
Programming Full Step#define stepper P1void delay();
void main(){while(1){
stepper = 0x0C;delay();stepper = 0x06;delay();stepper = 0x03;stepper = 0x03;delay();stepper = 0x09;delay();
}}
void delay(){unsigned char i,j,k;for(i=0;i
Programming Half Stepvoid main(){
while(1){stepper = 0x08;delay();stepper = 0x0C;delay();stepper = 0x04;delay();stepper = 0x06;stepper = 0x06;delay();stepper = 0x02;delay();stepper = 0x03;delay();stepper = 0x01;delay();stepper = 0x09;delay();
}}
Programming (bipolar)#define RIGHT 1
#define LEFT 0
unsigned char direction;
unsigned char i;
flash unsigned char string[]={0x8, 0xC, 0x4, 0x6, 0x2, 0x3, 0x1, 0x9};
void main() {
....
while (1) {
if (direction==LEFT) { if (direction==LEFT) {
i--;
if (i==0xFF) i=7;
PORTC=(string[i] | 0xF0);
} else {
i++;
if (i==8) i=0;
PORTC=(string[i] | 0xF0);
}
count++;
if (count%100==0)
direction =(direction ==RIGHT)? LEFT:RIGHT;
}; // end while
}
Connection
Power Power
AVR
Micro
Controller
LCD
PC
measurement
AVR
Micro
Controller
SensorPC
Switch
button
LCD LED
Opto-couplerMotor
Stepper
Reading Encoderunsigned char i=0, count1=0, count2=0;unsigned int enc_pos;unsigned char enc_dir;
void countenc (void) { static unsigned char enc_last=0,enc_now;enc_now = (PIND & 0xC)>>2; //read the port pins and shift result to bottom bits if (enc_now==enc_last) return;enc_dir = (enc_last & 1)^((enc_now & 2) >> 1); //determine direction of rotationif(enc_dir==0) enc_pos++; else enc_pos--; //update encoder positionenc_last=enc_now; //remember last state i=(unsigned char) enc_pos;
}}
// External Interrupt 0 service routineinterrupt [EXT_INT0] void ext_int0_isr(void){
countenc(); count1++;
}
// External Interrupt 1 service routineinterrupt [EXT_INT1] void ext_int1_isr(void){
countenc(); count2++;
}