fantabulous68 said:/*generating 40KHz low duty cycle pulses which will drive an Infrared transmitter. When an object is
infront of the infrared transmitter then the receiver IC will detect the object and an LED will GLOW.
i want the project to be immune to ambient light such as a desk lamp.
1)when i shine light on the transmitter i want the led to be off.
2)when there is no object infront of the transmitter i want the led to be off.
3)when there is an object infront of the transmitter i want the led to glow.
i programmed the pic16f690 and put it in my circuit. this is what happens:
1)LED pulses continuously when no object in front of it.
2)when object infront of transmitter it stops glowing
3)when shine light on transmitter the LED pulses very fast
hardware connections:
LED connected to RC3
output of receiver connected to RC2
micro generates pulses at RA2
here is my code:
*/
#include <pic.h>
#include "pic.h"
#include "math.h"
#include <stdio.h>
#include <stdlib.h>
#include "delay.h"
void Transmit40(void);
bit ir1,ir2; / 2 different outputs of sensor during On & Off pulse
unsigned char ir; // stores final result
void Transmit40(void)
{
int t=0;
int cnt=0;
while(cnt<20) //Tx 10 IR pulses/burst
{ if (t==0)
{
PORTA=PORTA^0x04; //send IR, Toggles RA2
DelayUs(5);
ir1=RC2; // IR bit1= RC2
cnt++;
t=1;
}
else if (t==1)
{
PORTA=PORTA^0x04; //stop IR, Toggles RA2, RA2 is source of low duty cycle pulses
DelayUs(20);
ir2=RC2; // O/P IR bit2= O/P pin of receiver IC
cnt++;
t=0;
}
if ((ir1 == 1)&(ir2 == 0)) //Obstacle detected
{
ir = 1;
}
else
{
ir = 0; // The way is clear in front of the sensor
}
RC3=ir; // led connected to RC3, LED indicates detection
}
}
void main(void)
{
ANSEL=0; // Set inputs to digital
TRISC=0x04; //making pin RC2 input. output of detector IC connected to RC2
while (1)
Transmit40();
}
/*
the hardware part of my circuit works. i initially used a 555timer to generate pulses and the hardware worked.
all im doin now is using a microcontroller to generate pulses instead of a 555timer. so the problem lies in the software.
please do assist me to correct my code. it is for a project Im a beginner with the pic*/
//////////////////////CALCULATING DISTANCE///////////////////////
void enable_interrupt(void)
//This function is basically designed to set the specific bits of the timer and capture registers
{
CCP1CON=0x05; //bits 3-0 of the capture control register are set to 0101.
//This mode is to capture every rising edge of the pulse being
//transmitted and then received
TMR1IF = 0; //The interrupt flag bit of the PIR1 register is cleared before
//enabling the interrupt.
CCP1IF = 0; //Before a capture is made, the Interrupt Request Flag bit
//CCP1IF of the PIR1 register is cleared.
CCP1IE = 1; //The capture interrupt enable bit is set to enable the interrupt
TMR1IE = 1;
//An interrupt will occur immediately provided that the GIE and PEIE bits of the INTCON
//register are set.
PEIE = 1;
GIE = 1;
}
void interrupt ISR(void)
{
if (TMR1IF) //if there is an overflow
{
TMR1IF = 0; //and the flag is then cleared
TMR1_OF++;
}
if (CCP1IF) //if there is a capture
{
CCP1IF = 0; //Zero Capture flag
if (Cap_1st) //allow only 1 capture to be taken
{
Cap_1st=0; //Cap_1st is cleared as if another value is captured before the previous value is read
//then the
t_capL=CCPR1L; //lower byte of the capture register
t_capH=CCPR1H; //higher byte of the capture register
capture_status=1; //signal that a capture occured is enabled.
}
}
}
void calc_distance(void)
{
long sample=0;
i=1; j=0;
Cap_1st = 1;
capture_status=0; //signal that a capture occured is disabled
TMR1ON = 0; //stop timer
//tH=TMR1H; tL=TMR1L;
tH=0; tL=0; t_capL=0; t_capH=0; //initialise capture
TMR1_OF=0;
Transmit40khz();
DelayUs(100);
enable_interrupt(); //Interrupt is enabled to capture time when the rising edge occurs
TMR1ON = 1; // start timer 1
//when timer 1 starts, the enable_interrupt function will be called to
//capture the time from the 40kHz signal once the first rising edge of the transmitted
//pulse occurs
//A continuous loop is run such that when the signal to state that a capture has occurred
// is 0, then the 40 kHz will be transmitted and received when sending out 10 pulses.
//Once this condition is satisfied, the global interrupt enable bit will be cleared to
//prevent the interrupt from occurring when an interruption is in progress.
//The timer is then stopped by disabling TMR1ON and TMR1IE.
while((capture_status==0)&&(TMR1_OF<1));
GIE = 0; //disable global interrupt
TMR1ON = 0; //stop timer 1
TMR1IE = 0; // disable timer 1 interrupts on overflow//
//Another condition arises such that if the signal to state that a capture has occurred
// is then enabled, then the sample time is calculated and stored in an array. The sample
// time is calculated via the difference between high byte of the capture time and the high
// byte of Timer1 added to the low byte of the capture time and the low byte of Timer1.
//The index is incremented. This then deduces the end of the function to get the samples.
//However, if the index is less than 20, then the function will continue.
if (capture_status==1)
{
sample = ((t_capH)*0x100)+(t_capL);
i = 2;
GIE=0; //Global interrupt is disabled to avoid another interrupt from occurring while
// an interrupt is taking place
t_capH = 0;
t_capL = 0;
}
if (i==1)
distance = 0;
//if and only if a capture is made will the process of distance calculation continue.
if (i==2)
{
time = sample * 1e-6; //since each count takes 1us to complete, this value is multiplied
//to the sample time.
//Distance is calculated via multiplying the time and distance light travels
//light travels 300m in 1 us.This result is divided by 2
//as speed travels twice the measured distance.
distance=(time*30000)/2;
//liquidLEVEL=heightOFtank-distance im still working on this part
}
return;
}
////////////////////END CALCULATING DISTANCE//////////////////////
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?