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.

can anyone tell me whats wrong with this program

Status
Not open for further replies.
iam back again
Code:
#include <htc.h>

#define _XTAL_FREQ 10000000    //10MHz xtal speed

__CONFIG(LVPDIS & BORDIS & PWRTEN & WDTDIS & HS);

  
unsigned int sensorRead()
{
  
  unsigned int SENSOR = 0;//variable used to combine two 8 bit variables into 1 16 bit variable

  TRISC2 = 0;    //RC2 output
  RC2 = 1;    //set RC2 high
  __delay_us(5);    //for 5uS
  TRISC2 = 1;    //RC2 input
  TMR1H = 0;    //clear timer 1
  TMR1L = 0;    
  T1CON = 0b00110000;  //init timer 1

  while(!RC2);    //wait until RC2 goes high via echo pulse
  TMR1ON = 1;    //start timer 1
  CCP1CON = 0x04;    //enable capture module, capture every falling edge of RC2/CCP1
  while(!CCP1IF);    //wait for falling edge on RC2
  CCP1CON = 0x00;    //CCP module off
  CCP1IF = 0;    //clear CCP interrupt flag
  TMR1ON = 0;    //stop timer 1

  SENSOR = (CCPR1H + CCPR1L); // 1:8 prescaler ie 3.2uS so minimum value 115uS/3.2uS = 36 (approx)
  return SENSOR;
}

void main(void)

{
  PORTB=0;         //clear portB
  TRISB=0;        //PORTB as output
  unsigned int my_Time;    //variable for storing the time to calculate distance
  my_Time=sensorRead();
  unsigned int my_Distance= my_Time/58;
  while(1)
{
  if(my_Distance > 30 && my_Distance < 50)  
{
  RB1=1;
}
else 
 RB2=1;
}
}


in wrote this program to do what i mentioned above but the RB1 is high/low randomly don't know whats the problem

here i have divided my_Time by 58 to obtain the distance in cm
as
Distance = Velocity(speed)*time

speed of sound in air is approx 340m/s and the time measured in uS hence i derived the constant 58

somebody please tell me whats wrong with this program
 
Try setting TRISB before clearing PORTB. I'm not sure if it'll help, but it's worth a try.

Also, change

"if(my_Distance > 30 && my_Distance < 50)"

to

"if((my_Distance > 30) && (my_Distance < 50))"

Another thing is that it looks like you only do the last part repeatedly. It only checks the sensor once and won't ever go back to your sensor read function until you restart the program. Is this really what you wanted?
 
Last edited:
i guess the problem is here

Code:
  SENSOR = (CCPR1H + CCPR1L);

as the timer1 is a 16bit data the capture has to store it in two registers but if we combine both the register we end up with a 8 bit register

what can i do no ?
 
I don't know why RB1 is fluctuating..... But nothing happens in your whlie loop... you read the sensor once before the loop and then don't...

actually this a test program i am just trying to obtain a high output at RB1 if the sensor obtains a signal
 
Code:
SENSOR = (CCPR1H + CCPR1L);
should be
SENSOR = CCPR1H; 
SENSOR << = 8 + CCPR1L;

I've tried casting SFR's before... but it doesn't work well.

sir do the data type of the variable sensor and my_Time is correct ?

and i din't understand this "I've tried casting SFR's before... but it doesn't work well."
 
sir do the data type of the variable sensor and my_Time is correct ?

and i din't understand this "I've tried casting SFR's before... but it doesn't work well."

SFRs are Special Function Registers. Your CCPR1 is a SFR. I feel that you can do this without using the CCPR1 function. I recently made a knock detector program that measured and stored the time between knocks on a door, and saved them for comparison later on. Every time the sensor heard a "knock", two things would happen--one, it would stop the timer, read it, and save the value, and then it would clear it and start it, and wait for the next one. It works in much the same way as your rangefinder, only a bit slower. I did not need CCPR. I only needed to set up, start, stop, and read Timer0.
 
Try setting TRISB before clearing PORTB. I'm not sure if it'll help, but it's worth a try.

Also, change

"if(my_Distance > 30 && my_Distance < 50)"

to

"if((my_Distance > 30) && (my_Distance < 50))"

Another thing is that it looks like you only do the last part repeatedly. It only checks the sensor once and won't ever go back to your sensor read function until you restart the program. Is this really what you wanted?

yes sir the sensor reads the data only once and i am trying to check the hardware by this program
 
SFRs are Special Function Registers. Your CCPR1 is a SFR. I feel that you can do this without using the CCPR1 function. I recently made a knock detector program that measured and stored the time between knocks on a door, and saved them for comparison later on. Every time the sensor heard a "knock", two things would happen--one, it would stop the timer, read it, and save the value, and then it would clear it and start it, and wait for the next one. It works in much the same way as your rangefinder, only a bit slower. I did not need CCPR. I only needed to set up, start, stop, and read Timer0.


would you mind sharing the function code alone ?
 
Did you change the "if" statement? That could cause it to fluctuate, if it's not set up correctly.

As for the "function code alone", it's quite a bit more complicated than that. I had to set it up to work specifically with my hardware, and it's not something I could easily transfer. Also, it was written in C18, which is incompatible with Hi-Tech. You would have to re-write it anyway.

Anyway, basically what it did was this: First, the timer was cleared. The program then waited for the first knock to be heard before starting the timer. Once it sensed a knock, it went through a short loop ("de-bouncing", so to speak) so that it was responding only to a single knock. Then the timer was started, and the ADC was continuously re-checked for a value (from the knock sensor). When it sensed another knock, it stopped the timer, stored the value in an array, was cleared, and then started again. This process was repeated until the timer read the maximum value, which said that there were no more knocks coming, and it would time out. The same setup should work for yours, except it will be going a lot faster. Also, you would not need to store all the different times between signals. If you're only testing once, you only need the first part of my description (start at the first signal, stop at the next, and save the value).
 
SFRs are Special Function Registers. Your CCPR1 is a SFR. I feel that you can do this without using the CCPR1 function. I recently made a knock detector program that measured and stored the time between knocks on a door, and saved them for comparison later on. Every time the sensor heard a "knock", two things would happen--one, it would stop the timer, read it, and save the value, and then it would clear it and start it, and wait for the next one. It works in much the same way as your rangefinder, only a bit slower. I did not need CCPR. I only needed to set up, start, stop, and read Timer0.

Yeah... What DS said:D:D I don't come across as good as DS
 
ok sir got it

Ian even if i use the timer1 i need to handle the 16bit rite ? how to do that ?
which datatype should i use for retrieving the data from the function

ie what datatype should be the variable sensor ?
 
When I get home I'll bang it on ISIS and find out what gives ( i'll need to simulate the sensor, what sensor is it anyway?)


I've checked it on ISIS.... works no problem... Here's the code I changed..

SENSOR = ((int)(CCPR1H) << 8)+ CCPR1L ; // 1:8 prescaler ie 3.2uS so minimum value 115uS/3.2uS = 36 (approx)

I use a generated 80hz square wave to achieve a value in my_Distance of 33..
 
Last edited:
When I get home I'll bang it on ISIS and find out what gives ( i'll need to simulate the sensor, what sensor is it anyway?)


I've checked it on ISIS.... works no problem... Here's the code I changed..


SENSOR = ((int)(CCPR1H) << 8)+ CCPR1L ; // 1:8 prescaler ie 3.2uS so minimum value 115uS/3.2uS = 36 (approx)

I use a generated 80hz square wave to achieve a value in my_Distance of 33..
sorry for the late response

sir i am using a PING))) sensor here is the datasheet https://www.electro-tech-online.com/custompdfs/2012/03/28015-PING-v16.pdf

it sends 40KHz signal
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top