你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

请教串口通信问题

[复制链接]
fieree 提问时间:2015-5-7 02:45 /
本帖最后由 Eagleson 于 2015-5-7 08:40 编辑

的意图是用电脑的超级终端先向usart1发送任意一个命令,然后usart1返回SBSB这个语句,接着USART2发送出一条命令oreder,和Usart2连接的模块收到这个命令后会返回一个命令rec。但我在超级终端发了一个命令后,连SBSB这个语句都没返回。求教
int main(void)
{
char i;
char j;
uint32_t oreder[ ] = {0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A};
uint32_t rec[ ]={0x01, 0x03,0x00,0x48,0x00,0x08,0xC4,0x1A};;
USART1_Config();
USART2_Config();
NVIC_Configuration();
while(1){
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
{}
printf("SBSB");
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}

for(i=0;i<8;i++) {
USART_SendData(USART2, oreder);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
{} }
for(j=0;i<37;j++) {
while (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{}
rec[j] = (USART_ReceiveData(USART2));
USART_SendData(USART1, rec[j]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{}
}





} }








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

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
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_Cmd(USART1, ENABLE);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 4800;
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_Cmd(USART2, ENABLE);
}
/*
* 函数名:fputc
* 描述 :重定向c库函数printf到USART1
* 输入 :无
* 输出 :无
* 调用 :由printf调用
*/
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));

return (ch);
}
收藏 评论7 发布时间:2015-5-7 02:45

举报

7个回答
fieree 回答时间:2015-5-7 10:39:41
求求各位大神了
巧子儿 回答时间:2015-5-7 11:34:36
帮顶,希望能有答案
为什么是EEFOCUS小白 回答时间:2015-5-7 11:35:47
帮顶         
数码小叶 回答时间:2015-5-7 11:51:08
帮顶     .。。。。。。。。。。
s7631001 回答时间:2015-5-7 12:21:44
你在超级终端发了一个字节后,先让一个LED闪烁,检测一下收数据的情况。
eurphan 回答时间:2015-5-7 23:36:01
看不到哎
回答时间:2015-5-8 09:25:34
RCC_APB2Periph_AFIO,把这个时钟开一下试试。

所属标签

相似问题

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
微信公众号二维码 微信公众号
手机版二维码 手机版