可以用systick进行高精度时钟进行延时,将工程中包含delay.h文件就可以。
这种延时和定时器中断不同。
USART串口通信,初始化过后,利用中断函数,当接收到电脑发送的函数后,触发中断,接收到的字符加一,并发回电脑,单片机接受数据需要利用中断,在中断函数中写判断程序,可以把收到的数据放入数组,发送数据可以直接发送,也可以利用循环把数组中的数据发出去。
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
USART_InitTypeDef USART_InitStructure; //串口结构体定义
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
USART_InitStructure.USART_BaudRate=115200; //波特率设置为9600 //波特率
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //失能硬件流
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //开启发送和接受模式
USART_InitStructure.USART_Parity=USART_Parity_No; //无效验
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据长8位
USART_Init(USART1,&USART_InitStructure); /* 初始化USART1 */
USART_Cmd(USART1, ENABLE); /* 使能USART1 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中断 接收中断
USART_ClearFlag(USART1,USART_FLAG_TC);//清除USARTx的待处理标志位
/* 设置NVIC参数 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开USART1的全局中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为0
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void) //串口1中断函数
{
u8 k;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//检查指定的USART中断发生与否
{
k=USART_ReceiveData(USART1);
k++;
USART_SendData(USART1,k);//通过外设USARTx发送单个数据
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
usart_init();//串口1初始化
while(1);
}
可以直接用发送函数,检测串行口是否好使。
#include "stm32f10x.h"
#include "delay.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
USART_InitTypeDef USART_InitStructure; //串口结构体定义
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
USART_InitStructure.USART_BaudRate=115200; //波特率设置为115200
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //失能硬件流
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //开启发送和接受模式
USART_InitStructure.USART_Parity=USART_Parity_No; //无效验
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据长8位
USART_Init(USART1,&USART_InitStructure); /* 初始化USART1 */
USART_Cmd(USART1, ENABLE); /* 使能USART1 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中断 接收中断
USART_ClearFlag(USART1,USART_FLAG_TC);//清除USARTx的待处理标志位
/* 设置NVIC参数 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开USART1的全局中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为0
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
int a;
delay_init();
usart_init();//串口1初始化
for(a=0;a<10;a++)
{
USART_SendData(USART1,a);//通过外设USARTx发送单个数据
delay_ms(1000);
}
}
结果如下图。

可以用systick进行高精度时钟进行延时,将工程中包含delay.h文件就可以。
这种延时和定时器中断不同。
USART串口通信,初始化过后,利用中断函数,当接收到电脑发送的函数后,触发中断,接收到的字符加一,并发回电脑,单片机接受数据需要利用中断,在中断函数中写判断程序,可以把收到的数据放入数组,发送数据可以直接发送,也可以利用循环把数组中的数据发出去。
#include "stm32f10x.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
USART_InitTypeDef USART_InitStructure; //串口结构体定义
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
USART_InitStructure.USART_BaudRate=115200; //波特率设置为9600 //波特率
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //失能硬件流
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //开启发送和接受模式
USART_InitStructure.USART_Parity=USART_Parity_No; //无效验
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据长8位
USART_Init(USART1,&USART_InitStructure); /* 初始化USART1 */
USART_Cmd(USART1, ENABLE); /* 使能USART1 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中断 接收中断
USART_ClearFlag(USART1,USART_FLAG_TC);//清除USARTx的待处理标志位
/* 设置NVIC参数 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开USART1的全局中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为0
NVIC_Init(&NVIC_InitStructure);
}
void USART1_IRQHandler(void) //串口1中断函数
{
u8 k;
if(USART_GetITStatus(USART1,USART_IT_RXNE))//检查指定的USART中断发生与否
{
k=USART_ReceiveData(USART1);
k++;
USART_SendData(USART1,k);//通过外设USARTx发送单个数据
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
usart_init();//串口1初始化
while(1);
}
可以直接用发送函数,检测串行口是否好使。
#include "stm32f10x.h"
#include "delay.h"
void usart_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
USART_InitTypeDef USART_InitStructure; //串口结构体定义
NVIC_InitTypeDef NVIC_InitStructure;//中断结构体定义
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
/* 配置GPIO的模式和IO口 */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX //串口输出PA9
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化串口输入IO */
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //模拟输入
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX //串口输入PA10
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure); /* 初始化GPIO */
USART_InitStructure.USART_BaudRate=115200; //波特率设置为115200
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None; //失能硬件流
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //开启发送和接受模式
USART_InitStructure.USART_Parity=USART_Parity_No; //无效验
USART_InitStructure.USART_StopBits=USART_StopBits_1; //1位停止位
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //数据长8位
USART_Init(USART1,&USART_InitStructure); /* 初始化USART1 */
USART_Cmd(USART1, ENABLE); /* 使能USART1 */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能或者失能指定的USART中断 接收中断
USART_ClearFlag(USART1,USART_FLAG_TC);//清除USARTx的待处理标志位
/* 设置NVIC参数 */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //打开USART1的全局中断
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为0
NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
int a;
delay_init();
usart_init();//串口1初始化
for(a=0;a<10;a++)
{
USART_SendData(USART1,a);//通过外设USARTx发送单个数据
delay_ms(1000);
}
}
结果如下图。

举报