changing a simple code from MikroC format to HI-TECH format

Status
Not open for further replies.
Re: changing a simple code from MikroC format to HI-TECH for

ok i tried the debugger as u asked me...by choosing the MPLAB SIM and then running the simulation (by pressing the play button).....what happend is that i recieved this error:

CORE-E0002: Stack under flow error occurred from instruction at 0x000014

and the green arrow was jumping from the start to the end of the following code:

Code:
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)
    sysclock++;
    }
  }

and i seriously don't know what does that means, and how to solve it !
...
 

Re: changing a simple code from MikroC format to HI-TECH for

I have just run the code I posted in mplab sim and it runs ok.

You have to move the call to the delay function to the end of init otherwise the delay wont work, it needs the interrupt to be going.

Code:
void init() 
  { 
  TRISB = 0b00000110;   //set RB2 pin as input, other as output 
  TRISC = 0b00000000;   //set PORTA as output 
  TRISD = 0b00000000; 
 
  ADCON1 = 0x80;        // Configure analog inputs and Vref 
  TRISA  = 0xFF;        // PORTA is input 
  RD1=1; 
  RB4=1;                //5V to sensor 

  // 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 

  delay(250);           //sensor module power up time 
  }

I don't know what code you are using to get a stack underflow error.
You must have something in your code that is causing recursion, like a function calling itself.
 

Re: changing a simple code from MikroC format to HI-TECH for

that doesn't make sense !
i just copied the code you gave me and pasted it in the MPLAB and started the simulator ! and the exact code is here :
Code:
#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>

//============================================================================
//   Configuration
//============================================================================

__CONFIG ( 0x3F32 );

//============================================================================
//   Function Prototype
//============================================================================

void ultrasonic(void);   // Find range based on input
void init(void);
void rangecal(unsigned int distance);
void ir_sensor(void); 
unsigned int Adc_Read(char channel);
void ad_function(void);
void delay(unsigned int count);


//============================================================================
//   global variable

unsigned int To = 0;
unsigned int sysclock;

//============================================================================
//   interrupt prototype
//============================================================================================================
 
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)
    sysclock++;
    }
  }

void delay(unsigned int count)
  {
  unsigned int delay = sysclock;

  while((sysclock - delay) < count){
    ;
    }
  }
 
//============================================================================
//   Main Function
//============================================================================

void main(void)
  {
  init();
  ultrasonic();
  }

// Initailization
// Description : Initialize the microcontroller
//============================================================================================================

void init()
  {

  TRISB = 0b00000110;   //set RB2 pin as input, other as output
  TRISC = 0b00000000;   //set PORTA as output
  TRISD = 0b00000000;
 
  ADCON1 = 0x80;        // Configure analog inputs and Vref
  TRISA  = 0xFF;        // PORTA is input
  RD1=1;
  RB4=1;                //5V to sensor

  // 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

  delay(250);           //sensor module power up time
  } 
  

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
 
void ultrasonic(void)     
  {   
  unsigned int distance, value, TH;
 
  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 = (unsigned int)(value * 1.75616);   // calculate inch value per inch = 147us with 20Mhz internal clock.   
      rangecal(distance);
      }
    ad_function();
    }                                                         
  }

void rangecal(unsigned int distance)
  {
  if(distance>300){
    PORTD=0B00000010;
    }
  else if(distance>270){    // 150cm
    PORTD=0B00010010;
    }
  else if(distance>215){    // 120cm
    PORTD=0B00100010;
    }
  else if(distance>140){    //80cm
    PORTD=0B01000010;
    }
  else if(distance>90){     //0 to 50cm
    PORTD=0B10000010;
    }       
  }

/*--- A/D function ---*/

void ad_function(void)
  {
  unsigned int ADC_result;

  ADC_result = Adc_Read(2);

  if(ADC_result <= 205){
    PORTC= 0b00010000;
    }
  else if((ADC_result >= 206) && (ADC_result <= 410)){
    PORTC= 0b00100000;
    }
  else{
    PORTC = 0;
    }
  }     

/*--- Read A/D conversion ---*/

unsigned int Adc_Read(unsigned char channel)
  {
  unsigned int result = 0U;
  unsigned char acquisition_time = 3U;

  ADCON1 = 0x80U;   /* 10 bit Right justified result, Vdd as ref */
  ADCON0 = 0x81U;   /* Conversion clock Fosc/32 */

  ADCON0 |= (unsigned char)(channel << 2U);  /* Select channel */
       
  while(acquisition_time--){ /* Sample channel */
    ;
    }
   
  ADGO = 1U;    /* Start conversion */
 
  while(ADGO){  /* Wait for conversion end */
    ;
    }
   
  result = ADRESH;
  result <<= 8U;
  result |= ADRESL;
  return result;
  }

/*--- End of File ---*/

i haven't changed a single letter

Added after 42 seconds:

and changing the delay place didn't hep also
 

Re: changing a simple code from MikroC format to HI-TECH for

I have just copy and pasted the code and compiled it in mplab.
Compiles ok.
I then chose mplab sim as the debugger, placed a breakpoint in the interrupt routine and ran the code.
It runs ok???

I dont get it.

Place breakpoints in the code and use the sim to step through and find out where yours falls over.
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

ok..i tried to open a new project in a new file and started the simulation again...i think im getting something...
the green arrow moves starting from the void main....as following :
Code:
void main(void)
  {
  init();
  ultrasonic();
  }

// Initailization
// Description : Initialize the microcontroller
//============================================================================================================

void init()
  {

  TRISB = 0b00000110;   //set RB2 pin as input, other as output
  TRISC = 0b00000000;   //set PORTA as output
  TRISD = 0b00000000;
 
  ADCON1 = 0x80;        // Configure analog inputs and Vref
  TRISA  = 0xFF;        // PORTA is input
  RD1=1;
  RB4=1;                //5V to sensor

  // 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

  delay(250);           //sensor module power up time


THEN IT JUMPS TO:

void delay(unsigned int count)
  {
  unsigned int delay = sysclock;

  while((sysclock - delay) < count){


AND THEN STOPS RIGHT THERE !

when it stops at the while((sysclock - delay) < count) line, this note occurs:
ADC-W0008: No stimulus file attached to ADRESL for A/D.


any idea what's that ?
 

Re: changing a simple code from MikroC format to HI-TECH for

No stimulus file attached to ADRESL for A/D.

The Debugger is quite sophisticated and powerful, you can attach stimulus files to it to simulate the peripherals. It is just saying that the A/D results wont be meaningful.
That wont matter.

It will stop at the delay because it has to delay,
Place a break point past the delay, double click a line to place a break point, a red dot. Then run to the break point. Also place a breakpoint in the interrupt routine to check its working.
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

ok the simulation worked but i think with some problems...which i think with the delay...i installed the program into my PIC16F877A and tried to apply voltage at one of PORTA pins and waited for voltage at PORTC.4 and PORTC.5....it didn't work for like 15 secs....then it worked...where when ever there is no voltage at portA; portc pins gives voltage, and vice-versa....but after a while the program stops again...and it doesn't work again unless i do reset for the microcontroller and wait for the 15 secs to finsh !! so i guess the problem is related to the Delay...or looping problem.
any suggestions ?
 

Re: changing a simple code from MikroC format to HI-TECH for

You only call the delay once in your program and then stay in this loop:
Code:
  {    
  unsigned int distance, value, TH; 
  
  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 = (unsigned int)(value * 1.75616);   // calculate inch value per inch = 147us with 20Mhz internal clock.    
      rangecal(distance); 
      } 
    ad_function(); 
    }                                                          
  }

Are you sure this line is right?

if(RB2 == 0) //if RB2 is high

High would normally be a 1.

Also, you keep setting the timer to 0, so the interrupt wont happen and sysclock wont increment so the delay wont work.
But that shouldnt matter as you dont call it again.

At least the code is running, now you have to debug it and get it woking properly.
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

in this line : if(RB2 == 0) //if RB2 is high
the mistake is in the comment not the code, so instead of high; it should be low.

" Also, you keep setting the timer to 0, so the interrupt wont happen and sysclock wont increment so the delay wont work.
But that shouldnt matter as you dont call it again. "

so what should i do in this case ? should i use another timer for example Timer-1 ????
 

Re: changing a simple code from MikroC format to HI-TECH for

Yes, I would use Timer 1 as a free running timer. It's also a 16-bit timer so you could get rid of the To variable.
Don't use the timer 1 interrupt. Set it up like so, with whatever pre-scale value you need.

Code:
/*--- Initialise Timer 1 ---*/

static void init_timer1(void)
  {
  T1CON = 0x30; /* Prescale 1:8 */
  TMR1IF = 0;
  TMR1IE = 0; 
  TMR1ON = 1; 
  }


Then, if you pre-load timer 0 to generate a 1mS interrupt, assuming a 20MHz xtal, you have an accurate 1mS delay routine.

Code:
void interrupt isr(void) 
  { 
  if(TMR0IF)      // TMR0 is overflow 
    { 
    TMR0IF = 0;   // clear flag bit 
    sysclock++; 
    TMR0 = 0xeb;  /* Pre-load for 1mS interrupt */
    } 
  }
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

so do adding those subroutines to the code directly will do ??

Code:
#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>

//============================================================================
//   Configuration
//============================================================================

__CONFIG ( 0x3F32 );

//============================================================================
//   Function Prototype
//============================================================================

void ultrasonic(void);   // Find range based on input
void init(void);
void rangecal(unsigned int distance);
void ir_sensor(void); 
unsigned int Adc_Read(char channel);
void ad_function(void);
void delay(unsigned int count);


//============================================================================
//   global variable

unsigned int To = 0;
unsigned int sysclock;

//============================================================================
//   interrupt prototype
//============================================================================================================
 
void interrupt isr(void)
  {
  if(TMR0IF)      // TMR0 is overflow
    {
    TMR0IF = 0;   // clear flag bit
    sysclock++;
    TMR0 = 0xeb;  /* Pre-load for 1mS interrupt */
    }
  } 
/*--- Initialise Timer 1 ---*/

static void init_timer1(void)
  {
  T1CON = 0x30; /* Prescale 1:8 */
  TMR1IF = 0;
  TMR1IE = 0;
  TMR1ON = 1;
  } 

void delay(unsigned int count)
  {
  unsigned int delay = sysclock;

  while((sysclock - delay) < count){
    ;
    }
  }
 
//============================================================================
//   Main Function
//============================================================================

void main(void)
  {
  init();
  ultrasonic();
  }

// Initailization
// Description : Initialize the microcontroller
//============================================================================================================

void init()
  {
  TRISB = 0b00000110;   //set RB2 pin as input, other as output
  TRISC = 0b00000000;   //set PORTA as output
  TRISD = 0b00000000;
  
  ADCON1 = 0x80;        // Configure analog inputs and Vref
  TRISA  = 0xFF;        // PORTA is input
  RD1=1;
  RB4=1;                //5V to sensor

  // 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
  delay(250);           //sensor module power up time
  }

//================================================================================
// FUNCTIONS
//================================================================================   
//**************************************************
//Calculate the range from the sensor depending on PWM signal input
 
void ultrasonic(void)     
  {   
  unsigned int distance, value, TH;
 
  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 = (unsigned int)(value * 1.75616);   // calculate inch value per inch = 147us with 20Mhz internal clock.   
      rangecal(distance);
      }
    ad_function();
    }                                                         
  }

void rangecal(unsigned int distance)
  {
  if(distance>300){
    PORTD=0B00000010;
    }
  else if(distance>270){    // 150cm
    PORTD=0B00010010;
    }
  else if(distance>215){    // 120cm
    PORTD=0B00100010;
    }
  else if(distance>140){    //80cm
    PORTD=0B01000010;
    }
  else if(distance>90){     //0 to 50cm
    PORTD=0B10000010;
    }       
  }

/*--- A/D function ---*/

void ad_function(void)
  {
  unsigned int ADC_result;

  ADC_result = Adc_Read(2);

  if(ADC_result <= 205){
    PORTC= 0b00010000;
    }
  else if((ADC_result >= 206) && (ADC_result <= 410)){
    PORTC= 0b00100000;
    }
  else{
    PORTC = 0;
    }
  }     

/*--- Read A/D conversion ---*/

unsigned int Adc_Read(unsigned char channel)
  {
  unsigned int result = 0U;
  unsigned char acquisition_time = 3U;

  ADCON1 = 0x80U;   /* 10 bit Right justified result, Vdd as ref */
  ADCON0 = 0x81U;   /* Conversion clock Fosc/32 */

  ADCON0 |= (unsigned char)(channel << 2U);  /* Select channel */
       
  while(acquisition_time--){ /* Sample channel */
    ;
    }
   
  ADGO = 1U;    /* Start conversion */
 
  while(ADGO){  /* Wait for conversion end */
    ;
    }
   
  result = ADRESH;
  result <<= 8U;
  result |= ADRESL;
  return result;
  }

/*--- End of File ---*/


????
 

Re: changing a simple code from MikroC format to HI-TECH for

I have modified your capture routine, I don't know if it will work not having the hardware to test it on?

You are not making full use of the peripherals in the Micro that are designed to support applications.
I suggest you carefully read the data sheet about the capture/compare module.
This does in hardware what you are trying to achieve, and much more accuratly than polling an input pin.

Code:
#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> 

//============================================================================ 
//   Configuration 
//============================================================================ 

__CONFIG ( 0x3F32 ); 

//============================================================================ 
//   Function Prototype 
//============================================================================ 

void ultrasonic(void);   // Find range based on input 
void init(void); 
void rangecal(unsigned int distance); 
void ir_sensor(void); 
unsigned int Adc_Read(char channel); 
void ad_function(void); 
void delay(unsigned int count);
void init_timer1(void); 


//============================================================================ 
//   global variable 

unsigned int To = 0; 
unsigned int sysclock; 

//============================================================================ 
//   interrupt prototype 
//============================================================================================================ 

/*--- Initialise Timer 1 ---*/ 

void init_timer1(void) 
  { 
  T1CON = 0x30; /* Prescale 1:8 */ 
  TMR1IF = 0; 
  TMR1IE = 0; 
  TMR1ON = 1; 
  }
  
void interrupt isr(void) 
  { 
  if(TMR0IF)      // TMR0 is overflow 
    { 
    TMR0IF = 0;   // clear flag bit 
    sysclock++; 
    TMR0 = 0xeb;  /* Pre-load for 1mS interrupt */
    } 
  } 

void delay(unsigned int count) 
  { 
  unsigned int delay = sysclock; 

  while((sysclock - delay) < count){ 
    ; 
    } 
  } 
  
//============================================================================ 
//   Main Function 
//============================================================================ 

void main(void) 
  { 
  init_timer1();
  init(); 
  ultrasonic(); 
  } 

// Initailization 
// Description : Initialize the microcontroller 
//============================================================================================================ 

void init() 
  { 
  TRISB = 0b00000110;   //set RB2 pin as input, other as output 
  TRISC = 0b00000000;   //set PORTA as output 
  TRISD = 0b00000000; 
  
  ADCON1 = 0x80;        // Configure analog inputs and Vref 
  TRISA  = 0xFF;        // PORTA is input 
  RD1=1; 
  RB4=1;                //5V to sensor 

  // 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 

  delay(250);           //sensor module power up time 
  } 
  

//================================================================================ 
// FUNCTIONS 
//================================================================================    
//************************************************** 
//Calculate the range from the sensor depending on PWM signal input 
  
void ultrasonic(void)      
  {    
  unsigned int distance, value; 
  
  while(1) 
    {    
    while(RB2 == 0){
      ;
      }

    TMR1L = 0;
    TMR1H = 0;
    TMR1ON = 1;

    while(RB2 == 1){
      ;
      }

    TMR1ON = 0;
    value = TMR1H;
    value <<= 8U;
    value |= TMR1L;

    distance = (unsigned int)(value * 1.75616);   // calculate inch value per inch = 147us with 20Mhz internal clock.    
    rangecal(distance); 
    ad_function(); 
    }                                            
  } 

void rangecal(unsigned int distance) 
  { 
  if(distance>300){ 
    PORTD=0B00000010; 
    } 
  else if(distance>270){    // 150cm 
    PORTD=0B00010010; 
    } 
  else if(distance>215){    // 120cm 
    PORTD=0B00100010; 
    } 
  else if(distance>140){    //80cm 
    PORTD=0B01000010; 
    } 
  else if(distance>90){     //0 to 50cm 
    PORTD=0B10000010; 
    }        
  } 

/*--- A/D function ---*/ 

void ad_function(void) 
  { 
  unsigned int ADC_result; 

  ADC_result = Adc_Read(2); 

  if(ADC_result <= 205){ 
    PORTC= 0b00010000; 
    } 
  else if((ADC_result >= 206) && (ADC_result <= 410)){ 
    PORTC= 0b00100000; 
    } 
  else{ 
    PORTC = 0; 
    } 
  }      

/*--- Read A/D conversion ---*/ 

unsigned int Adc_Read(unsigned char channel) 
  { 
  unsigned int result = 0U; 
  unsigned char acquisition_time = 3U; 

  ADCON1 = 0x80U;   /* 10 bit Right justified result, Vdd as ref */ 
  ADCON0 = 0x81U;   /* Conversion clock Fosc/32 */ 

  ADCON0 |= (unsigned char)(channel << 2U);  /* Select channel */ 
        
  while(acquisition_time--){ /* Sample channel */ 
    ; 
    } 
    
  ADGO = 1U;    /* Start conversion */ 
  
  while(ADGO){  /* Wait for conversion end */ 
    ; 
    } 
    
  result = ADRESH; 
  result <<= 8U; 
  result |= ADRESL; 
  return result; 
  } 

/*--- End of File ---*/
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

I have tested the software on my hardware...
the IR part (which is the ADC conversion part) is working very well.
but the ultrasonic sensor part is not working at all...it used to work b4 the last adjustment.

this is the ultrasonic sensor part that used to work:
Code:
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>300)
      {
      PORTD=0B00000010;
      }
	  else if(distance>270)      // 150cm
      {
      PORTD=0B00010010;
      }
      else if(distance>215)      // 120cm
      {
      PORTD=0B00100010;
      }
      else if(distance>140)      //80cm
      {
      PORTD=0B01000010;
      }
      else if(distance>90)      //0 to 50cm
      {
      PORTD=0B10000010;
      }
      
}

and the is the latest part with the adjustments which didnt work:
Code:
void ultrasonic(void)     
  {   
  unsigned int distance, value;
 
  while(1)
    {   
    while(RB2 == 0){
      ;
      }

    TMR1L = 0;
    TMR1H = 0;
    TMR1ON = 1;

    while(RB2 == 1){
      ;
      }

    TMR1ON = 0;
    value = TMR1H;
    value <<= 8U;
    value |= TMR1L;

    distance = (unsigned int)(value * 1.75616);   // calculate inch value per inch = 147us with 20Mhz internal clock.   
    rangecal(distance);
    ad_function();
    }                                           
  }

void rangecal(unsigned int distance)
  {
  if(distance>300){
    PORTD=0B00000010;
    }
  else if(distance>270){    // 150cm
    PORTD=0B00010010;
    }
  else if(distance>215){    // 120cm
    PORTD=0B00100010;
    }
  else if(distance>140){    //80cm
    PORTD=0B01000010;
    }
  else if(distance>90){     //0 to 50cm
    PORTD=0B10000010;
    }       
  }

i was trying to adjust the code but it didnt work out very well for me.
 

Re: changing a simple code from MikroC format to HI-TECH for

Try reversing the high/low from this:

Code:
   while(RB2 == 0){ 
      ; 
      } 

    TMR1L = 0; 
    TMR1H = 0; 
    TMR1ON = 1; 

    while(RB2 == 1){ 
      ; 
      } 

    TMR1ON = 0; 
    value = TMR1H; 
    value <<= 8U; 
    value |= TMR1L;

To this:

Code:
   while(RB2 == 1){ 
      ; 
      } 

    TMR1L = 0; 
    TMR1H = 0; 
    TMR1ON = 1; 

    while(RB2 == 0){ 
      ; 
      } 

    TMR1ON = 0; 
    value = TMR1H; 
    value <<= 8U; 
    value |= TMR1L;
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

no...still the same...IR working...ultrasonic not
:|
 

Re: changing a simple code from MikroC format to HI-TECH for

If you explain how this works, we can fix it.
The program stays in this loop.

while(1)
{

if(RB2==0) /* if RB2 is at 0 */
{
TMR0=0; /* Clear the counters */
To=0;
}
else
{
TH=TMR0+To; /* If RB2 is at 1, use counter value to calculate distance */
value=TH;
distance=value*1.75616;
rangecal();
}
}

The thing is, as soon as RB2 goes high, you use the counter values, but the counter has not had time to count, because as soon as it goes high, you use the the value.

It hasnt had time to count anything??????

While it is high, the value will be increasing until it goes low again.
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

RB2 is the PWM output of the ultrasonic sensor.
the timer start counting at the first edge of the digital siganl

_|''''''''''''''''''''''''''|___|''''''''''''''''''''''''''''|___|''''''''''''''''''''''''''''''''''|___|'''''''''''''''''''''''''''''''

and take the value at the fall of the signal, and then send it to the distance equation.
is there's any other way than using Timer1 to make both sensors work at the same time ?
 

Re: changing a simple code from MikroC format to HI-TECH for

Well this code should work?

Code:
 while(RB2 == 0){    /* Wait for RB2 to go high */ 
      ; 
      } 

    TMR1L = 0; 
    TMR1H = 0; 
    TMR1ON = 1;        /* Zero the counter, start counting */

    while(RB2 == 1){   /* Wait for RB2 to go low */ 
      ; 
      } 

    TMR1ON = 0;        /* Stop the counter, save the counter value */
    value = TMR1H; 
    value <<= 8U; 
    value |= TMR1L;

Maybe the count is wrong and the timer is overflowing, you could change timer 1 pre-scaler to suit the expexcted pulse widths.

The capture/compare module in the Pic is designed to do this sort of thing in hardware using interrupts. You should use that. It will be more accurate than polling the input pins.
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Re: changing a simple code from MikroC format to HI-TECH for

your telling a 7 years old kid to drive a car...if u know what i mean

 

Re: changing a simple code from MikroC format to HI-TECH for

If you have a debugger, such as ICD2, you could set a breakpoint to break after RB2 goes low and see what value is in the counter.

If you can see what value you are getting it will help you debug it.

If you looked at Microchip application notes, I'm sure you would find example code that measures a PWM duty cycle. using the capture compare module, thats whats its designed to do, among other things.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…