初级会员

- 积分
- 169
- 金钱
- 169
- 注册时间
- 2013-4-6
- 在线时间
- 3 小时
|
发表于 2013-6-29 18:01:37
|
显示全部楼层
void uart3_init(u32 bound) //串口3配置
{
//GPIO端口設置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//USART3_TX   B.10
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);
//USART3 _RX   B.11
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //上拉懸空
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Usart3 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; // 搶佔優先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; // 響應優先級1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據NVIC_InitStruct中指定的參數初始化外設NVIC寄存器USART1
//USART 初始化設置
USART_InitStructure.USART_BaudRate = bound; //傳送速率9600bps;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //資料位元長度=8 , 停止位=1 , 總長9bits
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位 = 1
USART_InitStructure.USART_Parity = USART_Parity_No; // 不做檢查和 NONE
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_ClearFlag(USART3, USART_FLAG_TC); //清零發送置位 ???????
USART_ITConfig(USART3,USART_IT_TC,ENABLE); //開發送中斷
USART_Cmd(USART3, ENABLE); //使能串口
}
void beginSend3(void)
{
sendEnable3(); //設為發送//
UsartBuf[1].sendPosi = 0;
if(UsartBuf[1].sendCount > 1)
UsartBuf[1].sendCount--;
USART_SendData(USART3,UsartBuf[1].sendBuf[0]);
}
如果中断设置都没有问题, 那么要注意, USART的中断是要先送出一个字元以后, 才会开始循环工作喔. 是不是这里漏了 ??? --> USART_SendData(USARTx, char) |
|