#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
/* Whatever you require here. If the only file you are using is */
/* standard output using printf() for debugging, no file handling */
/* is required. */
};
/* FILE is typedef’ d in stdio.h. */
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
_sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
USART2->DR = (u8) ch;
while((USART2->SR&0X40)==0);
return ch;
}
#endif
#ifdef EN_USART2_RX //如果使能了接收
void send_usart2(unsigned char GPS_DATA)
{
USART2->DR=GPS_DATA;
LED0=!LED0;
while((USART2->SR&0X40)==0)LED0=!LED0;//等待发送结束
}
void USART2_IRQHandler(void)
{
LED1=!LED1;
if(USART2->SR&(1<<5))//接收到数据
{
send_usart2(USART1->DR);
}
}
#endif
void uart2_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);//得到USARTDIV
mantissa=temp; //得到整数部分
fraction=(temp-mantissa)*16; //得到小数部分
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2; //使能PORTA口时钟
RCC->APB1ENR|=1<<17;//使能串口2时钟
GPIOA->CRL=0X44448344;//
RCC->APB1RSTR|=1<<17;
RCC->APB1RSTR&=~(1<<17);
USART2->BRR=mantissa;
USART2->CR1|=0X200C;
#ifdef EN_USART2_RX //如果使能了接收
//使能接收中断
USART2->CR1|=1<<8;
USART2->CR1|=1<<5;
MY_NVIC_Init(3,3,USART2_IRQChannel,2);
#endif
}
我是改改原子哥原来的代码,发送正常,但是接收就会出错了!
|