#include"stdio.h"
#include"stm32f10x.h"
void uart_init(uint32_t pclk2,uint32_t bound);
void USART1_IRQHandler(void);
int main(void)
{
RCC->APB2ENR|=(uint32_t)RCC_APB2ENR_IOPBEN;
GPIOB->CRH|=(uint32_t)(GPIO_CRH_CNF10_0|GPIO_CRH_MODE10);
uart_init(72,9600);
while(1)
{
}
}
void uart_init(uint32_t pclk2,uint32_t bound)
{
float temp;
uint16_t mantissa;
uint16_t fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
mantissa<<=4;
mantissa+=fraction;
//UART1³õʼ»¯
NVIC->ISER[1]|=NVIC_ISER_SETENA;
NVIC->ISER[0]|=NVIC_ISER_SETENA;
RCC->APB2ENR|=(uint32_t)RCC_APB2ENR_IOPAEN; //´ò¿ªGPIOAʱÖÓ
RCC->APB2RSTR|=(uint32_t)(RCC_APB2RSTR_USART1RST); //¸´Î»´®¿Ú1
RCC->APB2RSTR&=~(uint32_t)(RCC_APB2RSTR_USART1RST); //Í£Ö¹¸´Î»´®¿Ú1
RCC->APB2ENR|=(uint32_t)RCC_APB2ENR_USART1EN; //´ò¿ª´®¿ÚʱÖÓ
GPIOA->CRH|=(uint32_t)(GPIO_CRH_CNF9_1|GPIO_CRH_MODE); //TXÍÆÍ츴Óù¦ÄÜÊä³ö
GPIOA->CRH|=(uint32_t)(GPIO_CRH_CNF10_0); //RX¸¡¿ÕÊäÈë
USART1->BRR=mantissa; //ÉèÖò¨ÌØÂÊ
USART1->CR1|=(uint32_t)USART_CR1_UE; //UE=1£¬¼¤»îUSART1
USART1->CR1|=(uint32_t)USART_CR1_TE; //TE=1£¬·¢ËÍʹÄÜ
USART1->CR1|=(uint32_t)USART_CR1_RE; //RE=1£¬½ÓÊÜʹÄÜ
USART1->CR1|=(uint32_t)USART_CR1_RXNEIE; //½ÓÊÕ»º³åÇø·Ç¿ÕÖжÏʹÄÜ
}
void USART1_IRQHandler(void)
{
uint8_t temp;
GPIOB->ODR=~GPIOB->ODR;
if(USART1->SR&USART_SR_RXNE)
{
temp=USART1->DR;
USART1->DR=temp;
}
}
这是我写的STM32串口程序,想实现的功能是我往USART1发送一个字符,然后单片机再把这个字符发回给我。
但我这程序现在的状况就是:我给他发字符,而他却不进中断。熟悉STM32的大虾们帮我看看吧,谢谢!!!
|