[HARD] [SOFT] help understand a simple C program

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello , I just got started with AVR , and I am building a Lan tester for a project

My hardware part is done basically for the send pins I used port A , from A0 to A7 and for the Receiver port C when the cable is inserted in the RJ jack a signal should be sent and compared to

some existing vectors , the result of the vector comparison is sent to port B and a led corresponding to the cable type should turn on

here is the code,I commented the parts that I cant understand, please help me with them


#define DSEND				DDRA
#define DRECEIVE			DDRC
#define DLED				DDRD
#define ISEND				PINA
#define IRECEIVE			PINC
#define ILED				PIND
#define OSEND				PORTA
#define ORECEIVE			PORTC
#define OLED				PORTD
 
// the cabels types vectors 
int STRAIGHT[8]=		{1, 2, 3, 4, 5, 6, 7, 8 };
int CROSS_T568A[8]=		{3, 6, 1, 7, 8, 2, 4, 5 };
int CROSS_T568B[8]=		{3, 6, 1, 4, 5, 2, 7, 8 };
int ROLLOVER[8]=		{8, 7, 6, 5, 4, 3, 2, 1 };

// why do we need to start with zeros ? and why is the status 0 ? 
int TEST[8]=		{0, 0, 0, 0, 0, 0, 0, 0 };

int status = 0;

//why retrun i+1? 
int getActiveBit(int x) {
	int i;
	for(i=0; i<8; i++)
		if(bit_is_set(x, i))
			return i + 1;
	return 0;
}

// comparing the two vectors 
// but why return  this time ?
int compare(int *a, int *b) {
	int i;
	for(i=0; i<8; i++)
		if(a[i]!=b[i])
			return 0;
	return 1;
}

//setting the status, what status exactly
void setStatus(int value) {
	status = value;
}

//get the status 
int getStatus() {
	return status;
}

//display the cable type with a corresponding led each type with a led
// what is this << I know it shift but what 
void displayMessage() {
	if(compare(TEST, STRAIGHT))
		OLED |= 1<<1;
	else if(compare(TEST, CROSS_T568A))
		OLED |= 1<<4;
	else if(compare(TEST, CROSS_T568B))
		OLED |= 1<<5;
	else if(compare(TEST, ROLLOVER))
		OLED |= 1<<6;
	else 
		OLED |= 1<<0;
}

//check if the cable is inserted
int isCableInserted() {
	int i, r;
	for(i=0; i<8; i++) {
		OSEND = 1<<i;
		r = IRECEIVE;
		
		if(getActiveBit(r) != 0) {
			OSEND=0x00;
			return 1;
		}
	}
	OSEND = 0x00;
	return 0;
}

int main() {	
	int i, r, k;
	
	DSEND = 0xFF;
	DRECEIVE = 0x00;
	DLED = 0xFF;
	
	
	do {
		if(isCableInserted()) {
		
			OLED = 0x00;
			OLED |= (1<<6);
			OLED |= (1<<5);
			OLED |= (1<<4);
			OLED |= (1<<1);

			for(i=0; i<8; i++) {
			
				OSEND = 1<<(7-i);
				for(k=0;k<10;k++)
					_delay_ms(100);
				//_delay_ms(1000);
				
				// what is this exactly ?
				r = IRECEIVE;
				
				// memorise
				TEST[i] = 8-getActiveBit(r)+1;
				
				
				if(TEST[i] != 0) {
					
					//if at least one wire is connected the cable is connected
					if(getStatus() == 0)
						setStatus(1);
				} 
				
				for(k=0;k<2;k++)
					_delay_ms(100);
				//_delay_ms(200);
			}

			
			OSEND=0x00;
			
			if(getStatus() == 1) {
				OLED=0x00;
				displayMessage();
				for(k=0;k<30;k++)
					_delay_ms(100);
				//_delay_ms(3000);
				setStatus(0);
			}

		}
		else{
			//the error led not insterted or defected
			OLED = 0x01;
			for(k=0;k<30;k++)
				_delay_ms(100);
			//_delay_ms(3000);
		}
	} while(1);
	
	return 0;
	
}

 

This topic has a solution.

Last Edited: Fri. Jan 30, 2015 - 07:40 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
>>>> See answers to questions noted with >>>>
>>> this is a LAN CABLE tester, right?

#define DSEND				DDRA
#define DRECEIVE			DDRC
#define DLED				DDRD
#define ISEND				PINA
#define IRECEIVE			PINC
#define ILED				PIND
#define OSEND				PORTA
#define ORECEIVE			PORTC
#define OLED				PORTD
 
// the cabels types vectors 
int STRAIGHT[8]=		{1, 2, 3, 4, 5, 6, 7, 8 };
int CROSS_T568A[8]=		{3, 6, 1, 7, 8, 2, 4, 5 };
int CROSS_T568B[8]=		{3, 6, 1, 4, 5, 2, 7, 8 };
int ROLLOVER[8]=		{8, 7, 6, 5, 4, 3, 2, 1 };

// why do we need to start with zeros ? and why is the status 0 ? 
int TEST[8]=		{0, 0, 0, 0, 0, 0, 0, 0 };

int status = 0;
// >>>> This is assigning an initial vaue to variables. Can't have indeterminate.

//why retrun i+1? 
>>>> returns bit number 1..8 whereas internally to software normal bit numbers are 0..7 as in the for loop

int getActiveBit(int x) {
	int i;
	for(i=0; i<8; i++)
		if(bit_is_set(x, i))
			return i + 1;
	return 0;
}

// comparing the two vectors 
// but why return  this time ?
int compare(int *a, int *b) {
	int i;
	for(i=0; i<8; i++)
		if(a[i]!=b[i])
			return 0;
	return 1;
}

//setting the status, what status exactly
>>>> this code makes is OK but written by someone with poor skills.
void setStatus(int value) {
	status = value;
}

//get the status 
int getStatus() {
	return status;
}

//display the cable type with a corresponding led each type with a led
// what is this << I know it shift but what 
>>>> 1<<0  is in binary 8 bit = 00000001. The value 1 shifted left 0 places
>>>> 1<<1  is in binary 8 bit = 00000010. The value 1 shifted left 1
>>>> normally you'd see 1<<DEFINED_BIT_NUMBER 
>>>> read the C reference books.
void displayMessage() {
	if(compare(TEST, STRAIGHT))
		OLED |= 1<<1;
	else if(compare(TEST, CROSS_T568A))
		OLED |= 1<<4;
	else if(compare(TEST, CROSS_T568B))
		OLED |= 1<<5;
	else if(compare(TEST, ROLLOVER))
		OLED |= 1<<6;
	else 
		OLED |= 1<<0;
}

//check if the cable is inserted
int isCableInserted() {
	int i, r;
	for(i=0; i<8; i++) {
		OSEND = 1<<i;
		r = IRECEIVE;
		
		if(getActiveBit(r) != 0) {
			OSEND=0x00;
			return 1;
		}
	}
	OSEND = 0x00;
	return 0;
}

int main() {	
	int i, r, k;
	
	DSEND = 0xFF;
	DRECEIVE = 0x00;
	DLED = 0xFF;
	
	
	do {
		if(isCableInserted()) {
		
			OLED = 0x00;
			OLED |= (1<<6);
			OLED |= (1<<5);
			OLED |= (1<<4);
			OLED |= (1<<1);

			for(i=0; i<8; i++) {
			
				OSEND = 1<<(7-i);
				for(k=0;k<10;k++)
					_delay_ms(100);
				//_delay_ms(1000);
				
				// what is this exactly ?
				>>>> the local variable named r = the #define constant IRECEIVE
				r = IRECEIVE;
				
				// memorise
				TEST[i] = 8-getActiveBit(r)+1;
				
				
				if(TEST[i] != 0) {
					
					//if at least one wire is connected the cable is connected
					if(getStatus() == 0)
						setStatus(1);
				} 
				
				for(k=0;k<2;k++)
					_delay_ms(100);
				//_delay_ms(200);
			}

			
			OSEND=0x00;
			
			if(getStatus() == 1) {
				OLED=0x00;
				displayMessage();
				for(k=0;k<30;k++)
					_delay_ms(100);
				//_delay_ms(3000);
				setStatus(0);
			}

		}
		else{
			//the error led not insterted or defected
			OLED = 0x01;
			for(k=0;k<30;k++)
				_delay_ms(100);
			//_delay_ms(3000);
		}
	} while(1);
	
	return 0;
	
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That is correct it is a Lan tester and thank you very much for your prompt and helpful reply ,I just started with avr it's my first project actually ,  may I bother you with some more few questions ?
why did the programmer use  x and i ?

 

int getActiveBit(int 

Last Edited: Fri. Jan 30, 2015 - 08:50 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 2

I think the issue here is that you do not yet understand the basics of the C programming language.  I suggest that you get a good book about C and study it well.  Then you will have the answers to all of your questions.

 

Only then should you try to work with a program such as this for an AVR.  Learn to walk first, then run.

 

Have you written a program to blink an LED, or cycle through the pins of a port turning them on/off sequentially? 

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

"Make XMEGA Great Again!"  - Greg Muth

Last Edited: Fri. Jan 30, 2015 - 12:13 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Last Edited: Fri. Jan 30, 2015 - 12:17 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

"Make XMEGA Great Again!"  - Greg Muth

Last Edited: Fri. Jan 30, 2015 - 01:43 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I commented the parts that I cant understand, please help me with them

And what makes you think that posting in this forum would do the trick? Did you read the purpose of this forum BEFORE POSTING?

http://www.avrfreaks.net/forum/p...

 

I'll move the tread to the mega and tiny forum for now as you don't say which chip you are using.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Actually, most of the questions seem to just basic 'C' - not specific to any particular microcontroller?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to note that globals are guaranteed to be 0 if not initialised (so called .bss (block started by symbol) variables) so there is no need to initialize to 0 as that will happen anyway (though I once found a C compiler that didn't do this that was VERY confusing!).