满意答案

kfak434215
2013.09.11

采纳率:41% 等级:12
已帮助:8966人
你说的是什么型号的单片机啊,你先看看串口的初始化
51
void main()
{ uchar OutDat; //定义输出变量
TMOD=0x20; //TMOD=0
TH1=0xf3; //12MHZ ,BPS:4800,N,8,1
TL1=0xf3;
PCON=0x80; //方式一
TR1=1;
SCON=0x40; //串口通信控制寄存器 模式一
OutDat=0xaa; //向串口发送固定数据值
for(;;) //循环程序
{
SBUF=OutDat;//发送数据
for(;;)
{ if(TI) //发送中断位 当发送停止位时置1,表示发送完成
break;
}
mDelay(500);
TI=0; //清零中断位
}
stm32单片机
首先是配置UART的GPIO口
/*******************************************************************************
* Function Name : UART1_GPIO_Configuration
* Description : Configures the uart1 GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void UART1_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// Configure USART1_Tx as alternate function push-pull
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);
// Configure USART1_Rx as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
然后是配置串口参数
/* 如果使用查询的方式发送和接收数据 则不需要使用串口的中断
如果需要使用中断的方式发送和接收数据 则需要使能串口中断
函数原形 void USART_ITConfig(USART_TypeDef* USARTx, u16 USART_IT, FunctionalState NewState)
功能描述 使能或者失能指定的 USART 中断
USART_IT 描述
USART_IT_PE 奇偶错误中断
USART_IT_TXE 发送中断
USART_IT_TC 传输完成中断
USART_IT_RXNE 接收中断
USART_IT_IDLE 空闲总线中断
USART_IT_LBD LIN中断检测中断
USART_IT_CTS CTS中断
USART_IT_ERR 错误中断
*/
/*******************************************************************************
* Function Name : UART1_Configuration
* Description : Configures the uart1
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void UART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 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;
/* Configure the USART1*/
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 Receive and Transmit interrupts */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
具体转化成ASCII码要看你的单片机的16进制的规律,从中可以找出其转换规律。
00分享举报