?? usart1.c
字號(hào):
#include "stm32f10x.h"
#include <stdio.h>
#include "Usart1.h"
#include <string.h>
/******************************************************************
- 功能描述:將一個(gè)32位的變量dat轉(zhuǎn)為字符串,比如把1234轉(zhuǎn)為"1234"
- 隸屬模塊:公開函數(shù)模塊
- 函數(shù)屬性:外部,用戶可調(diào)用
- 參數(shù)說明:dat:帶轉(zhuǎn)的long型的變量
str:指向字符數(shù)組的指針,轉(zhuǎn)換后的字節(jié)串放在其中
- 返回說明:無
******************************************************************/
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;
}
/******************************************************************
- 功能描述:將一個(gè)字符串轉(zhuǎn)為32位的變量,比如"1234"轉(zhuǎn)為1234
- 隸屬模塊:公開函數(shù)模塊
- 函數(shù)屬性:外部,用戶可調(diào)用
- 參數(shù)說明:str:指向待轉(zhuǎn)換的字符串
- 返回說明:轉(zhuǎn)換后的數(shù)值
******************************************************************/
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相關(guān)配置
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時(shí)鐘
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發(fā)送一字符串
void USART1_Putc(unsigned char c)
{
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
//串口1發(fā)送一字符串
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發(fā)送一字符
void USART2_Putc(unsigned char c)
{
USART_SendData(USART2, c);
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET );
}
//串口2發(fā)送一字符串
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);
}
}
/**************************************************************************
- 功能描述:?jiǎn)纹瑱C(jī)的串口發(fā)送數(shù)值
- 隸屬模塊:串口操作
- 函數(shù)屬性:外部,使用戶使用
- 參數(shù)說明:dat:要發(fā)送的數(shù)值
- 返回說明:無
- 注:函數(shù)中會(huì)將數(shù)值轉(zhuǎn)為相應(yīng)的字符串,發(fā)送出去。比如 4567 轉(zhuǎn)為 "4567"
**************************************************************************/
void UART_Put_Num(unsigned long dat)
{
char temp[20];
u32tostr(dat,temp);
UART_Send_Str(temp);
}
/**************************************************************************
- 功能描述:?jiǎn)纹瑱C(jī)的串口發(fā)送調(diào)試信息
- 隸屬模塊:串口操作
- 函數(shù)屬性:外部,使用戶使用
- 參數(shù)說明:inf:指向提示信息字符串的指針
dat:一個(gè)數(shù)值,前面的提示信息就是在說明這個(gè)數(shù)值的意義
- 返回說明:無
**************************************************************************/
void UART_Put_Inf(char *inf,unsigned long dat)
{
UART_Send_Str(inf);
UART_Put_Num(dat);
UART_Send_Str("\n");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -