C- pointer in embedded programming

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

Usually I use AVR chip & 8051 for my project. And for language most of the time use 'C'. So far I am not so comfortable enough using 'c-pointer' in embedded programming. If anyone give an example code using c-pointer by using ATMEGA microcontroller it will be very helpful for me to use of "C- pointer" in embedded programming.

Last Edited: Mon. Apr 21, 2014 - 10:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here are three examples:

Any function needing to return a value through one of its parameters.

Pointer to char being the return value from several of the standard functions for string handling (e.g. strchr() ).

Passing an IO port register as a parameter to a function.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

harunduet wrote:
So far I am not so comfortable enough using 'c-pointer' in embedded programming.

When you say not so comfortable do you mean you don't see the benefit or you are not sure of the syntax or something else?

if you can explain your concern a little then I am sure that information is available.

The examples mentioned above are good examples of situations where you would use a pointer.

regards
Greg

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

Quote:
I am not so comfortable enough using 'c-pointer' in embedded programming
But it is no different than using a c pointer in any environment. There is nothing special about pointers in embedded programming.

Regards,
Steve A.

The Board helps those that help themselves.

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

harunduet wrote:
If anyone give an example code using c-pointer by using ATMEGA microcontroller

I think it would be rather hard to find any significant example for any microcontroller which does not make some use of pointers :!:

Koshchi wrote:
But it is no different than using a c pointer in any environment. There is nothing special about pointers in embedded programming.

Absolutely - and use of pointers it pretty much bread-and-butter to any 'C' programmer!

So here's some 'C' learning resources (includes a link to the 'C' books & tutorials thread on this very site):

http://blog.antronics.co.uk/2011...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
If anyone give an example code using c-pointer by using ATMEGA microcontroller

uart_puts("Hello World");

Perhaps you are actually puzzled over "pointers to functions", which are more confusing. Here's an example of that (with several other uses of pointers as well), straight out of the avr-libc documentation:

    static int uart_putchar(char c, FILE *stream);

    static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
                                             _FDEV_SETUP_WRITE);

    static int
    uart_putchar(char c, FILE *stream)
    {

      if (c == '\n')
        uart_putchar('\r', stream);
      loop_until_bit_is_set(UCSRA, UDRE);
      UDR = c;
      return 0;
    }

    int
    main(void)
    {
      init_uart();
      stdout = &mystdout;
      printf("Hello, world!\n");

      return 0;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

For many mikrocontrollers it is the case, that they have different types of memories, each with its own addresses
( so muutiple address-spaces).

For AVR you have RAM, PROGMAM-ROM and EEPROM
Each adress-space has its own pointer-type.

Then you always should think of "a pointer to xx",
where xx is a suitable datatype.

For example you have a pointer to an uint_16t in pgm_space

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

Quote:
If anyone give an example code using c-pointer by using ATMEGA microcontroller it will be very helpful for me

PORTB = 0x55;

EDIT: didn't have a lot of time to add detail to that last night but just to point out that when you use a line like that in your code what you are really writing (built for mega128) is:

(*(volatile uint8_t *)((0x18) + 0x20)) = 0x55;

0x38 (that is 0x18 plus 0x20) is the RAm address of the "PORTB" register. The typecast tells the compiler to interpret this numbers as a pointer to volatile uint8_t then the * operator means write to the location of this pointer. So when you use "PORTB = 0x55" you are saying write the 0x55 value to the location of PORTB using pointer access.

Obviously it's deliberately done to make a simple looking syntax so that you don't really need to be aware that a pointer is involved "under the hood".

Apart from the obvious of reading your K&R about pointers you will also find if useful to Google "C pass by value pass by reference" which will lead you to articles that show C's two different mechanisms for passing values into a function, the reference one uses pointers and lets the thing being passed get updated by the called function.

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

I am from EEE background. As the lack of proper programming knowledge I am always thirsty to learn better programming technique. I am so much delighted to see your contribution to help others. This will give me more inspiration to go ahead.

I have found a video tutorial :
https://www.youtube.com/watch?v=...

Thanks for the guideline, actually which I need.

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

westfw wrote:
Quote:
If anyone give an example code using c-pointer by using ATMEGA microcontroller

uart_puts("Hello World");

Perhaps you are actually puzzled over "pointers to functions", which are more confusing. Here's an example of that (with several other uses of pointers as well), straight out of the avr-libc documentation:

    static int uart_putchar(char c, FILE *stream);

    static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
                                             _FDEV_SETUP_WRITE);

    static int
    uart_putchar(char c, FILE *stream)
    {

      if (c == '\n')
        uart_putchar('\r', stream);
      loop_until_bit_is_set(UCSRA, UDRE);
      UDR = c;
      return 0;
    }

    int
    main(void)
    {
      init_uart();
      stdout = &mystdout;
      printf("Hello, world!\n");

      return 0;
    }

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

harunduet wrote:
westfw wrote:
Quote:
If anyone give an example code using c-pointer by using ATMEGA microcontroller

uart_puts("Hello World");

Perhaps you are actually puzzled over "pointers to functions", which are more confusing. Here's an example of that (with several other uses of pointers as well), straight out of the avr-libc documentation:

    static int uart_putchar(char c, FILE *stream);

    static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL,
                                             _FDEV_SETUP_WRITE);

    static int
    uart_putchar(char c, FILE *stream)
    {

      if (c == '\n')
        uart_putchar('\r', stream);
      loop_until_bit_is_set(UCSRA, UDRE);
      UDR = c;
      return 0;
    }

    int
    main(void)
    {
      init_uart();
      stdout = &mystdout;
      printf("Hello, world!\n");

      return 0;
    }

Thanks for the guideline, actually which I need.