Razbot
Published

Ultrasonic Sensor with PIC18F45K22 Microcontroller

Using an HC-SR04 ultrasonic sensor with PIC18F45K22 MCU - 32MHz oscillator to measure CM and display binary.

AdvancedProtip2,434
Ultrasonic Sensor with PIC18F45K22 Microcontroller

Things used in this project

Hardware components

PIC18F
Microchip PIC18F
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
LED (generic)
LED (generic)
×1
Solderless Breadboard Half Size
Solderless Breadboard Half Size
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 330 ohm
Resistor 330 ohm
×1

Software apps and online services

mikroC PRO for PIC
MIKROE mikroC PRO for PIC

Story

Read more

Schematics

boardd_5PbUd9n7AX.PNG

just the leds are missing

Code

ultrasonic PIC18F45K22

C/C++
 /*
   Ultrasonic in the backGround!
   By Raz Cohen
   5/3/2019
   What:
   In this code using PIC18F45K22 with 32 MHz Oscillator,
   The microcontroller checks the distance with ultrasonic sensor (HC-SR04)
   every 65.535ms and send the centimeter result to PORT(LAT)D .
   this set of actions requrie just first settings and then runs in the backgroud
   
   
   HOW:
   by using CCP modules:
   CCP1 - compare:
    1. sending 10us Trigger signal
    2. repeat the actions every 65.535ms
   CCP2 - capture:
   Measuring the time it took the ECHO signal to end.
   The Time(in uS) is then divided by 58 (From datasheet)
   and sent to PORTD

   
   More to it:
   first is the setting and then The CCP1 compare is set to 10us, and Trigger(of ultrasonic) is set.
   evey CCP uses interrupts, chronolgy order:
   the first interrupt is triggered from compare- 10us:
   clear TRIG, compare mode transfer to 65.535ms, CCP2 capture mode is set to ecery rise
   the second interrupt is triggered from capture -rise:
   reset the Timer2 register and change the CCP2 to capture mode on fall
   third is triggered from capture - fall:
   Dist = CCPR2H -> Dist = (Dist<<8)|CCPR2L -> Dist = Dist/58 -> LATD=Dist
   fourth (and final) triggered from compare - 65.535ms:
   set TRIG , set CCP1 compare to 10us.
   repeat
    */
 
 
 sbit TRIG at LATC0_bit;
 sbit ECHO at RC1_bit;
 short i=0;
 int Dist=0;
void interrupt()
{
 if(CCP1IF_bit)
 {
  CCP1IF_bit=0;
   if(TRIG)
   {
    TRIG=0;
    CCPR1H=CCPR1L=0xFF;
    CCP2CON=0b0101;
   }
   else
   {
    CCPR1H=0;
    CCPR1L=10;
    TRIG=1;
   }
 }
 if(CCP2IF_bit)
 {
  CCP2IF_bit=0;
 if(CCP2CON==0b0101)
 {
  CCP2CON=0b0100;
  TMR3H=TMR3L=0;
  Dist=0;
 }
 else
 {
 Dist = CCPR2H;
 Dist = (Dist<<8)| CCPR2L;
 Dist = Dist/58;
 LATD=Dist;
 }
 }
}

void S_Distance()
{
  CCPR1H=0;
  CCPR1L=10;
  TRIG=1;
  CCP1IE_bit=1;
}

void main() {
TRISC=0b11111010;
TRISD=0;
LATC=0;
ANSELA=ANSELB=ANSELC=ANSELD=ANSELE=0;
CCP1CON=0b1010;
CCP2CON=0b0101;
T3CON=0b00110001;
T1CON=0b00110001;
CCPTMRS0=0b00001000;
INTCON=0b11000000;
CCP1IE_bit=CCP2IE_bit=1;
CCP1IF_bit=CCP2IF_bit=0;
CCPR1H=CCPR1L=0xFF;
TMR1L=TMR1H=TMR3H=TMR3L=0;
TRIG=1;
CCPR1H=0;
CCPR1L=10;
while(1)
{
}

}

Credits

Razbot
4 projects • 0 followers

Comments