Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[MOVED] circuit related questions

Status
Not open for further replies.
Code:
while(1)
{
generate_square(); // in 38 khz
delay_ms(120);
output_low();        // IR led in Off state
delay_ms(380);      // 380+120 = 500ms period
}

- - - Updated - - -

use this procedure to generate square wave...


Now look at the recevier output oFF time that matters with distance.....


that can be found easily

- - - Updated - - -

Code:
while(1)
{
for( i = 0 ; recieve == 0; i++) // Count for number of ms the output is low
delay_ms(1);                        // i is the distance ouput  may be for 1m i = 75; 1.5m i = 25;

while(recieve == 1);
}

- - - Updated - - -

They are not meant to change the duty cycle, They are meant to set a constant square wave for some time.....

#include <pic.h>
#define _XTAL_FREQ 20000000

//Timer0
//Prescaler 1:1; TMR0 Preload = 124; Actual Interrupt Time : 26.3 us


void init(void){
TRISD = 0X07;
TRISC = 0X00; //configure Rc2 as output for Emitter(pwm)
CCP1CON = 0X0C; //select PWM mode.
PR2 = 131; //Set period register for approx. 38khz.
CCPR1L = 65; //Set duty cycle for approx 50%
T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2.

}


void main()
{
init();
int i;
while(1){
CCPR1L =0; //Set duty cycle for 0%
__delay_ms(380); //delay 380ms
CCPR1L = 65; //Set duty cycle for approx 50%
__delay_ms(120); //delay 120ms

this is my code...can u tell me how to proceed to get the distance as u said???
 

i panic already,don't know how to proceed...and i only manage to detect 10-15cm...zzz...

with the code u given can u give me the full code?i got no idea about it -.-

#include <pic.h>
#define _XTAL_FREQ 20000000

//Timer0
//Prescaler 1:1; TMR0 Preload = 124; Actual Interrupt Time : 26.3 us


void init(void){
TRISD = 0X07;
TRISC = 0X00; //configure Rc2 as output for Emitter(pwm)
CCP1CON = 0X0C; //select PWM mode.
PR2 = 131; //Set period register for approx. 38khz.
CCPR1L = 65; //Set duty cycle for approx 50%
T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2.

}

void generateSquare()
{

CCPR1L =0; //Set duty cycle for 0%
__delay_ms(380); //delay 380ms
CCPR1L = 65; //Set duty cycle for approx 50%
__delay_ms(120); //delay 120ms
}
void main()
{
init();
int i;
generateSquare();
for(i=0;RD==0;i++){
__delay_ms(1);
}
if(i<=120&&i>60){
RD1=1;
__delay_ms(1000);
}
if(i<=60){
RD2=1;
__delay_ms(1000);

}
 

Hey are you joking, how did you got that 10 to 15 cms???

- - - Updated - - -

You are generating square wave... and after finishing whole cycle you are getting input... both things must be mixed for the operation..... You need at least one timer interrupt...
 

Hey are you joking, how did you got that 10 to 15 cms???

- - - Updated - - -

You are generating square wave... and after finishing whole cycle you are getting input... both things must be mixed for the operation..... You need at least one timer interrupt...


i check with the cro when my hand is about 10cm away from the detector, the wave changed..

you mean when i start send square wave, and when output of receiver is low,start the timer and count the time?
 

Hey here is 80% of code make it full

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <pic.h>
#define _XTAL_FREQ 20000000
 
#define OFFSET 80   // You can vary this and try
 
//Timer0
//Prescaler 1:1; TMR0 Preload = 124; Actual Interrupt Time : 26.3 us
 
void pwm_init(void);
void timer_init(void);
void interrupt Timer0_ISR(void);
void display(void);
 
int i,j;
 
 
void main()
{
    int i;
    
    pwm_init();
    timer_init();   
    generateSquare();
    
    while(1)
    {   
        for(i=0;RD==0;i++)
        __delay_ms(1);
        
        if(i != 0)
        display();
        else
        RD1 = RD0 = 1; // noting got in reception
        
        while( RD == 1 );
    }
 
 
}
 
// modify timer isr with "if".. If you got compiler warning..
//
//
//
void interrupt Timer1_ISR(void)
{
    // check and reset interrupt flag
    if( ++j > 2 )
    j = 0;
    
    switch ( j )
    {
        case 0:
        case 1:
            CCPR1L =0;  break
        case 2:
            CCPR1L = 65; //Set duty cycle for approx 50%
            break;
    }
    
    // Do timer Reloadings
}
 
 
//
//
//
void timer_init(void)
{
    //Configure timer 1 to interrupt every 200ms and start  
}
 
 
// I didnt look inside this function
//
//
void pwm_init(void){
TRISD = 0X07;
TRISC = 0X00; //configure Rc2 as output for Emitter(pwm)
CCP1CON = 0X0C; //select PWM mode.
PR2 = 131; //Set period register for approx. 38khz.
CCPR1L = 65; //Set duty cycle for approx 50%
T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2.
}
 
 
// To find the i value If you got nothing try changing OFFSET
//
//
void display(void) {
 
RD1 = RD2 = 0;
 
if(i<OFFSET)
RD1=1;
else
RD2=1;
 
__delay_ms(400);
 
RD1 = RD2 = 0;
 
}

 

the case 0 is for what?offset is what...?can i use timer 0 to interrupt for 200ms?but why 200ms?and i don't understand about the j...Do timer Reloadings mean reload the timer?

void display(void) {

RD1 = RD2 = 0;

if(i<OFFSET)
RD1=1;
else
RD2=1;

__delay_ms(400);

RD1 = RD2 = 0;

}

RD1=RD2=0 for what?
 
Last edited:

case 0:
We are slightly modifying the concept and there is going to be pulses for 200ms and be idle for 400ms...
the value of j will be 0,1,2,0,1 and it will go on for every interrupt for j is 0 and 1 the duty cycle is 0 (so that 400ms) and for case 2 the dutycycle will be 50%..

Timer:
You can use timer0 also but make clear changes every where... ( like the function in Timer'1'_ISR )
I mean reload the value of timer counter for 200ms

- - - Updated - - -

the offset is for knowing the i value for us..... if RD1 is high the i is lessthan OFFSET if RD2 is 1 the the value of i is greater... and if both are high ther is no reception........
 

quite confused...is this in hi-tech universal toolsuite?

can i use this step?

1. Set PWM Duty Cycle to 0%

2. Delay 500ms

3. Set PWM Duty Cycle to 50%

4. Clear counter

5. Check if counter > 120, handle no result, repeat from start
(If counter is > 120 then 120ms have elapsed with no transition, so no result is available. Handle that case and repeat from start.)

6. Check for detector transition from low to high, handle result, repeat from start
(Read the digital input pin connected to the TSSP4P38 output - for example, RA0 - and check if the state changed from low to high. If the state changed, then the current value in counter will be the elapsed time in milliseconds, from which you can calculate the distance. Do whatever processing is required and repeat from start.)

7. Delay 1ms

8. Increment counter

9. Repeat from 5


but also don't know how to start...
 

ya you are right only but how will you stop the pulses after 200ms, you cant do that without an ISR.. configure the timer in my program....
 

#include <pic.h>
#define _XTAL_FREQ 20000000

#define OFFSET 117 // You can vary this and try

//Timer0
//Prescaler 1:1; TMR0 Preload = 124; Actual Interrupt Time : 26.3 us

void pwm_init(void);
void timer_init(void);
void interrupt Timer0_ISR(void);
void display(void);

int i,j,counter;


void main()
{
int i;

pwm_init();
timer_init();


while(1)
{
for(i=0;RD==0;i++)
__delay_ms(1);

if(i != 0)
display();
else
RD1 = RD0 = 1; // noting got in reception

while( RD == 1 );
}


}

// modify timer isr with "if".. If you got compiler warning..
//
//
//
void interrupt Timer0_ISR(void)
{
// check and reset interrupt flag
if( ++j > 3 )
j = 0;

switch ( j )
{
case 0:
case 1:
case 2:
CCPR1L =0; break; // 351ms
case 3:
CCPR1L = 65; //Set duty cycle for approx 50%
break;
}

// Do timer Reloadings
}


//
//
//
void timer_init(void)
{
OPTION = 0X47;
INTCON = 0xA0;
TMR0=0;

if(T0IE && T0IF) //TMR0 Overflow ISR
{

counter++; //Increment Over Flow Counter

if(counter==9) // 117ms
{
counter=0; //Reset Counter
}

TMR0IF=0; //Clear Flag

}
}



//
//
//
void pwm_init(void){
TRISD = 0X07;
TRISC = 0X00; //configure Rc2 as output for Emitter(pwm)
CCP1CON = 0X1C; //select PWM mode.
PR2 = 131; //Set period register for approx. 38khz.
CCPR1L = 65; //Set duty cycle for approx 50%
T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2.
}


// To find the i value If you got nothing try changing OFFSET
//
//
void display(void) {

RD1 = RD2 = 0;

if(i<OFFSET)
RD1=1;
else
RD2=1;

__delay_ms(351);

RD1 = RD2 = 0;

}

in the void pwm_init(void),i no need to set the ccpr1l=65 already right?so can remove?how to reload timer....?
 

ya remove them and load the value for 200ms delay

I think you know timer configuration else look at the micro chip timer0 tutorial part 2 copy and paste in google..
 

after that consider finished? but when i program and transfer to pic,i connect to cro the rc2 pin doesn't give square wave at all...

reloading mean just called the function name? for example:
void interrupt Timer0_ISR(void)
{
// check and reset interrupt flag
if( ++j > 3 )
j = 0;

switch ( j )
{
case 0:
case 1:
case 2:
CCPR1L =0; break; // 351ms
case 3:
CCPR1L = 65; //Set duty cycle for approx 50%
break;
}

timer_init();
}
like this??

under the main loop,the last code is RD0==1 or just RD?

after load this program seem get nothing...
 
Last edited:

++j > 2 and you have clear the interrupt flag

Its not meaning timer init you have to load the value like just TC0 = 0xsomething..
 

put ++j > 2 in condition and you have to clear the interrupt flag before if condition..

There is no timer init in ISR you have to load the value like just TC0 = 0xsomething..
 

So I load tmr0=0 something like that to start timer 0 and then how to reload the timer in that line u mentioned?the isr loop no need to call?it will call itself?
 

this is the code i developed by myself,is that ok?

#include <pic.h>
#define _XTAL_FREQ 20000000

void pwm_init(void);
void timer_init(void);

int counter;
void main()
{
int i;
pwm_init();
timer_init();
TRISC2=0;
while (1){
CCPR1L=0;
__delay_ms(351);
CCPR1L=65;
__delay_ms(117);
}
while(1)
{
if(counter <=118 && counter>60 )
{
RD1==1;
}
else if( counter <= 60 && counter >0)
{
RD2== 1;
}
else
{
PORTD==0;
}
}
}

void pwm_init(void){
TRISD = 0X07;
TRISC = 0X00; //configure Rc2 as output for Emitter(pwm)
CCP1CON = 0X1C; //select PWM mode.
PR2 = 131; //Set period register for approx. 38khz.
T2CON = 0X04; //Set timer2 pre/post scalers to 1 & start Timer2.
}

void timer_init(void)
{
OPTION = 0X47; //prescaler 256
INTCON = 0xA0;
TMR0=235; //1.02ms

if(T0IE && T0IF && RD0==0) //TMR0 Overflow ISR and detected
{

counter++; //Increment Over Flow Counter

if(counter>118 || RD0==1) // 120ms or detect nothing
{
counter=0; //Reset Counter
}

T0IF=0; //Clear Flag

}
}
 

Can't just put delay_ms there?because I don't understand the offset and the rd1=rd2=1..
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top