智能车制作
标题:
大神帮忙看看这个HC-SR04测距的问题,在线等!!!
[打印本页]
作者:
孤旅人
时间:
2015-3-20 17:50
标题:
大神帮忙看看这个HC-SR04测距的问题,在线等!!!
#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */
//trig->A0
//echo->A1
unsigned int a,s;
void setbus(void) //设置总线频率为40MHz
{
CLKSEL_PLLSEL = 0;
PLLCTL_PLLON = 1;
SYNR = 0xc0 | 0x04;
REFDV = 0x80 | 0x01;
POSTDIV = 0x00;
_asm(nop);
_asm(nop);
while(!(CRGFLG_LOCK==1))
{
;
}
CLKSEL_PLLSEL = 1;
}
void PIT(void) //PIT初始化子程序
{
PITCFLMT = 0x00; //禁止PIT模块
PITCE_PCE0 = 1; //使能定时器通道0
PITMUX = 0x00; //定时器通道使用Base Timer0(微定时基准0)
PITMTLD0 = 0x04; //设置8位微定时装载寄存器0初值(8位计数器初值)
PITLD0 = 0x000a; //设置16位微定时装载寄存器0初值(16位计数器初值)
PITCFLMT =0x80; //使能PIT模块
}
#pragma CODE_SEG __NEAR_SEG NON_BANKED //中断服务子程序
interrupt VectorNumber_Vpit0 void PIT_ISR(void)
{
PITTF_PTF0 = 1;
a++;
}
#pragma CODE_SEG DEFAULT
void signal() //触发信号
{
DDRA = 0x01;
PORTA = 0x01;
PITINTE = 0x01;
while(a<=20)
{
; //等待20us
}
PITINTE = 0x00;
a = 0;
PORTA = 0x00;
}
void distance() //测试距离
{
while(!(PORTA == 0x02));
PITINTE = 0x01;
while(!(PORTA == 0x00));
PITINTE = 0x00;
s =(char) (a * 17/100);
}
void SCI() //SCI0初始化程序
{
SCI0BD = 206;
SCI0CR1 = 0x00;
SCI0CR2 = 0x2;
}
void SCI_sendbyte(unsigned int data)
{
while(!(SCI0SR1&0x80));
SCI0DRH = 0;
SCI0DRL = data;
}
void main(void) {
EnableInterrupts;
setbus();
PIT();
SCI();
for(;;) {
_FEED_COP();
signal();
distance();
SCI_sendbyte(s);
}
}
这个程序是测距的,但是编译器编译时在
void SCI_sendbyte(unsigned int data)
{
while(!(SCI0SR1&0x80));
SCI0DRH = 0;
SCI0DRL = data;
}
老是说SCI0DRL = data可能会丢失数据,是什么情况啊?
作者:
孤旅人
时间:
2015-3-20 20:22
有没有大神啊 帮帮忙 我看了一天了 都没看出是哪出错了
作者:
孤旅人
时间:
2015-3-21 19:31
就没有一个人能帮下忙吗
欢迎光临 智能车制作 (http://dns.znczz.com/)
Powered by Discuz! X3.2