完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
MCU:STC12C5A60S2
晶振:11.0592Mhz 出现问题:用测出的距离发到串口上为0mm,或者就是计时器溢出中断时的1114mm 调试了一天,找不到问题出在哪里? #include #include "typedef.h" #include "ultrasonic.h" #include "uart.h" #include "intrins.h" ***it ultrasonic_trig = P1^3; ***it ultrasonic_echo = P3^2; bit flag_ultrasonic_gl = 0; bit time_flag = 0; ***it LED = P2^5; uint32 ultrasonic_time_gl = 0; uint32 ultrasonic_dist_gl = 0; void ultrasonic_process(void) //启动程序 { TR1 = 1; ultrasonic_trig = 0; while( TL1<20 ) { ultrasonic_trig = 1; } ultrasonic_trig = 0; } void count_dist(void) { uint32 dist_buff; if (flag_ultrasonic_gl ==1) { flag_ultrasonic_gl = 0; ultrasonic_dist_gl = ultrasonic_time_gl*17/1000; // ultrasonic_time_gl = 0; } if (time_flag == 1) { PrintCom("time_flag = 1"); PrintCom("n"); dist_buff = 65535*17/1000; ultrasonic_dist_gl = dist_buff; time_flag = 0; } else { /* do nothing */ } } void timer1_init(void) { AUXR &= 0xBF; //定时器时钟12T模式 TMOD &= 0x0F; //设置定时器模式 TF1 = 0; //清除TF1标志 ET1 = 1; TL1 = 0; TH1 = 0; } void timer1irq(void) interrupt 3 //T0中断用来计数器溢出,超过测距范围 { TL1 = 0; TH1 = 0; time_flag=1; //中断溢出标志 } void exint0_init(void) { EX0 = 1; IT0 = 1; /* 跳变沿触发 */ EA = 1; } void exint0irq(void) interrupt 0 { ultrasonic_time_gl = ( TH1<<8 )| TL1; flag_ultrasonic_gl = 1; TR1 =0; TL1 = 0; TH1 = 0; } void main() { exint0_init(); timer1_init(); UartInit(); ultrasonic_echo = 0; ultrasonic_trig = 0; while (1) { ultrasonic_process(); count_dist(); UART_Put_Num(ultrasonic_dist_gl); PrintCom("n"); } } |
|
相关推荐
1 个讨论
|
|
只有小组成员才能发言,加入小组>>
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 15:18 , Processed in 0.473651 second(s), Total 43, Slave 33 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号