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.

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

Status
Not open for further replies.

RobotHeart

Junior Member level 3
Junior Member level 3
Joined
Apr 8, 2010
Messages
25
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Malaysia
Activity points
1,505
Hello every1....this is my 1st post in this forum, and i hope i can get some help.

Im using PIC16F877A MCU, and I want to change the format of a code which do the following:
1/ convert the analog voltage received in PortA.2 (pin 4) to digital.
2/ if the analog voltage is less than 2V and more than 1V...set PortC.4 (pin 23).
3/ If analog voltage is less than 1V...set PortC.5 (pin 24).

thats it !!!!
the code i have works in MikroC and its working very well...i have already tested it in my hradware.....but i want to make it work in MPLAB (HI-TECH)...the code i have is :

Code:
unsigned int t;

 void main() {
  ADCON1 = 0x80;  // Configure analog inputs and Vref
  TRISA  = 0xFF;  // PORTA is input
   TRISC  = 0;     // PORTD is output

    while (1)

     t  = Adc_Read(2);                        // get ADC value from 2nd channel

     if(t >= 0 & t <= 410){
     PORTC= 0b00010000;
     }
     else if (t >= 411 & t <= 615 ){
     PORTC= 0b00100000;
                     }
     else PORTC = 0;     }
    }

my main code actually is in MPLAB format....thats why i want to change it.
my main code is (no need for changes...only to help understanding):

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>
#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 = 0b00010001;      //set PORTA as output
   TRISB = 0b00000110;      //set RB2 pin as input, other as output
   TRISC = 0b00000000;      //set PORTA as output
   TRISD = 0b00000000;
  
   RD1=1;
   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>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;
      }
      
}

This code works on MPLAB....so i want to add the first code to this one.
please help...im running out of time here.
thank you
 

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

If your code is working in MikroC then it truly is a miracle compiler!
It can not be what you posted as it has errors.

Code:
unsigned int t; 

void main() 
  { 
  ADCON1 = 0x80;  // Configure analog inputs and Vref 
  TRISA  = 0xFF;  // PORTA is input 
  TRISC  = 0;     // PORTD is output 

  while (1) 
    t  = Adc_Read(2);   // get ADC value from 2nd channel  /* Infinite loop? */

  /* The program never gets this far ??? */    

  if(t >= 0 & t <= 410){  /* This expression won't work! */ 
    PORTC= 0b00010000;
    } 
  else if(t >= 411 & t <= 615 ){  /* This expression won't work! */  
    PORTC= 0b00100000; 
    } 
  else PORTC = 0;     
  } 
  }  /* Extra bracket? */

'if(t >= 0 & t <= 410)' should be 'if((t >= 0) && (t <= 410))'

'else if(t >= 411 & t <= 615 )' should be 'else if((t >= 411) && (t <= 615))'
 

    RobotHeart

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

sorry for the mistakes....the code below is the exact one i used.... i need to trnasfare it to MPLAB format...
Code:
unsigned int t;

 void main() {
  ADCON1 = 0x80;  // Configure analog inputs and Vref
  TRISA  = 0xFF;  // PORTA is input
   TRISC  = 0;     // PORTD is output


    while (1) {
     t  = Adc_Read(2);                        // get ADC value from 2nd channel

     if(t >= 0 && t <= 410){
     PORTC= 0b00010000;
     }
     else if (t >= 411 && t <= 615 ){
     PORTC= 0b00100000;
                     }
     else PORTC = 0;     }
     }


thank you
 

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

Your conditional if, else if, expressions won't work the way you intend.
You need more brackets.
The following code should compile ok with Hi-Tech in mplab.

Code:
unsigned int t; 

void main() 
  { 
  ADCON1 = 0x80;  // Configure analog inputs and Vref 
  TRISA  = 0xFF;  // PORTA is input 
  TRISC  = 0;     // PORTD is output 

  while(1) 
    { 
    t  = Adc_Read(2);           // get ADC value from 2nd channel 

    if((t >= 0) && (t <= 410)){ 
      PORTC = 0b00010000; 
      } 
    else if((t >= 411) && (t <= 615)){ 
      PORTC = 0b00100000; 
      } 
    else{ 
      PORTC = 0;     
      } 
    }
  }
 

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

ok..what about the adc_read(2) ? is it going to" work " in MPLAB ? i mean does MPLAB understand it ?
because i heard that MPLAB uses totally different code to do ADC conversion.

besides it might compile successfully in HI-TECH sometimes, but when it come to hardware testing...nothing works.
ok this is the code after i added the modifications you gave me, (note the void IR_detection (void); line in functions prototype)

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>
#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);
void IR_detection (void);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;

//============================================================================
//   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 = 0b00010001;      //set PORTA as output
   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
  TRISC  = 0;     // PORTD is output 

   RD1=1;
   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>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;
      }
}
 void IR_detection (void) {

     while(1)
    {
    t  = Adc_Read(2);           // get ADC value from 2nd channel

    if ((t >= 0) && (t <= 410)) {
      PORTC = 0b00010000;
      }
    else if((t >= 411) && (t <= 615)){
      PORTC = 0b00100000;
      }
    else{
      PORTC = 0;     
      }
    }
 }

im still having some compiling problems though, what do you think ?

thank for helping
 

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

adc_read(2) is most probably a MikroC librbary function.
You will have to write an equivalent function for it to work.
If you are using Hi-Tech compiler look in the samples directory. There you will find sample code to do an A/D reading. It is quite simple.
I cant see in the code where you call your function?
Post the compiler errors you are getting.
 

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

this is the main reason of starting this topic....i need someone to help me converting this format with the help of the MPLAB directory cuz first; i dont really have enough time (busy with reports) to search in the directory, plus i really dont know how to use this directory, so if its simple...please help me out.

the errors are:

1/ Warning [361] .................... function declared implicit int ( at line : t = Adc_Read(2); // get ADC value from 2nd channel ).


2/ Warning [765] .................... degenerate unsigned comparison (at line : if ((t >= 0) && (t <= 410)) )

please help....once in my life...i want to ask for a favor in forums (a software help) n get the final results that i need....
 

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

1/ Warning [361] .................... function declared implicit int ( at line : t = Adc_Read(2); // get ADC value from 2nd channel ).

This means that the function doesn't exist, you will have to write it.

2/ Warning [765] .................... degenerate unsigned comparison (at line : if ((t >= 0) && (t <= 410)) )

This means that the comparison (t >= 0) will always be true, as an unsigned variable can never be less than 0.

There are a few other funnies in your code that need looking at.
The only way to learn is to do.
 

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

before i start this topic i tried my best to solve the problem, i knew that the adc_read(2) doesn't work in MPLAB, thats why i kept searching for the replacement code in MPLAB, and i found it...and i had problems when trying to compiling it...and i solved those problem...program build successfully, but after i install in my PIC, i dont get results...not like the code done in MikroC which work on my hardware with no problems.
the best code that i could do after replacing the MikroC formate with MPLAB fromat is as following :
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>
#include "delay.h"
#include "delay.c"
#define START_CONVERSION ADGO = 1
#define ADC_BUSY ADGO

//============================================================================
//   Configuration
//============================================================================
__CONFIG ( 0x3F32 );

//============================================================================
//   Function Prototype
//============================================================================
void ultrasonic(void);   // Find range based on input
void init(void);
void rangecal(void);
void ir_sensor(void);
void Init_ADC(void);
unsigned int Start_ADC_conversion(void);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int ADC_result;

//============================================================================
//   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)
   
   TRISB = 0b00000110;      //set RB2 pin as input, other as output
   TRISC = 0b00000000;      //set PORTA as output
   TRISD = 0b00000000;
   DelayMs(250);  //sensor module power up time
   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
}

//================================================================================
// 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>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;
      }
      
}


void Init_ADC(void)
{
ADCON0 = 0X81;
ADCON1 = 0X80; //RA0 analog,RA3 analog reference, rest digital IO's
}
//*******************************************************************
// This function converts the ADC Channel 0(RA0)
// to its equivalent digital output
//*******************************************************************
unsigned int Start_ADC_conversion(void)
{
unsigned int ADC_result;

START_CONVERSION;
while(ADC_BUSY){
ADC_result = ADRESH;
ADC_result = (ADC_result << ADRESL);
while (1) {
     if(ADC_result >= 0 & ADC_result <= 205){
     PORTC= 0b00010000;
     }
     else if (ADC_result >= 206 & ADC_result <= 410 ){
     PORTC= 0b00100000;
                     }
     else PORTC = 0;     
}
return(ADC_result);
}
}

this code is compiled successfully as mention previously...but it doesn't work in my hardware...means i dont get any output in portC.4 and portC.5 when an analog input is applied in portA.2.

i am really trying my best here, im not that lazy guy who want everything ready, but i just went straight to the point....well i guess in the end i had to explain everythin in details...

can anyone tell me now what is wrong with this code ????? why it doesn't work on my hardware...why i dont have output in portC.4 and portC.5 when an analog input is applied in portA.2 ??????????????????

tq
 

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

You could define your A/D conversion like this.

Code:
unsigned int Adc_Read(char channel);

/*--- 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 */
    ;
    }
    
  GODONE = 1U;    /* Start conversion */
  
  while(GODONE){  /* Wait for conversion end */
    ;
    }
    
  result = ADRESH;
  result <<= 8U;
  result |= ADRESL;
  return result;
  }

Below is your code to which I have added some comments as to where it goes wrong.

Code:
unsigned int Start_ADC_conversion(void) 
{ 
unsigned int ADC_result; 

START_CONVERSION; 

while(ADC_BUSY){ 

/* You are reading the result while the conversion is in progress! */

ADC_result = ADRESH; 
ADC_result = (ADC_result << ADRESL); 

/* This starts an infinate loop that  never ends */

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

/* Code never gets here */

return(ADC_result); 
} 
}

eda board is here to help, but as you know, you don't always have the time to do as much as you would like.

Added after 39 minutes:

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

//================================================================================ 
// 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 */ 
    ; 
    } 
    
  GODONE = 1U;    /* Start conversion */ 
  
  while(GODONE){  /* 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

ok it means that my code actually reads the output will the conversion is in progress ! i dont think correcting my code will be easy if i want to make it work the way i want !
lets try your code....i have added your code to mine as u can c below:


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>
#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);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;

//============================================================================
//   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 = 0b00010001;      //set PORTA as output
   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
  TRISC  = 0;     // PORTD is output 

   RD1=1;
   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>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;
      }
}


  
unsigned int Adc_Read(char channel);

/*--- 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 */
    ;
    }
   
  GODONE = 1U;    /* Start conversion */
 
  while(GODONE){  /* Wait for conversion end */
    ;
    }
   
  result = ADRESH;
  result <<= 8U;
  result |= ADRESL;

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

  return result;


}

the code was compiled with 1 error, which says: (Error [192]............... undefined identifier "GODONE" ) at the line : GODONE = 1U; /* Start conversion */

do u think the way i added your code to mine is acceptable ?? especially the while (1) part

thank you for giving me your precious time
 

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

Change 'GODONE' to 'ADGO'.

Your code wont work.
I cant see in the program where you call the A/D function?
If you did call it, it would never return as you have a never ending loop?

while (1) {
if(result >= 0 & result <= 205){ Ive told you this wont work!!! its crap.
PORTC= 0b00010000;
}
else if (result >= 206 & result <= 410 ){ Ive told you, this wont work, more crap!
PORTC= 0b00100000;
}
else PORTC = 0;
}

Try the full program code that I have posted or at least look at it.
I think it will do what you want it to.

Use mplab sim to step through your code, you can then see whats going on.

Don't thank me, press the helped me button.
 

    RobotHeart

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

how about now.....

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>
#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);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;

//============================================================================
//   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 = 0b00010001;      //set PORTA as output
   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
  TRISC  = 0;     // PORTD is output

   RD1=1;
   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>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;
      }
}


 
unsigned int Adc_Read(char channel);

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

while(1)
    {
    t  = Adc_Read(2);           // get ADC value from 2nd channel

    if((t >= 0) && (t <= 410)){
      PORTC = 0b00010000;
      }
    else if((t >= 411) && (t <= 615)){
      PORTC = 0b00100000;
      }
    else{
      PORTC = 0;     
      } 
}
  return result;
}

:|

if still wrong...can show me the right thing ?
 

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

No good!!!!!

Delete your code and try this!
It is your code tidied up a bit.
Copy and paste it!


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

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

    RobotHeart

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

your code is compiled successfully, but it didnt work on my hardware...didnt get any reading on portC.4 and portC.5
also i tried to change the code from the one u just gave me to this one:

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>
#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);
void IR_detection (void);
unsigned int Adc_Read(char channel);
unsigned int Adc_Read(unsigned char channel);
//============================================================================
//   global variable
unsigned int To=0;
unsigned int TH=0;
unsigned int value;
unsigned int distance;
unsigned int data=0;
unsigned int t;
unsigned int ADC_result;
  unsigned int result;

//============================================================================
//   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 = 0b00010001;      //set PORTA as output
   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
  TRISC  = 0;     // PORTD is output

   RD1=1;
   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>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;
      }
}


 
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;
  } 

void ad_function(void)
  {
  unsigned int ADC_result;



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

which was compiled successfully also...any idea whats wrong ?? :cry:
 

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

You haven't used my code?????

Your code continuously loops in the ultrasonic(); function, so you need to call the ad_function() from this function.

Your code doesn't call the function???????

Have you used mplab sim to step through the code?
It is a very powerful and useful debugger.
You need to use the debugger to see if the code is doing what you expect.

I cant see a problem with the code I posted.
 

    RobotHeart

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

i think the code you just gave me still contains the Adc_Read(2) which doesn't work in MPLAB :) plus the ADC_result (which is changed to result on the following subroutine which is Read A/D conversion function, thats why i thought about placing this part of the code at the bottom (after the Read A/D conversion function)...but it seems its doesn't work like that.
is it difficult to use the simulator in MPLAB ? never tried it b4
 
Re: changing a simple code from MikroC format to HI-TECH for

The A/D code I gave you works fine, I have used it many times, it is part of my library of peripheral routines.
I just changed the name of the function to match what you were using!

To use the debugger,
Just go to debugger menu option and choose mplab sim.
You can set breakpoints by double clicking, you can step through the code.
Try it, it's easy, but very powerful.
 

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

alright then....i'll try the debugger...but just to make sure...is this part of the code you gave me is correct? just to double check with you

Code:
.
.
.
/*--- 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;
    }
  }     
.
.
.

??
 

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

Looks ok to me!
 

    RobotHeart

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top