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;
}
相关推荐
秦jh_26 分钟前
【Linux】多线程(概念,控制)
linux·运维·前端
蜗牛快跑21339 分钟前
面向对象编程 vs 函数式编程
前端·函数式编程·面向对象编程
Dread_lxy40 分钟前
vue 依赖注入(Provide、Inject )和混入(mixins)
前端·javascript·vue.js
娅娅梨1 小时前
C++ 错题本--not found for architecture x86_64 问题
开发语言·c++
汤米粥1 小时前
小皮PHP连接数据库提示could not find driver
开发语言·php
冰淇淋烤布蕾1 小时前
EasyExcel使用
java·开发语言·excel
拾荒的小海螺1 小时前
JAVA:探索 EasyExcel 的技术指南
java·开发语言
涔溪1 小时前
Ecmascript(ES)标准
前端·elasticsearch·ecmascript
马剑威(威哥爱编程)1 小时前
哇喔!20种单例模式的实现与变异总结
java·开发语言·单例模式
榴莲千丞2 小时前
第8章利用CSS制作导航菜单
前端·css