初级会员

- 积分
- 79
- 金钱
- 79
- 注册时间
- 2015-5-4
- 在线时间
- 0 小时
|
5金钱
初始化里面的数据位是 8
下面是原程序: 芯片 stm32F103RB
#include"modbus485.h"
u8 a[] = {1,2,3};
int main(void)
{
UsartInit();
while(1)
{
send_mb(a,3);
}
}
#include"modbus485.h"
#include"time.h"
void modbus485_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
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_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_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_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
GPIO_ResetBits(GPIOG, GPIO_Pin_12); //???í????
}
void send_mb(u8 *buf, u8 len)
{
u8 i;
GPIO_SetBits(GPIOA, GPIO_Pin_12); //???í·???
for(i=0;i<len;i++)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
USART_SendData(USART1, buf);
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
GPIO_ResetBits(GPIOA, GPIO_Pin_12); //???í????
}
void UsartInit(void)
{
modbus485_init();
}
|
|