forked from ypwhs/smartCar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
舵机.c
executable file
·124 lines (105 loc) · 2.59 KB
/
舵机.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "dma.h"
#include "ftm.h"
#include "lptmr.h"
#define offset 77
void turn(int angel){
angel += offset;
int pwm = (int)((angel/90.0 + 0.5) * 500); //90度是1.5ms
printf("turn:%d\r\n", pwm);
FTM_PWM_ChangeDuty(HW_FTM1, HW_FTM_CH0, pwm);
}
#define DRIVER_PWM_WIDTH 1000
void initDriver(){
printf("initPWM\r\n");
for(int i=0;i<0;i++)
GPIO_QuickInit(HW_GPIOC, i, kGPIO_Mode_OPP);
PCout(0)=1;
//使能INH
FTM_PWM_QuickInit(FTM1_CH0_PA12, kPWM_EdgeAligned, 50); //设置FTM,边沿对齐模式
turn(0);
//初始化舵机
FTM_PWM_QuickInit(FTM0_CH0_PC01, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH1_PC02, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH2_PC03, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
FTM_PWM_QuickInit(FTM0_CH3_PC04, kPWM_EdgeAligned, DRIVER_PWM_WIDTH);
//初始化电机PWM输出
}
void setLeftSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH1, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH0, -spd);
}
}
void setRightSpeed(int spd){
if(spd>0){
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, spd);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, 0);
}else
{
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH2, 0);
FTM_PWM_ChangeDuty(HW_FTM0, HW_FTM_CH3, -spd);
}
}
void delay(){
DelayMs(500);
}
#define spd 1000
//串口接收中断
void UART_RX_ISR(uint16_t byteRec){
PCout(3)=!PCout(3);
switch(byteRec){
case 'w':
setLeftSpeed(spd);
setRightSpeed(spd);
break;
case 's':
setLeftSpeed(-spd);
setRightSpeed(-spd);
break;
case 'a':
turn(-30);
break;
case 'd':
turn(30);
break;
case 'x':
turn(0);
break;
default:
setLeftSpeed(0);
setRightSpeed(0);
break;
}
printf("%c", byteRec);
}
void initUART(){
UART_QuickInit(UART0_RX_PB16_TX_PB17, 115200);
/*
UART0_RX_PB16_TX_PB17
UART4_RX_PC14_TX_PC15
*/
/* 注册中断回调函数 */
UART_CallbackRxInstall(HW_UART0, UART_RX_ISR);
/* 开启UART Rx中断 */
UART_ITDMAConfig(HW_UART0, kUART_IT_Rx, true);
}
int main(void)
{
DelayInit();
/* 打印串口及小灯 */
GPIO_QuickInit(HW_GPIOD, 10, kGPIO_Mode_OPP);
initUART();
initDriver();
while(1)
{
PDout(10)=!PDout(10);
DelayMs(100);
}
}