typedef in PORTx

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

how can I use typedef in a PORT?

Using the following struct...

typedef struct {
   unsigned char led1: 1;
   unsigned char led2: 1;
   unsigned char led3: 1;
   unsigned char led4: 1;
   unsigned char led5: 1;  
   unsigned char led6: 1;
   unsigned char led7: 1;
   unsigned char led8: 1;
}leds_struct;

...I want to manipulate PORTB bits using the typedef bit-names, as following.

leds.led1 = 1;  //PORTB.0 = 1
leds.led2 = 1;  //PORTB.1 = 1 
leds.led3 = 0;  //PORTB.2 = 0

how can I do that?

Learn as long as you live

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use:

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 BIT_buffer_status		((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit0 
#define BIT_recv_error		((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit1 
#define BIT_active			((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit2 
#define BIT_use_sample1		((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit3 

#define BIT_byte_count		((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit4 
#define BIT_receive_132		((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit5 
#define BIT_wait_uart_low	((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit6 
#define BIT_RS232_calibrated	((volatile io_reg*)_SFR_MEM_ADDR(TWAR))->bit7 

This is just using bits in the otherwise unused TWAR on a mega16 as bit variables but TWAR could just as easily be PORTB

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you Cliff, and have a nice day :)

Learn as long as you live