how to interface ultrasonic sensor hc-sr04 with easyPic v7 microcontroller - microcontroller

Good evening guys!
Is there anyone who have an idea about how to interface ultrasonic sensor with pic microcontroller easyPic v7 with PIC18F45K22 chip, in order to make person counter.
I found a useful code and I tried to edit it but still it's not working...
Here is my code:
// 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);
}
}
Thanks and regards..

The code that you have posted above is 100% incorrect. Here is how the code is supposed to look like.
HC-SR04 is an ultrasonic distance sensor. It measures the distance between the sensor and an obstacle. While you can definitely use the distance information to make a counter, it seems like that is a little above your skillset.
So have three options:
Learn to use pic micro-controllers and write a lot of timer code
to get the distance reading from the sensor.
Switch to Arduino and use it's builtin libraries to get the
distance.
Change your hardware. You do not need an ultrasonic sensor (or a
microcontroller for that matter) to make a person counter. I suggest
you simply use a pair of infrared leds to make yourself a simple
circuit that gives a pulse when a person walks though them.

Thanks for you reply Mr Hassan Nadeem, I have an update for the code and it's almost work when I implemented it.
Have a look and tell me what you think:
// 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);
}
}

Related

does PIC 16f877a external interrupt can affect all port?

the circuitI was working with PIC 16f877a and LCD, I want to powering a motor with transistor that used for relay with 9v battery and my LCD become error, does this because external interrupt?, because i used the external interrupt for a button on other port
I already change the program but its doesn't working and my LCD always display random character when the transistor is switching with 9v battery
int SetMoist=85;
int SetMoistR=0;
int SetMoistL=0;
int cset=0;
int sensingCounter=0;
int screenCounter=0;
int getMoist=0;
char *buffer = "00";
int turnON=1;
#define LCD_ON lcd_cmd(_LCD_TURN_ON);
#define LCD_OFF lcd_cmd(_LCD_TURN_OFF);
//LCD
sbit LCD_RS at RC1_bit;
sbit LCD_EN at RC2_bit;
sbit LCD_D4 at RC3_bit;
sbit LCD_D5 at RC4_bit;
sbit LCD_D6 at RC5_bit;
sbit LCD_D7 at RC6_bit;
sbit LCD_RS_Direction at TRISC1_bit;
sbit LCD_EN_Direction at TRISC2_bit;
sbit LCD_D4_Direction at TRISC3_bit;
sbit LCD_D5_Direction at TRISC4_bit;
sbit LCD_D6_Direction at TRISC5_bit;
sbit LCD_D7_Direction at TRISC6_bit;
//End of LCD
void interrupt() {
//Hardware interrupt flag
if(INTCON.b0 ==1){
turnON=1;
if(portb.b7==1){
if(SetMoist==99)
{}
else{
SetMoist++;
cset=0;
}
}
if(portb.b6==1){
turnON=1;
if(SetMoist==5)
{}
else{
SetMoist--;
cset=0;
}
}
if(portb.b5==1){
turnON=1;
portc.b0 =!portc.b0;
}
intcon.b0=0;
}
if(portb.b4==1){
turnON=1;
}
//End of Hardware interrupt flag
// Timer0 flag
if(INTCON.b2 ==1){
sensingCounter++;
screenCounter++;
//2 minutes timer for moisture
if(sensingCounter==50){
sensingCounter=0;
getMoist=ADC_Read(0);
getMoist=(getMoist/1024.0)*100.0;
if(getMoist>setMoist){
portc.b0=0b1;
}
else if(getMoist<setMoist){
portc.b0=0b0;
}
} //end moisture
if(screenCounter==1000){
screenCounter=0;
turnON=0;
}
intcon.b2=0;
}
//End of timer0 flag
//interrupt
}
void main() {
INTCON = 0b10101000;
//Timer0
option_reg= 0b10000111;
//TMR0=0;
//End of timer0
trisb= 0b11111111;
trisd = 0b00000000;
trisc.b7 =0;
trisc.b0=0;
portc.b0=0;
portd.b7=1;
portc.b7= 1;
trisa=0xff;
Lcd_Init();
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,1,"PENYIRAM TANAMAN");
lcd_out(2,5,"OTOMATIS");
delay_ms(1500);
Lcd_cmd(_LCD_CLEAR);
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,3,"DENGAN CINTA");
lcd_out(2,4,"KELOMPOK 2");
delay_ms(1500);
Lcd_cmd(_LCD_CLEAR);
lcd_cmd(_LCD_CURSOR_OFF);
lcd_out(1,3,"MENGHITUNG");
lcd_out(2,2,"KEKERINGAN...");
delay_ms(1500);
lcd_cmd(_LCD_CLEAR);
lcd_out(1,4,"KEKERINGAN:");
while(1){
if(cset==0){
cset=1;
//calculate R
SetMoistR= SetMoist % 10;
switch (SetMoistR) {
case 0: SetMoistR=0x3F; break;
case 1: SetMoistR=0x06; break;
case 2: SetMoistR=0x5B; break;
case 3: SetmoistR=0x4F; break;
case 4: SetmoistR=0x66; break;
case 5: SetmoistR=0x6D; break;
case 6: SetmoistR=0x7D; break;
case 7: SetmoistR=0x07; break;
case 8: SetmoistR=0x7F; break;
case 9: SetmoistR=0x6F; break;
}
// end of R calculation
//calculate L
SetMoistL= (SetMoist/10) % 10;
switch (SetMoistL) {
case 0: SetMoistL=0x3F; break;
case 1: SetMoistL=0x06; break;
case 2: SetMoistL=0x5B; break;
case 3: SetmoistL=0x4F; break;
case 4: SetmoistL=0x66; break;
case 5: SetmoistL=0x6D; break;
case 6: SetmoistL=0x7D; break;
case 7: SetmoistL=0x07; break;
case 8: SetmoistL=0x7F; break;
case 9: SetmoistL=0x6F; break;
}
//end of L calculation
}
//7Segment
portd=SetmoistR;
portc.b7=0;
portd.b7=1;
delay_ms(100);
portd=SetmoistL;
portd.b7=0;
portc.b7=1;
delay_ms(50);
//End_7Segment
//IntToStr(getMoist,buffer);
buffer[0] = (getMoist/10)%10 +48;
buffer[1] = (getMoist)%10+48;
lcd_chr(2,8,buffer[0]);
lcd_chr(2,9,buffer[1]);
lcd_chr(2,11,'%');
if(turnON==1){
LCD_ON;
}
else{
LCD_OFF;
}
} //while
} //main

Stroboscope with Arduino

first sorry for my bad english
I'm a student and I want to make a Stroboscope with Arduino for my school project
The frequency is variable between 10hz to 3000 hz and it changes using a rotary encoder
that when normally rotate the encoder 1 step frequency \pm 1hz and when rotate encoder when it pushed down frequency \pm 100hz
and Arduino make a PWM signal on pin 13 and it connect to a high power npn transistor and it turn on and off a 10 watt led
I code it using Encoder.h library by Paul Stoffregen and tone() function
but I have a PROBLEM
I code this program and upload it to Arduino Uno but it doesn't work IDK where is the problem
#include <Encoder.h>
#define ENCODER_PULSES_PER_STEP 1
int f = 10;
int direction;
Encoder myEnc(2, 3);
int t = 0;
void setup() {
pinMode(13, OUTPUT);
pinMode(4, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
direction = myEnc.read();
}
void loop() {
if (abs(direction) >= ENCODER_PULSES_PER_STEP) {
if (direction > 0) {
if (digitalRead(4) == 1) {
f++;
if (f >> 2500)f = 2500;
}
else {
f = f + 100;
if (f >> 2500)f = 2500;
}
} else {
if (digitalRead(4) == 1) {
f--;
if (f << 10)f = 10;
}
else {
f = f - 100;
}
}
myEnc.write(0);
}
tone(13, f);
}
When your program starts the function setup is executed once.
Then in an infinite loop the function loop is executed.
As you have direction = myEnc.read(); only in setup you'll only read the encoder once.
From the Encoder library's documentation:
/* Encoder Library - TwoKnobs Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these pin numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder knobLeft(5, 6);
Encoder knobRight(7, 8);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("TwoKnobs Encoder Test:");
}
long positionLeft = -999;
long positionRight = -999;
void loop() {
long newLeft, newRight;
newLeft = knobLeft.read();
newRight = knobRight.read();
if (newLeft != positionLeft || newRight != positionRight) {
Serial.print("Left = ");
Serial.print(newLeft);
Serial.print(", Right = ");
Serial.print(newRight);
Serial.println();
positionLeft = newLeft;
positionRight = newRight;
}
// if a character is sent from the serial monitor,
// reset both back to zero.
if (Serial.available()) {
Serial.read();
Serial.println("Reset both knobs to zero");
knobLeft.write(0);
knobRight.write(0);
}
}
Notice the differences between your and their code.
Another more simple example from the GitHub repository to satisfy Juraj.
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(5, 6);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(9600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
Also note that << is the binary left shift operator, not the less than operator < !
In if (f << 10)f = 10; you'll shift f 10 bits to the left. As this results in a number > 0, which is true, this condition will alway be met.
Same for >> which is the bitwise right shift operator, not greater then!

microphone sph0645 with I2S less sensitive after watchdog sleep with Adafruit Feather M0

I am using the Adafruit Feather M0 RFM69 with the Adafruit I2S MEMS Microphone Breakout SPH0645. Every second I take a reading (sampleRate = 16000, bits per sample = 32) using the I2S library and send it over the radio. This all works fine.
My problem is that, when I want to save power, I am getting weird readings after I wake the board from sleep (using Adafruit_SleepyDog library). The microphone somewhat still works, although it is much less sensitive, only picks up loud sounds and also returns 60dB in a quiet room. When I don't put it to sleep, in the same sound setting, I get 40dB. However, if I put a delay of 250ms after waking up, the microphone works fine again, like before, but this is obviously not saving energy then.
I wonder why this is happening. Is there something I can do to get the microphone to work quicker? I checked the datasheet, but it only says: "When Vdd is applied the microphone senses the
CLOCK line, if the frequency is greater than 900KHz, the microphone enters the normal mode of operation." This should not even take a few ms though?
Thanks in advance
#include <I2S.h>
#include <Adafruit_SleepyDog.h>
#include <SPI.h>
#include <RH_RF69.h>
/************ Radio Setup ***************/
#define RF69_FREQ 433.0
#define SLEEP
//#if defined(ARDUINO_SAMD_FEATHER_M0) // Feather M0 w/Radio
#define RFM69_CS 8
#define RFM69_INT 3
#define RFM69_RST 4
#define LED 13
//#endif
// radio
// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);
int transmit_interval = 1000;
int time_counter = 0;
int packetnum = 0;
// MIC
#define SAMPLES 1024//2048 // make it a power of two for best DMA performance
int samples[SAMPLES];
int measurementsdB = 0;
int current_measure;
#define ADC_SOUND_REF 65
#define DB_SOUND_REF 41
int sampleRate1 = 16000;
int bitsPerSample1 = 32;
typedef struct
{
uint8_t measurementdB = 123;
uint8_t battery = 111;
uint8_t test = 222;
} RadioMessage;
RadioMessage struct_message;
void setup()
{
delay(2000); // Wait so its easier to program
Serial.begin(115200);
//while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer
// Init Mic
if (!I2S.begin(I2S_PHILIPS_MODE, sampleRate1, bitsPerSample1)) {
while (1); // do nothing
}
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
pinMode(RFM69_RST, OUTPUT);
digitalWrite(RFM69_RST, LOW);
Serial.println("Feather RFM69 TX Test!");
Serial.println();
// manual reset
digitalWrite(RFM69_RST, HIGH);
delay(10);
digitalWrite(RFM69_RST, LOW);
delay(10);
if (!rf69.init()) {
Serial.println("RFM69 radio init failed");
while (1);
}
Serial.println("RFM69 radio init OK!");
// Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
// No encryption
if (!rf69.setFrequency(RF69_FREQ)) {
Serial.println("setFrequency failed");
}
// If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
// ishighpowermodule flag set like this:
rf69.setTxPower(20, true); // range from 14-20 for power, 2nd arg must be true for 69HCW
// The encryption key has to be the same as the one in the server
uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
rf69.setEncryptionKey(key);
Serial.print("RFM69 radio #"); Serial.print((int)RF69_FREQ); Serial.println(" MHz");
//GCLK->GENCTRL.bit.RUNSTDBY=1; // !! can go
}
void loop() {
Serial.println("START");
///// MIC
//PM->APBCMASK.reg |= PM_APBCMASK_I2S;
int a = 0;
while (a == 0) a = I2S.available();
uint8_t current_measure = sample_audio_signal(samples);
///// RADIO
if (true)//((time_counter + transmit_interval) < millis())
{
struct_message.measurementdB = current_measure;
//struct_message.battery = measuredvbat;
// Send a message!
/*
Serial.print("Array content: ");
uint8_t* bla = (uint8_t*) &struct_message;
for (int i = 0; i < 3; i++)
{
Serial.println(bla[i]);
}*/
rf69.send((const uint8_t*) &struct_message, sizeof(struct_message));
rf69.waitPacketSent();
Serial.print("Wait for reply");
// Now wait for a reply
uint8_t buf[RH_RF69_MAX_MESSAGE_LEN];
uint8_t len = sizeof(buf);
if (rf69.waitAvailableTimeout(100)) {
// Should be a reply message for us now
if (rf69.recv(buf, &len)) {
Serial.print("Got a reply: ");
Serial.println((char*)buf);
} else {
Serial.println("Receive failed");
}
} else {
Serial.println("No reply, is another RFM69 listening?");
}
Serial.println("Radio sleeping");
rf69.sleep();
time_counter = millis();
}
// sleep time
#ifdef SLEEP
int sleepMS = Watchdog.sleep(10);
delay(250);
#else
delay(1000);
#endif
Serial.println("loop ended");
}
void Blink(byte PIN, byte DELAY_MS, byte loops) {
for (byte i=0; i<loops; i++) {
digitalWrite(PIN,HIGH);
delay(DELAY_MS);
digitalWrite(PIN,LOW);
delay(DELAY_MS);
}
}
float sample_audio_signal(int samples[])
{
for (int i=0; i<SAMPLES; i++) {
int sample = 0;
while ((sample == 0) || (sample == -1) ) {
sample = I2S.read();
}
// convert to 18 bit signed
sample >>= 14;
samples[i] = sample;
}
// ok we have the samples, get the mean (avg)
float meanval = 0;
for (int i=0; i<SAMPLES; i++) {
meanval += samples[i];
}
meanval /= SAMPLES;
// subtract it from all samples to get a 'normalized' output
for (int i=0; i<SAMPLES; i++) {
samples[i] -= meanval;
}
// find the 'peak to peak' max
float maxsample, minsample;
minsample = 100000;
maxsample = -100000;
for (int i=0; i<SAMPLES; i++) {
minsample = min(minsample, samples[i]);
maxsample = max(maxsample, samples[i]);
}
int newdB = 20 * log10((float)maxsample / (float)ADC_SOUND_REF) + DB_SOUND_REF;
return newdB;
Ok, the best I got it down to is 3.8mA. I only got so far by leaving the voltage regulator and the internal oscillator (DFLL) on during sleeping.
After adding the following code to my setup routine, when board goes to sleep, the microphone still works after waking up:
SYSCTRL->DFLLCTRL.bit.RUNSTDBY=1;
SYSCTRL->VREG.bit.RUNSTDBY=1;
However, ideally I would like to get much less than that, but then the mic doesn't work...

Interrupts in 8051 Comparator

I am working Silicon Labs C8051F120.
Below is the code I am using for Comparator.
By right when I decrease the power supply I should get an interrupt.
But I am not getting the interrupt.
I am doubting the initialization routine.
Normal voltage is 5V. When voltage is decreased to 4.2V, I should get Comparator 1 interrupt. When voltage is decreased to 3.V, I should get Comparator 0 interrupt.
Also I am confused why there are two ways by which Comparator Interrupt can be enabled:
1)By CPTT0MD/CPT1MD: Comparator0/Comparator1 Mode Selection
2) EIE1: Extended Interrupt Enable 1
Which one to use when and why?
#define COMPARATOR_SETTLE 30
void Comp0_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CPT0_PAGE;
CPT0CN = 0x83; // Comparator0 enabled // 20 mV Negative hysteresis
Delay_us (COMPARATOR_SETTLE); // Allow CP0 output to settle for 20 us
CP0RIF = 0;
CP0FIF = 0;
CPT0MD = 0x10;
EIE1 |= 0x10;
SFRPAGE = SFRPAGE_SAVE;
}
void Comp0_ISR(void) interrupt (10)
{
CP0FIF = 0;
comp_flag = 1;
}
void Comp1_Init()
{
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CPT1_PAGE;
CPT1CN = 0x83; // Comparator0 enabled // 20 mV Negative hysteresis
Delay_us (COMPARATOR_SETTLE); // Allow CP0 output to settle for 20 us
CP1RIF = 0;
CP1FIF = 0;
CPT1MD = 0x10;
EIE1 |= 0x40;
SFRPAGE = SFRPAGE_SAVE;
}
void Comp1_ISR(void) interrupt (12)
{
CP1FIF = 0;
comp_flag = 2;
}

spi interfacing between two microcontroller in mikroC

I'm doing spi interfacing between two pic18F452 microcontrollers in mikroC but my program doesn't give any output. I send a data from Master and slave have to display it on LCD.
Here is my code for both master and slave:
MASTER CODE:
unsigned char key='a';
void main(void)
{
TRISC.F2=0; //output for Slave select
TRISC.F3=0; //SCK output
TRISC.F4=1; //SDI input
TRISC.F5=0; //SDO output
Spi_Init_Advanced(Master_OSC_div64, Data_SAMPLE_MIDDLE, CLK_Idle_HIGH, HIGH_2_LOW);
SSPCON1.SSPEN=1; //Synchronus serial port enable
SSPSTAT.SMP=0; //sampled at middle of data output time
while(1)
{
PORTC.F2=0;
SSPBUF=key;
while(!SSPSTAT.bf);
spi_write(key);
}
}
///////end//////
SLAVE CODE:
#define LCD PORTB
unsigned char key;
void main(void)
{
TRISB=0; //LCD output
TRISC.F4=1; //SDI input
TRISC.F3=1; //SCK input from master
TRISC.F5=0; //SDO output
TRISA.F5=1; //Slave select input from master
SSPSTAT.SMP=0; //input data sampled at middle
SSPSTAT.CKE=0; // transition from idle to active
SSPCON1.=0x22;
Spi_Init_Advanced(SLAVE_SS_ENABLE, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
Lcd_init(&PORTB);
Lcd_cmd(Lcd_clear);
Lcd_cmd(Lcd_cursor_off);
while(1)
{
if(SSPSTAT.BF)
{
key=spi_read(SSPBUF);
}
}
}
////////end///////
i am not sure about this part
Lcd_init(&PORTB);
Lcd_cmd(Lcd_clear);
Lcd_cmd(Lcd_cursor_off);
try declaring your lcd port like this . i use port d
//lcd
sbit LCD_RS at RD0_bit;
sbit LCD_RW at RD1_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_RW_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;
and for initing and clearing
LCD_Init(); // Initialize LCD
DELAY_MS(100);
Lcd_Cmd(_LCD_CLEAR); // Clear display
that delay part might comes handy some times.

Resources