airwill 发表于 2014-2-25 17:18 
STM32的串口2为啥就不能和51通信了啊?
注意 STM32 的两个串口用的时钟频率不同, 留意一下波特率的设置 ...
请教你一下,这是我用寄存器的写法修改的串口2,可是为什么传的数据在电脑上显示的全是问号啊???
这是usart.c里面的:
#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)
{
while((USART2->SR&0X40)==0);
USART2->DR = (u8) ch;
return ch;
}
#endif
//end
//////////////////////////////////////////////////////////////////
#if EN_USART2_RX
u8 USART_RX_BUF[USART_REC_LEN]; .
u16 USART_RX_STA=0;
void USART2_IRQHandler(void)
{
u8 res;
if(USART2->SR&(1<<5))
{
res=USART2->DR;
if((USART_RX_STA&0x80)==0)
{
if(USART_RX_STA&0x40)
{
if(res!=0x0a)USART_RX_STA=0;
else USART_RX_STA|=0x80;
}else //»¹Ã»ÊÕµ½0X0D
{
if(res==0x0d)USART_RX_STA|=0x40;
else
{
USART_RX_BUF[USART_RX_STA&0X3F]=res;
USART_RX_STA++;
if(USART_RX_STA> 63)USART_RX_STA=0;
}
}
}
}
}
#endif
//bound:²¨ÌØÂÊ
//CHECK OK
//091209
void uart_init(u32 pclk2,u32 bound)
{
float temp;
u16 mantissa;
u16 fraction;
temp=(float)(pclk2*1000000)/(bound*16);
mantissa=temp;
fraction=(temp-mantissa)*16;
mantissa<<=4;
mantissa+=fraction;
RCC->APB2ENR|=1<<2;
RCC->APB1ENR|=1<<17;
GPIOA->CRL&=0XFFFF00FF;//IO״̬ÉèÖÃ
RCC->APB1RSTR|=1<<17;
RCC->APB1RSTR&=~(1<<17);
USART2->BRR=mantissa;
USART2->CR1|=0X200C;
#if EN_USART2_RX
USART2->CR1|=1<<8;
USART2->CR1|=1<<5;
MY_NVIC_Init(3,3,USART2_IRQChannel,2);
#endif
}
main函数:
int main(void)
{
u8 key = '2';
u8 tmp;
u8 ch[5]={'1','2','3','4'};
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,9600);
LED_Init();
KEY_Init();
while(1)
{
USART2->DR='1';
while((USART2->SR&0X40)==0);
delay_ms(2200);
USART2->DR='1';
while((USART2->SR&0X40)==0);
delay_ms(2200);
}
}
|