Is there a way to tell a function which pin I want to use so I can change it's state inside of the function?
Basically, I want to mimic BASCOMs SOUND function. It is defined in BASCOM as SOUND pin, duration, pulses.
So far I've done the code below, and it is working (it sounds the same as the function from bascom), but I've hardwired the output pin (Spkr).
typedef struct { unsigned char bit0:1; unsigned char bit1:1; unsigned char bit2:1; unsigned char bit3:1; unsigned char bit4:1; unsigned char bit5:1; unsigned char bit6:1; unsigned char bit7:1; } io_reg; #define Spkr ( (volatile io_reg*)(&PORTD) )->bit7 // mimics BASCOM SOUND function void sound(uint16_t duration, uint16_t pulses) { // Delay function based on AVR-libc "_delay_loop_2" // with 2 NOPs, so it sounds the same as the sound function of BASCOM void my_delay(uint16_t __count) { __asm__ volatile ( "1: sbiw %0,1" "\n\t" "nop" "\n\t" "nop" "\n\t" "brne 1b" : "=w" (__count) : "0" (__count) );} while (duration-- > 0) { Spkr = 1; my_delay(pulses); Spkr = 0; my_delay(pulses); } }
So, how can I make a C function like this?
void sound(one pin, uint16_t duration, uint16_t pulses) { ... while (duration-- > 0) { pin = 1; my_delay(pulses); pin = 0; my_delay(pulses); } }
Thank you