stm32单片机USART发送数据给电脑的程序 如果解决全分赠送。
#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.。。。。。现在显示的是一堆乱码和其他字符。
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.。。。。。现在显示的是一堆乱码和其他字符。
...全文
相关推荐
STM32USART串口单片机发送数据
非常详细的介绍了如何使用STM32的串口USART发送数据,还附带非常完整的程序。
STM32串口USART2发送接收程序
运行的芯片是STM32F103RCT6 使用单独的USART2通道 功能:使用串口调试助手,发送什么就返回什么 程序说明:本程序是用野火的程序移植后修改的
STM32库函数USART_SendData的缺陷和解决方法
学习STM32单片机过程中遇到的问题现象、原因与解决方法。
STM32串口USART1的查询和中断方式程序
资源介绍和使用方法见博客:http://www.cnblogs.com/zitech/p/4989001.html
STM32串口USART2程序
对控制LED指示灯的IO口进行了初始化,将端口配置为推挽上拉输出,口线速度为50Mhz。PA2,PA2端口复用为串口2的TX,RX。在配置某个口线时,首先应对它所在的端口的时钟进行使能。否则无法配置成功,由于用到了端口B, 因此要对这个端口的时钟。进行使能,同时由于用到复用IO口功能用于配置串口。因此还要使能AFIO(复用功能IO)时钟。


单片机/工控
