Link to home
Start Free TrialLog in
Avatar of ibm979
ibm979

asked on

converting to unsigned char from a pointer to unsigned char using avr-gcc

hi

I am compling following program using avr gcc.

void Send_Chunk(int length, char *cmd)
{
        int i=0;
       unsigned char *data, chr;
---------->   chr=&data[0];
       for(i=0; i< length; i++)
       {
                            TransmitByte(chr);
       }
       TransmitByte('\n');

}

Error is coming that illegal types `unsigned char' and `pointer to unsigned char'. How i can do such conversion.

Need urgent reply

Avatar of sunnycoder
sunnycoder
Flag of India image

Hi ibm979,

>       unsigned char *data, chr;
> ---------->   chr=&data[0];


      unsigned char *data, * chr; Note the extra * in front of chr to declare it as a pointer
---------->   chr=data;   ... however, since both chr and data are initialized, this assignment is of no consequence ... both would still be dangling pointers ... you need to initialize them.

Cheers!
Sunnycoder
Avatar of ibm979
ibm979

ASKER

Hi... I have made some modification to the code and want the solution for this case.
void Send_Chunk(int length, char *cmd)
{
       int i=0;
      unsigned char *data, chr;
       chr=&cmd[0];
      for(i=0; i< length; i++)
      {
                      chr=&cmd[0];  
                      TransmitByte(chr);
      }
      TransmitByte('\n');

}


unsigned char * cmd is a pointer to string. For example i will use following call to this function
Send_Chunk(6,"helloo");

NOw in the definition of the function Send_chunk i want to transmit string character by character using serial.
The function TransmitByte(unsigned char) require unsigned chr. So i cannot call this function with porinter parameter.
How i can convert string stored in pointer to unsigned char to unsigned char.

regards
ibm979
>> since both chr and data are initialized
that should be uninitialized of course before you get confused ... don't accept my reply !!!
      chr=&cmd[0];

Still the same problem sunnycoder pointed out : &cmd[0] is of type char*, whereas chr is of type unsigned char. You either need to declare chr as unsigned char* (as sunnycoder suggested), or you need to assign a char to chr.

In this case, the last is what you want to do :

        chr = cmd[0];

And the same inside the loop :

        chr = cmd[i];

Also note that the one before the loop is reduncant, and can be left out !
Avatar of ibm979

ASKER

void Send_Chunk(int length,unsigned char *cmd )
{
        int i=0;
       unsigned char chr;
       for(i=0; i< length; i++)
       {
                            chr=cmd[i];
                          TransmitByte(chr);
       }
       TransmitByte('\n');

}

Its too not working with avr -gcc. This code is working in visual studio. what is the problem?
And finally two more comments :

1) data is never used in the function, so that can be left out.

2) You don't need to use the intermediary chr variable, as you can just pass it to the TransmitByte function immediately :

        TransmitByte(cmd[i]);
>> Its too not working with avr -gcc. This code is working in visual studio. what is the problem?
What error do you get ?

Note that you also changed the function prototype from :

        void Send_Chunk(int length, char *cmd)

to :

        void Send_Chunk(int length, unsigned char *cmd)

although you didn't have to. Was that your intention ?
Avatar of ibm979

ASKER

I am sorry for mistake. It is void Send_Chunk(int length, unsinged char * cmd).

Now the error is in that file where TransmitByte(unsigned char) function is defined.

!E uartintr.o(96): Code address 0x26 already contains a value
!E uartintr.o(96): Code address 0x27 already contains a value
!E uartintr.o(98): Code address 0x2e already contains a value
!E uartintr.o(98): Code address 0x2f already contains a value
C:\iccv7avr\bin\imakew.exe: Error code 1
Done: there are error(s). Exit code: 1

void TransmitByte( unsigned char data )
      {
      //PORTB()
----->      unsigned char tmphead; ------------------------------> line no 96
      /* calculate buffer index */
      tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK;
            /* wait for free space in buffer */

      while ( tmphead == UART_TxTail )
            ;
      UART_TxBuf[tmphead] = data; /* store data in buffer */
      UART_TxHead = tmphead; /* store new index */
      UCSR0B |= (1<<UDRIE0); /* enable UDRE interrupt */
      }
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Did you get it to work ?
Avatar of ibm979

ASKER

You are champion man................!!

I was making program for ATMEGA162 and mega8 was selected. My problem is solved by just selecting the proper device....

So nice of you.
Perfect :)