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.
C- pointer in embedded programming
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.
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.
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
I think it would be rather hard to find any significant example for any microcontroller which does not make some use of pointers :!:
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):
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; }
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
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.
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.
Quote:If anyone give an example code using c-pointer by using ATMEGA microcontrolleruart_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; }
westfw wrote:Quote:If anyone give an example code using c-pointer by using ATMEGA microcontrolleruart_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.