stm32 普通io模拟串口程序

本程序中

使用GPIOB.5-作为TX,,,GPIOD.6作为RX

并将这两个IO口接在led上,能够查看发送和接受的数据状态。

使用tim3作为定时器中断,本程序中将波特率设置为 1bps  的原因是想从led灯的状态读取发送和接受的数据

 

下面为我的程序

#include 
#include "stm32f10x.h"

#include "misc.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"

void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void TIM3_Configuration(void);

unsigned char receive(int a);
int receive_bit(void);
void send(int value);
void send_bit(int send_bit_vlaue);
int send_bit_flag=0;
unsigned char receive_final_value=0;
unsigned char receive_disfinal_value=0;
int i = 0,k=0,j=0;
unsigned char  result = 0;
int value_bit[10];
int receive_flag=0;
int u=0x5a;


int main(void)
{
	RCC_Configuration();
  GPIO_Configuration();
	TIM3_Configuration();
	NVIC_Configuration();
	GPIO_SetBits(GPIOB,GPIO_Pin_2);
	GPIO_SetBits(GPIOB,GPIO_Pin_5);
	value_bit[9] = 1;
	while(1)
	{
		

		if(receive_final_value ==0x44 )
		{
			GPIO_SetBits(GPIOD,GPIO_Pin_3);
    }
		if(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5)==Bit_RESET)
	  {
		  send(0x44);
      send_bit_flag=1;			
    }
		
		
  }
	return 0;
}

void send(int value)
{
	value_bit[0]=0;
	value_bit[9]=1;
	for(i = 1; i < 9; i++)
	{
		value_bit[i] = value & 1;
		value = value >> 1;
  }
}


void send_bit(int send_bit_vlaue)
{
	if(send_bit_vlaue == 1)
	{
		GPIO_SetBits(GPIOB,GPIO_Pin_5);
  }
	else
	{
		GPIO_ResetBits(GPIOB,GPIO_Pin_5);
  }
}



int receive_bit()
{ 
	int receive_bit_value=1;
	receive_bit_value=GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_6);
	return receive_bit_value;
}

unsigned char  receive(int a)
{
	result=result>>1;
	if(a==1)
	{
		result=result|0x80;
	}
	return result;
}

void TIM3_IRQHandler(void)
{
	TIM_ClearITPendingBit(TIM3,TIM_IT_Update);	
	if(send_bit_flag==1)
	{
		send_bit(value_bit[k]);
		k++;
	}
	if(k==10)
	{
		send_bit_flag=0;
		k=0;
	}
	
	if(receive_flag==1)
	{
		receive_disfinal_value=receive(receive_bit());
		j++;
	}
	if(receive_bit() == 0)
	{
		receive_flag=1;
	}
	if(j==8)
	{
		receive_final_value=receive_disfinal_value;
		u=receive_final_value;
		receive_flag=0;
		j=0;
	}

}

void RCC_Configuration(void)
{
	SystemInit();
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); 
}

void GPIO_Configuration(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOB,&GPIO_InitStructure);
	GPIO_SetBits(GPIOB,GPIO_Pin_5);
	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init(GPIOD,&GPIO_InitStructure);
	

	
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
	GPIO_Init(GPIOD,&GPIO_InitStructure);
	

	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  GPIO_Init(GPIOC,&GPIO_InitStructure);
	

}

void TIM3_Configuration(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
	
  TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
  TIM_TimeBaseStruct.TIM_Period=1999;
  TIM_TimeBaseStruct.TIM_Prescaler=35999;
  TIM_TimeBaseStruct.TIM_ClockDivision=0;
  TIM_TimeBaseStruct.TIM_CounterMode=TIM_CounterMode_Up;
	
  TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStruct);
	
  TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
  TIM_Cmd(TIM3,ENABLE);
}


void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
	
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

 

你可能感兴趣的:(stm32,io模拟串口)