Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC Programming for Ultrasonic Sensor Help.

Status
Not open for further replies.

brightjoey

New Member
I'm having trouble understanding this particular phrase in this Ultrasonic Sensor PIC Programming guide pdf file in page 8 of 12.


Note - assumes a 20MHz crystal, which is 5MHz timer clock
// A 1:4 prescaler is used to give a 1.25MHz timer count (0.8uS per tick)

I assume it means the oscillator that is inserted in the PIC. But I still don't get "crystal" and the whole phrase. ><
 

Attachments

  • 救星.pdf
    701.4 KB · Views: 333
I'm having trouble understanding this particular phrase in this Ultrasonic Sensor PIC Programming guide pdf file in page 8 of 12.
I assume it means the oscillator that is inserted in the PIC. But I still don't get "crystal" and the whole phrase. ><

Hi,
The PIC divides the crystal frequency of 20mHz by 4 to give a 5mHz internal clock for the PIC.

A prescaler which is internal to the PIC can be programmed to divide by certain ratio's.
A 1:4 prescale will reduce the internal clock for use with say an internal Timer by 4, the system clock will still be 5mHz, but the Timer clock will be 1.25mHz.

A crystal or xtal is an external device that generates in conjunction with the PIC a 20mHz frequency, in this case.
 
Last edited:
Hi,
The PIC divides the crystal frequency of 20mHz by 4 to give a 5mHz internal clock for the PIC.

A prescaler which is internal to the PIC can be programmed to divide by certain ratio's.
A 1:4 prescale will reduce the internal clock for use with say an internal Timer by 4, the system clock will still be 5mHz, but the Timer clock will be 1.25mHz.

A crystal or xtal is an external device that generates in conjunction with the PIC a 20mHz frequency, in this case.
Oh, so in normal English (a newb in electronics), crystal is something that controls the frequency, and the prescaler sets the timer clock(internal clock) lower than the main clock(system clock)?
 
1 More question

range = get_srf04(); // get range from srf04 (round trip flight
time in 0.8uS units)
cursor(24); // sets cursor to 2nd row of LCD03
sprintf(s,"Range = %dcm ", range/72); // convert to cm
print(s); // send it to the LCD03
cursor(44); // sets cursor to 3rd row of LCD03
sprintf(s,"Range = %dinch ", range/185); // convert to inches
print(s); // send it to the LCD03
TMR1H = 0; // 52mS delay - this is so that the
SRF04 ranging is not too rapid
TMR1L = 0; // and the previous pulse has faded
away before we start the next one
T1CON = 0x21; // 1:4 prescale and running
TMR1IF = 0;
while(!TMR1IF); // wait for delay times
TMR1ON = 0; // stop timer

In the same datasheet in page 9 of 12. In the bolded fonts What does it mean when the range is divided into 72 and 185? The notes say its to convert to cm and inches respectively but I still don't get it?
 
In the same datasheet in page 9 of 12. In the bolded fonts What does it mean when the range is divided into 72 and 185? The notes say its to convert to cm and inches respectively but I still don't get it?

hi,
I don't use that particular programming language.

"range is divided into 72 and 185" its whether you want the displayed range in inches or centimetres

I cannot open your pdf,,???
 
i don't understand why 72 and 185 was need to convert to cms or inches?
is this no. is specific?


thanz for your co-operation.
 
i don't understand why 72 and 185 was need to convert to cms or inches?
is this no. is specific?


thanz for your co-operation.


hi,
IF the basic counting period of the range finder is 1 microsec , the time taken for sound to travel 1 mtr and back is approx 58 microsec.

But his design is set for a time count period of 0.8 microsec so 58/0.8 = 72 , so if he divides by 72 he gets the distance value in cms and if he divides the distance count by 185 he gets the distance in inches...OK.

BTW: these are the designers calculations, not mine!

There 2.54cms in 1 inch so 185/72 is an approximation....2.569!
 
Last edited:
range = get_srf04(); // get range from srf04 (round trip flight
time in 0.8uS units)
cursor(24); // sets cursor to 2nd row of LCD03
sprintf(s,"Range = %dcm ", range/72); // convert to cm
print(s); // send it to the LCD03
cursor(44); // sets cursor to 3rd row of LCD03
sprintf(s,"Range = %dinch ", range/185); // convert to inches
print(s); // send it to the LCD03
TMR1H = 0; // 52mS delay - this is so that the
SRF04 ranging is not too rapid

TMR1L = 0; // and the previous pulse has faded
away before we start the next one
T1CON = 0x21; // 1:4 prescale and running
TMR1IF = 0;
while(!TMR1IF); // wait for delay times
TMR1ON = 0; // stop timer

In the bolded text, how did we get the 52ms delay?
 
In the bolded text, how did we get the 52ms delay?

He is using TIMER1 of the PIC to set the TMR1 interrupt flag every 52mSec, which is used as a delay between ranging samples.
 
Oo, is the 52ms set as default? Can we change the delay time?
Examples provided would be really helpful.

You could just count a number of TMR1 interrupts for a longer delay or pre load the TIMER1 counters with higher value for a shorter delay.
 
Does the crystal affect the value of the delay then?
And for example if I want it to have 100ms delay do I set "TMR1H = 100ms" instead of 0?
 
Does the crystal affect the value of the delay then?
And for example if I want it to have 100ms delay do I set "TMR1H = 100ms" instead of 0?

If you want ~100mSecdelay just count two of the TMR1 interrupts.
 
Oo..so the value of the range is keep until the next 52ms ?
During this delay time, can i able to show the range in LCD?
TQ again for your help :)
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top