BASIC4MCU | 질문게시판 | ATmega328p + pca9685 + 서보모터
페이지 정보
작성자 토비 작성일2023-02-08 10:30 조회201회 댓글0건본문
안녕하세요, atmega328p에 pca9685를 이용하여 서보모터를 움직이려고 합니다.
원래는 아래의 링크에서 pca9685.h에서 각도를 조절하여 사용하였습니다.
https://github.com/prestonsn/PCA9685-Multi-Channel-Servo-Controller-Driver-for-AVR-ATmega
이 상태에서 pulse값을 이용하여 서보모터를 조작하려고 하니 어떻게 해야할지 감이 안 옵니다.
혹시 알려주실 수 있나해서 고민하다 글 올립니다.
아래는 챗GPT에 질문하였더니 알려준 코드입니다.
위 깃허브 주소에는 pca9685_pwm() 함수가 없어 사용할 수 없습니다...
```#define F_CPU 16000000L
#include <avr/io.h>
#include <util/delay.h>
#include "pca9685.h"
#define SERVO_PIN 0 // Pin number for the servo on PCA9685
#define SERVO_MIN 500 // Minimum pulse width in microseconds
#define SERVO_MAX 2500 // Maximum pulse width in microseconds
void init_servo() {
pca9685_init(0x00, 50); // Initialize the PCA9685 with a 50Hz frequency
}
void set_servo_pulse(uint16_t pulse) {
pca9685_pwm(SERVO_PIN, 0, pulse); // Set the pulse width on the specified pin
}
int main(void) {
DDRB |= 0x03; // Set PORTB as output
init_servo(); // Initialize the servo
while (1) {
for (uint16_t pulse = SERVO_MIN; pulse <= SERVO_MAX; pulse += 10) {
set_servo_pulse(pulse);
_delay_ms(50);
}
for (uint16_t pulse = SERVO_MAX; pulse >= SERVO_MIN; pulse -= 10) {
set_servo_pulse(pulse);
_delay_ms(50);
}
}
return 0;
}
```
댓글 0
조회수 201등록된 댓글이 없습니다.