I have learnt C++ before at school, then C. Haven't got a formal training in C like C++, mostly self taught. The idea of using static variables and static functions are not yet clear for me. As much as i know that if a static variable is declared in a class,it is shared by all the objects of the class, and a static functions is used to work with the static variables.
But here in Joey's code i see -
/* Serial calibration program. Untested. */ #include <avr/io.h> #include <stdio.h> #include <util/delay.h> static int usart_put(char c, __attribute__ ((__unused__)) FILE *stream) { while (!(UCSRA & (1<<UDRE))); UDR = c; return 0; } int main(void) { static FILE usart = FDEV_SETUP_STREAM(&usart_put, NULL, _FDEV_SETUP_WRITE); stdout = &usart; UBRRL = 51; UCSRB = (1<<TXEN); OSCCAL = 0; do { _delay_ms(50); printf("OSCCAL=%u\r\n", OSCCAL); _delay_ms(50); } while (++OSCCAL); while (1); } /* 9600 baud @ 8 MHz */ /* 1200 baud @ 1 MHz */
Why did he use static in the function, usart_put ( ). What are the general guidelines for a function to be static in gcc ?
Can i / should I use static in simple adc read functions like this -
int adc_read() // adc read function { ADCSRA|=(1<<ADSC); while(bit_is_set(ADCSRA,ADSC)); return ADC; }
Also, why don't we see static before functions like this -
void adc_setup() // adc setup function for ATtiny 13A { ADMUX|=(1<<MUX0); //Adc at pin 7 ADMUX=(1<<REFS0); ADCSRA|=(1<<ADPS1)|(1<<ADPS0)|(1<<ADEN); }