void Timer_init();
unsigned int overflow = 0;
unsigned int pwm_start = 0;
unsigned int pwm_end =0;
unsigned long interval =0;
int distance=0;
char buff[25];
int i=0,n=0;
volatile unsigned int a;
volatile unsigned int b;
volatile unsigned int c;
volatile unsigned int d;
volatile unsigned int *f;
void main()
{ WDTCTL=WDTPW+WDTHOLD;
#include "HAL_Dogs102x6.h"
#include "stdio.h"
#include "stdlib.h"
void Timer_init();
unsigned int overflow = 0;
unsigned int pwm_start = 0;
unsigned int pwm_end =0;
unsigned long interval =0;
int distance=0;
char buff[25];
int i=0,n=0;
volatile unsigned int a;
volatile unsigned int b;
volatile unsigned int c;
volatile unsigned int d;
volatile unsigned int *f;
void main()
{ WDTCTL=WDTPW+WDTHOLD;
/*****************Set up LCD**********************************/
Dogs102x6_init(); //初始化LCD
Dogs102x6_backlightInit(); //背光初始化
Dogs102x6_setBacklight(8); //设置初始背光值
Dogs102x6_setContrast(5); //设置初始对比度值
Dogs102x6_clearScreen(); //清屏
Timer_init();
__enable_interrupt();
while(1){
a=distance/1000+48;//千
f=&a;
Dogs102x6_charDraw(1,54,*f,0);
b=distance%1000/100+48;//百
f=&b;
Dogs102x6_charDraw(1,60,*f,0);
c=distance%1000%100/10+48;
f=&c;
Dogs102x6_charDraw(1,66,*f,0);
d=distance%1000%100%10+48;
f=&d;
Dogs102x6_charDraw(1,72,*f,0);
}
}
#pragma vector=TIMER2_A0_VECTOR
__interrupt void TAIV_ISR(void)
{
if(TA2CTL &TAIFG)
{ overflow=0;
TA2CTL &= ~TAIFG;
overflow++;
P1OUT &=~ BIT2;
}
if(TA2CCTL0 & COV)
{
TA2CCTL0 &= ~COV;
// P1OUT |= BIT3;
}
if((TA2CCTL0 & CCI)==0) //按键松开,上升沿触发
{
P1OUT ^= BIT3;
TA1CCTL0 &= ~CCIE;
pwm_end = TA2CCR0;
interval = (pwm_end-pwm_start);//+ overflow*65535
distance=interval*0.02125;
TA2CCR0=0;
overflow=0;
}
}
void Timer_init()
{
TA0CTL = TASSEL_1 + ID_1 + MC_1 + TACLR + TAIE; // ACLK, contmode, clear TAR
TA0CCR0 = 32767;
P2DIR |= BIT0;
P2OUT &= ~BIT0;
P2REN |= BIT0;
P2OUT |= BIT0;
// P2SEL |= BIT3 ; //p2.3作为捕获CCIxA的输入
P2IES &= ~BIT0;
P2IFG &= ~BIT0;
P2IE |= BIT0;
P2DIR |= BIT4 ;
P1DIR |= BIT0+BIT2+BIT3;
P1OUT &= ~BIT0;
}
// Timer0_A5 Interrupt Vector (TAIV) handler
#pragma vector=TIMER0_A1_VECTOR
__interrupt void TIMER0_A1_ISR(void)
{
switch(__even_in_range(TA0IV,14))
{
case 0: break; // No interrupt
case 2: break; // CCR1 not used
case 4: break; // CCR2 not used
case 6: break; // reserved
case 8: break; // reserved
case 10: break; // reserved
case 12: break; // reserved
case 14:
// n++;
// if(n==2){
P2OUT &=~BIT4;
for(i=10;i>0;i--);
P2OUT |=BIT4;
for(i=20;i>0;i--);
P2OUT &=~BIT4;
// }
break;
default: break;
}
}
// Port 1 interrupt service routine
#pragma vector=PORT2_VECTOR
__interrupt void Port_1(void)
{if( P2IFG&BIT0==1 )
{ P1OUT^=BIT0;
TA2CTL = TASSEL_2 + ID_0 + MC_2+TACLR; //定时器取ACLK,无分频,连续计数
TA2CCTL0 = CM_3 + CCIS_0 + SCS + CAP+CCIE; //上下沿都触发捕捉,用于测脉宽,同步模式,使能中断
P2DIR &= ~BIT3 ;
P2SEL |= BIT3; //p1.7作为捕获CCIxA的输入
P2REN |= BIT3;
P2OUT |= BIT3;
P2IFG &= ~BIT0;
}
}
一周热门 更多>