STM32串口发送数据模块
更新时间:2024-04-22 05:57:01 阅读量: 综合文库 文档下载
主函数:
#include \#include \int main(void) {
//系统时钟初始化函数定义 SystemInit();
//串口初始化函数定义 USART1_Config();
printf(\
printf(\欢迎使用CHD1807_STM32实验板:) \\r\\n\printf(\
USART1_printf(USART1, \
USART_SendData(USART1,0x31); //发送数据,参数1为串口外设,参数2为数据的ASCLL码
while(1); }
串口定义函数: #include \#include
* 函数名:USART1_Config
* 描述 :USART1 GPIO 配置,工作模式配置。115200 8-N-1 * 输入 :无 * 输出 : 无
* 调用 :外部调用 */
void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据长度为8为 USART_InitStructure.USART_StopBits = USART_StopBits_1; //帧结尾定义一个停止位 USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE); } /*
* 函数名:fputc
* 描述 :重定向c库函数printf到USART1 * 输入 :无 * 输出 :无
* 调用 :由printf调用 */
int fputc(int ch, FILE *f) { // } /*
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch); while (!(USART1->SR & USART_FLAG_TXE));
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); return (ch);
* 函数名:itoa
* 描述 :将整形数据转换成字符串
* 输入 :-radix =10 表示10进制,其他结果为0 * -value 要转换的整形数 * -buf 转换后的字符串 * -radix = 10 * 输出 :无 * 返回 :无
* 调用 :被USART1_printf()调用 */
static char *itoa(int value, char *string, int radix) {
int i, d; int flag = 0;
char *ptr = string;
/* This implementation only works for decimal numbers. */ if (radix != 10) {
*ptr = 0; return string; }
if (!value) {
*ptr++ = 0x30; *ptr = 0; return string; }
/* if this is a negative value insert the minus sign. */ if (value < 0) {
*ptr++ = '-';
/* Make the value positive. */ value *= -1; }
for (i = 10000; i > 0; i /= 10) {
d = value / i;
if (d || flag) {
*ptr++ = (char)(d + 0x30); value -= (d * i); flag = 1; } }
/* Null terminate the string. */ *ptr = 0;
return string;
} /* NCL_Itoa */ /*
* 函数名:USART1_printf
* 描述 :格式化输出,类似于C库中的printf,但这里没有用到C库 * 输入 :-USARTx 串口通道,这里只用到了串口1,即USART1 * -Data 要发送到串口的内容的指针 * -... 其他参数 * 输出 :无 * 返回 :无
* 调用 :外部调用
* 典型应用USART1_printf( USART1, \ * USART1_printf( USART1, \ * USART1_printf( USART1, \ */
void USART1_printf(USART_TypeDef* USARTx, uint8_t *Data,...) { const char *s; int d; char buf[16]; va_list ap; va_start(ap, Data);
while ( *Data != 0) // 判断是否到达字符串结束符 { if ( *Data == 0x5c ) //'\\' { switch ( *++Data ) { }
case 'r': //回车符 USART_SendData(USARTx, 0x0d); Data ++; break;
case 'n': //换行符 USART_SendData(USARTx, 0x0a); Data ++; break;
default: Data ++; break;
}
else if ( *Data == '%') { switch ( *++Data ) {
//
case 's': //字符串 s = va_arg(ap, const char *); for ( ; *s; s++) { USART_SendData(USARTx,*s); while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET ); } Data++; break; case 'd': //十进制 d = va_arg(ap, int); itoa(d, buf, 10); for (s = buf; *s; s++) { USART_SendData(USARTx,*s); while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET ); } Data++; break; default: Data++; break; } } /* end of else if */ else USART_SendData(USARTx, *Data++); while( USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET ); } }
/******************* CHD1807 Team *****END OF FILE************/
正在阅读:
STM32串口发送数据模块04-22
广东省江门市2017届高考语文一轮复习模拟试题 08含解析10-05
学籍证明(共5则范文)08-22
吐血整理,水利工程质量检测员知识06-20
我国地理标志保护制度的选择07-29
人教版数学六年级下册第三单元:《圆柱的认识》教学设计及反思03-18
电磁学实验复习要点09-09
泛函分析答案2.308-30
2016庆祝建党92周年征文:党在我心中03-10
- 多层物业服务方案
- (审判实务)习惯法与少数民族地区民间纠纷解决问题(孙 潋)
- 人教版新课标六年级下册语文全册教案
- 词语打卡
- photoshop实习报告
- 钢结构设计原理综合测试2
- 2014年期末练习题
- 高中数学中的逆向思维解题方法探讨
- 名师原创 全国通用2014-2015学年高二寒假作业 政治(一)Word版
- 北航《建筑结构检测鉴定与加固》在线作业三
- XX县卫生监督所工程建设项目可行性研究报告
- 小学四年级观察作文经典评语
- 浅谈110KV变电站电气一次设计-程泉焱(1)
- 安全员考试题库
- 国家电网公司变电运维管理规定(试行)
- 义务教育课程标准稿征求意见提纲
- 教学秘书面试技巧
- 钢结构工程施工组织设计
- 水利工程概论论文
- 09届九年级数学第四次模拟试卷
- 串口
- 模块
- 发送
- 数据
- STM32