//=============================================================================
// USART1 72MHZ USART2/3 36MHZ
// 9600波特率, 1个停止位,8个数据位, 偶校验
//2. 只有USART1使用PCLK2(最高72MHz)。其它USART使用PCLK1(最高36MHz)。
//=============================================================================
void STM32_USART_Configuration(void)
{
//USART_InitTypeDef USART_InitStructure;
//GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA Periph clock enable */
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE);
STM32_Rcc_Regs->apb2enr.all |=(RCC_IOPAEN|RCC_AFIOEN); // 时钟使能
/* USART1 Periph clock enable */
//RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART1, ENABLE);
STM32_Rcc_Regs->apb2enr.all|=RCC_USART1EN; // USART1时钟使能
STM32_Rcc_Regs->apb2enr.bit.USART1EN=1; // USART1时钟使能
/* Configure USART1 Tx (PA.09) 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);
STM32_Gpioa_Regs->crh.bit.CNF9=Output_Af_push_pull; // 2
STM32_Gpioa_Regs->crh.bit.MODE9=Output_Mode_50mhz; // 2
/* 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);
STM32_Gpioa_Regs->crh.bit.CNF10=Input_floating; // 1
STM32_Gpioa_Regs->crh.bit.MODE10=Input_Mode; // 0
// USART1_DeInit(USART1);
STM32_Rcc_Regs->apb2rstr.all |= RCC_USART1RST;// usart1 复位
STM32_Rcc_Regs->apb2rstr.all &= ~RCC_USART1RST;
/*
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;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_Low;
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
// Configure USART1
USART_Init(USART1, &USART_InitStructure);
*/
STM32_Usart1_Regs->cr2.bit.STOP=0;// 00:1个停止位
STM32_Usart1_Regs->cr2.bit.CLKEN=0;//
STM32_Usart1_Regs->cr2.bit.LBCL=0;//
STM32_Usart1_Regs->cr2.bit.CPOL=0;//
STM32_Usart1_Regs->cr2.bit.CPHA=1;//1:时钟第二个边沿进行数据捕获
STM32_Usart1_Regs->cr1.bit.M=1; // 字长 0:8数据位 ; 1:9个数据位
STM32_Usart1_Regs->cr1.bit.PCE=1; // 0:校验控制被禁止1:校验控制被使能
STM32_Usart1_Regs->cr1.bit.PS=0; // 0:偶校 EVEN
STM32_Usart1_Regs->cr1.bit.TE=1; // 1:发送被使能
STM32_Usart1_Regs->cr1.bit.RE=1; // 1:接收被使能
STM32_Usart1_Regs->cr3.bit.RTSE=0; //RTS硬件流控制被禁止
STM32_Usart1_Regs->cr3.bit.CTSE=0; //CTS硬件流控制被禁止
STM32_Usart1_Regs->cr3.bit.SCEN=0; //1:智能卡模式被禁止 ?***
// USARTDIV="72",000,000/(16*9600)=467.75=(467<<1)+(0.75*16)=1D40+0XC=1D4C
STM32_Usart1_Regs->brr.all=0x1d4c; // 9600波特率的分频
/* Enable USART1 Receive and Transmit interrupts */
//USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
STM32_Usart1_Regs->cr1.bit.RXNEIE=0;//接收缓冲区非空中断使能 1:当USART_SR中的ORE或者RXNE为1时,产生USART中断
STM32_Usart1_Regs->cr1.bit.TXEIE=0; // 发送缓冲区空中断使能 1:当USART_SR中的TXE为1时,产生USART中断
/* Enable the USART1 */
// USART_Cmd(USART1, ENABLE);
STM32_Usart1_Regs->cr1.bit.UE=1;// 1:USART模块使能
/* enabling interrupt 设置USART1中断优先级,和关中断*/
STM32_Nvic_Regs->Priority[37].all=0x40;
STM32_Nvic_Regs->Enable[1].bit.INT5=0; // 37 (37-32=5) 44 可设置 USART1 USART1全局中断 0x0000_00D4
}//END SUB
用户592313 2011-8-15 13:55