A DC servo motor comes with an electronic hardware control, to rotate to any specific angle. Typically, It can rotate between -90 degrees to +90 degrees.
The SG90 simple servo motor |
Servo Angle Positioning |
It come with three pins, in which two pins for supply voltage, the last one is for DC control signal.
The control signal is a square wave with the amplitude of 5 V. Typically, it accept a frequency of 50 Hz. The angle positioning is made by the High time varies from 1 ms to 2 ms.
Servo timing diagram 1 ms to 2 ms duty makes -90 degree to +90 degree angle rotation. |
Schematic Diagram. A servo motor connects to PB0. PC0.3 connect to the switches controlling the angle of servo. |
Servo is controlled by output signal from PB0. I use three button to control the servo angle to -90 degree, 0 degree and +90 degree.
Source code could be download here./*
* servoExample_1.c
*
* Created: 4/26/2020 9:10:13 AM
* Author : bonTha
*/
#include <avr/io.h>
#define F_CPU 16000000UL
#include "util/delay.h"
#define rotate__90 (PINC&0x01)
#define rotate_0 (PINC&0x02)
#define rotate_90 (PINC&0x04)
void servoRotate(short angle){
switch(angle){
case -90:
PORTB=0x01;
_delay_us(1000);
PORTB=0x00;
_delay_us(19000);
break;
case 0:
PORTB=0x01;
_delay_us(1500);
PORTB=0x00;
_delay_us(18500);
break;
case 90:
PORTB=0x01;
_delay_us(2000);
PORTB=0x00;
_delay_us(18000);
break;
default:
break;
}
}
int main(void)
{
DDRB=0x01; //PB0 IS OUTPUT
DDRC=0xF8; //PC0.2 ARE INPUT
PORTC=0x07;
while (1)
{
if(rotate__90==0) servoRotate(-90);
else if(rotate_0==0) servoRotate(0);
else if(rotate_90==0) servoRotate(90);
}
}
Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.
If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.
No comments:
Post a Comment