Hi guys,
AVR noobie here trying to figure out how to fast read multiple bits in uint32_t variable.
I am using SBIT() macro to read and write bits.
sbit.h
#ifndef SBIT_H_ #define SBIT_H_ struct bits { uint8_t b0:1; uint8_t b1:1; uint8_t b2:1; uint8_t b3:1; uint8_t b4:1; uint8_t b5:1; uint8_t b6:1; uint8_t b7:1; uint8_t b8:1; uint8_t b9:1; uint8_t b10:1; uint8_t b11:1; uint8_t b12:1; uint8_t b13:1; uint8_t b14:1; uint8_t b15:1; uint8_t b16:1; uint8_t b17:1; uint8_t b18:1; uint8_t b19:1; uint8_t b20:1; uint8_t b21:1; uint8_t b22:1; uint8_t b23:1; uint8_t b24:1; uint8_t b25:1; uint8_t b26:1; uint8_t b27:1; uint8_t b28:1; uint8_t b29:1; uint8_t b30:1; uint8_t b31:1; } __attribute__((__packed__)); #define SBIT(port,pin) ((*(volatile struct bits*)&port).b##pin) #endif /* SBIT_H_ */
Example.c
#include <avr/io.h> #include <sbit.h> volatile uint32_t data = 0xAAAAAAAA; volatile uint8_t temp = 0; int main(void) { SBIT(data,0) = 1; // Works fine no problem. temp = SBIT(data,0); // Also works. for(int count=0; count < 32; count++) // To check each individual bits of data { if(SBIT(data,count) == 1) // Does not work ERROR - 'volatile struct bits' has no member named 'bcount' { // Do something. } } while (1) { __asm__ __volatile__ ("nop"); } }
The problem is I am passing the value of variable count into SBIT(port,pin). But the "pin" expects a manual input like 0,1,2 etc. Is there a way to make this work so I can just pass the variable containing the number. I am sure there must be other ways, but I need this specific way with the loop and SBIT() macro.
Second Question - Is there a faster way to do this? Less clock cycles.
My project contains handling individual 32 LEDS + other code.
Thanks.