Skip to content

Instantly share code, notes, and snippets.

@bangonkali
Last active December 12, 2015 00:08
Show Gist options
  • Save bangonkali/4681577 to your computer and use it in GitHub Desktop.
Save bangonkali/4681577 to your computer and use it in GitHub Desktop.
Multiple Ultrasonic Distance Sensors on a Single PIC 18F4550.
#include "PIC18F4550lib.h"
void ___init_core___() {
ADCON1 = 0x0F; // page 268, disable analaog
CMCON = 0x07; // disable comparator
INTCON2 = 0x80; // disable pull up in port b
LATA = 0x00; // Make all output ports 0
LATC = 0x00; // Make all output ports 0
LATD = 0x00; // Make all output ports 0
LATE = 0x00; // Make all output ports 0
TRISA = 0x03; // Make 00000011 directional ports.
TRISC = 0x00; // Make all ports output
TRISD = 0x00; // Make all ports output
TRISE = 0x00; // Make all ports output
PORTA = 0x00; // Make all ports 0
PORTC = 0x00; // Make all ports 0
PORTD = 0x00; // Make all ports 0
PORTE = 0x00; // Make all ports 0
LATA = 0x03;
}
void main_init() {
___init_core___();
}
#ifndef ___PIC18F4550lib_H__
#define ___PIC18F4550lib_H__
void ___init_core___();
void main_init();
#endif
#include "PIC18F4550lib.h"
unsigned long count_distance[3];
unsigned char finalcount[3];
double calculation[3];
char txt[15];
char msg[40];
void main() {
ADCON1 |= 0x0F;
CMCON |= 7;
main_init();
TRISD = 0x2A;
TRISB = 0x00;
LATB = 0x00;
LATD = 0;
UART1_Init(9600);
Delay_ms(100);
while(1){
count_distance[0] =0;
count_distance[2] =0;
count_distance[1] =0;
LATD = 0x01;
Delay_us(20);
LATD = 0x00;
while (PORTD.RD1 == 0)
count_distance[0] = 0;
while (PORTD.RD1 == 1)
{
count_distance[0]++;
}
if (count_distance[0] > 1000)
count_distance[0] = 1000;
calculation[0] = (255*count_distance[0]/1000);
finalcount[0] = calculation[0];
UART1_Write(finalcount[0]);
////////////////////////////////////////////////
LATD = 0x04;
Delay_us(20);
LATD = 0x00;
while (PORTD.RD3 == 0)
count_distance[1] = 0;
while (PORTD.RD3 == 1)
{
count_distance[1]++;
}
if (count_distance[1] > 1000)
count_distance[1] = 1000;
calculation[1] = (255*count_distance[1]/1000);
finalcount[1] = calculation[1];
UART1_Write(finalcount[1]);
////////////////////////////////////////////////
LATD = 0x10;
Delay_us(20);
//Delay_ms(1000);
LATD = 0x00;
while (PORTD.RD5 == 0)
count_distance[2] = 0;
while (PORTD.RD5 == 1)
{
count_distance[2]++;
}
if (count_distance[2] > 1000)
count_distance[2] = 1000;
calculation[2] = (255*count_distance[2]/1000);
finalcount[2] = calculation[2];
UART1_Write(finalcount[2]);
////////////////////////////////////////////////
UART1_Write(0);
Delay_ms(1000);
LATB = !PORTB;
}
}
rd0 output -> trigger
rd1 input -> echo
rd2 output -> led if (lessthan 3 inches)
trisd
0000 1010
7654 3210
0010 0000
initial output
0000 0001
7654 3210
0000 0001
0010 1010
0000 0100
0001 0000
trisD
7654 3210
0010 1010
latc
0001 0000
char inputString[200] = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
void setup() {
// initialize serial:
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
// print the string when a newline arrives:
while (Serial1.available() ) {
stringComplete=true;
Serial.println(Serial1.read(),DEC);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment