晚上好伙计们!
有没有人知道如何用PIC18F45K22芯片将超声波传感器与pic单片机easyPic v7进行接口,从而实现人的计数。
我找到了一个有用的代码,我试着编辑它,但它仍然不工作……
下面是我的代码:
// Lcd module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
void main()
{
int a;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,5,"ITCE444");
Lcd_Out(2,3,"Term Project");
Delay_ms(3000);
Lcd_Cmd(_LCD_CLEAR);
TRISA.RA0 = 0; //RB0 as Input PIN (TRG)
TRISA.RA4 = 0; //RB4 as Input PIN (ECHO)
while(1)
{
if(PORTA.RA4==1 && PORTA.RA0==1)
{
a = a + 1;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Person in: ");
Lcd_Out(1,12,a);
Lcd_Out(1,15,"Person");
}
else
{
a = a - 1;
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1,"Person out: ");
Lcd_Out(1,13,a);
}
Delay_ms(400);
}
}
谢谢和问候..
发布于 2016-06-05 17:25:10
您在上面发布的代码是100%不正确的。Here is代码应该是什么样子的。
HC-SR04
是一种超声波距离传感器( distance )。它测量传感器和障碍物之间的距离。虽然你绝对可以使用距离信息来制作一个计数器,但这似乎有点超出了你的技能范围。
所以有三个选择:
发布于 2016-06-06 05:20:04
感谢您的回复先生Hassan Nadeem,我有一个更新的代码,它几乎是工作时,我实现它。
看一看,告诉我你的想法:
// LCD module connections
sbit LCD_RS at LATB4_bit;
sbit LCD_EN at LATB5_bit;
sbit LCD_D4 at LATB0_bit;
sbit LCD_D5 at LATB1_bit;
sbit LCD_D6 at LATB2_bit;
sbit LCD_D7 at LATB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// Ultrasonic module connection
sbit Ultrasonic at RD0_bit;
sbit Ultrasonic_Direction at TRISD0_bit;
// End of Ultrasonic module connections
#define Pole_Height 200
void main()
{
unsigned long Tm;
unsigned char Tl, Th;
unsigned int h, Person_Height,dist;
char Txt[7];
int cont =0 ;
ANSELB = 0;
ANSELD = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(2,3,"Term Project");
Lcd_Out(1,5,"ITCE444");
Delay_Ms(2000);T0CON = 0x00;
for(;;)
{
Ultrasonic_Direction = 0;
TMR0H = 0;
TMR0L = 0;
Ultrasonic = 0;
Delay_us(3);
Ultrasonic = 1;
Delay_us(10);
Ultrasonic = 0;
while(PORTD.RD5 == 0);
T0CON.TMR0ON = 1;
while(PORTD.RD5 == 1);
T0CON.TMR0ON = 0;
Tl = TMR0L;
Th = TMR0H;
Tm = Th*256 + Tl;
Tm = Tm / 2;
Tm = 34 * Tm;
Tm = Tm / 1000;
h = (unsigned int)Tm;
Person_Height= Pole_Height - h;
if (Person_Height > 168 && Person_Height < 196)
cont = cont + 1 ;
else if(Person_Height > 132 && Person_Height < 160 && cont>0)
cont = cont - 1 ;
IntToStr(cont, Txt);
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,1, "Person in");
Lcd_Out(2,1, Txt);
Delay_Ms(1000);
}
}
https://stackoverflow.com/questions/37621732
复制相似问题