Light WROTE:thanks a lot for sharing your experience with others.
do i have to add those .H files into 'include' directory? if yes, can you please upload it here because i tried searching for them and i keep finding different results where i dont know which one is the correct one.
Light WROTE:Im using MikroC PRO for compiling source codes...well..i have a problem when i tried to compile your code, the errors below occurs:
luqman hakim WROTE:can't open include file "delay.h": No such file or directory..
this is error that i found..
#define PIC_CLK 20000000 //change this to 3.6864, 4, 16 or 20MHz
//============================================================================
// Include
//============================================================================
#include <pic.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "delay.h"
#include "delay.c"
//============================================================================
// Configuration
//============================================================================
__CONFIG ( 0x3F32 );
//============================================================================
// Function Prototype
//============================================================================
void ultrasonic(void); // Find range based on input
void init(void);
void rangecal(void);
//============================================================================
// global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
//============================================================================
// interrupt prototype
//============================================================================================================
static void interrupt isr(void)
{
if(TMR0IF) // TMR0 is overflow
{
TMR0IF=0; // clear flag bit
To+=0x100; // count number of TMR0 overflow ( make it to 16bit TMR)
}
}
//============================================================================
// Main Function
//============================================================================
void main(void)
{
init();
ultrasonic();
}
// Initailization
// Description : Initialize the microcontroller
//============================================================================================================
void init()
{
// Tris configuration (input or output)
TRISA = 0b00000000; //set PORTA as output
TRISB = 0b00000100; //set RB2 pin as input, other as output
TRISC = 0b00000000; //set PORTA as output
RB4=1; //5V to sensor
DelayMs(250); //sensor module power up time
// TMR 0 configuation
T0CS=0;
PSA=0;
PS2=1; // prescale 1:256
PS1=1; //
PS0=1; //
TMR0IE=1; // TMR0 Interrupt
TMR0=0;
GIE = 1; //global interrupt
}
//================================================================================
// FUNCTIONS
//================================================================================
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
void ultrasonic(void)
{
while(1)
{
if(RB2==0) //if RB2 is high
{
TMR0=0; // clear all counter involved, start new count for period of RB2 high
To=0;
}
else
{
TH=TMR0+To; // RB2 is 0 mean is falling from 1 save TH, RB2 high period
value=TH; //value of tmr0+to
distance=value*1.75616; // calculate inch value per inch = 147us with 20Mhz internal clock.
rangecal();
}
}
}
void rangecal(void)
{
if(distance>100)
{
PORTC=0B00000001;
}
else if(distance>80)
{
PORTC=0B00000010;
}
else if(distance>60)
{
PORTC=0B00000100;
}
else if(distance>40)
{
PORTC=0B00001000;
}
else if(distance>20)
{
PORTC=0B00010000;
}
}
/*
KeyGhost
Designed by Shane Tolmie Apr 99.
Microprocessor: see mainkg.c
Compiled with: see mainkg.c
Overall goal: Delay Routines
*/
#ifndef __DELAY_C
#define __DELAY_C
#include "always.h"
#include "delay.h"
void DelayBigUs(unsigned int cnt)
{
unsigned char i;
i = (unsigned char)(cnt>>8);
while(i>=1)
{
i--;
DelayUs(253);
CLRWDT();
}
DelayUs((unsigned char)(cnt & 0xFF));
}
void DelayMs(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
/*
//this copy is for the interrupt function
void DelayMs_interrupt(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
} while(--i);
} while(--cnt);
}
*/
void DelayBigMs(unsigned int cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayUs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
/*
void DelayS(unsigned char cnt)
{
unsigned char i;
do {
i = 4;
do {
DelayMs(250);
CLRWDT();
} while(--i);
} while(--cnt);
}
*/
#endif
Light WROTE:hi again
i tried compiling the code in HI-TECH C Compiler....it wont work unless i delete the following lines:
#include "delay.h"
#include "delay.c"
the other question is....in the following equation:
distance=value*1.75616; // calculate inch value per inch = 147us with 20Mhz internal clock.
how the number 1.75616 have being calculated ? the datasheet mentions that 147us is the pulse width..thats all.
tq
Users browsing this forum: No registered users and 14 guests