OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 4782|回复: 4

串口3中断使用问题

[复制链接]

5

主题

9

帖子

0

精华

初级会员

Rank: 2

积分
54
金钱
54
注册时间
2013-6-28
在线时间
4 小时
发表于 2014-7-17 10:36:01 | 显示全部楼层 |阅读模式
5金钱
为什么我的串口3跟串口1是一样的中断配置,实现的功能是将PC串口发送给STM32的数据返回给PC,为什么串口3在使用过程中一发送,会返回一样的数据两次,求原子哥指点。

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

9

主题

57

帖子

0

精华

初级会员

Rank: 2

积分
104
金钱
104
注册时间
2014-6-21
在线时间
2 小时
发表于 2014-7-17 11:10:57 | 显示全部楼层
Email:lancher@yeah.net QQ: 838603205
回复

使用道具 举报

5

主题

9

帖子

0

精华

初级会员

Rank: 2

积分
54
金钱
54
注册时间
2013-6-28
在线时间
4 小时
 楼主| 发表于 2014-7-17 11:17:46 | 显示全部楼层
#include "stm32f4xx.h"
#include <stdio.h>
#include "uart.h"

//?®??1????·??????ò
//×???,????USARTx->SR??±??????????????í?ó  
u8 USART_RX_BUF[USART_REC_LEN];     //????????,×??óUSART_REC_LEN??×???.
//????×???
//bit15?? ?????ê??±ê??
//bit14?? ??????0x0d
//bit13~0?? ???????????§×???????
u16 USART_RX_STA=0;       //????×???±ê??

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

/*static void Delay_ARMJISHU(__IO uint32_t nCount)
{
  for (; nCount != 0; nCount--)
  {
  }
}*/

void STM32_Shenzhou_COMInit(USART_InitTypeDef* USART_InitStruct)
{
NVIC_InitTypeDef NVIC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); /* Enable GPIO clock */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Enable UART clock */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); /* Connect PXx to USARTx_Tx*/

  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3);

  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

 /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  USART_Init(USART3, USART_InitStruct); /* USART configuration */ 

#if EN_USART3_RX  //??????????????  
   //Usart1 NVIC ????
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2 ;//??????????2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //×???????2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷

  USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//????????
#endif

  USART_Cmd(USART3, ENABLE); /* Enable USART */
}  

void STM32_AD9854_COMInit(USART_InitTypeDef* USART_InitStruct)
{
NVIC_InitTypeDef NVIC_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); /* Enable GPIO clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); /* Enable UART clock */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); /* Connect PXx to USARTx_Tx*/

  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

 /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  USART_Init(USART1, USART_InitStruct); /* USART configuration */ 

#if EN_USART1_RX  //??????????????  
   //Usart1 NVIC ????
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//??????????3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //×???????3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ?¨??????
NVIC_Init(&NVIC_InitStructure); //?ù?????¨????????????VIC?????÷

  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//????????
#endif

  USART_Cmd(USART1, ENABLE); /* Enable USART */
}  

void Printf_Init(void)
{
  USART_InitTypeDef USART_InitStructure;   
  USART_InitStructure.USART_BaudRate = 57600;
  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;

  STM32_Shenzhou_COMInit(&USART_InitStructure);
}

void AD9854_COM_Init(void)
{
  USART_InitTypeDef USART_InitStructure;   
  USART_InitStructure.USART_BaudRate = 57600;
  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;

  STM32_AD9854_COMInit(&USART_InitStructure);
}

PUTCHAR_PROTOTYPE
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART3, (uint8_t) ch);

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

  return ch;
}

void USART1_IRQHandler(void)                 //?®??1????·??????ò
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntEnter();    
#endif
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //????????(????????????±?????0x0d 0x0a?á??)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //????????????????
//USART_RX_STA = 0x8000;

//USART_RX_BUF[0]= Res ;

if((USART_RX_STA&0x8000)==0)//???????ê??
{
if(USART_RX_STA&0x4000)//????????0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?????í?ó,????????
else 
{
USART_RX_STA|=0x8000; //?????ê???? 
//printf("?????ê??\r\n");
}
}
else //????????0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//?????????í?ó,????????????  
}  
}
}    
     } 
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntExit();    
#endif


void USART3_IRQHandler(void)                 //?®??1????·??????ò
{
u8 Res;

#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntEnter();    
#endif
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //????????(????????????±?????0x0d 0x0a?á??)
{
Res =USART_ReceiveData(USART3);//(USART1->DR); //????????????????

if((USART_RX_STA&0x8000)==0)//???????ê??
{
if(USART_RX_STA&0x4000)//????????0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//?????í?ó,????????
else 
{
USART_RX_STA|=0x8000; //?????ê???? 
//printf("?????ê??\r\n");
}
}
else //????????0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//?????????í?ó,????????????  
}  
}
}    
     } 
#ifdef OS_TICKS_PER_SEC //?????±?????????¨????,???÷??????ucosII??.
OSIntExit();    
#endif
}


回复

使用道具 举报

5

主题

9

帖子

0

精华

初级会员

Rank: 2

积分
54
金钱
54
注册时间
2013-6-28
在线时间
4 小时
 楼主| 发表于 2014-7-17 11:18:29 | 显示全部楼层
芯片用的是STM32F439
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-7-17 23:33:33 | 显示全部楼层
F439我也没整过...
串口1搞通了么?
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-5 02:15

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表