初中生
最后登录1970-1-1
在线时间 小时
注册时间2016-7-18
|
1.WIFI板块+野火STM32MINI板
2.通过APP控制WIFI板块串口输出“ONA”,“ONB”,“ONF”,MINI板接收,控制电平转换以控制电机正转,反转,停止
现在问题是程序烧好了,发送端(WIFI串口输出)也没问题了,但是不知道程序问题出在哪?
3.附上程序:
#include "stm32f10x_lib.h"
#include "stdio.h"
#include "stm32f10x_it.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
void delay_nms(u16 time);
void TIM4_Configuration(void);
void SendCom_Juge(void);
void TIM3_Configuration(void);
char instruct[5],j;
u8 Recv_Comp;
u16 Rev=4000;//转速
/*******************************************************************************
* 函数名 : main
* 函数描述 : Main 函数
*******************************************************************************/
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART_Configuration();
TIM4_Configuration();
TIM3_Configuration();
while(1)
{
delay_nms(30);
SendCom_Juge();
}
}
void delay_nms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000; //自己定义
while(i--) ;
}
}
/*******************************************************************************
* 函数名 : NVIC_Configuration
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);/* Set the Vector Table base location at 0x20000000 */
#else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel ; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //中断响应优先级0,赋予高的优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel; /*TIM3中断*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*先占优先级0级*/
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; /*从优先级3级*/
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; /*IRQ通道被使能*/
NVIC_Init(&NVIC_InitStructure);
}
/*******************************************************************************
* 函数名 : USART_Configuration
*******************************************************************************/
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
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_Cmd(USART2 , ENABLE);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //一定要开启串口接收中断
USART_ClearFlag(USART2,USART_FLAG_TC); //发送完成标志位
}
/*******************************************************************************
* 函数名 : RCC_Configuration
* 函数描述 : 设置系统各部分时钟
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; /* 定义枚举类型变量 HSEStartUpStatus */
RCC_DeInit(); /* 复位系统时钟设置*/
RCC_HSEConfig(RCC_HSE_ON); /* 开启HSE*/
HSEStartUpStatus = RCC_WaitForHSEStartUp(); /* 等待HSE起振并稳定*/
if(HSEStartUpStatus == SUCCESS) /* 判断HSE起是否振成功,是则进入if()内部 */
{
RCC_HCLKConfig(RCC_SYSCLK_Div1); /* 选择HCLK(AHB)时钟源为SYSCLK 1分频 */
RCC_PCLK2Config(RCC_HCLK_Div1); /* 选择PCLK2时钟源为 HCLK(AHB) 1分频 */
RCC_PCLK1Config(RCC_HCLK_Div2); /* 选择PCLK1时钟源为 HCLK(AHB) 2分频 */
FLASH_SetLatency(FLASH_Latency_2); /* 设置FLASH延时周期数为2 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* 使能FLASH预取缓存 */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); /* 选择锁相环(PLL)时钟源为HSE 1分频,倍频数为9,则PLL输出频率为 8MHz * 9 = 72MHz */
RCC_PLLCmd(ENABLE); /* 使能PLL */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); /* 等待PLL输出稳定 */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); /* 选择SYSCLK时钟源为PLL */
while(RCC_GetSYSCLKSource() != 0x08); /* 等待PLL成为SYSCLK时钟源 */
}
/* 打开APB2总线上的GPIOA时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4|RCC_APB1Periph_TIM3|RCC_APB1Periph_USART2 , ENABLE ); //
}
/*******************************************************************************
* 函数名 : GPIO_Configuration
* 函数描述 : 设置各GPIO端口功能
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; /* 设置USART2的Tx脚(PA.2)为第二功能推挽输出模式 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; /* 设置USART2的Rx脚(PA.3)为浮空输入脚 */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//脉冲
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;/* 定义 TIM_TimeBase 初始化结构体 TIM_TimeBaseStructure */
TIM_OCInitTypeDef TIM_OCInitStructure; /* 定义 TIM_OCInit 初始化结构体 TIM_OCInitStructure */
TIM_TimeBaseStructure.TIM_Period = 4600; //15KHZ 计数重载值
TIM_TimeBaseStructure.TIM_Prescaler = 0;// 预分频值为(0+ 1 = 1)
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //时钟分割0
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//向上计数模式
TIM_TimeBaseInit(TIM3 , &TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; // 工作模式为 PWM 输出模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = Rev;//
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3 , TIM_OCPreload_Enable); /* 使能预装载寄存器 */
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3 , ENABLE); /* 启动 TIM 计数 */
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (u8) ch);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET);
return ch;
}
void TIM4_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_Period = 4;
TIM_TimeBaseStructure.TIM_Prescaler = 7199;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM4, TIM_FLAG_Update);
TIM_ARRPreloadConfig(TIM4, DISABLE);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
}
void SendCom_Juge(void)
{
if(Recv_Comp==1)
{
Recv_Comp=0;
j=0;
if((instruct[0] == 'O') && (instruct[1] == 'N')&& (instruct[2] == 'A'))
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);
}
if((instruct[0] == 'O') && (instruct[1] == 'N')&& (instruct[2] == 'B'))
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
}
if((instruct[0] == 'O') && (instruct[1] == 'N')&& (instruct[2] == 'F'))
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_SetBits(GPIOA,GPIO_Pin_5);
}
}
}
|
|