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;
}
相关推荐
2401_8574396933 分钟前
SSM 架构下 Vue 电脑测评系统:为电脑性能评估赋能
开发语言·php
迷雾漫步者38 分钟前
Flutter组件————FloatingActionButton
前端·flutter·dart
SoraLuna1 小时前
「Mac畅玩鸿蒙与硬件47」UI互动应用篇24 - 虚拟音乐控制台
开发语言·macos·ui·华为·harmonyos
向前看-1 小时前
验证码机制
前端·后端
xlsw_1 小时前
java全栈day20--Web后端实战(Mybatis基础2)
java·开发语言·mybatis
燃先生._.2 小时前
Day-03 Vue(生命周期、生命周期钩子八个函数、工程化开发和脚手架、组件化开发、根组件、局部注册和全局注册的步骤)
前端·javascript·vue.js
Dream_Snowar2 小时前
速通Python 第三节
开发语言·python
高山我梦口香糖3 小时前
[react]searchParams转普通对象
开发语言·前端·javascript
m0_748235243 小时前
前端实现获取后端返回的文件流并下载
前端·状态模式