LM3S811 使用心得二 之超声波测距
用LM3S811控制超声波传感器,
计算出到障碍物的距离,并通过5110lcd显示出来
//main.c
#include "LM3S811.h"
#include "uart1.h"
#include "uart0.h"
#include <stdio.h>
/*****************超声波数据结构声明**********************/
typedef struct suWaveData
{
uint8 flag;
uint8 tempcmd[4];
//温度
uint8 tempdata[2];
uint8 distcmd[4];
//距离
uint8 distdata[2];
}suWave_t;
suWave_t suWave1={0x00,0x11,0x00,0x00,0x11,0x00,0x00,
0x22,0x55,0x00,0x77,0x00,0x00}; //初始化超声波数据变量
/******************函数声明*********************************/
static void init_LM3S811(void);
static void delay(uint16 dly);
void process_udata(suWave_t *pfile);
/*********************************************
function:main 函数
input: null
output: null
author: gyk
data:
2011_11_18
*********************************************/
int main(void)
{
init_LM3S811();
init_UART0(9600,2);
init_UART1(9600,3);
IntMasterEnable();
send_str_UART1("System initialization sucessful!!!\n");
send_str_UART1("Waiting for the data from URM37V3.2...\n");
for(;;)
{
send_UART0(suWave1.tempcmd,4);
delay(100);
send_UART0(suWave1.distcmd,4);
delay(100);
process_udata(&suWave1);
delay(5000);
}
}
/************************************************
function:初始化
input: null
output: null
author: gyk
data:
2011_11_18
*********************************************/
static void init_LM3S811(void)
{
SysCtlClockSet(SYSCTL_SYSDIV_1|SYSCTL_USE_OSC|SYSCTL_OSC_MAIN|SYSCTL_XTAL_6MHZ);
}
/************************************************
function:延时
input: dly
output: null
author: gyk
data:
2011_11_18
*********************************************/
static void delay(uint16 dly)
{
uint16 i;
for(;dly>0;dly--)
for(i=0;i<100;i++);
}
/************************************************
function: 超声波数据处理
input: null
output: null
author: gyk
data:
2011_11_18
*********************************************/
void process_udata(suWave_t *pfile)
{
uint8 buffer[30];
uint16 tmp;
fp32 ftmp;
if(pfile->flag&&0x01)
{
if((pfile->tempdata[0]==0xFF)&&(pfile->tempdata[1]==0xFF))
{
send_str_UART1("Temperature: invalid data!\n");
return;
}
if((pfile->tempdata[0]&0xF0)==0xF0)
{
tmp=(uint16)(pfile->tempdata[0]&0xF0);
tmp<<=8;
tmp|=(uint16)pfile->tempdata[1];
ftmp=(fp32)tmp*0.1;
send_UART1(buffer,sprintf((char *)buffer,"temerature: -%f\n",ftmp));
}
if((pfile->tempdata[0]&0xF0)==0x00)
{
tmp=(uint16)(pfile->tempdata[0]);
tmp<<=8;
tmp|=(uint16)pfile->tempdata[1];
ftmp=(fp32)tmp*0.1;
send_UART1(buffer,sprintf((char *)buffer,"temerature: %f\n",ftmp));
}
pfile->flag&=0xFE;
}
if(pfile->flag&0x02)
{
tmp=(uint16)pfile->distdata[0];
tmp<<=8;
tmp|=pfile->distdata[1];
if(tmp==0xFFFF)
{
send_str_UART1("distance: invalid data!\n");
return;
}
send_UART1(buffer,sprintf((char *)buffer,"distance: %f\n",tmp));
pfile->flag&=0xFD;
}
}
/************************************************
function: interrupt
input: null
output: null
author: gyk
data:
2011_11_18
*********************************************/
void uart0_isr(void)
{
uint8 ch;
unsigned long ulStatus;
ulStatus=UARTIntStatus(UART0_BASE,true);
UARTIntClear(UART0_BASE,ulStatus);
if((ulStatus&UART_INT_RT)||(ulStatus&UART_INT_RX))
{
ch=UARTCharGet(UART0_BASE);
if(ch==0x11)
{
suWave1.tempdata[0]=UARTCharGet(UART0_BASE);
suWave1.tempdata[1]=UARTCharGet(UART0_BASE);
suWave1.flag|=0x01;
}
if(ch==0x022)
{
suWave1.distdata[0]=UARTCharGet(UART0_BASE);
suWave1.distdata[1]=UARTCharGet(UART0_BASE);
suWave1.flag|=0x02;
}
}
}
//uart0.h
#ifndef _UART0_H_
#define _UART0_H_
extern uint8 init_UART0(uint32 baudrate,uint8 prio);
extern void send_UART0(uint8 *pbuffer,uint16 size);
extern void recv_UART0(void);
extern void send_str_UART0(uint8 *pstr);
#endif
//uart1.h
#ifndef _UART1_H_
#define _UART1_H_
extern uint8 init_UART1(uint32 baudrate,uint8 prio);
extern void send_UART1(uint8 *pbuffer,uint16 size);
extern void recv_UART1(void);
extern void send_str_UART1(uint8 *pstr);
#endif
//uart0.c
#include"LM3S811.h"
/************************************************
function: 初始化串口0
input: 波特率,ISR优先级
output: fasle失败,true成功
author: gyk
data:
2011_11_18
*********************************************/
uint8 init_UART0(uint32 baudrate,uint8 prio)
{
if(baudrate>15200)
{
return FALSE;
}
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeUART(GPIO_PORTA_BASE,GPIO_PIN_0|GPIO_PIN_1);
UARTConfigSetExpClk(UART0_BASE,SysCtlClockGet(),baudrate,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
UARTFIFOLevelSet(UART0_BASE,UART_FIFO_TX1_8,UART_FIFO_RX4_8);
UARTIntEnable(UART0_BASE,UART_INT_RX|UART_INT_RT);
IntEnable(INT_UART0);
IntPrioritySet(INT_UART0,prio);
UARTEnable(UART0_BASE);
return TURE;
}
/************************************************
function: 串口0发送数据
input: 缓冲区大小,数据大小
output: null
author: gyk
data:
2011_11_18
*********************************************/
void send_UART0(uint8 *pbuffer,uint16 size)
{
uint16 i;
for(i=0;i<size;i++)
{
UARTCharPut(UART0_BASE,pbuffer);
}
while(UARTBusy(UART0_BASE));
}
/************************************************
function: 串口0发送字符串
input: 字符串指针
output: null
author: gyk
data:
2011_11_18
*********************************************/
void send_str_UART0(uint8 *pstr)
{
while(*pstr!='\0')
UARTCharPut(UART0_BASE,*pstr++);
while(UARTBusy(UART0_BASE));
}
//uart1.c
#include"LM3S811.h"
/************************************************
function: 初始化串口1
input: 波特率,ISR优先级
output: fasle失败,true成功
author: gyk
data:
2011_11_18
*********************************************/
uint8 init_UART1(uint32 baudrate,uint8 prio)
{
if(baudrate>15200)
{
return FALSE;
}
(void)prio;
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
GPIOPinTypeUART(GPIO_PORTD_BASE,GPIO_PIN_2|GPIO_PIN_3);
UARTConfigSetExpClk(UART1_BASE,SysCtlClockGet(),baudrate,(UART_CONFIG_WLEN_8|UART_CONFIG_STOP_ONE|UART_CONFIG_PAR_NONE));
UARTFIFOLevelSet(UART1_BASE,UART_FIFO_TX1_8,UART_FIFO_RX4_8);
UARTEnable(UART1_BASE);
return TURE;
}
/************************************************
function: 串口1发送数据
input: 缓冲区大小,数据大小
output: null
author: gyk
data:
2011_11_18
*********************************************/
void send_UART1(uint8 *pbuffer,uint16 size)
{
uint16 i;
for(i=0;i<size;i++)
{
UARTCharPut(UART1_BASE,pbuffer);
}
while(UARTBusy(UART1_BASE));
}
/************************************************
function: 串口1发送字符串
input: 字符串指针
output: null
author: gyk
data:
2011_11_18
*********************************************/
void send_str_UART1(uint8 *pstr)
{
while(*pstr!='\0')
UARTCharPut(UART1_BASE,*pstr++);
while(UARTBusy(UART1_BASE));
}
下载 (46.53 KB)2011-11-22 18:25