OpenEdv-开源电子网

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

stm32f103rc usart1,2,3 uart4,5 多串口程序卡死 求帮看程序

[复制链接]

9

主题

44

帖子

0

精华

初级会员

Rank: 2

积分
108
金钱
108
注册时间
2014-10-29
在线时间
2 小时
发表于 2014-11-29 19:19:43 | 显示全部楼层 |阅读模式
5金钱
[mw_shl_code=c,true]<main.c>[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true]#include "delay.h" #include "sys.h" #include "usart.h" #include "adc.h" #include "dma.h" #define N 50 //?50???? #define M 15 //15?AD u16 value[N][M]; u16 aftervalue[M]; float avalue[M]; u8 i=0,j=0; u8 RX1,RX2,RX3,RX4,RX5; int temp,temp2,temp3,temp4,temp5; void filter(void) { int sum = 0; u8 count; for(i=0;i<M;i++) { for(count=0;count<N;count++) { sum+=value[count]; } aftervalue=sum/N; sum=0; } } int deal(int temp) { if(temp>=0 && temp<=7) { return temp+=11; } if(temp>7 && temp<=30) { return temp=temp+3; } if(temp>23 && temp<=37) { return temp=temp-5; } if(temp>40) { return 32; } } int main(void) { delay_init(); //??????? NVIC_Configuration(); //??NVIC????2:2??????,2?????? uart_init(9600); //??????9600 uart2_init(9600); uart3_init(9600); uart4_init(9600); uart5_init(9600); Adc_Init(); //ADC??? MYDMA_Config(DMA1_Channel1,(u32)&ADC1->DR,(u32)&value,M*N); ADC_SoftwareStartConvCmd(ADC1,ENABLE); DMA_Cmd(DMA1_Channel1, ENABLE); while(1) { filter(); for(i=0;i<M;i++) { avalue=(float)aftervalue*(3.3/4096); aftervalue=avalue; } for(i=0;i<M;i++) { avalue+=aftervalue; avalue*=40; } temp=deal((int)avalue[0]/5); temp2=deal((int)avalue[1]/5); temp3=deal((int)avalue[4]/5); temp4=deal((int)avalue[5]/5); temp5=deal((int)avalue[6]/5); USART_SendData(USART1,temp); USART_SendData(USART2,temp2); USART_SendData(USART3,temp3); USART_SendData(UART4,temp4); USART_SendData(UART5,temp5); delay_ms(85); } }[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true]uart.c[/mw_shl_code] [mw_shl_code=c,true]
[mw_shl_code=c,true]#include "sys.h" #include "usart.h" void uart_init(u32 bound) { //GPIO???? GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //??USART1,GPIOA?? USART_DeInit(USART1); //????1 //USART1_TX PA.9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?????? GPIO_Init(GPIOA, &GPIO_InitStructure); //???PA9 //USART1_RX PA.10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//???? GPIO_Init(GPIOA, &GPIO_InitStructure); //???PA10 //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 ????? USART_InitStructure.USART_BaudRate = bound;//?????9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b;//???8????? 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); //???? } void uart2_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); USART_DeInit(USART2); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA.9 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //?????? GPIO_Init(GPIOA, &GPIO_InitStructure); //???PA9 //USART1_RX PA.10 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=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_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); //????? USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//???? USART_Cmd(USART2, ENABLE); //???? } void uart3_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); USART_DeInit(USART3); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_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(USART3, &USART_InitStructure); //????? USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//???? USART_Cmd(USART3, ENABLE); //???? } void uart4_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &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(UART4, &USART_InitStructure); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//?????3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //????3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ???? NVIC_Init(&NVIC_InitStructure); //??????????VIC??? } void uart5_init(u32 bound) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &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(UART5, &USART_InitStructure); USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); USART_Cmd(UART5, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } [/mw_shl_code]

adc.c[/mw_shl_code] [mw_shl_code=c,true]
[mw_shl_code=c,true] #include "adc.h" #include "delay.h" #define M 15 //15路AD void Adc_Init(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_ADC1, ENABLE ); //使能ADC1通道时钟 RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置ADC分频因子6 72M/6=12,ADC最大时间不能超过14M //PA1 作为模拟通道输入引脚 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //模拟输入引脚 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin= GPIO_Pin_1; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin= GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5; GPIO_Init(GPIOC, &GPIO_InitStructure); ADC_DeInit(ADC1); //复位ADC1,将外设 ADC1 的全部寄存器重设为缺省值 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC工作模式:ADC1和ADC2工作在独立模式 ADC_InitStructure.ADC_ScanConvMode = ENABLE; //扫描模式 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //循环模式 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件而不是外部触发启动 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC数据右对齐 ADC_InitStructure.ADC_NbrOfChannel = M; //顺序进行规则转换的ADC通道的数目 ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数初始化外设ADCx的寄存器 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 6, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 7, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 9, ADC_SampleTime_239Cycles5 ); ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 9, ADC_SampleTime_239Cycles5 ); ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 10, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 11, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 12, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 13, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 14, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 15, ADC_SampleTime_239Cycles5 ); //配置采样时间为239.5周期 ADC_Cmd(ADC1, ENABLE); //使能指定的ADC1 ADC_DMACmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); //使能复位校准 while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束 ADC_StartCalibration(ADC1); //开启AD校准 while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束 } dma.c[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true]
[mw_shl_code=c,true]#include "dma.h" u8 DMA1_MEM_LEN; //DMA1的各通道配置 //这里的传输形式是固定的,这点要根据不同的情况来修改 //从外设模式->存储器/16位数据宽度/存储器增量模式 //DMA_CHxMA通道CHx //cpar:外设地址 //cmar:存储器地址//cndtr:数据传输量 void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr) { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟 DMA_DeInit(DMA_CHx); //将DMA的通道1寄存器重设为缺省值 DMA1_MEM_LEN=cndtr;//通道数据长度 DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //DMA外设基地址 DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //DMA内存基地址 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //数据传输方向,从外设发送到内存 DMA_CCRX位4 DMA_InitStructure.DMA_BufferSize = cndtr; //DMA通道的DMA缓存的大小 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器不变 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址寄存器递增 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设数据宽度为16位 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //内存数据宽度为16位 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //工作在循环缓存模式 DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA通道 x拥有高优先级 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输 DMA_Init(DMA_CHx, &DMA_InitStructure); //根据DMA_InitStruct中指定的参数初始化DMA的通道USART1_Tx_DMA_Channel所标识的寄存器 } [/mw_shl_code]
[/mw_shl_code] [mw_shl_code=c,true]  ?串口145同时用可以    ?   串口45同时用可以     串口124同时可以    串口1245发几个数据后停止     串口245 同时用 接收42个数据就停止            串口24同时正常          单独串口2正常     单独1正常     12组合正常     123不行 发送一个或者两个数据后停止     串口25同时用也不正常发送几十个数据后停止       串口3单独或者组合使用 发送一个或者两个数据后停止  是不是哪里有冲突? [/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code] [mw_shl_code=c,true] [/mw_shl_code]


[/mw_shl_code] [mw_shl_code=c,true][/mw_shl_code]

最佳答案

查看完整内容[请看2#楼]

回复【5楼】正点原子: --------------------------------- 幸苦原子哥   应该是和ADC DMA结合使用出问题了  初始化没问题   应为我找了一个单独5个串口同时输出的程序(有用)再结合ADC  DMA就回到原来那样  程序就输出几个数据后就死了
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

9

主题

44

帖子

0

精华

初级会员

Rank: 2

积分
108
金钱
108
注册时间
2014-10-29
在线时间
2 小时
 楼主| 发表于 2014-11-29 19:19:44 | 显示全部楼层
回复【5楼】正点原子:
---------------------------------
幸苦原子哥   应该是和ADC DMA结合使用出问题了  初始化没问题   应为我找了一个单独5个串口同时输出的程序(有用)再结合ADC  DMA就回到原来那样  程序就输出几个数据后就死了
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-11-29 23:13:15 | 显示全部楼层
估计你这不是死在串口,是其它地方死了吧。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

9

主题

44

帖子

0

精华

初级会员

Rank: 2

积分
108
金钱
108
注册时间
2014-10-29
在线时间
2 小时
 楼主| 发表于 2014-11-30 00:24:15 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
我也不清楚什么原因  求解
回复

使用道具 举报

9

主题

44

帖子

0

精华

初级会员

Rank: 2

积分
108
金钱
108
注册时间
2014-10-29
在线时间
2 小时
 楼主| 发表于 2014-11-30 00:24:35 | 显示全部楼层
回复【2楼】正点原子:
---------------------------------
我的串口初始化有问题吗?
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2014-11-30 22:45:28 | 显示全部楼层
回复【4楼】忘川:
---------------------------------
看不出问题。
我是开源电子网www.openedv.com站长,有关站务问题请与我联系。
正点原子STM32开发板购买店铺http://openedv.taobao.com
正点原子官方微信公众平台,点击这里关注“正点原子”
回复

使用道具 举报

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

本版积分规则



关闭

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

正点原子公众号

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

GMT+8, 2025-6-28 13:14

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

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