7-1作业

1.实验目的:完成字符收发

led.h

cs 复制代码
#ifndef __GPIO_H__ 
#define __GPIO_H__

#include "stm32mp1xx_rcc.h"
#include "stm32mp1xx_gpio.h"
#include "stm32mp1xx_uart.h"

//RCC,GPIO,UART初始化
void init();

//字符数据发送
void set_tt(const char str);

//字符数据接收
char ret_tt();

//字符串数据发送
void seting_tt(const char* string);

//字符串数据接收
char reting_tt();


#endif                                            

led.c

cs 复制代码
 #include "led.h"
 
 //PB2,PG11,UART初始化
 void init()
 {
     //PB2,PG11,UART使能
     RCC-> MP_AHB4ENSETR |= (0X1<<1);
     RCC-> MP_AHB4ENSETR |= (0X1<<6);
     RCC-> MP_APB1ENSETR |= (0X1<<16);
 
     //PB2
     GPIOB->MODER |=(0X3<<4);
     GPIOB->MODER &=(~(0X1<<4));
 
     GPIOB->AFRL |=(0X15<<8);
     GPIOB->AFRL &=(~(0X7<<8));
 
     //PG11
     GPIOG->MODER |=(0X3<<22);
     GPIOG->MODER &=(~(0X1<<22));
 
     GPIOG->AFRH |=(0X15<<12);
     GPIOG->AFRH &=(~(0X9<<12));
 }
 
 //发送一个字符
 void set_tt(const char data)
 {
     //数据位
     USART4->CR1 &=(~(0X1<<12));
     USART4->CR1 &=(~(0X1<<28));
 
 
     USART4->CR1 &=(~(0X1<<10));
     USART4->CR1 &=(~(0X1<<15));
 
 
     //设置串口波特率
     USART4->BRR =0X22b;
 
     //设置停止位
     USART4->CR2 &=(~(0X3<<12));
 
     //使能USART 
     USART4->CR1 |=(0X1<<0);
 
     //设置发送位使能
     USART4->CR1 |=(0X1<<3);
 
     //发送数据
     if(USART4->ISR &(0X1<<7))
     {
         USART4->TDR=data;
         if(USART4->ISR &(0X1<<6))
         {
             return;
         }
     }                                                                                                                                            
 }
 
 //接受一个字符
 char ret_tt()
 {
     //数据位
     USART4->CR1 &=(~(0X1<<12));
     USART4->CR1 &=(~(0X1<<28));
 
     USART4->CR1 &=(~(0X1<<10));
     USART4->CR1 &=(~(0X1<<15));
 
 
     //设置串口波特率
     USART4->BRR =0X22b;
 
     //设置停止位
     USART4->CR2 &=(~(0X3<<12));
 
     //使能USART 
     USART4->CR1 |=(0X1<<0);
 
     //设置接受位使能
     USART4->CR1 |=(0X1<<2);
 
     //接受数据
     char data;
     while(1)
     {
         if(USART4->ISR &(0X1<<5))
         {
             data=USART4->RDR;
             return data;
         }
     }
     return 0;
 }
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        

main.c

cs 复制代码
#include "led.h"

int main()
{
    char data;
    init();
    while(1)
    {
        data=ret_tt();
        set_tt(data);
    }
    return 0;
}                             

2.实验目的:完成字符串收发

相关推荐
1379号监听员_3 小时前
hc05蓝牙模块
stm32·单片机·物联网
error:(3 小时前
【保姆级】WSL 2 中使用 PlatformIO 开发 ESP32 完整教程
stm32·单片机·嵌入式硬件
就是蠢啊3 小时前
51单片机——DS18B02(二)
单片机·嵌入式硬件·51单片机
沧海一条狗3 小时前
Ecat从站SSC代码与GD32H75E接口的适配注意事项
单片机·嵌入式硬件
minglie13 小时前
clion+RP2040-Zero的ws2812
单片机
点灯小铭3 小时前
基于单片机的加油站加油机显示控制系统设计
单片机·嵌入式硬件·毕业设计·课程设计·期末大作业
m0_690780524 小时前
串口通信,嵌入式系统
单片机·嵌入式硬件
沧海一条狗4 小时前
SOEM与ESC无法通讯问题解决方案
单片机
雾削木4 小时前
STM32 HAL 软件SPI任意GPIO控制st7735
stm32·单片机·嵌入式硬件
田甲4 小时前
【STM32】SHT30温湿度芯片驱动
stm32·单片机·嵌入式硬件