Pointers problem! [C]

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

Hi everyone!

I'm making a little project and I need to use the USART for communication. The hardware works ok but I've got problems with the pointers I'm using.

I've got three files:

Globals.h

extern volatile unsigned char uartBufferIn[25];
extern volatile unsigned char* pBin;

extern unsigned char uartBufferParse[25];
extern unsigned char* pParse;

extern volatile unsigned char txEnd, rxEnd;



uart.c & .h

//***DEFINITIONS***
#define F_CPU 16000000UL

#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))4


//***LIBRARIES INCLUDED***
#include 
#include 
#include 
#include 
#include "Globals.h"


volatile unsigned char status=0;
volatile unsigned char counterRX=0, counterTX=0, length=0;
unsigned char numBytes=0;
//volatile unsigned char txEnd=0, rxEnd=0;


//***Interruption Service Routines***

ISR(USART_RX_vect)
{
	*(pBin+counterRX)=UDR0;
	counterRX++;

	if(UDR0==0x04) 
	{
		length=counterRX;
		counterRX=0;
		rxEnd=1;
	}
	
}

ISR(USART_TX_vect)
{
	counterTX++;
	if(txEnd==0)
	{
		UDR0=*(pBout+counterTX);
		if(*(pBout+counterTX)==0x04) txEnd=1;

	}
	else
	{
		counterTX=0;
		CLEARBIT(PORTD,2);//MAX485 config receive
		txEnd=0;
	}
	
}

unsigned char uart_rx(void)
{
	
	unsigned char c=0;
	
	ATOMIC_BLOCK(ATOMIC_RESTORESTATE)
	{
		numBytes=length;
	}

	length=0;


	for(c=0; c

I'm only flipping led because I don't have a debugger. I'm using arduino duemilanove.

The problem is that the led on port 2 only flip once, the first time that the uC receive a data frame. That means that the copy of the bufferIn to the parseBuffer doesn't work fine.

The position 1 of the inBuffer is always 0x31 that's why I'm checking this one.

I don't understand very well why. Is there a problem using pointers as extern?? Am I doing a very big mistake??

Thanks in advance!!!

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

If it didn't work though it wouldn't flip it at all, is my thought. What are you doing to change it.

1st time through...

2nd time through...

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Post a minimal but complete compilable program exhibiting the problem, so that we can reproduce it. Don't forget, that we don't have your setup, so describe it (software and hardware) too.

Describe, what the expected behaviour is and in what way the reality falls short of the expectation.

JW

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

Thanks for the answers!!
The code for this concrete case is the code above, only miss the uart.h file.
I'm working on an Arduino duemilanove with atmega 328P and I'm using AvrStudio 4 and AVRdude.

The behaviour I expected is once the data frame is received (when an EOT 0x04 arrives) copy this array received (uartBufferIn) to a local array(uartBufferParse) for that way work on it.
The first check I'm doing is to see if the byte in position 1 of the array es the same that it has been sent from the computer, and it isn't.

I thought that the function uart_rx() would copy perfectly the array but it's not true. Even more, I have doubts about the uartBufferIn too.

I'm not sure that is a good thing to work with pointer declared extern and volatile.

I'm really sorry about my english, I'm trying to explain myself the best I can.

Thank you very much for the help!!!! :D

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

Thanks for the answers!!

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

Hmmm what does this do?

*(pParse+c)=*(pBin+c);

That's like saying "3 = 5".

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

Hi Cameron707

I'm copying from pBin to pParse with the same offset, assigning the value in the position 'c' of the uartBufferIn to the position 'c' of the uartBufferParse.

Is it not the same than uartBufferParse[c]=uartBufferIn[c]??

Thanks for the answer!

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

C certainly gives you not only rope but a smokescreen too!

*(pParse+c)=*(pBin+c);   // let's use smoke and mirrors 
pParse[c] = pBin[c];     // this looks better.
uartBufferParse[c] = uartBufferIn[c]; // is too simple

It appears that you never change those global pointers pParse, pBin so they wil always refer to your global arrays.

No. I have not looked at your code. It makes my head hurt. Rewrite it and post a minimal compilable version, and we will help fix it.

Meanwhile you could look to see how the rest of civilisation has handled UART interupts for the last 30 years.

David.

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

Thanks you for your sincerity David.
I'll try it, but I had already searched info and tried more ways of do this.
Thanks again.

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

One problem (perhaps "the" problem) is this:

ISR(USART_RX_vect)
{
   *(pBin+counterRX)=UDR0;
...
   if(UDR0==0x04) 

You can't read UDR0 multiple times in the ISR. The second access would give you the next data out of the fifo. Not sure what you get in case of there is no next data (which is the case here), but I doubt it is the previous data again.

Stefan Ernst

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

Silly question - but have you ever heard of memcpy()?

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

Like David said, my code is not precisely a good one, so I think that the better thing I could do is first of all search more and learn more about programming and after that, post a (at least) compilable code. This way I'll learn more about this and I won't waste people's time. I'm new at this so...

On the other hand.
Sternst: I didn't know that I only could read once the UDR0 (another lack of knowledge, sorry).
This could be the problem.

Clawson: Yes I've already read about the memcpy(). Like I said before I posted this because I thought I was doing a better thing than it really is.

Thanks both!!