伺服馬達(Servo motor)憑準確、小型、高效,易用在機器人領(lǐng)域應(yīng)用廣泛,其高扭矩特性非常適合升降重物。本項目用電位器改變馬達轉(zhuǎn)子位置,用STM32F103C8(藍丸)控制伺服馬達,用LCD顯示角度值。
項目BOM表及電路連接
項目BOM表如下:
1、STM32F103C8藍丸板 x1
2、伺服馬達(SG90) x1
3、LCD(16x2) x1
4、電位器 x2
5、面包板 x1
6、跳線 若干
?
STM32F103C8有10路ADC引腳(PA0-PB1),本項目使用PA3作為analogread(),通過電位器設(shè)定馬達的位置。在STM32引腳的15 PWM引腳中(PA0, PA1, PA2, PA3, PA6, PA7, PA8, PA9, PA10, PB0, PB1, PB6, PB7, PB8, PB9),我們只使用其中的一個引腳,為伺服馬達的PWM引腳(該引腳通常帶有顏色)提供脈沖信號。
STM32F103C8與LCD的連接如下:
STM32F103C8 LCD
GND VSS
+5V VDD
PB0 RS
GND RW
PB1 E
PB10 D4
PB11 D5
PC13 D6
PC14 D7
+5V A
GND K
伺服馬達與STM32F103C8的連接如下:
STM32F103C8 伺服馬達
+5V RED (+5V)
PA0 ORANGE (PWM pin)
GND BROWN (GND)
?
本項目使用兩個電位器:
(1)右邊的用來改變LCD對比度。左邊接5V電源,右邊接地,中間引腳與LCD的V0連接。
(2)左邊的用來模擬輸入電壓的大小,以改變伺服馬達轉(zhuǎn)子的位置。左邊引腳接3.3V電源,右邊接地,中間連接與的STM32板子的PA3引腳。
編程及代碼說明
如果電腦安裝了Arduino IDE,連接上USB接口,就可以像使用Arduino一樣,對STM32F103C8進行編程了,無須使用FTDI編程器。
首先,載入馬達和LCD函數(shù):
#include
#include
其次,聲明LCD顯示器引腳并初始化。同時還要聲明其他幾個用于PWM和電位器的變量:
const int rs = PB0, en = PB1, d4 = PB10 , d5 = PB11 , d6 = PC13, d7 = PC14;
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);
int servoPin = PA0;
int potPin = PA3;
這里,我們創(chuàng)建了伺服變量,并將其賦予前述聲明的PWM引腳。
Servo servo;
servo.attach(servoPin);
然后,從ADC引腳——PA3讀取模擬值,將模擬電壓(0-3.3)轉(zhuǎn)換成數(shù)字形式(0-4095)。
analogRead(potPin);
該ADC為12位,我們需要獲得的0-170模擬值,要將其均分為(0-4096)數(shù)字形式。
angle = (reading/24);
下面指令使伺服馬達以給定的角度旋轉(zhuǎn)轉(zhuǎn)軸:
servo.write(angle);
完整的代碼如下:
//INTERFACE SERVO WITH STM32
//CIRCUIT DIGEST
#include //including servo library
#include //including LCD display library
const int rs = PB0, en = PB1, d4 = PB10 , d5 = PB11 , d6 = PC13, d7 = PC14; //declaring pin names and pin numbers of lcd
LiquidCrystal lcd(rs,en,d4,d5,d6,d7);//setting lcd and its paramaters
int servoPin = PA0; //declare and initialize pin for servo output PWM
int potPin = PA3; //potentiometer ADC input
Servo servo; // creating variable servo with datatype Servo
void setup()
{
lcd.begin(16,2); //setting lcd as 16x2
lcd.setCursor(0,0); //setting cursor at first row and first column
lcd.print("CIRCUIT DIGEST"); //puts CIRCUIT DIGEST in LCD
lcd.setCursor(0,1); //setting cursor at second row and first column
lcd.print("SERVO WITH STM32"); //puts SERVO WITH STM32 in LCD
delay(3000); // delays for 3 seconds
lcd.clear(); //clears lcd display
servo.attach(servoPin); //it connects pin PA0 with motor as control feedback by providing pulses
}
void loop()
{
lcd.clear(); //clears lcd
int angle; //declare varible angle as int
int reading; //declare varible reading as int
reading = analogRead(potPin); //read analog value from pin PA3
angle = (reading/24); //it divides ADC the value according to max angle 170 deg
servo.write(angle); //it puts angle value at servo
lcd.setCursor(0,0); //setting cursor at first row and first column
lcd.print("ANGLE:"); //puts ANGLE in LCD
lcd.print(angle); //puts value at angle
delay(100); //delay in time
-
lcd
+關(guān)注
關(guān)注
34文章
4436瀏覽量
167952 -
伺服馬達
+關(guān)注
關(guān)注
0文章
21瀏覽量
7750 -
電路連接
+關(guān)注
關(guān)注
0文章
12瀏覽量
6680 -
STM32F103C8
+關(guān)注
關(guān)注
1文章
23瀏覽量
8100
發(fā)布評論請先 登錄
相關(guān)推薦
評論