通过CC2530驱动超声波模块,测量距离,并在串口中打印出来。
单片机源程序如下:
- #include <ioCC2530.h>
- #include "Ultrasound.h"
- #include <stdio.h>
- #include <string.h>
- uchar RG;
- uchar H1;
- uchar L1;
- uchar H2;
- uchar L2;
- uchar H3;
- uchar L3;
- uint data;
- uchar TXData;
- float distance;
-
- uchar LoadRegBuf[4];//全局数据,用以存储定时计数器的值。
-
- void Delay_1us(uint microSecs)
- { while(microSecs--)
- { /* 32 NOPs == 1 usecs 因为延时还有计算的缘故,用了31个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");
- asm("nop");
- }
- }
- void Delay_10us(uint n)
- { /* 320NOPs == 10usecs 因为延时还有计算的缘故,用了310个nop*/
- uint tt,yy;
- for(tt = 0;tt<n;tt++);
- for(yy = 310;yy>0;yy--);
- {asm("NOP");}
- }
-
- void Delay_1s(uint n)
- { uint ulloop=1000;
- uint tt;
- for(tt =n ;tt>0;tt--);
- for( ulloop=1000;ulloop>0;ulloop--)
- {
- Delay_10us(100);
- }
-
- }
- void SysClkSet32M()
- {
- CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
- while(CLKCONSTA & 0x40); //等待晶振稳定
- CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
- //此时的CLKCONSTA为0x88。即普通时钟和定时器时钟都是32M。
- }
-
- void Init_UltrasoundRanging()
- {
- P1DIR = 0x08; //0为输入1为输出 00001000 设置TRIG P1_3为输出模式
- TRIG=0; //将TRIG 设置为低电平
-
- P0INP &= ~0x80; //有上拉、下拉 有初始化的左右
- P0IEN |= 0x80; //P0_7 中断使能
- PICTL |= 0x01; //设置P0_7引脚,下降沿触发中断
- IEN1 |= 0x20; // P0IE = 1;
- P0IFG = 0;
- }
-
- void UltrasoundRanging(uchar *ulLoadBufPtr)
- {
- SysClkSet32M();
- Init_UltrasoundRanging();
-
- // T1CC0H = 0x00;
- //T1CC0L = 0xfa;
- EA = 0;
- TRIG =1;
-
- Delay_1us(10); //需要延时10us以上的高电平
- TRIG =0;
-
- T1CNTL=0;
- T1CNTH=0;
- while(!ECHO);
- T1CTL = 0x09; //0000 1010
- //0000 1001
- //通道0,中断有效,32分频;自动重装模式(0x0000->0xffff);
- L1=T1CNTL;
- H1=T1CNTH;
- *ulLoadBufPtr++=T1CNTL;
- *ulLoadBufPtr++=T1CNTH;
- EA = 1;
- //Delay_10us(60000);
- //Delay_10us(60000);
-
- }
- /**********串口初始化函数****************************/
- void InitUart(void){
- PERCFG = 0x00;
- P0SEL = 0x0c;
- P2DIR &= ~0xc0;
- U0CSR |= 0x80;
- U0GCR |= 11;
- U0BAUD |= 216;
- UTX0IF = 0;
- }
- /**********串口发送函数****************************/
- void UartSendString(char *Data, int len){
- uint i;
- for(i=0;i<len;i++){
- U0DBUF = *Data++;
- while(UTX0IF == 0);
- UTX0IF = 0;
- }
- }
- #pragma vector = P0INT_VECTOR
- __interrupt void P0_ISR(void)
- {
- EA=0;
- T1CTL = 0x00;
- LoadRegBuf[2]=T1CNTL;
- LoadRegBuf[3]=T1CNTH;
- L2=T1CNTL;
- H2=T1CNTH;
-
- if(P0IFG&0x080) //外部ECHO反馈信号
- {
- P0IFG = 0;
- }
- T1CTL = 0x09;
- T1CNTL=0;
- T1CNTH=0;
- P0IF = 0; //清中断标志
- EA=1;
- }
- void InitLed(void){
- P1DIR |= 0x01;
- LED1 = 0;
- }
- void main(void)
- {
- InitUart();
- InitLed();
- char strTemp[6];
-
- while(1)
- {
- UltrasoundRanging(LoadRegBuf);
- Delay_1s(1);
- data=256*H2+L2-L1-256*H1;
- distance=(float)data*340/10000;
- memset(strTemp, 0, 6);
- sprintf(strTemp,"%.02f", distance);//将浮点数转成字符串
- UartSendString(strTemp, 5);
- UartSendString("CM",2);
- UartSendString("\r\n",2);
- if((int)distance<=20){
- LED1 = 1;
- }else{
- LED1 = 0;
- }
- distance=0;
- Delay_1s(2);
- };
- }
复制代码
所有资料51hei提供下载:
超声波+串口.zip
(48.48 KB, 下载次数: 14)
|