⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart1.c

📁 STM32F103VET6驱动CMOS摄像头
💻 C
字号:
#include "stm32f10x.h"
#include <stdio.h>
#include "Usart1.h"
#include <string.h>

/******************************************************************
 - 功能描述:将一个32位的变量dat转为字符串,比如把1234转为"1234"
 - 隶属模块:公开函数模块
 - 函数属性:外部,用户可调用
 - 参数说明:dat:带转的long型的变量
             str:指向字符数组的指针,转换后的字节串放在其中           
 - 返回说明:无
 ******************************************************************/

void u32tostr(unsigned long dat,char *str) 
{
 char temp[20];
 unsigned char i=0,j=0;
 i=0;
 while(dat)
 {
  temp[i]=dat%10+0x30;
  i++;
  dat/=10;
 }
 j=i;
 for(i=0;i<j;i++)
 {
  str[i]=temp[j-i-1];
 }
 if(!i) {str[i++]='0';}
 str[i]=0;
}

/******************************************************************
 - 功能描述:将一个字符串转为32位的变量,比如"1234"转为1234
 - 隶属模块:公开函数模块
 - 函数属性:外部,用户可调用
 - 参数说明:str:指向待转换的字符串           
 - 返回说明:转换后的数值
 ******************************************************************/

unsigned long strtou32(char *str) 
{
 unsigned long temp=0;
 unsigned long fact=1;
 unsigned char len=strlen(str);
 unsigned char i;
 for(i=len;i>0;i--)
 {
  temp+=((str[i-1]-0x30)*fact);
  fact*=10;
 }
 return temp;
}
//串口1相关配置
void Usart_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  USART_ClockInitTypeDef USART_ClockInitStructure;

  //使能串口1,PA,AFIO总线
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1,ENABLE);
  /* Configure USART1 Tx (PA9) as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Configure USART1 Rx (PA10) as input floating */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

  USART_ClockInit(USART1, &USART_ClockInitStructure);
  USART_Init(USART1, &USART_InitStructure);
  /* Enable the USARTx IT */
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  /* Enable the USARTx */
  USART_Cmd(USART1, ENABLE); 

  /*
  //使能串口2时钟
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
	
	// A2 做T2X
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    // A3 做R2X 
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
	USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
	USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
	USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

    USART_ClockInit(USART2, &USART_ClockInitStructure);
    USART_Init(USART2, &USART_InitStructure);
    
    USART_Cmd(USART2, ENABLE);
	//串口2使用接收中断
	USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
	*/
 }

//串口1发送一字符串
void USART1_Putc(unsigned char c)
{
    USART_SendData(USART1, c);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
//串口1发送一字符串
void USART1_Puts(char * str)
{
    while(*str)
    {
        USART_SendData(USART1, *str++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    }
}
//串口2发送一字符
void USART2_Putc(unsigned char c)
{
    USART_SendData(USART2, c);
    while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET );
}
//串口2发送一字符串
void USART2_Puts(char * str)
{
    while(*str)
    {
        USART_SendData(USART2, *str++);
        /* Loop until the end of transmission */
        while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
    }

}

void UART_Send_Byte(char c)
{
	USART_SendData(USART1,c);
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );	

}
void UART_Send_Enter(void)
{
 UART_Send_Byte(0x0d);
 UART_Send_Byte(0x0a);
}

void UART_Send_Str(char *s)
{
 
 for(;*s;s++)
 {
 if(*s=='\n') 
  	UART_Send_Enter();
 else
  	UART_Send_Byte(*s);
 }
}

/**************************************************************************
 - 功能描述:单片机的串口发送数值
 - 隶属模块:串口操作
 - 函数属性:外部,使用户使用
 - 参数说明:dat:要发送的数值
 - 返回说明:无
 - 注:函数中会将数值转为相应的字符串,发送出去。比如 4567 转为 "4567" 
 **************************************************************************/

void UART_Put_Num(unsigned long dat)
{
 char temp[20];
 u32tostr(dat,temp);
 UART_Send_Str(temp);
}

/**************************************************************************
 - 功能描述:单片机的串口发送调试信息
 - 隶属模块:串口操作
 - 函数属性:外部,使用户使用
 - 参数说明:inf:指向提示信息字符串的指针
             dat:一个数值,前面的提示信息就是在说明这个数值的意义
 - 返回说明:无
 **************************************************************************/

void UART_Put_Inf(char *inf,unsigned long dat)
{
 UART_Send_Str(inf);
 UART_Put_Num(dat);
 UART_Send_Str("\n");  
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -