stm32单片机USART发送数据给电脑的程序 如果解决全分赠送。
羽辰席 2013-05-27 04:58:41 #include"stm32f10x.h"
u16 sheng[]={1,2,3,4,5,6,7,8,9};
int i=0;
void GPIO_Configuration()
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //将pa9口设置
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_Configuration()
{
RCC_APB2PeriphClockCmd( RCC_APB2Periph_ADC1| RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC|RCC_APB2Periph_USART1,ENABLE);//使能时钟
}
void NVIC_Configuration()
{
NVIC_InitTypeDef nvic_initstructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//配置中断优先级
nvic_initstructure.NVIC_IRQChannel=USART1_IRQn;
nvic_initstructure.NVIC_IRQChannelPreemptionPriority=NVIC_PriorityGroup_0;
nvic_initstructure.NVIC_IRQChannelSubPriority=NVIC_PriorityGroup_4;
nvic_initstructure.NVIC_IRQChannelCmd=ENABLE;//使能中断相当于中断的总开关
NVIC_Init(&nvic_initstructure);
}
void USART0_SendByte(u16 Data)
{
while (!(USART1->SR & USART_FLAG_TXE));
USART1->DR = (Data & (uint16_t)0x01FF);
}
//void USART_SendData(USART_TypeDef * USARTx, uint16_t Data)
int main(void)
{
// u16 temp;
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);//相当于中断的分开关,串口开关
NVIC_Configuration();
USART_Cmd( USART1, ENABLE);
while(1)
{
//USART0_SendByte(sheng[i]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
//while(USART_GetFlagStatus(USART1, USART_FLAG_TXE)==RESET);
}
void USART1_IRQHandler()
{
if(USART_GetITStatus( USART1, USART_IT_TXE)!=RESET)
{
USART0_SendByte(sheng[i]);
i++;
//USART_ClearFlag(USART0,USART_FLAG_TXE);
}
}
大神们看一下程序哪里有问题 实现的功能是网电脑上发送数据 电脑串口小助手接收区会显示1,2,3,4,5,6,7,8,9.。。。。。现在显示的是一堆乱码和其他字符。