完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include #include"OLED.h" #define uint unsigned int #define uchar unsigned char #define ulong unsigned long uchar flag_time = 0; ulong TIME0 = 0, TIME1 = 0; //存放时间的函数 ulong L_juli = 0; ulong l = 0; uint ge,shi,bai,qian; uchar str[10]={0}; void delay_us(uint n) { uint i; for(i=0;i<8;i++) n--; } //时钟初始化 void CLK_Init(void) { if (CALBC1_8MHZ ==0xFF || CALDCO_8MHZ == 0xFF) { while(1); // If calibration constants erased // do not load, trap CPU!! } BCSCTL1 = CALBC1_8MHZ; // Set range DCOCTL = CALDCO_8MHZ; // Set DCO step + modulation */ BCSCTL2 |= 0X04; //SMCLK为DCO的2分频,4M } //端口初始化 void GPIO_Init(void) { P1DIR = 0xf7; P1OUT |=BIT3; P1REN = 0X08; P2DIR |= 0x07; P2SEL |= 0x08; //P2.3作为捕获输入 P2OUT &= 0xfb; //P2.2输出低电平 // P1IES = 0x00; //P1.3为上升沿触发中断 // P1IE = 0x08; //P1.3中断允许 // P1IFG = 0x00; //清除中断标志 } //定时器TIME1_A3初始化函数 void TIME1_A0_Init(void) { TA1CTL |= TASSEL1 + MC_2; //SMCLK作为时钟源,连续计数模式 TA1CCTL0 |= CM_1 + CCIS_1 + SCS + CAP + CCIE; //上升沿捕获, //输入源为CCI0B,同步捕获模式,使能中断 } void Start_Measure() //由P2.0向Trig输出10us以上的高电平,触发测距 { // _DINT(); P2OUT|=BIT2; delay_us(20); P2OUT&=~BIT2; //__enable_interrupt(); } //主函数 void main(void) { WDTCTL = WDTPW + WDTHOLD; //关闭看门狗定时器 CLK_Init(); GPIO_Init(); TIME1_A0_Init(); initial_lcd(); ///LCD模块初始化 clear_screen(); //清屏 _EINT(); //中断使能 dis_GB_str(4,1,"Hello!"); while(1) { //LPM0; //Start_Measure(); _NOP(); // delay_us(1000); if(!(P1IN&0X08)) { delay_us(2000); if(!(P1IN&0X08)) { delay_us(2000); if(!(P1IN&0X08)) { while(!(P1IN&0X08)); P2OUT &=~BIT2; P2OUT |= 0x04; //P2.2输出高电平 delay_us(20); P2OUT&=~BIT2; // dis_GB_str(2,1,"abCD!"); } } } } } //定时器Timer1_A CCR0中断服务函数 #pragma vector = TIMER1_A0_VECTOR __interrupt void TIMER1_A0__ISR(void) { if (flag_time == 0) { TA1CCTL0=(TA1CCTL0&(~CM0))|CM1; //改为下降沿捕获:CM0置零,CM1置一 P2OUT &= ~0x04; //P2.2输出低电平 TIME0 = TA1CCR0; flag_time ++; } else { TIME1 = TA1CCR0; TA1CCTL0=(TA1CCTL0&(~CM1))|CM0; //改为上升沿捕获:CM1置零, flag_time = 0; if (TIME1 > TIME0) { TIME1 = TIME1 - TIME0; } else { TIME1 = TIME1 + (65536 - TIME0); } l = TIME1*170; L_juli = l/2000+14; ge = L_juli % 10 + 48; shi = (L_juli/10) % 10 + 48; bai = (L_juli/100) % 10 + 48; qian = (L_juli/1000) % 10 + 48; str[0]='D'; str[1]=':'; str[2]=qian; str[3]=bai; str[4]=shi; str[5]='.'; str[6]=ge; str[7]='c'; str[8]='m'; str[9]=' |