新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2024-2-29
- 在线时间
- 20 小时
|
请问我舵机无法控制,冒犯是什么问题
#include "pwmdac.h"
#include "led.h"
#include "usart.h"
//////////////////////////////////////////////////////////////////////////////////
//±¾3ìDòÖ»1©Ñ§Ï°ê1óã¬Î′¾-×÷ÕßDí¿é£¬2»μÃóÃóúÆäËüèÎoÎóÃí¾
//ALIENTEK STM32F407¿a·¢°å
//¶¨ê±Æ÷ Çy¶ˉ′úÂë
//ÕyμãÔ-×ó@ALIENTEK
//¼¼êõÂÛì3:www.openedv.com
//′′½¨èÕÆú:2014/5/4
//°æ±¾£oV1.0
//°æè¨ËùóD£¬μá°æ±Ø¾¿¡£
//Copyright(C) 1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾ 2014-2024
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
u16 comp;
extern double du;
void TIM2_CH2_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE); //TIM9ê±Öóê1Äü
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //ê1ÄüPORTAê±Öó
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //GPIOA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //Ëù¶è100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//éÏà-
GPIO_Init(GPIOA,&GPIO_InitStructure); //3õê¼»ˉPA3
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_TIM2); //GPIOA3¸′óÃ붨ê±Æ÷9 AF3
TIM_TimeBaseStructure.TIM_Prescaler=psc; //¶¨ê±Æ÷·ÖÆμ
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //ÏòéϼÆêyÄ£ê½
TIM_TimeBaseStructure.TIM_Period=arr; //×Ô¶ˉÖØ×°ÔØÖμ
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//3õê¼»ˉ¶¨ê±Æ÷9
//3õê¼»ˉTIM14 Channel1 PWMÄ£ê½
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
// TIM_OCInitStructure.TIM_Pulse=0;
TIM_OC2Init(TIM2, &TIM_OCInitStructure); //¸ù¾YTÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIM9 OC2
TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable); //ê1ÄüTIM9ÔúCCR2éÏμÄԤװÔؼÄ′æÆ÷
TIM_ARRPreloadConfig(TIM2,ENABLE);//ARPEê1Äü
TIM_Cmd(TIM2, ENABLE); //ê1ÄüTIM9
}
void Angle(double du)
{
comp = (u16)(10 * (du / 9)) + 50;
TIM_SetCompare2(TIM2,comp-1);//Ä¿±êÕ¼¿Õ±è»»Ëã3é×a»»¶èêy
}
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "lcd.h"
#include "key.h"
#include "pwmdac.h"
//ALIENTEK ì½Ë÷ÕßSTM32F407¿a·¢°å êμÑé22
//PWM DAC êμÑé -¿aoˉêy°æ±¾
//¼¼êõÖ§3Ö£owww.openedv.com
//ìÔ±|μêÆì£ohttp://eboard.taobao.com
//1ãÖYêDDÇòíμç×ó¿Æ¼¼óDÏT1«Ë¾
//×÷ÕߣoÕyμãÔ-×ó @ALIENTEK
#define pwm_zhong 90.0
#define pwm_max_L 68.0
#define pwm_max_R 30.0
double du;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÏμí3ÖD¶ÏóÅÏ輶·Ö×é2
delay_init(168); //3õê¼»ˉÑóê±oˉêy
uart_init(115200); //3õê¼»ˉ′®¿ú2¨ìØÂêÎa115200
LED_Init(); //3õê¼»ˉLED
LCD_Init(); //LCD3õê¼»ˉ
KEY_Init(); //°′¼ü3õê¼»ˉ
TIM2_CH2_PWM_Init((20000-1),84-1); //84M/84=1MhzμļÆêyÆμÂê,ÖØ×°ÔØÖμ500£¬ËùòÔWMÆμÂêÎa 1M/500=2Khz.
POINT_COLOR=RED;
LCD_ShowString(30,50,200,16,16,"Explorer STM32F4");
LCD_ShowString(30,70,200,16,16,"WM DAC TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
LCD_ShowString(30,110,200,16,16,"2014/5/6");
while(1)
{
Angle(pwm_zhong);
LCD_ShowString(30,70,200,16,16,"pwm_zhong");
delay_ms(1000);
Angle(pwm_max_L);
LCD_ShowString(30,70,200,16,16,"pwm_max_L");
delay_ms(1000);
Angle(pwm_max_R);
LCD_ShowString(30,70,200,16,16,"pwm_max_L");
delay_ms(1000);
}
}
|
|