level 2
恒过能改
楼主
/* Filename: CSB.c
Revised: $2013-12-11
Author: zqh
*/
//#ifndef CSB_T
#define CSB_T
/**********************************************************************************
* MACROS
*/
#define IO_DIR_PORT_PIN(port, pin, dir) \
do { \
if (dir == IO_OUT) \
P#
#port#
#DIR |= (0x01<<(pin)); \
else \
P#
#port#
#DIR &= ~(0x01<<(pin)); \
}while(0)
/**********************************************************************************
* CONTANTS
*/
#define IO_IN 0
#define IO_OUT 1
#include "ZComDef.h"
#include "iocc2430.h"
/* 定义发射、接收端口*/
#define Trig P0_1
#define Echo P0_4
unsigned int time=0;
unsigned int valL=0;
unsigned int valH=0;
float S=0;
/*本地函数*/
static void startCSB(void);//启动传感器
static void SDelay(uint8 N);//延时N微秒
static void Delay_10us(unsigned int n);
static void init_T1(void);
__interrupt void P0_ISR(void);
static void init32(void);
/*全局函数*/
void readCSB(uint8 *pData, uint8 *pStartIndex);
// 32 NOPs == 1 usecs 因为延时还有计算的缘故,用了31个nop
void SDelay(uint8 N)
{
while(N--)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
void Delay_10us(unsigned int n)
{ /* 320NOPs == 10usecs 因为延时还有计算的缘故,用了310个nop*/
int tt,yy;
for(tt = 0;tt0;yy--);
{asm("NOP");}
}
void init32(void)
{
SLEEP &= ~0x04;
while(!(SLEEP&0x04));
CLKCON &=~ 0x47;
SLEEP |= 0x04;
}
void init_T1(void)
{
init32();
IO_DIR_PORT_PIN(0, 1, IO_OUT); //0为输入1为输出 设置TRIG 为输出模式
Trig=0; //将TRIG 设置为低电平
P0INP &= ~0x80; //有上拉、下拉 有初始化的左右
P0IE |= 0x10; //P0_4 中断使能 cc2530为P0IEN
PICTL |= 0x01; //设置P0_7~P0_0引脚,下降沿触发中断
IEN1 |= 0x20; // P0IE = 1; //打开P0中断
P0IFG = 0; //设置中断标志位,中断出发为1
}
/*启动传感器函数*/
void startCSB()
{
init_T1();
//IO_DIR_PORT_PIN(0, 1, IO_OUT);
Trig = 1;
SDelay(10);
Trig = 0;
IO_DIR_PORT_PIN(0, 4, IO_IN);
}
void readCSB(uint8 *pData, uint8 *pStartIndex)
{
startCSB();
T1CNTL=0;
T1CNTH=0;
while(!Echo); //当RX为零时等待
T1CTL = 0x09; //开启计数
/*while(Echo); //当RX为1计数并等待
T1CTL = 0x00; //关闭计数*/
EA = 1;
Delay_10us(60000);
// Delay_10us(60000);
//100MS等待中断
//Count();
//valL = T1CNTL;
// valH = T1CNTH;
time=valH*256+ valL;
S=(time*1.87)/100; //算出来是CM
pData[*pStartIndex] = S;
*pStartIndex = *pStartIndex + 1;
}
#pragma vector = P0INT_VECTOR
__interrupt void P0_ISR(void)
{
EA=0;
T1CTL = 0x00;
valL = T1CNTL;
valH = T1CNTH;
if(P0IFG&0x10) //外部ECHO反馈信号
{
P0IFG = 0;
}
// T1CTL = 0x09;
//T1CNTL=0;
// T1CNTH=0;
P0IF = 0; //清中断标志
EA=1;
}
2013年12月15日 10点12分
1
Revised: $2013-12-11
Author: zqh
*/
//#ifndef CSB_T
#define CSB_T
/**********************************************************************************
* MACROS
*/
#define IO_DIR_PORT_PIN(port, pin, dir) \
do { \
if (dir == IO_OUT) \
P#
#port#
#DIR |= (0x01<<(pin)); \
else \
P#
#port#
#DIR &= ~(0x01<<(pin)); \
}while(0)
/**********************************************************************************
* CONTANTS
*/
#define IO_IN 0
#define IO_OUT 1
#include "ZComDef.h"
#include "iocc2430.h"
/* 定义发射、接收端口*/
#define Trig P0_1
#define Echo P0_4
unsigned int time=0;
unsigned int valL=0;
unsigned int valH=0;
float S=0;
/*本地函数*/
static void startCSB(void);//启动传感器
static void SDelay(uint8 N);//延时N微秒
static void Delay_10us(unsigned int n);
static void init_T1(void);
__interrupt void P0_ISR(void);
static void init32(void);
/*全局函数*/
void readCSB(uint8 *pData, uint8 *pStartIndex);
// 32 NOPs == 1 usecs 因为延时还有计算的缘故,用了31个nop
void SDelay(uint8 N)
{
while(N--)
{
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
asm("nop");
}
}
void Delay_10us(unsigned int n)
{ /* 320NOPs == 10usecs 因为延时还有计算的缘故,用了310个nop*/
int tt,yy;
for(tt = 0;tt0;yy--);
{asm("NOP");}
}
void init32(void)
{
SLEEP &= ~0x04;
while(!(SLEEP&0x04));
CLKCON &=~ 0x47;
SLEEP |= 0x04;
}
void init_T1(void)
{
init32();
IO_DIR_PORT_PIN(0, 1, IO_OUT); //0为输入1为输出 设置TRIG 为输出模式
Trig=0; //将TRIG 设置为低电平
P0INP &= ~0x80; //有上拉、下拉 有初始化的左右
P0IE |= 0x10; //P0_4 中断使能 cc2530为P0IEN
PICTL |= 0x01; //设置P0_7~P0_0引脚,下降沿触发中断
IEN1 |= 0x20; // P0IE = 1; //打开P0中断
P0IFG = 0; //设置中断标志位,中断出发为1
}
/*启动传感器函数*/
void startCSB()
{
init_T1();
//IO_DIR_PORT_PIN(0, 1, IO_OUT);
Trig = 1;
SDelay(10);
Trig = 0;
IO_DIR_PORT_PIN(0, 4, IO_IN);
}
void readCSB(uint8 *pData, uint8 *pStartIndex)
{
startCSB();
T1CNTL=0;
T1CNTH=0;
while(!Echo); //当RX为零时等待
T1CTL = 0x09; //开启计数
/*while(Echo); //当RX为1计数并等待
T1CTL = 0x00; //关闭计数*/
EA = 1;
Delay_10us(60000);
// Delay_10us(60000);
//100MS等待中断
//Count();
//valL = T1CNTL;
// valH = T1CNTH;
time=valH*256+ valL;
S=(time*1.87)/100; //算出来是CM
pData[*pStartIndex] = S;
*pStartIndex = *pStartIndex + 1;
}
#pragma vector = P0INT_VECTOR
__interrupt void P0_ISR(void)
{
EA=0;
T1CTL = 0x00;
valL = T1CNTL;
valH = T1CNTH;
if(P0IFG&0x10) //外部ECHO反馈信号
{
P0IFG = 0;
}
// T1CTL = 0x09;
//T1CNTL=0;
// T1CNTH=0;
P0IF = 0; //清中断标志
EA=1;
}