打印

STM32发送数据问题

[复制链接]
1525|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xsgy123|  楼主 | 2011-12-22 20:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
STM32 485能接受数据,但发送只能发出第一个字符
部分程序:
void USART_Configuration1(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* 第1步:打开GPIO和USART部件的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* 第2步:将USART Tx的GPIO配置为推挽复用模式 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* 第3步:将USART Rx的GPIO配置为浮空输入模式
由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的
但是,我还是建议加上便于阅读,并且防止其它地方修改了这个口线的设置参数
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第3步已经做了,因此这步可以不做
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
*/
GPIO_Init(GPIOA, &GPIO_InitStructure);


/* 第4步:配置USART参数
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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(USART1, &USART_InitStructure);

/* Enable USART1 Receive interrupts */
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* 第5步:使能 USART, 配置完毕 */
USART_Cmd(USART1, ENABLE);

/* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
如下语句解决第1个字节无**确发送出去的问题 */
USART_ClearFlag(USART1, USART_FLAG_TC); /* 清发送外城标志,Transmission Complete flag */
}

void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* 第1步:打开GPIO和USART部件的时钟 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOF | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //USART2、USART3是低速设备(APB1),而USART是高速设备(APB2),注意两者的区别
// RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
/* 第2步:将USART Tx的GPIO配置为推挽复用模式 */


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);

/* 第3步:将USART Rx的GPIO配置为浮空输入模式
由于CPU复位后,GPIO缺省都是浮空输入模式,因此下面这个步骤不是必须的
但是,我还是建议加上便于阅读,并且防止其它地方修改了这个口线的设置参数
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 第3步已经做了,因此这步可以不做
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
*/
//GPIO_Init(GPIOC, &GPIO_InitStructure);
// GPIO_Init(GPIOB, &GPIO_InitStructure);


/* 第4步:配置USART参数
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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);

/* 第5步:使能 USART, 配置完毕 */
USART_Cmd(USART2, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发送不出去
如下语句解决第1个字节无**确发送出去的问题 */
USART_ClearFlag(USART2, USART_FLAG_TC); /* 清发送外城标志,Transmission Complete flag */


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_ResetBits(GPIOF, GPIO_Pin_10); /* 禁止发送 */
// GPIO_SetBits(GPIOF, GPIO_Pin_11); /* 禁止接收 */


}
/*******************************************************************************
函数名:fputc
输 入:
输 出:
功能说明:
重定义putc函数,这样可以使用printf函数从串口1打印输出
*/
int fputc(int ch, FILE *f)
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);

/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}

return ch;
}

void Write_USART1(const unsigned char *SendData, unsigned int Length)
{
unsigned int i=0;  

for( i=0; i<Length; i++ )
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
USART_SendData(USART1, *(SendData + i) );
}
}

/*******************************************************************************
函数名:fputc
输 入:
输 出:
功能说明:
重定义getc函数,这样可以使用scanff函数从串口1输入数据
*/
int fgetc(FILE *f)
{
/* 等待串口1输入数据 */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}

return (int)USART_ReceiveData(USART1);
}


void NVIC_Configuration(void)  

{  

  NVIC_InitTypeDef NVIC_InitStructure;


  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);  


  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; /*3.4的库不是使用USART1_IRQChannel,看stm32f10x.h吧*/

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);  


}
void RS485PutChar(char c)   
{   
  //USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);   
  GPIO_SetBits(GPIOB, GPIO_Pin_9);   
  Delay(0x1ffff);   
  USART_SendData(USART2, c);   
  while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);   
  Delay(0x1ffff);   
  GPIO_ResetBits(GPIOB, GPIO_Pin_9);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);   
}   
void Delay(uint32_t nCount)   
{   
  for(; nCount != 0; nCount--);   

}

void USART2_IRQHandler(void)  
{  
   
  if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET)  
{
GPIO_ResetBits(GPIOB, GPIO_Pin_9);
RxBuffer1=USART_ReceiveData(USART2);
// USART_ClearITPendingBit(USART2, USART_IT_RXNE);   
GPIO_ResetBits(GPIOA, GPIO_Pin_12);
USART_SendData(USART1, RxBuffer1);
//Delay(0xffff);
RS485PutChar(RxBuffer1);
//Delay(0xffff);
//RS485PutChar(USART_ReceiveData(USART2));
  
}

}   
各位看看是什么原因呢
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

229

主题

2603

帖子

1

粉丝