///======================US100.c=================================
#include "US100.h"
#include "systick.h"
#include "usart.h"
extern char RxCounter2,RxBuffer2[100];
char buff[100] = {0};
int tem = 0;
int distance = 0;
int index = 0;
void clear_buff2()//串口2缓存清理函数
{
int i;
for(i=0;i<100;i++)
RxBuffer2[i]=0;
RxCounter2=0;
index=0;
}
void readTembuff()//读取温度与距离 :周期36+55+5=96ms;频率约等于10hz
{
UART2_send_byte(0x55);//发送测距命令
delay_ms(36);
delay_ms(55);
UART2_send_byte(0x50);//发送测温命令
delay_ms(5);
distance = RxBuffer2[0]*256+RxBuffer2[1];
tem = RxBuffer2[2]-45;
clear_buff2();
}
////==============================USART.c=====================================
#include "usart.h"
#include "stdio.h"
#include "string.h"
#include "timer.h"
void UART2_send_byte(char data)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, data);
}
char RxCounter2,RxBuffer2[100];
extern u8 Timeout2;
void uart2_init()
{
GPIO_InitTypeDef GPIO_InitStructure; //?????????,?????GPIO
USART_InitTypeDef USART_InitStructure; //???????
NVIC_InitTypeDef NVIC_InitStructure;//???????
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //????
/* ??GPIO????IO? usart2*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;//TX //????PA2
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //??????
GPIO_Init(GPIOA,&GPIO_InitStructure); /* ???????IO */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;//RX //????PA3
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //????
GPIO_Init(GPIOA,&GPIO_InitStructure); /* ???GPIO */
USART_InitStructure.USART_BaudRate=9600; //??????9600 //???
USART_InitStructure.USART_WordLength=USART_WordLength_8b; //???8?
USART_InitStructure.USART_StopBits=USART_StopBits_1; //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_Init(USART2,&USART_InitStructure); /* ???USART2 */
USART_Cmd(USART2, ENABLE); /* ??USART2 */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//?????????USART?? ????
USART_ClearFlag(USART2,USART_FLAG_TC);//??USARTx???????
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; //??USART2?????
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //??????0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //??????0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //??
NVIC_Init(&NVIC_InitStructure);
}
void Uart2_SendStr(char*SendBuf)
{
while(*SendBuf)
{
while((USART2->SR&0X40)==0)
{
}//µÈ´ý·¢ËÍÍê³É
USART2->DR = (u8) *SendBuf;
SendBuf++;
}
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //½ÓÊÕÖжϣ¬¿ÉÒÔÀ©Õ¹À´¿ØÖÆ
{
TIM_Cmd(TIM4, ENABLE);
Timeout2=0;
RxBuffer2[RxCounter2++] =USART_ReceiveData(USART2);//½ÓÊÕÄ£¿éµÄÊý¾Ý
}
}
#ifndef __US100_H_
#define __US100_H_
void readTembuff(void);
#endif
#ifndef __USART_H
#define __USART_H
#include "stdio.h"
#include "stm32f10x.h"
#define u8 int8_t
#define u16 int16_t
#define u32 int32_t
void uart2_init();
void UART2_send_byte(char data);
#endif
===================================================================