Skip to content

Instantly share code, notes, and snippets.

@surinoel
Created July 13, 2019 09:11
Show Gist options
  • Save surinoel/c1a93cafb0f7b7003bb5721dde8bc312 to your computer and use it in GitHub Desktop.
Save surinoel/c1a93cafb0f7b7003bb5721dde8bc312 to your computer and use it in GitHub Desktop.
/*
* servo.c
*
* Created: 2019-06-30 오후 3:41:18
* Author : yeong
*/
#include <avr/io.h>
#include "timer3.h"
#include "switch.h"
int main(void)
{
// 서보를 돌린다
// 돌리기 위해선 PWM 3번 타이머, 14번 고속 PWM(TOP ICR3, OCR3A => OC3A)
// ICR3 => 20ms 파형을 맞추고, OCR3A = ICR3 * 1/10 => -90, ICR3 * 3/20 => 0, ICR3 * 1/5 => 90
timer3_pwm_init();
switch_exti_init();
while (1)
{
}
}
#include "timer3.h"
void timer3_pwm_init(void)
{
DDRE |= (1<<3);
TCCR3A |= (1<<COM3A1) | (1<<WGM31);
TCCR3B |= (1<<WGM33) | (1<<WGM32) | (1<<CS32);
ICR3 = 1250;
OCR3A = 93;
}
void servo_control(void)
{
OCR3A = 35;
_delay_ms(1000);
OCR3A = 93;
_delay_ms(1000);
OCR3A = 150;
_delay_ms(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment