串口配置的寄存器被写成一个结构体的代码
2017-11-07 10:31
323 查看
typedef struct
{
uint32_t USART_BaudRate; //串口波特率
uint32_t USART_WordLength; //数据位宽
uint32_t USART_StopBits; //停止位宽
uint32_t USART_Parity; //效验位宽
uint32_t USART_Mode; //工作模式
uint32_t USART_HardwareFlowControl; //硬件流控制
} USART_InitTypeDef;
开发板与PC通信程序:
#include \"stm32f0xx.h\"
#include \"stdio.h\"
#define count(a) (sizeof(a)/sizeof(*(a)))
uint8_t Tx_Buffer[] = \"EEWORLD-[晒心得]STM32F03--USART实验\";
/*串口初始化配置 */
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/*初始化USART1时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;//设置串口波特率
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//设置工作模式
USART_InitStructure.USART_Parity = USART_Parity_No;//设置效验位
USART_InitStructure.USART_StopBits = USART_StopBits_1;//设置停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//设置数据位
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);//使能串口 1
}
/*GPIOA初始化配置 */
void GPIOA_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
/*初始化GPIOA时钟*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/* 配置PA9 ,PA10*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置端口复用
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*发送 1 字节数据*/
void UART_send_byte(uint8_t byte)
{
while(!((USART1->ISR)&(1<<7)));//等待发送完
USART1->TDR=byte; //发送一个字节
}
/*发送字符串*/
void UART_Send(uint8_t *Buffer, uint32_t Length)
{
while(Length != 0)
{
while(!((USART1->ISR)&(1<<7)));//等待发送完
USART1->TDR= *Buffer;
Buffer++;
Length--;
}
}
uint8_t UART_Recive(void)
{
while(!(USART1->ISR & (1<<5)));
return(USART1->RDR);
}
void Delay(uint32_t temp)
{
for(; temp!= 0; temp--);
}
int main(void)
{
GPIOA_Config();
USART_Config();
while(1)
{
UART_Se
9e38
nd( Tx_Buffer, count(Tx_Buffer)-1);
//USART_SendData(USART1, 0xff);
Delay(500000);
}
}
{
uint32_t USART_BaudRate; //串口波特率
uint32_t USART_WordLength; //数据位宽
uint32_t USART_StopBits; //停止位宽
uint32_t USART_Parity; //效验位宽
uint32_t USART_Mode; //工作模式
uint32_t USART_HardwareFlowControl; //硬件流控制
} USART_InitTypeDef;
开发板与PC通信程序:
#include \"stm32f0xx.h\"
#include \"stdio.h\"
#define count(a) (sizeof(a)/sizeof(*(a)))
uint8_t Tx_Buffer[] = \"EEWORLD-[晒心得]STM32F03--USART实验\";
/*串口初始化配置 */
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/*初始化USART1时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = 9600;//设置串口波特率
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//设置工作模式
USART_InitStructure.USART_Parity = USART_Parity_No;//设置效验位
USART_InitStructure.USART_StopBits = USART_StopBits_1;//设置停止位
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//设置数据位
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);//使能串口 1
}
/*GPIOA初始化配置 */
void GPIOA_Config()
{
GPIO_InitTypeDef GPIO_InitStructure;
/*初始化GPIOA时钟*/
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
/* 配置PA9 ,PA10*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //设置端口复用
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/*发送 1 字节数据*/
void UART_send_byte(uint8_t byte)
{
while(!((USART1->ISR)&(1<<7)));//等待发送完
USART1->TDR=byte; //发送一个字节
}
/*发送字符串*/
void UART_Send(uint8_t *Buffer, uint32_t Length)
{
while(Length != 0)
{
while(!((USART1->ISR)&(1<<7)));//等待发送完
USART1->TDR= *Buffer;
Buffer++;
Length--;
}
}
uint8_t UART_Recive(void)
{
while(!(USART1->ISR & (1<<5)));
return(USART1->RDR);
}
void Delay(uint32_t temp)
{
for(; temp!= 0; temp--);
}
int main(void)
{
GPIOA_Config();
USART_Config();
while(1)
{
UART_Se
9e38
nd( Tx_Buffer, count(Tx_Buffer)-1);
//USART_SendData(USART1, 0xff);
Delay(500000);
}
}
相关文章推荐
- Online Coding开发模式 (通过在线配置实现一个表模型的增删改查功能,无需写任何代码)
- SYD8801代码解析【BLE_SendData函数gap_att_report结构体设置】【蓝牙广播的实现】【协议栈回调事件】【自动睡眠】【使用内部晶振】【蓝牙状态机的说明】【断线时重新配置IO口】
- WCF基础 (续 通过代码和配置文件写一个WCF服务)
- Online Coding开发模式 (通过在线配置实现一个表模型的增删改查功能,无需写任何代码)
- 嵌入式系统总是要用户对变量或寄存器进行位操作。给定一个整型变量a,写两段代码,第一个设置a的bit 3,第二个清除a 的bit 3。在以上两个操作中,要保持其它位不变。
- Spring 3.0 学习-DI 依赖注入_创建Spring 配置-使用一个或多个XML 文件作为配置文件,使用自动注入(byName),在代码中使用注解代替自动注入,使用自动扫描代替xml中bea
- 串口寄存器配置
- 发一个51单片机的模拟串口代码
- 关于cas-client单点登录客户端拦截请求和忽略/排除不需要拦截的请求URL的问题(不需要修改任何代码,只需要一个配置)
- linux系统管理客户端1--串口配置代码编写
- 一个能够兼容IE6,IE8,IE9,IE10,IE11,谷歌,火狐,360等常见的10个浏览器的HTML头部信息配置代码
- 在内核代码包中寻找一个函数的,结构体等的定义的方法
- 一个串口操作的C++代码
- tomcat同一个端口同一个域名发布两个项目server.xml的配置代码
- STM8串口初始化寄存器配置
- python 从两个配置文件读取数据,形成一个url思路代码
- 一个自己用的代码备份工具,支持delphi,android,java,可以自己添加配置,灵活支持大部分编程语言
- js编程思路--把相同功能的代码,配置放到一个对象里
- 一个实际的sonar代码检查的配置文件
- apache配置一个域名读取多个路径代码(包括主干和分支)