Code check please!

Thread Starter

Ranayh

Joined May 6, 2013
8
hey everyone,

i was wondering if someone would help me make sure that these 2 codes are the same. one was meant to be compiled on mpLAB but i redjusted it a little to fit on mikroC. can some1 just make sure its right and not missing anything important? thank uu!!!

Original code:

Rich (BB code):
#include <p18f452.h>
#include <delays.h>
#include <adc.h>
#include <math.h>

#define gravity_ss 512
#define display_divider 20

//PORTD - 0bBGCDHAEF
#define one 	0b10100000
#define two 	0b11010110
#define three 	0b11110100
#define four 	0b11100001
#define five 	0b01110101
#define six 	0b01110111
#define seven 	0b10100100
#define eight 	0b11110111
#define nine 	0b11110101
#define zero 	0b10110111
#define blank   0b00000000

void update_char_display(int *char_disp)
{
	int i=0;
	int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
	for(i=0;i<3;i++)
	{

		switch(i)
		{
		case 0 :	PORTB = all_digits[char_disp] + 0b1000;
			break;
		case 1 :	PORTD = all_digits[char_disp];
			break;
		case 2 :	PORTC = all_digits[char_disp];
			break;
		default :
			break;
		}
	}

}

void main(void)
{

int char_disp[3];

int current_result, past_result = 0;
int g_val, temp = 0;

int i=0;

TRISA = 0xFF;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
				//Turn All Displays Off Initially
PORTB = blank;		//PORTB -> Display 1
PORTC = blank;		//PORTC -> Display 2
PORTD = blank;		//PORTD -> Display 3

					//Initialize A/D Converter
OpenADC( ADC_FOSC_32 
		& ADC_RIGHT_JUST 
		& ADC_8ANA_0REF, 
		ADC_CH0 & ADC_INT_OFF );

while(1){

update_char_display(char_disp);
Delay1KTCYx(250);


Delay10TCYx(5);
ConvertADC();
while( BusyADC() );
	current_result = ReadADC();

temp = current_result - past_result;

	if( temp > 2 || temp < -2 )
	{
	past_result = current_result;
	g_val = current_result - gravity_ss;
	g_val = g_val << 5;
	g_val = g_val / display_divider;

	if(g_val < 0)
		g_val = g_val * -1;

	i = 2;
	}
	while(i!= 255)
	{
		char_disp=g_val%10;
		g_val = g_val/10;
		i--;
	}

}



}


MikroC code:

Rich (BB code):
#device PIC18F452

#define gravity_ss 512
#define display_divider 20

//PORTD - 0bBGCDHAEF
#define one    0b10100000
#define two    0b11010110
#define three    0b11110100
#define four    0b11100001
#define five    0b01110101
#define six    0b01110111
#define seven    0b10100100
#define eight    0b11110111
#define nine    0b11110101
#define zero    0b10110111
#define blank   0b00000000

void update_char_display(int *char_disp)
{
   int i=0;
   int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
   for(i=0;i<3;i++)
   {

      switch(i)
      {
      case 0 :   PORTB = all_digits[char_disp] + 0b1000;
         break;
      case 1 :   PORTD = all_digits[char_disp];
         break;
      case 2 :   PORTC = all_digits[char_disp];
         break;
      default :
         break;
      }
   }

}

void main(void)
{

int char_disp[3];

int current_result, past_result = 0;
int g_val, temp = 0;

int i=0;

TRISA = 0xFF;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
            //Turn All Displays Off Initially
PORTB = blank;      //PORTB -> Display 1
PORTC = blank;      //PORTC -> Display 2
PORTD = blank;      //PORTD -> Display 3

               //Initialize A/D Converter
OpenADC( ADC_FOSC_32 
      & ADC_RIGHT_JUST 
      & ADC_8ANA_0REF, 
      ADC_CH0 & ADC_INT_OFF );

while(1){

update_char_display(char_disp);
Delay1KTCYx(250);


Delay10TCYx(5);
ConvertADC();
while( BusyADC() );
   current_result = ReadADC();

temp = current_result - past_result;

   if( temp > 2 || temp < -2 )
   {
   past_result = current_result;
   g_val = current_result - gravity_ss;
   g_val = g_val << 5;
   g_val = g_val / display_divider;

   if(g_val < 0)
      g_val = g_val * -1;

   i = 2;
   }
   while(i!= 255)
   {
      char_disp=g_val%10;
      g_val = g_val/10;
      i--;
   }

}



}


 

kubeek

Joined Sep 20, 2005
5,795
Actually, what exactly have you done to adapt the code? As far as diff tells me, you only deleted these three lines:
#include <delays.h>
#include <adc.h>
#include <math.h>
Have you at the very least tried to compile the code?
And to answer your question, you are missing three important libraries that are essential for the code to work. Ok make that two as I don´t see anything that needs math.h, but still.
 

Thread Starter

Ranayh

Joined May 6, 2013
8
Actually, what exactly have you done to adapt the code? As far as diff tells me, you only deleted these three lines:
#include <delays.h>
#include <adc.h>
#include <math.h>
Have you at the very least tried to compile the code?
And to answer your question, you are missing three important libraries that are essential for the code to work. Ok make that two as I don´t see anything that needs math.h, but still.
sorry i copied the wrong mikroC code. this is the correct one.
i only changed the parts related to the ADC initialization and read. i wana make sure i theres nothing missing that i couldve deleted to remove errors to build it.

Rich (BB code):
#define gravity_ss 512
#define display_divider 20

//PORTD - 0bBGCDHAEF
#define one         0b10100000
#define two         0b11010110
#define three         0b11110100
#define four         0b11100001
#define five         0b01110101
#define six         0b01110111
#define seven         0b10100100
#define eight         0b11110111
#define nine         0b11110101
#define zero         0b10110111
#define blank   0b00000000

void update_char_display(int *char_disp)
{
        int i=0;
        int all_digits[11] = {zero, one, two, three, four, five, six, seven, eight, nine};
        for(i=0;i<3;i++)
        {

                switch(i)
                {
                case 0 :        PORTB = all_digits[char_disp] + 0b1000;
                        break;
                case 1 :        PORTD = all_digits[char_disp];
                        break;
                case 2 :        PORTC = all_digits[char_disp];
                        break;
                default :
                        break;
                }
        }

}

void main(void)
{

int char_disp[3];

int current_result=0;
int past_result = 0;
int g_val, temp = 0;

int i=0;

TRISA = 0xFF;
TRISB = 0x00;
TRISC = 0x00;
TRISD = 0x00;
                                //Turn All Displays Off Initially
PORTB = blank;                //PORTB -> Display 1
PORTC = blank;                //PORTC -> Display 2
PORTD = blank;                //PORTD -> Display 3

                                        //Initialize A/D Converter
ADC_Init();
while(1){

update_char_display(char_disp);
Delay_ms(250);


Delay_ms(5);

while( 1 );
        current_result = ADC_read(1);
temp = current_result - past_result;

        if( temp > 2 || temp < -2 )
        {
        past_result = current_result;
        g_val = current_result - gravity_ss;
        g_val = g_val << 5;
        g_val = g_val / display_divider;

        if(g_val < 0)
                g_val = g_val * -1;

        i = 2;
        }
        while(i!= 255)
        {
                char_disp=g_val%10;
                g_val = g_val/10;
                i--;
        }

}



}
 

kubeek

Joined Sep 20, 2005
5,795
i never really used mikroc to be honest, doesn´t it need some header files too?
The only importatnt thing that has changed is that in the old code the main loop waited for the adc to be done with the last conversion, and now it doesn´t. Or is ADC_read(1) a blocking function?
 

ErnieM

Joined Apr 24, 2011
8,377
i never really used mikroc to be honest, doesn´t it need some header files too?
Mikro C does indeed secretly includes the most vital of include files: the individual device header. Actually, they include it into the project as a dot C file.
 

Brownout

Joined Jan 10, 2012
2,390
You can use comparison programs, such as "Beyond Compare" to see what differences there are between two source files. I think it might be a free program, not sure.

If you use Linux, you get "diff" as part of the package for comparison.
 

kubeek

Joined Sep 20, 2005
5,795
In windows, I use torotiseGIT for versioning, which comes with tortoiseDiff, a nice tool to compare versions graphically.
 
Top