[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 477
  • Last Modified:

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

0
ibm979
Asked:
ibm979
  • 7
  • 4
1 Solution
 
sunnycoderCommented:
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
0
 
ibm979Author Commented:
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
0
 
Infinity08Commented:
>> since both chr and data are initialized
that should be uninitialized of course before you get confused ... don't accept my reply !!!
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Infinity08Commented:
      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 !
0
 
ibm979Author Commented:
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?
0
 
Infinity08Commented:
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]);
0
 
Infinity08Commented:
>> 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 ?
0
 
ibm979Author Commented:
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 */
      }
0
 
Infinity08Commented:
Those errors are most likely because you aren't using the correct device (check your project options eg.), or maybe the interrupt handler you declared has a vector number that is out of range for the device.
0
 
Infinity08Commented:
Did you get it to work ?
0
 
ibm979Author Commented:
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.
0
 
Infinity08Commented:
Perfect :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 7
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now