I am relatively new in programming µC in C and have previously always used the arduino IDE. I would like to create a function that sets and clears a pin. I tried this
#include <avr/io.h>
#include <util/delay.h>
#define F_CPU 16000000UL
void set_led(int poort,int pin){
poort |= (1<<pin);
//PORTB |= (1<<pin); <-- this works
}
void clear_led(int poort,int pin){
poort &= ~(1<<pin);
}
int main(void)
{
DDRD = 0xff;
PORTD = 0x00;
while(1)
{
set_led(PORTD,PD7);
_delay_ms(500);
clear_led(PORTD,PD7);
_delay_ms(500);
}
}
The pin variable works like it should but when I implement the poort variable the led does not blink anymore. Does someone know how to fix this? I use eclipse(AVR) on manjaro and the controller is an arduino nano.
Since C is pass by value only the local variable is changed.
You could either use macros:
#define SET_LED(POORT, PIN) ((POORT) |= (1<<(PIN)))
or pass the variable as pointer:
void set_led(volatile uint8_t *poort, int pin)
{
*poort |= (1<<pin);
}
and call it with set_led(&port, pin);
for example.
The type int
is probably wrong and should be volatile uint8_t
.