am I using delay wrong?

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

Hi all my code has no other error than one that happens for each instance of calling delay

ive tried utils/delay.h and delay_x
i set the compiler optimization

i dunno what it could be

heres the code (its for a reciving side of a manchester rf circuit, but atm Im just worried about these delay errors)

#define F_CPU 8000000;
#include
//#include"delay_x.h"
#include


//code to define pins
#define	RFTx 		PORTD ,2	//RF transmit pin
#define	RFrecv		PORTD ,1	//RF receive pin
#define	RF_RecvEN	PORTD ,0	//RF receive enable pin, delay 700mS after first power on

#ifndef _AVR035_H_ 
#define _AVR035_H_ 

// from AVR035: Efficient C Coding for AVR 
//quick pin change macros
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT)) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) 

#define SETBITMASK(x,y) (x |= (y)) 
#define CLEARBITMASK(x,y) (x &= (~y)) 
#define FLIPBITMASK(x,y) (x ^= (y)) 
#define CHECKBITMASK(x,y) (x & (y)) 

#define VARFROMCOMB(x, y) x 
#define BITFROMCOMB(x, y) y 

#define C_SETBIT(comb) SETBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_CLEARBIT(comb) CLEARBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_FLIPBIT(comb) FLIPBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 
#define C_CHECKBIT(comb) CHECKBIT(VARFROMCOMB(comb), BITFROMCOMB(comb)) 

#endif 

//global varibles
int ReceivedData[4];		//receiver buffer, last byte is checksum
int Txdata[3];				//buffer for transmission
int Recv_error_flag;		//receive data error flag, no cofigureable data
int recvtimeout; 			//receiver timeout variable, no configurable data      
   

/*Function to get a bit in a data, return 0xFF if received bit length is incorrect*****
when tx sends 300ms H pulse and 300ms L pulse, the flowing counter should read:
Hpulse = 30 is data bit 1
Hpulse = 12 is data bit 0
*/
int get_bit()						//1ms per call on this route
{
	int Hpulse, i;
	Hpulse = 0;
	while(!C_CHECKBIT(RFrecv))		//wait for high pulse to occur
	{
	}//while
	for(i = 0; i <35; i++)			//now sample pulse for about 800us
	{
		if(C_CHECKBIT(RFrecv))
		{
			Hpulse ++;
		}//if
		_delay_us(10);
	}//for

	if((Hpulse > 25) && (Hpulse < 35))
	{
		return 1;
	}//if
	if((Hpulse > 7) && (Hpulse < 17))
	{
		return 0;
	}//if
	else
	{
		Recv_error_flag = 1;
	}//else   
}//get_bit


/* gets 1 byte (8bits)from the data transmission
*/
int get_byte(){
	int bits = 0;
	bits = (bits | get_bit()) << 1;		//get MSB bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 7 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 6 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 5 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 4 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 3 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit()) << 1;		//get 2 bit of a byte (or bits with get_bit(), then shift left 1)
	bits = (bits | get_bit());			//get LSB bit of a byte (or bits with get_bit(), then shift left 1)
	return bits;
}//get_byte

/* Sync() finds Sync Pulse and allows for initial transmitter reciever handshake

*/
int Sync()								//find 2ms low then high Sync. pulse
{
	int i;
	i = 0;
	while(!C_CHECKBIT(RFrecv))
	{
	}//while
	while(C_CHECKBIT(RFrecv))
	{
	}//while
	while(!C_CHECKBIT(RFrecv))			//Find 1500us low pulse
	{
		i++;
		_delay_us(10);
		if(i > 70)						//1500us reached
		{
			for(i = 0; i < 60; i++)		//within ~600us, if find high pulses, sync. successful
			{
				_delay_us(10);
				if(C_CHECKBIT(RFrecv))	//now get data
				{
					return 1;
				}//if
			}//for
			return 0;
		}//if
	}//while
}//Sync



/*	
int get_data(int time)
				1)	initally turn on receiver
				2)	time = # of secs to wait for data packet, max. is 127 sec.,
										return 1 if data valid, data will be stored at receiveddata[0~9] arary, 
				3)	at the end of the function, receiver will be turned off to save power
*/
int get_data(int time)
{
	int i,checksum;		//counter varibles
	
   //this section of code was origionally written by the manufacturers (dont know if need)
   //setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);  //524 ms overflow, reserved for RF transmission timing
   //set_timer1(0);


	C_SETBIT(RFTx);										//disable transmitter		
	C_CLEARBIT(RF_RecvEN);								//enable RF receiver
	recvtimeout=0;										//reset timeout flag
	_delay_ms(100);
	time = time * 2;
	while(recvtimeout <= time)							//while time is not expired
	{
		if(Sync())
		{
			Recv_error_flag = 0;						//reset receive error flag
			for(i = 0; i < 4; i++)
			{
				ReceivedData[i] = get_byte();			//try get 11 bytes
				if(Recv_error_flag == 1)				//if something wrong, exit get bytes loop
				{
					i = 4;
				}//if
			}//for
			if(Recv_error_flag == 0)					//after getting 11 bytes, if bits recieved OK, verify checksum
			{
				checksum = ReceivedData[0];				//calculate checksum                               
				for(i = 1; i < 3; i++)
				{
					checksum = ReceivedData[i] ^ checksum;
				}//for
				
				if(checksum == ReceivedData[3])			//if checksum is correct
				{
				   //disable_interrupts(INT_TIMER1); 	//disable timer 1
			   	C_SETBIT(RF_RecvEN);					//disable RF receiver
					return 1;							
				}//if
				else									//if checksum is incorrect
				{
//					disable_interrupts(INT_TIMER1); 	//disable timer 1
				   	C_SETBIT(RF_RecvEN);			//disable RF receiver
//					return 0;
				}//else
			}//if
		}//if
	}//while
	//disable_interrupts(INT_TIMER1);	//disable timer 1;
	C_SETBIT(RF_RecvEN);			//disable RF receiver
	return 0;										//cannot get data
}//get data



int main()
{
	
   	_delay_ms(1000);		//ititial delay to get things initialized
  
  	while(1)
  	{
		if(get_data(127))	//127sec delay to get data
		{

		}//if
  	}//while
	return(1);
}//main


and the builder returns

avr-gcc.exe -mmcu=atmega128 -Wall -gdwarf-2 -DF_CPU=8000000UL -Os -fsigned-char -MD -MP -MT Trendsetterreciever.o -MF dep/Trendsetterreciever.o.d -c ../Trendsetterreciever.c
../Trendsetterreciever.c:1:1: warning: "F_CPU" redefined
:1:1: warning: this is the location of the previous definition
In file included from ../Trendsetterreciever.c:2:
c:/program files/winavr/bin/../avr/include/util/delay.h: In function '_delay_us':
c:/program files/winavr/bin/../avr/include/util/delay.h:106: error: expected ')' before ';' token
c:/program files/winavr/bin/../avr/include/util/delay.h: In function '_delay_ms':
c:/program files/winavr/bin/../avr/include/util/delay.h:131: error: expected ')' before ';' token
make: *** [Trendsetterreciever.o] Error 1
Build failed with 2 errors and 2 warnings...

I have compier optimisations to -0s

so what am I doing wrong

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

It works once you remove the ; from line 1

(and why define F_CPU in the file anyway when you are already defining it in the Makefile anyway?)

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

Cliff has eagle eyes (and he probably stopped right at the first line, while the rest of us were getting dizzy reading the code).

Also '_delay_ms(1000);' will never get you 1 second delay, unless you want to run your clock at 262kHz or below (read the delay.h file), delay_x.h will work as intended.

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

Cliff: My hat is off to ya sir. You do have code eagle eyes. Thanks:-)

curtvm: Thanks for the delay_x hint

Yall both brigthed my debugging filled afternoon.:-)

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

Well I'd like to say it was eagle eyes but actually I had to copy the code. Build it. Make the .i file, study that and FINALLY it dawned what was going on as I studied the #include's at the top.

It had to be something to do with the #include's at the top rather than anything in the given C code as the error was reported only from the .h (and not a case of reporting multiple definition or any of that usual kind of error)

Cliff