how to control servo motor using embedded c using simple code?

2.5k views Asked by At

i'm working on a school project "automatic railway system" my project suppose to close the gate when the train coming to the station with a buzzer on with 90 sec count down display on 7-seg. and a led flashing. after the train leaving the station, the gate opens and the buzzer off and the led off . i tried to use a dc motor to open and close the gate but it didn't give me the accurate angle that i need to i try to use a servo motor . so i need it to open the gate at position zero and close it at position 90 . all the code i found on the internet they using PWM and timers which i didn't take it in my course , so can anyone help me to do this with simple code ,please ?

i'm using Atmega32 running at 16000000 HZ

1

There are 1 answers

0
duck On

Its depend on your analog servo (which is controlled by PWM) frequency specification. After you learn about the servo specification, you can set your PWM using build-in features on cvavr compiler, or you can do some research about PWM registers.
Here is some example of PWM setup

//using OC0 (B.3)
DDRB.3 = 1; //set B.3 as output
TCCR0=0b0111 0001;
TCNT0=0; //set to Phase Correct PWM mode, no prescaler, and inverted output

//to assign a value to your PWM
OCR0 = 127 //50% duty cycle since it was 8 bit