新手入门
- 积分
- 9
- 金钱
- 9
- 注册时间
- 2021-11-2
- 在线时间
- 2 小时
|
5金钱
本帖最后由 YANGWENSONG 于 2022-7-23 00:38 编辑
有没有大哥们写过这个GPIO模拟串口通信的问题这个是基于STM32F407ZGT6的板子写的
不知道为什么串口助手是收不到任何信息的,但是我的灯是一直亮的,我也不清楚就是哪个位置出现了问题。
下面是我的头文件的定义GPIO_TX_RX.h
#ifndef __GPIO_TX_RX_H
#define __GPIO_TX_RX_H
void GPIO_TX_Init(void);
void GPIO_RX_Init(void);
#endif
然后是GPIO_TX_RX.c
#include "stm32f4xx.h"
#include "delay.h"
#include "GPIO_TX_RX.h"
GPIO_InitTypeDef GPIO_InitStruction;
void GPIO_TX_Init()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStruction.GPIO_Mode=GPIO_Mode_OUT;
GPIO_InitStruction.GPIO_OType=GPIO_OType_PP;
GPIO_InitStruction.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStruction.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruction.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOF,&GPIO_InitStruction);
GPIO_SetBits(GPIOF,GPIO_Pin_9);
}
void GPIO_RX_Init()
{
GPIO_InitStruction.GPIO_Mode=GPIO_Mode_IN;
GPIO_InitStruction.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruction.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStruction.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOF,&GPIO_InitStruction);
GPIO_SetBits(GPIOF,GPIO_Pin_10);
}
最后是主函数main
#include "stm32f4xx.h"
#include "usart.h"
#include "delay.h"
#include "GPIO_TX_RX.h"
#define TX_H GPIO_SetBits(GPIOF,GPIO_Pin_9) //灯灭
#define TX_L GPIO_ResetBits(GPIOF,GPIO_Pin_9) //灯亮
#define READ_LEVEL (GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_12)) //读取电平
u8 txbuf[7] ="welcome";
u8 cnt;
u8 readata,i;
void Uart_tx_one_byte (u8 date)
{
int i;
TX_L;
delay_us(104);
for(i=0;i<8;i++)
{
if(date&(0x01<<i))
{
TX_H;
}
else
{
TX_L;
}
delay_us(104);
}
TX_H;
delay_us(104);
}
//void Uart_tx_bytes(u8 *pdata ,u8 len)
//{
// u8 i;
// for(i=0;i<len;i++)
// {
// Uart_tx_one_byte(*(pdata+i));
// }
//}
u8 UART_RX(void)
{
cnt=READ_LEVEL;
if(cnt==0)
{
delay_us(10);
if(!READ_LEVEL)
{
for(i=0;i<8;i++)
{
delay_ms(100);
readata = readata|(READ_LEVEL<<i);
}
delay_us(100);
if(READ_LEVEL)
{
return 0;
}
else
{
return 0xff;
}
}
else
{
return 0xff;
}
}
else
{
return 0xff;
}
}
int main(void)
{
delay_init(168);
GPIO_TX_Init();//TX端IO口初始化//
GPIO_RX_Init();//RX端IO口初始化//
//uart_init(9600);
while(1)
{
delay_us(50);
Uart_tx_one_byte (0x01);
}
}
|
|