stm32f103c8t6最小核心板 关于串口传输 跪求大神指导

[复制链接]
2780|6
 楼主| 扛把子滴 发表于 2017-5-26 14:41 | 显示全部楼层 |阅读模式
我想在stm32f103c8t6最小核心板上实现USART1接受数据后产生中断,用USART2发送;USART2接受数据后产生中断,用USART1发送。编译没有错误,但是USART2一直通讯不了。跪求大神指导。
 楼主| 扛把子滴 发表于 2017-5-26 14:42 | 显示全部楼层
#include "stm32f10x.h"


void My_USART_Init(void)
{

GPIO_InitTypeDef GPIO_InitStructure;       
USART_InitTypeDef USART_InitStructure;       
NVIC_InitTypeDef NVIC_InitStructure;       
       
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
       
       
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;       
GPIO_Init(GPIOA,&GPIO_InitStructure);       
       
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;       
GPIO_Init(GPIOA,&GPIO_InitStructure);               

       
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;       
GPIO_Init(GPIOA,&GPIO_InitStructure);       
       
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;       
GPIO_Init(GPIOA,&GPIO_InitStructure);               
       
       
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStructure);       

USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_Init(USART2,&USART_InitStructure);
       
USART_Cmd(USART1,ENABLE);        //使能串口1
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//打开接受中断

USART_Cmd(USART2,ENABLE);        //使能串口2
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//打开接受中断



NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStructure);

NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
}

void USART1_IRQHandler(void)
{
       
        u8 res1;
       
        if(USART_GetITStatus(USART1,USART_IT_RXNE))
        {
        res1=USART_ReceiveData(USART1);
        USART_SendData(USART2,res1);
        }
       


}

void USART2_IRQHandler(void)
{
       
       
        u8 res2;

        if(USART_GetITStatus(USART2,USART_IT_RXNE))
        {
        res2=USART_ReceiveData(USART2);
        USART_SendData(USART1,res2);
        }
       

}


int main(void)
{       
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
        My_USART_Init();
        while(1);
         
}
 楼主| 扛把子滴 发表于 2017-5-26 14:42 | 显示全部楼层
求大神指导,哪出错了?
dengxindieying 发表于 2017-5-31 19:49 | 显示全部楼层
用调试器跟进去,看看进入中断后是不是清除了
shahexu 发表于 2017-6-8 19:43 | 显示全部楼层
接收完成,要请中断的
zhazhahua 发表于 2017-6-17 11:42 | 显示全部楼层
用keil单步调试,还有初始化那里NVIC部分应该放在你USART_Cmd(USART1, ENABLE);前面
S2663265296 发表于 2017-6-22 17:52 | 显示全部楼层
去看下例程之类的教程应该就懂了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部