How does this library work?

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
Ahmed_Shahid_Jawad
Posts: 3
Joined: Sun Nov 11, 2018 11:11 am

How does this library work?

Post by Ahmed_Shahid_Jawad » Sun Nov 11, 2018 11:18 am

hello my friend

I want to measure distance by sensor jsn-sr04t

During the search I found this library

Code: Select all


/*
                        HC-SR04 Library
*/

extern sfr sbit HCSR04_Trigger;
extern sfr sbit HCSR04_Echo;
extern sfr sbit HCSR04_Trigger_Direction;
extern sfr sbit HCSR04_Echo_Direction;

//factor = 1 / ( ( 4 * Prescaler ) / Clock )

extern const float factor;

void HCSR04_Init( char mode )
{
 HCSR04_Trigger_Direction = 0;
 HCSR04_Echo_Direction = 1;
 TMR1L = 0;
 TMR1H = 0;
// T1CON = 0b00000000; //Clock 4Mhz
   T1CON = 0b00010000; //Clock 8 Mhz
 //T1CON = 0b00100000; //Clock 16 Mhz
 //T1CON = 0b00110000; //Clock 32Mhz
 T1CON.B4 = mode.B0;
 T1CON.B5 = mode.B1;
}

unsigned HCSR04_Read()
{
float distancia;
unsigned tempo;
unsigned i;
   
   HCSR04_Trigger = 0;
   Delay_us( 2 );
   HCSR04_Trigger = 1;
   Delay_us( 10 );
   HCSR04_Trigger = 0;
   
   i = 0xFFFF;
   while( !HCSR04_Echo && i-- );
   
   TMR1H = 0;
   TMR1L = 0;
   TMR1ON_Bit = 1;
   
   i = 0xFFFF;
   while( HCSR04_Echo && i-- );
   
   TMR1ON_Bit = 0;
   
   tempo = *(unsigned*)&TMR1L;
   distancia = (0.175 * tempo / factor); //distancia em mm
   
   return (unsigned)distancia;
}
I have a bunch of questions about it

1- What does this mean?

Code: Select all

extern sfr
Why write in the library only ??

2- How can I Configure timer 1 to calculate time ??

3- What does this parameter do

Code: Select all

void HCSR04_Init( char mode )
4- When we write

Code: Select all

unsigned Distancia; 
What is this variable? Is it float or int or long ?? And how do I know that ??

5- How this works

Code: Select all

  i = 0xFFFF;
   while( !HCSR04_Echo && i-- );
   
   TMR1H = 0;
   TMR1L = 0;
   TMR1ON_Bit = 1;
   
   i = 0xFFFF;
   while( HCSR04_Echo && i-- );
   
   TMR1ON_Bit = 0;
   
   tempo = *(unsigned*)&TMR1L;
   distancia = (0.175 * tempo / factor); //distancia em mm
   
   return (unsigned)distancia;
Sorry for the many questions

jremington
Support Volunteer
Posts: 2303
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How does this library work?

Post by jremington » Sun Nov 11, 2018 5:31 pm

There are other libraries for that sensor, but it is so simple that you don't need one.

I suspect "extern sfr" denotes a symbol for a special function register defined elsewhere.

Questions like the following depend on what controller and compiler are you using, which you forgot to post. Look up the specifics for your compiler. That code is for some PIC processor.
What is this variable? Is it float or int or long ?? And how do I know that ??

Ahmed_Shahid_Jawad
Posts: 3
Joined: Sun Nov 11, 2018 11:11 am

Re: How does this library work?

Post by Ahmed_Shahid_Jawad » Mon Nov 12, 2018 12:26 am

Hi teacher jremington

I'm a newbie in the microcontrollers and I'm an amateur and not a specialist so I have some problems
There are other libraries for that sensor, but it is so simple that you don't need one.
i used this code but read unstable

Code: Select all

  TMR1H = 0;                  //Sets the Initial Value of Timer
    TMR1L = 0;                  //Sets the Initial Value of Timer

    PORTB.F0 = 1;               //TRIGGER HIGH
    Delay_us(10);               //10uS Delay
    PORTB.F0 = 0;               //TRIGGER LOW

    while(!PORTB.F4);           //Waiting for Echo
    T1CON.F0 = 1;               //Timer Starts
    while(PORTB.F4);            //Waiting for Echo goes LOW
    T1CON.F0 = 0;               //Timer Stops

    a = (TMR1L | (TMR1H<<8));   //Reads Timer Value
    a = a/58.82;                //Converts Time to Distance
    a = a + 1;   
So I'm looking for a more powerful library in noise reduction

Questions like the following depend on what controller and compiler are you using, which you forgot to post. Look up the specifics for your compiler. That code is for some PIC processor.
i used compiler MikroC and mcu is pic16f877a

jremington
Support Volunteer
Posts: 2303
Joined: Fri Jun 15, 2007 9:41 pm
Location: Eugene, Or

Re: How does this library work?

Post by jremington » Mon Nov 12, 2018 9:08 am

That code should be fine. Post some examples of the output to show what you mean by "unstable" (you will not get the exact same result every time).

For noise reduction, just average a few readings (try 10). Again, you don't need a library to calculate an average.

Post Reply