11.13hw

main

cs 复制代码
 
#include "uart.h"
 
int main()
{
    uart_init();
 
    sendchar('h');
    char c = getchar();
    sendchar(c);
    while (1) {
    }
 
    return 0;
}

h

cs 复制代码
#ifndef __UART_H__
#define __UART_H__
 
typedef struct {
    unsigned int MODER;
    unsigned int OTYPER;
    unsigned int OSPEEDR;
    unsigned int PUPDR;
    unsigned int IDR;
    unsigned int ODR;
    unsigned int BSRR;
    unsigned int LCKR;
    unsigned int AFRL;
    unsigned int AFRH;
} gpio_t;
 
typedef struct {
    unsigned int CR1;
    unsigned int CR2;
    unsigned int CR3;
    unsigned int BRR;
    unsigned int GTPR;
    unsigned int RTOR;
    unsigned int RQR;
    unsigned int ISR;
    unsigned int ICR;
    unsigned int RDR;
    unsigned int TDR;
} uart_t;
 
#define UART4 ((uart_t*)0x40010000)
#define GPIOG ((gpio_t*)0x50008000)
#define GPIOB ((gpio_t*)0x50003000)
 
#define RCC_MP_AHB4ENSETR (*(unsigned int*)0x50000A28)
#define RCC_MP_APB1ENSETR (*(unsigned int*)0x50000A00)
 
void uart_init();
 
void sendchar(char c);
void led_flash();
char getchar();
#endif

c

cs 复制代码
#include "uart.h"
 
void uart_init()
{
    // GPIOG GPIOB时钟使能
    RCC_MP_AHB4ENSETR |= (1 << 1);
    RCC_MP_AHB4ENSETR |= (1 << 6);
    // URT4时钟使能
    RCC_MP_APB1ENSETR |= (1 << 16);
 
    // 复用
    GPIOB->MODER |= (2 << 2);
    GPIOG->MODER |= (2 << 22);
    // 复用功能
    GPIOB->AFRL |= (0x1 << 11);
    GPIOG->AFRH |= (0x6 << 12);
    // 位宽8
    UART4->CR1 &= (~(1 << 12));
    UART4->CR1 &= (~(1 << 28));
    // 16倍采样
    UART4->CR1 &= (~(1 << 15));
    // 无校验
    UART4->CR1 &= (~(1 << 10));
    // 停止位
    UART4->CR2 &= (~(3 << 12));
    // 波特率
    UART4->BRR |= (0x22b);
    // 发送使能
    UART4->CR1 |= (1 << 3);
    // 接收使能
    UART4->CR1 |= (1 << 2);
    // 串口使能
    UART4->CR1 |= 1;
}
 
void sendchar(char c)
{
 
     while (!(UART4->ISR & (1 << 7)))
        ;
    UART4->TDR = c;
    while (!(UART4->ISR & (1 << 6)))
        ;
}
 
char getchar()
{
    while (!(UART4->ISR & (1 << 5)))
        ;
    return (char)UART4->RDR;
}
相关推荐
kill bert1 小时前
第27周JavaSpringboot电商进阶开发 1.企业级用户验证
java·前端·数据库
拓端研究室TRL3 小时前
R软件线性模型与lmer混合效应模型对生态学龙类智力测试数据层级结构应用
开发语言·r语言
answerball3 小时前
🔥 Vue3响应式源码深度解剖:从Proxy魔法到依赖收集,手把手教你造轮子!🚀
前端·响应式设计·响应式编程
于慨4 小时前
计算机考研C语言
c语言·开发语言·数据结构
GGGGGGGGGGGGGG.4 小时前
使用dockerfile创建镜像
java·开发语言
请为小H留灯4 小时前
Python中很常用的100个函数整理
开发语言·python
Slow菜鸟4 小时前
ES5 vs ES6:JavaScript 演进之路
前端·javascript·es6
小冯的编程学习之路4 小时前
【前端基础】:HTML
前端·css·前端框架·html·postman
达斯维达的大眼睛5 小时前
QT小项目-简单的记事本
开发语言·qt
轩宇^_^5 小时前
C++ 类与对象的实际应用案例详解
开发语言·c++