金牌会员
 
- 积分
- 1604
- 金钱
- 1604
- 注册时间
- 2017-12-18
- 在线时间
- 173 小时
|
10金钱
用单片机向pc发送数据,通过串口打印,但收到的数据不正确!发送0x01,收到的是FE;发送0x02,收到的是FD;发送0x03,收到的是FC;有没有大神遇到过这种问题。帮小弟看看
int main(void)
{
USART1_Config();
Send_Test();
while(1)
{
}
}
------------------------------------------------------------------------------------------------------------------------------
void Send_Test(void)
{
USART1->SR;
USART_SendData(USART1,0x03);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
-------------------------------------------------------------------------------------------------------------------------------
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
USART_DeInit(USART1);
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);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
}
|
最佳答案
查看完整内容[请看2#楼]
那就是我说的问题了,DB9那个一般是RS232,0V表示没数据,-12(一般-9到-15V都可能)表示1,+12表示0。而串口是TTL电平,高电平1(3.3V),低电平0(0V)。你这么接没烧东西都算运气好了。
如果用单片机直接接电脑可以买usb转ttl那种小串口头。
如果要接DB9那种RS232的话需要电路这边增加一个max232芯片
|