推荐星级:
  • 1
  • 2
  • 3
  • 4
  • 5

MSP430驱动超声波模块(HC-SR04)

更新时间:2021-07-24 11:33:09 大小:90K 上传用户:BaiJhon查看TA发布的资源 标签:msp430驱动超声波模块 下载积分:2分 评价赚积分 (如何评价?) 打赏 收藏 评论(1) 举报

资料介绍

代码:

//Echo------->14

//Trig------->12

#include<msp430f5529.h>

#include "Uart.h"

#include "stdio.h"

#include "Echo.h"

unsigned int distance; //距离变量

unsigned int k=0;

unsigned int LastCCR1;      //上一次高电平持续的时间,和距离正相关

unsigned char Uart_Buf[20];

void main()

{

    HC_SR04Init();          //初始化超声模块HC_SR04

    TimeGetInit();          //初始化计数捕获引脚,模式等

    Uart_Init();

    _EINT();

    while(1)

    {

        if(++k == 1000){

            HC_SR04Start();

            sprintf(Uart_Buf,"distance:%d\n",distance);

            send_buf(Uart_Buf);

            _delay_cycles(1000);

        }

    }

}

#pragma vector=TIMER0_A1_VECTOR

__interrupt void TIMER0_A1_ISR(void)

{

    _DINT();                        //关中断

    static unsigned char times=1;

    if(times==1)

    {

        LastCCR1=TA0CCR3;       //记录下上次CCR3的值

        TA0CCTL3&=~CM_1;        //清上升沿捕获

        TA0CCTL3|=CM_2;         //改为下降沿捕获

        times++;

    }

    if(times==0)

    {

        distance =TA0CCR3-LastCCR1;

        distance = distance*0.052;

        if(distance > 200) distance = 200;

        TA0CCTL3&=~CM_2;        //清除下降沿触发

        TA0CCTL3|=CM_1;         //变为上升沿触发

        times++;                //改变times的值

    }

    times&=0x01;                //times>1时清0

    LPM0_EXIT;                  //退出低功耗模式

    TA0CCTL3&=~CCIFG;           //清除中断响应标志

    _EINT();                    //开中断

}


image.png


微信图片_20210724112731.jpg

部分文件列表

文件名大小
Echo_exp1/
Echo_exp1/.ccsproject1KB
Echo_exp1/.cproject
Echo_exp1/.launches/
Echo_exp1/.launches/Echo_exp1.launch5KB
Echo_exp1/.project1KB
Echo_exp1/.settings/
Echo_exp1/.settings/org.eclipse.cdt.codan.core.prefs
Echo_exp1/.settings/org.eclipse.cdt.debug.core.prefs
Echo_exp1/.settings/org.eclipse.core.resources.prefs
Echo_exp1/Debug/
...

全部评论(1)

  • 2021-11-20 17:38:29fanye38934

    感谢分享,很有用的一手资料

上传资源 上传优质资源有赏金

  • 打赏
  • 30日榜单

推荐下载