//基于MSP430G2553单片机和HC-SR超声传感器测距程序
// 连接图:
// ACLK = n/a, MCLK = SMCLK = 1MHZ
//
// MSP430G2553
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// |P1.4 P2.5|-->LCD_RST
// | P2.4|-->LCD_CE
// | P2.3|-->LCD_DC
// | P2.2|-->LCD_DIN
// | P2.1|-->LCD_CLK
// | |
// | P1.4|-->Trig
// | P1.2|-->Echo
// | |
// | |
#include "msp430g2553.h"
#include "5110/nokia_5110.h"
#include "stdio.h"
#define TRIG BIT4
#define ECHO BIT2 //P1.1 DIR.0=0 + SEL.1=1 + SEL2.1=0 --> TA0.CCI0A
#define USOUND_DIR P1DIR
#define USONUD_OUT P1OUT
#define USOUND_IE P1IE
#define USOUND_IES P1IES
#define USOUND_SEL P1SEL
unsigned int capV = 0;
float distance;
void BCSplus_init(void);
char str[100];
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
BCSplus_init();
LCD_init();
LCD_clear();
LCD_write_english_string(0,0,"USONIC DIS MEASURE");
//sprintf(str, "%x", 255);
LCD_write_english_string(0,1,"EliteZhe");
LCD_write_english_string(0,2,"-----------------");
LCD_write_english_string(0,3,"TIME:");
LCD_write_english_string(0,4,"DIST:");
USONUD_OUT |= TRIG;
USOUND_DIR |= TRIG;
//USOUND_IES |= ECHO;
USOUND_SEL |= ECHO ; //CCI0A
while(1)
{
TA0CTL |= MC_2 + TASSEL_2 + TACLR; //计数 SMCLK 清计数
TA0CCTL1 |= CM_1 + CAP + SCS + CCIE + CCIS_0;//上升沿捕获 捕获模式 同步模式 使能中断 CCI0A
USONUD_OUT |= TRIG;
// _NOP();_NOP();_NOP();_NOP();_NOP();
// _NOP();_NOP();_NOP();_NOP();_NOP();
// _NOP();_NOP();_NOP();_NOP();_NOP();
__delay_cycles(200);
USONUD_OUT &= ~TRIG;
//_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/interrupt
while((TA0CCTL1 & CCIFG) ==0); //等待上升沿
TA0CTL &= ~MC_3; //清除MC_的两位,否则 CM_1 | CM_2 = CM_3
TA0CTL |= MC_2 + TACLR; //连续计数 清除TA的计数值
TA0CCTL1 &= ~CCIFG; //清中断标志
TA0CCTL1 |= CM_2; //下降沿捕捉
while((TA0CCTL1 & CCIFG) ==0); //等待下降沿
capV = TA0CCR1; //得到计数器的值
distance = 1.12826e-3 * capV ;//计算距离
sprintf(str,"%4x",capV);
LCD_write_english_string(30,3,str);
sprintf(str,"%.3f",distance);
LCD_write_english_string(30,4,str);
TA0CCTL1 &= ~CCIFG; //清中断标志
unsigned int x,y;
for(x=5000;x>0;x--)
for(y=500;y>0;y--);
}
return 0;
}
/****************************************
* ======== BCSplus_init ========
* Initialize MSP430 Basic Clock System
*/
//MCLK = SMCLK = 1MHZ
void BCSplus_init(void)
{
/*
* Basic Clock System Control 2
*
* SELM_0 -- DCOCLK
* DIVM_0 -- Divide by 1
* ~SELS -- DCOCLK
* DIVS_0 -- Divide by 1
* ~DCOR -- DCO uses internal resistor
*
* Note: ~ indicates that has value zero
*/
BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0;
if (CALBC1_16MHZ != 0xFF) {
/* Adjust this accordingly to your VCC rise time */
__delay_cycles(100000);
/* Follow recommended flow. First, clear all DCOx and MODx bits. Then
* apply new RSELx values. Finally, apply new DCOx and MODx bit values.
*/
DCOCTL = 0x00;
BCSCTL1 = CALBC1_16MHZ; /* Set DCO to 16MHz */
DCOCTL = CALDCO_16MHZ;
}
/*
* Basic Clock System Control 1
*
* XT2OFF -- Disable XT2CLK
* ~XTS -- Low Frequency
* DIVA_0 -- Divide by 1
*
* Note: ~XTS indicates that XTS has value zero
*/
BCSCTL1 |= XT2OFF + DIVA_0;
/*
* Basic Clock System Control 3
*
* XT2S_0 -- 0.4 - 1 MHz
* LFXT1S_0 -- If XTS = 0, XT1 = 32768kHz Crystal ; If XTS = 1, XT1 = 0.4 - 1-MHz crystal or resonator
* XCAP_1 -- ~6 pF
*/
BCSCTL3 = XT2S_0 + LFXT1S_0 + XCAP_1;
}
文章评论(0条评论)
登录后参与讨论