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.

How to get the time period of a pulse in mikroc pro

Status
Not open for further replies.

madhushan90

Newbie level 6
Joined
Jul 26, 2012
Messages
12
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,362
Hi everyone, i want to get the time period of a pulse in mikroc pro. I'm using SR04 ultrasonic sensor, when it triggered it will generate a pulse. To calculate the distance, we have to divide pulse time period by constant given by the manufactures. The thing is time period should be within rising edge to the falling edge in same pulse. Please help me to do this. thanks.
 

Re: Get pulse width in mikroc

madhussan its may be easy if u use exrternal interrupt first by register setting iunterrupt on rising edge on this interrupt on the timer and adjust interrupt on falling edge. when falling edge interrupt comes stop timer and calculate the time
 
Re: Get pulse width in mikroc

zia, your great idea works for me thanks, but now i can't get the time. I used 16bit timer, internal clock without prescale and 18f452 microcontroller in 4Mhz. When I try to get time from t = TMR0H; it didn't work for me. But t = TMR0L; gives some crazy values. RB0 is the pin i gave the pulse. Here is the code i used.
Code:
// LCD module connections
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;

sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
// End LCD module connections
unsigned int t;
char tStr[16];

void printTime(){
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Cmd(_LCD_CLEAR);
t = TMR0L;
IntToStr(t,tStr);
Lcd_Out(1,1,tStr);
}

void interrupt(void){
   if(RB0_bit == 1){
   TMR0ON_bit = 1;//on timer1
   RC0_bit = 1;
   INT0IF_bit = 0;
   INTEDG0_bit = 0;
   }
   if(RB0_bit == 0){
    TMR0ON_bit = 0;//stops timer1
    RC0_bit = 0;
    INT0IF_bit = 0;
    INTEDG0_bit = 1;
    printTime();
   }
}

void main() {
TRISC0_bit = 0;
TRISB = 1;
INTCON.GIE = 1;//enable global interrupts
INTCON.INT0IE =  1;//enable external interrupts
INTCON.PEIE = 0; //disable all unmasked interrupts
INTEDG0_bit = 1;//interrupt on RB0 rising edge
T08BIT_bit = 0;//TMR0 configured as 16bit timer
T0CS_bit = 0;//use internal instruction cycle
PSA_bit = 1;//timer1 prescaler not assigned
Lcd_Init();
TMR0L = 0;
}

can you tell me how to get the time. And the time period of pulse I am going to measure is not exceed 38ms.Thank you
 

Re: Get pulse width in mikroc

Zia, can I have a copy of the project please?

I am struggling for the last couple of days with the sr04 module.

Thanks!
 

Re: Get pulse width in mikroc

check mail and here is code in mikroc u may easily convert it to mikrocpro
unsigned char i=0,op[12]={48,48,48,48,48,48,48,48,48,48,48,48},lcd[12];
unsigned long timeperiod=0;
void interrupt(void)
{
i++;
if(intcon.int0if)
{

intcon.int0if=0;
///start timer
if(i==1)
{
TMR0L=0;
TMR0H=0;
T0CON.TMR0ON=1; ///START TIMER
INTCON2.INTEDG0=0;///INTERRUPT ON FALLING EDGE
}
if(i==2)
{
T0CON.TMR0ON=0; ///STop TIMER
INTCON2.INTEDG0=0;
INTCON2.INTEDG0=1;///INTERRUPT ON risING EDGE
timeperiod=TMR0H*65536+TMR0L;
timeperiod=timeperiod*256;
longtostr(timeperiod,op);
i=0;
}
}
if(intcon.TMR0IF)
{
intcon.TMR0IF=0;
}
}
void main(void)
{
trisb.f0=1;
trisd=0;
lcd_config(&portd,0,2,1,7,6,5,4);
lcd_cmd(lcd_cursor_off);
T0CON=7;///PREESCALAR 256
INTCON=0xb0; ///enable timer0 and external interrupt
TMR0L=0;
TMR0H=0;
while(1)
{

lcd_out(1,1,"timeperiod:");
// lcd_chr(2,1,i+48);
lcd_out(2,1,op);

lcd_out_cp("usec");
delay_ms(500);
lcd_cmd(lcd_clear);
INTCON2.INTEDG0=1;
}
}

- - - Updated - - -

elsol why not Knowledge is *** given
i have attached code bt if not understanding kindly send me email address
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top