#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "adc.h"
float ADC_CH_1=0.0;
float ADC_CH_2=0.0;
static unsigned char TxBuffer[256];
static unsigned char TxCounter=0;
static unsigned char count=0;
extern void CopeSerial1Data(unsigned char ucData);
void UART2_Put_Char(unsigned char DataToSend);
void UART2_Put_String(unsigned char *Str);
void UART_DM_ReportData(unsigned char data[]);
unsigned char duoji0_init[15]={ 0xff, 0x01, 0x00, 0x14, 0x00, 0xff, 0x02, 0x00, 0xdc, 0x05 }; //旋转舵机
unsigned char duoji2_init[15]={ 0xff, 0x01, 0x02, 0x14, 0x00, 0xff, 0x02, 0x02, 0xf4, 0x01 }; //升降舵机
unsigned char duoji4_init[15]={ 0xff, 0x01, 0x04, 0x14, 0x00, 0xff, 0x02, 0x04, 0xd0, 0x07 }; //前后舵机
unsigned char duoji6_init[15]={ 0xff, 0x01, 0x06, 0x14, 0x00, 0xff, 0x02, 0x06, 0xc4, 0x09 }; //机械爪舵机
unsigned char duoji8_init[15]={ 0xff, 0x01, 0x08, 0x14, 0x00, 0xff, 0x02, 0x08, 0xf4, 0x01 }; //升降舵机
unsigned char duoji0_work[15]={ 0xff, 0x01, 0x00, 0x14, 0x00, 0xff, 0x02, 0x00, 0xdc, 0x05 }; //旋转舵机
unsigned char duoji2_work[15]={ 0xff, 0x01, 0x02, 0x14, 0x00, 0xff, 0x02, 0x02, 0xf4, 0x01 }; //升降舵机
unsigned char duoji4_work[15]={ 0xff, 0x01, 0x04, 0x14, 0x00, 0xff, 0x02, 0x04, 0xd0, 0x07 }; //前后舵机
unsigned char duoji6_work[15]={ 0xff, 0x01, 0x06, 0x14, 0x00, 0xff, 0x02, 0x06, 0xc4, 0x09 }; //机械爪舵机
unsigned char duoji8_work[15]={ 0xff, 0x01, 0x08, 0x14, 0x00, 0xff, 0x02, 0x08, 0xf4, 0x01 }; //升降舵机
unsigned char duoji6_open[15]={ 0xff, 0x01, 0x06, 0x14, 0x00, 0xff, 0x02, 0x06, 0xdc, 0x05 }; //机械爪舵机
unsigned char duoji6_close[15]={ 0xff, 0x01, 0x06, 0x14, 0x00, 0xff, 0x02, 0x06, 0xc4, 0x09 }; //机械爪舵机
void NVIC_Configuration(void);
void usart2_init(void);
//void Delay_Ms(u32 nTime);
void TIM2_Base_Init(u16 Count);
extern float Get_AdcValue(u8 ch);
/*******************变量定义**************************************************/
u32 TimingDelay = 0;
u8 str[30];
u8 flag=0;
u8 flag2=0;
u8 j=0;
u16 pwm=500;
u8 pwm_h=0;
u8 pwm_l=0;
u16 PWM_X=1500;
u16 PWM_Y=500;
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
}
void duoji_init(void)
{
UART_DM_ReportData(duoji0_init);
Delay_Ms (1000);
UART_DM_ReportData(duoji2_init);
Delay_Ms (1000);
UART_DM_ReportData(duoji4_init);
Delay_Ms (1000);
UART_DM_ReportData(duoji6_init);
Delay_Ms (1000);
}
int main(void)
{
uint8_t j; //定义变量
u8 key2=0;
LED_Init();//LED初始化
KEY_Init();//按键初始化
SysTick_Init();//延时初始化
NVIC_Configuration();
usart2_init();
Adc_Init();
Delay_Ms (1000);
duoji_init();
while (1)
{
ADC_CH_1=Get_AdcValue(ADC_Channel_0); //旋转
ADC_CH_2=Get_AdcValue(ADC_Channel_1); //升降
if(ADC_CH_1>2)
{
PWM_X=PWM_X+11;
if(PWM_X>2500) PWM_X=2500;
pwm_h=PWM_X >> 8;
pwm_l=PWM_X % 256;
duoji0_work[8]=pwm_l;
duoji0_work[9]=pwm_h;
UART_DM_ReportData(duoji0_work);
Delay_Ms (100);
}
if(ADC_CH_1<1)
{
PWM_X=PWM_X-11;
if(PWM_X<500) PWM_X=500;
pwm_h=PWM_X >> 8;
pwm_l=PWM_X % 256;
duoji0_work[8]=pwm_l;
duoji0_work[9]=pwm_h;
UART_DM_ReportData(duoji0_work);
Delay_Ms (100);
}
if(ADC_CH_2>2)
{
PWM_Y=PWM_Y+11;
if(PWM_Y>2500) PWM_Y=2500;
pwm_h=PWM_Y >> 8;
pwm_l=PWM_Y % 256;
duoji2_work[8]=pwm_l;
duoji2_work[9]=pwm_h;
UART_DM_ReportData(duoji2_work);
Delay_Ms (100);
}
if(ADC_CH_2<1)
{
PWM_Y=PWM_Y-11;
if(PWM_Y<500) PWM_Y=500;
pwm_h=PWM_Y >> 8;
pwm_l=PWM_Y % 256;
duoji2_work[8]=pwm_l;
duoji2_work[9]=pwm_h;
UART_DM_ReportData(duoji2_work);
Delay_Ms (100);
}
if(!S1)
{
Delay_ms(10);
if(!S1)
{
while(!S1);//等待按键释放
LED2_REV;
if(key2==0)
{
UART_DM_ReportData(duoji6_open);
Delay_Ms (1000);
key2=1;
}
else if(key2==1)
{
UART_DM_ReportData(duoji6_close);
Delay_Ms (1000);
key2=0;
}
}
}
////////////////////////////////////////////
if(!S2)
{
Delay_ms(10);
if(!S2)
{
// while(!S2);
// LED3_REV;
// pwm=pwm-110;
// if(pwm<500) pwm=500;
// pwm_h=pwm >> 8;
// pwm_l=pwm % 256;
// duoji1_init[8]=pwm_l;
// duoji1_init[9]=pwm_h;
// UART_DM_ReportData(duoji1_init);
}
}
//////////////////////////////////////////
if(!S3)
{
Delay_ms(10);
if(!S3)
{
// while(!S3);
// LED2_REV;
// LED3_REV;
// pwm=pwm+110;
// if(pwm>2500) pwm=2500;
// pwm_h=pwm >> 8;
// pwm_l=pwm % 256;
// duoji1_init[8]=pwm_l;
// duoji1_init[9]=pwm_h;
// UART_DM_ReportData(duoji1_init);
}
}
///////////////////////////////////////////
if(S4)
{
Delay_ms(10);
if(S4)
{
while(S4);
for(j=0;j<10;j++)
{
LED2_REV;
LED3_REV;
Delay_ms(100);
}
}
}
}
}
void usart2_init(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE );
USART_DeInit(USART2);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
USART_Cmd(USART2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void UART2_Put_StringL(unsigned char *Str,unsigned char len)
{
unsigned char i = 0;
for (i=0;i<len;i++) UART2_Put_Char(*(Str + i));
}
void UART_DM_ReportData(unsigned char data[])
{
UART2_Put_StringL(data,10); //打印长度根据所打印的数组长度来改,动作组是5,速度状态是10。最好填10。
}
void UART2_Put_Char(unsigned char DataToSend)
{
TxBuffer[count++] = DataToSend;
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
} //以上仅是部分程序
全部程序51hei下载地址:
STM32F103双轴摇杆+舵机.7z
(182.84 KB, 下载次数: 74)
|