新手上路
- 积分
- 29
- 金钱
- 29
- 注册时间
- 2013-8-28
- 在线时间
- 0 小时
|

楼主 |
发表于 2013-8-28 13:21:35
|
显示全部楼层
回复【8楼】Cary_Liu:
---------------------------------
这是我的串口程序
void USART2_Configuration(void) //串口2初始化函数
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
USART_ClockInitTypeDef USART_ClockInitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //选择GPIO的引脚 TX,管脚2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //GPIO输出模式:推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //GPIO输出频率:2MHZ
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIOA初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //选择GPIO的引脚 RX,管脚3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //GPIO浮空输入
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //GPIO输出频率:2MHZ
GPIO_Init(GPIOA,&GPIO_InitStructure); //GPIOA初始化
// USART_InitTypeDef USART_InitStructure;
// RCC_APB1PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO |RCC_APB1Periph_USART2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); //RCC中打开相应串口
USART_InitStructure.USART_BaudRate = 57600; //波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //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; //使能串口2 接收和发送
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(USART1,&USART_ClockInitStructure);
USART_Init(USART2,&USART_InitStructure); //串口初始化
USART_Cmd(USART2,ENABLE); //启动串口
}
//从串口2等待1字节数据
unsigned char USART2GetChar(void)
{
while(!USART2_SR_bit.RXNE);
return USART2_DR_bit.DR;
}
//从串口2等待1个字符串
void usart2GetStr(unsigned char *puiStr,unsigned long ulNum)
{
for(;ulNum>0;ulNum--){
*puiStr++=USART2GetChar();
}
}
//往串口2发送1字节
void USART2PutChar(unsigned char Value)
{
while(!USART2_SR_bit.TXE);
USART2_DR_bit.DR=Value;
}
//往串口2发送1个字符串
void USART2PutString(unsigned char *pString, unsigned char s)
{
unsigned int i;
for(i = 0;i < s; i++)
{
USART2PutChar(pString);
}
} |
|