JY901是一款网上能很方便买到的模块,价格亲民,方便操作,适合个人学习购买练手。
以下是原创操作代码
void USART2_IRQHandler(void)
{
//unsigned char data;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
g_usart2_recebuf[g_usart2_count]=USART_ReceiveData(USART2);
if(0==g_usart2_count)
{
if(0x55==g_usart2_recebuf[0])
{
g_usart2_count++;
}
}
else
{
g_usart2_count++;
}
if(10==g_usart2_count)
{
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
TIM_Cmd(TIM3,ENABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
}
}
else
{
USART_ReceiveData(USART2);//如果接收没有处理,放弃接收的数据
}
if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)!=RESET)
{
USART_ClearFlag(USART1,USART_FLAG_ORE); //清溢出位
USART_ReceiveData(USART1); //读DR
}
}
void TIM3_IRQHandler( void )
{
//TIM3中断处理函数
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
{
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE);
TIM_Cmd(TIM3,DISABLE);
TIM_SetCounter(TIM3,0);
//判断是否是TIM3中断
g_usart2_count=0;
USART2_CheckReceived();//接收串口数据完成,打开标志位函数
//g_usart2_count=0;
}
//此处写入用户自己的处理程序
TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
}
/*******************************************读取加速度
void Mpu_Getveloc(void)
{
if(0x55==g_usart2_recebuf[0])
{
if(g_usart2_recebuf[1]==0x51)
{
g_acc_x[0]=(int16_t)(g_usart2_recebuf[2]|g_usart2_recebuf[3]<<8)*3920/65535;
g_acc_y[0]=(int16_t)(g_usart2_recebuf[4]|g_usart2_recebuf[5]<<8)*3920/65535;
g_acc_z[0]=(int16_t)(g_usart2_recebuf[6]|g_usart2_recebuf[7]<<8)*3920/65535;
//printf("acc_x = %d ,acc_y = %d ,acc_z = %d ************* \n",g_acc_x[0],g_acc_y[0],g_acc_z[0]);
}
}
}
对完整代码文件请关注后续博客