高中生
最后登录1970-1-1
在线时间 小时
注册时间2015-12-21
|
本来返回的数据应该是十六进制数01 20 02 00 00 B3 C0,但实际返回的是 01 20 02 00 FE 32 01,开发板通过485口发送命令给传感器,传回来的数据通过串口1打印到电脑上,相应代码如下
485配置函数
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
//ÅäÖÃÖD¶ÏÔ′
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART2 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(mac485REDE_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* USART2 GPIO config */
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 485 ½óêÕ·¢Ëíê1Äü GPIO
GPIO_InitStructure.GPIO_Pin = mac485REDE_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(mac485REDE_PORT, &GPIO_InitStructure);
/* USART2 mode config */
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_9b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_Even ;
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,ENABLE);//ÔêDí′®¿ú2·¢ËíÖD¶Ï
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//ÔêDí′®¿ú2½óêÕÖD¶Ï
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
}
接受中断函数
static void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--){}
}
}
int iup;
void USART2_IRQHandler(void)
{
extern uint8_t che[7];
uint8_t ch;
// for(i=0;i<8;i++)
// {
if(USART_GetITStatus(USART2, USART_IT_RXNE)==SET)
{
//ch = USART1->DR;
ch = USART_ReceiveData(USART2);
USART_ClearFlag(USART2,USART_FLAG_RXNE);
//GPIO_SetBits(mac485REDE_PORT,mac485REDE_PIN);
delay_ms(1);
//printf( "%02X", ch ); //½«½óêüμ½μÄêy¾YÖ±½ó·μ»Ø′òó¡
}
che[iup]=ch;
iup++;
//}
}
主函数
#include "stm32f10x.h"
#include "bsp_usart2_485.h"
#include "bsp_usart1.h"
#include <stdio.h>
/*
* oˉêyÃû£omain
* Ãèêö £oÖ÷oˉêy
* êäèë £oÎT
* êä3ö £oÎT
±¾1¤3ìÎaÖ÷»ú£¬êμÑéê±Ïè¸ø′ó»úéÏμç
*/
uint8_t send_AHRSO[6]={0x01,0x20,0x00,0x39,0xC0,0xC0};
uint8_t che[7];
uint8_t ch;
extern int iup;
uint8_t temp;
uint16_t RXD_Data;
u8 RS485_RX_BUF[8];
u8 RS485_RX_CNT;
static void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=12000;
while(i--){}
}
}
void USART_PutHEX(USART_TypeDef* USARTx,uint8_t Data)
{
USART_SendData(USARTx, Data);
while(USART_GetFlagStatus(USARTx,USART_FLAG_TXE)==RESET);
}
int SendUrt()
{
GPIO_SetBits(mac485REDE_PORT,mac485REDE_PIN);
delay_ms(1);
USART_PutHEX(USART2,send_AHRSO[0]);
USART_PutHEX(USART2,send_AHRSO[1]);
USART_PutHEX(USART2,send_AHRSO[2]);
USART_PutHEX(USART2,send_AHRSO[3]);
USART_PutHEX(USART2,send_AHRSO[4]);
USART_PutHEX(USART2,send_AHRSO[5]);
}
int main(void)
{
temp = 0;
ch=0;
iup=0;
/* USART2 config */
USART2_Config(); //′®¿ú2óÃóúêÕ·¢485μÄêy¾Y
NVIC_Configuration();
USARTx_Config(); //′®¿ú1óÃóúêä3öμ÷êÔDÅÏ¢
while(1)
{
GPIO_SetBits(mac485REDE_PORT,mac485REDE_PIN); //½øèë·¢ËíÄ£ê½
delay_ms(1);
SendUrt();//·¢Ëíêy¾Y
//delay_ms(1000);
while (USART_GetFlagStatus(USART2,USART_FLAG_TC) !=SET);//ÂÖÑˉÖ±μ½·¢Ëíêy¾Yíê±Ï
GPIO_ResetBits(mac485REDE_PORT,mac485REDE_PIN);//½øèë½óêüÄ£ê½
//printf("\r\n·¢Ëíêy¾Y3é1|£¡\r\n"); //ê1óÃ′®¿ú1′òó¡μ÷êÔDÅÏ¢μ½ÖÕ¶Ë
delay_ms(10);
if(iup>7)
{
iup=0;
printf("%02X %02X %02X %02X %02X %02X %02X 02X\n",che[0],che[1],che[2],che[3],che[4],che[5],che[6]);
}
delay_ms(100);
}
}
|
|