超声波的原理大家百度一下就应该知道了,因此基于MSP430的超声波主要是调整时钟就行了。
代码如下
#include "msp430f5529.h"
#include "usart.h"
#include "stdio.h"
double a = 1.111; //距离
long long next; //寄存器值
/*
* P3.6与TB0.6相连接,输出信号
*/
int tim = 0; //溢出次数
void SetVcoreUp (unsigned int level)
{
PMMCTL0_H = PMMPW_H;
SVSMHCTL = SVSHE + SVSHRVL0 * level + SVMHE + SVSMHRRL0 * level;
SVSMLCTL = SVSLE + SVMLE + SVSMLRRL0 * level;
while ((PMMIFG & SVSMLDLYIFG) == 0);
PMMIFG &= ~(SVMLVLRIFG + SVMLIFG);
PMMCTL0_L = PMMCOREV0 * level;
if ((PMMIFG & SVMLIFG))
while ((PMMIFG & SVMLVLRIFG) == 0);
SVSMLCTL = SVSLE + SVSLRVL0 * level + SVMLE + SVSMLRRL0 * level;
PMMCTL0_H = 0x00;
}
void set(void)
{
volatile unsigned int i;
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
P1DIR |= BIT1; // P1.1 output
P1DIR |= BIT0; // ACLK set out to pins
P1SEL |= BIT0;
P2DIR |= BIT2; // SMCLK set out to pins
P2SEL |= BIT2;
P7DIR |= BIT7; // MCLK set out to pins
P7SEL |= BIT7;
// Increase Vcore setting to level3 to support fsystem=25MHz
// NOTE: Change core voltage one level at a time..
SetVcoreUp (0x01);
SetVcoreUp (0x02);
SetVcoreUp (0x03);
UCSCTL3 = SELREF_2; // Set DCO FLL reference = REFO
UCSCTL4 |= SELA_2; // Set ACLK = REFO
__bis_SR_register(SCG0); // Disable the FLL control loop
UCSCTL0 = 0x0000; // Set lowest possible DCOx, MODx
UCSCTL1 = DCORSEL_7; // Select DCO range 50MHz operation
UCSCTL2 = FLLD_0 + 762; // Set DCO Multiplier for 25MHz
__bic_SR_register(SCG0); // Enable the FLL control loop
__delay_cycles(782000);
do
{
UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);
// Clear XT2,XT1,DCO fault flags
SFRIFG1 &= ~OFIFG; // Clear fault flags
}while (SFRIFG1&OFIFG); // Test oscillator fault flag
P6DIR |= BIT0;
}
void main()
{
set();
WDTCTL = WDTPW+WDTHOLD; // Stop WDT
UART_Init(); //串口初始化
P3DIR |= BIT6;
P3OUT &=~ BIT6;
P3DIR &=~ BIT5;
P3IN &=~ BIT5;
P2DIR |= 0X00;
P2IFG = 0X00;
P2IE = BIT0;
P2IES = BIT0;
P2IN = BIT0;
P2OUT = BIT0;
P2REN = BIT0;
__enable_interrupt();
P1SEL |= BIT4;
P1DIR |= BIT4;
P1DS |= BIT4;
P1OUT &=~ BIT4;
while(1)
{
P3OUT = BIT6;
__delay_cycles(4);
P3OUT &=~ BIT6;
P2OUT &=~ BIT0;
__delay_cycles(40000);
}
}
#pragma vector=PORT2_VECTOR
__interrupt void Port2_ISR(void)
{
unsigned int temp;
if((P2IN&0X01)!=0X01)
{
temp=P2IFG;
P2IFG=0x00;
if(temp==0x01)
{
P2OUT = BIT0;
//开始计时
TA0CCTL0 = CCIE;
TA0CCR0 = 20000;
TA0CTL = TASSEL_2 + MC_1 + TACLR;
__bis_SR_register(GIE);
while((P3IN&0X20) == 0X20);
//a + TA0R
TA0CTL = MC0;
//next = TA0R;
printf("next = %ldn",TA0R);
}
}
}
#pragma vector = TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
{
tim++;
if(a>10)
{
tim = 0;
P3OUT = BIT6;
__delay_cycles(4);
P3OUT &=~ BIT6;
}
}
上一篇:初学MSP430——按键中断控制小灯翻转
下一篇:【MSP430】捕获模式测量信号占空比
推荐阅读最新更新时间:2024-11-08 16:13
设计资源 培训 开发板 精华推荐
- AM2G-4815DZ ±15V 2 瓦 DC/DC 转换器的典型应用
- STC15F开发板
- 【征集令】抠搜点阵时钟
- 基于MCP41010的高精度移相放大模块
- MIKROE-3399,用于 AL1782 双通道 LED 驱动器的 Light Temp Click 板,设计用于可调谐智能连接照明 (SCL) 应用
- ADR420 超精密、低噪声、5.00 Vout XFET 电压基准的典型应用,具有开尔文连接的优势
- LTC1257 的典型应用 - SO-8 中的完整单电源 12 位电压输出 DAC
- LTC4055EUF 高级拓扑 USB 电池充电器优化了电源利用率以实现更快充电
- LTC1565 新型连续时间低通滤波器具有差分 I/O、线性相位和宽动态范围
- PN532读写卡器(支持NFC、RFID)