- UID
- 373967
- 帖子
- 8867
- 主題
- 2609
- 精華
- 0
- 積分
- 1000
- 楓幣
- 15
- 威望
- 977
- 存款
- 34822
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 455 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-11-22
|
製作聲控地球儀所需的零件清單如下:
Atmel ATtiny2313-20PU 微控制器
MAX4466 麥克風放大器模塊
3D列印的地球儀模型
SG90 9克微型伺服馬達
L293D 馬達驅動器IC
HC-05 藍牙模組
5V 電源供應器
電線、焊錫、焊接工具等
連接腳位如下:
ATtiny2313 MAX4466 SG90 L293D HC-05
PB0 OUT - - TXD
PB1 - SIGNAL IN1 RXD
PB2 - VCC VCC VCC
PB3 - GND GND GND
PB4 - - IN2 -
PB5 - - ENABLE -
程式碼如下:
#include <avr/io.h>
#include <util/delay.h>
#include <string.h>
#define F_CPU 1000000UL
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1
void usart_init(unsigned int ubrr) {
UBRRH = (unsigned char)(ubrr >> 8);
UBRRL = (unsigned char)ubrr;
UCSRB = (1 << RXEN) | (1 << TXEN);
UCSRC = (1 << URSEL) | (1 << USBS) | (3 << UCSZ0);
}
void usart_transmit(unsigned char data) {
while (!(UCSRA & (1 << UDRE)));
UDR = data;
}
void servo_write(int angle) {
OCR1A = (unsigned int)(2000 + angle * 11.11); // calculate duty cycle for given angle
_delay_ms(500);
}
int main(void) {
DDRB |= (1 << PB4) | (1 << PB5); // configure motor driver pins as outputs
PORTB &= ~((1 << PB4) | (1 << PB5)); // set motor driver pins to low
TCCR1A |= (1 << COM1A1) | (1 << WGM11); // configure timer1 for fast PWM mode
TCCR1B |= (1 << WGM13) | (1 << WGM12) | (1 << CS11); // prescaler = 8
ICR1 = 20000; // f_PWM = 50 Hz
servo_write(0); // initial position
usart_init(MYUBRR); // initialize USART
while (1) {
unsigned char data;
while (!(UCSRA & (1 << RXC))); // wait for data to be received
data = UDR;
if (data == 'N') { // North
servo_write(0);
} else if (data == 'S') { // South
servo_write(180);
} else if (data == 'E') { // East
Copy code
servo_write(90);
} else if (data == 'W') { // West
servo_write(270);
}
}
}
這個程式使用了ATtiny2313的PWM功能控制SG90微型伺服馬達的角度,當接收到HC-05藍牙模組發送的聲控指令時,會根據指令調整伺服馬達的角度,以顯示正確的地球儀方向。 |
|