OpenEdv-开源电子网

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

快疯了,板子是STM32F103ET6的跟论坛里面的前辈们设置的一样,串口2就是不能用

[复制链接]

4

主题

54

帖子

0

精华

初级会员

Rank: 2

积分
145
金钱
145
注册时间
2015-4-18
在线时间
17 小时
发表于 2015-6-4 10:09:52 | 显示全部楼层 |阅读模式
5金钱
[mw_shl_code=c,true]#include "sys.h" #include "usart.h" #include "led.h" #include "delay.h" #if 1 #pragma import(__use_no_semihosting) struct __FILE { int handle; }; FILE __stdout; _sys_exit(int x) { x = x; } int fputc(int ch, FILE *f) { while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET); USART_SendData(USART2,(uint8_t)ch); return ch; } #endif //#if EN_USART1_RX u32 USART_RX_BUF[USART_REC_LEN]; u16 USART_RX_STA=0; void uart_init(u32 bound){ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); USART_DeInit(USART2); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = bound; 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); #if EN_USART2_RX NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //USART_ITConfig(USART1, USART_IT_TXE, ENABLE); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); #endif USART_Cmd(USART2, ENABLE); // USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); } // void USART1_Putc(char c) // { // USART_SendData(USART1,c); // while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET); // } void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART2, USART_IT_RXNE); USART_SendData(USART2, USART_ReceiveData(USART2)); while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); } } [/mw_shl_code] [mw_shl_code=c,true] [/mw_shl_code] [mw_shl_code=c,true]#include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "usart.h" int main(void)  { delay_init();     NVIC_Configuration(); uart_init(19200);   LED_Init(); KEY_Init();         while(1);  } [/mw_shl_code]
各位大神给指点下,谢了

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

使用道具 举报

17

主题

342

帖子

0

精华

高级会员

Rank: 4

积分
544
金钱
544
注册时间
2014-4-24
在线时间
20 小时
发表于 2015-6-4 12:22:21 | 显示全部楼层
你仔细瞧瞧你的NVIC初始化部分,开的是哪个串口的中断通道?
这么马虎的移植,能仔细点么。
回复

使用道具 举报

4

主题

54

帖子

0

精华

初级会员

Rank: 2

积分
145
金钱
145
注册时间
2015-4-18
在线时间
17 小时
 楼主| 发表于 2015-6-4 13:19:11 | 显示全部楼层
回复【2楼】STM32VBT6:
---------------------------------
之前改的是USART2,串口1通过USB转232通信,所以改串口2的时候也用的USB转232,一直不通。然后又改回串口1,来回改了几次就改乱了,现在终于好了,串口1通信通过USB-232,串口2直接通过232接口,我一直把接口搞错了。谢谢大神。
回复

使用道具 举报

0

主题

133

帖子

0

精华

初级会员

Rank: 2

积分
169
金钱
169
注册时间
2014-12-21
在线时间
1 小时
发表于 2015-6-4 14:04:41 | 显示全部楼层
看标题名字就知道会有这个问题,嘿嘿
流过汗水才能代表自己真的学会了如何不流汗。
回复

使用道具 举报

30

主题

705

帖子

1

精华

新手上路

积分
21
金钱
21
注册时间
2013-3-14
在线时间
52 小时
发表于 2015-6-4 15:51:28 | 显示全部楼层
我的这个配置测试可以用的。
[mw_shl_code=c,true]void usart1_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); //Usart1 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0; //1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1 //USART 初始化设置 USART_InitStructure.USART_BaudRate = bound;//一般设置为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); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启中断 USART_Cmd(USART1, ENABLE); //使能串口 //USART1->CR3|=1<<7; //使能串口DMA } void usart2_init(u32 bound) { //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_ClockInitTypeDef USART_ClockInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //USART2_TX PA.2 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); //USART3_RX PA.3 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1; //0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART1 USART_InitStructure.USART_BaudRate = bound; 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_ClockInitStructure.USART_Clock = USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable; USART_ClockInit(USART2, &USART_ClockInitStructure); //--------------------------------------------------------------------------------- USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断 USART_Cmd(USART2, ENABLE); //使能串口 //USART2->CR3|=1<<7;//使能串口DMA } void USART1_IRQHandler(void) //串口1中断服务程序 { u8 ch; if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){ ch =USART_ReceiveData(USART1); if (ch==USART1_START_BYTE){ usart1_count=0; usart1_buf[usart1_count]=ch; } else { usart1_count++; usart1_count&=0x3f; usart1_buf[usart1_count]=ch; if(ch==USART1_END_BYTE){ usart1_flag=1; } } } } void USART2_IRQHandler(void) { u8 ch; if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET){ ch = USART_ReceiveData(USART2); usart2_buf[usart2_count] = ch; usart2_count++; usart2_count&=0x3f; usart2_delay=2; } }[/mw_shl_code]

原子哥是不是又要出黑科技了呀。^_^...
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-7-28 23:23

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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