I am trying to understand this code
But I am having trouble understanding this part of the code.
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= -_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
sbi and cbi are later being used in the code as
void system_sleep()
{
sbi(MCUCR,PUD); //Disables All Internal Pullup Resistors
sbi(GIMSK,PCIE); //Enable Pin Change Interrupts Interrups
sbi(PCMSK,PCINT0); //Changes Interrupt to PIN1 (PCINT1)
cbi(ADCSRA,ADEN); //switch Analog to Digital Converter OFF
cbi(MCUCR,SM0); //Power Down Mode
sbi(MCUCR,SM1); //Power Down Mode
sbi(MCUCR,SE); //sleep Mode Power down enable (Sleep_enable(); should set this-- not tested yet)
sleep_enable(); //Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sleep_mode(); //sleep begins here
sleep_disable(); //Coming out of sleep
sbi(ADCSRA,ADEN); //switch Analog to Digital Converter ON
cbi(MCUCR,PUD); //Enables Pullup Resistors Again
}
The code is for ATtiny85 and I read the data sheet and I came to know that all these like MCCUR and ADCSRA etc are all registers. Also it states that there are two kinds of instructions SBI and CBI.
I also read some tutorials about micro controller programming using C and understand that each register has 8 bits. Each of these bits can be set using programming for different functionality. Also PUD, PCIE are different bits of these registers and these are being set in the system_sleep function. So I understand what system_sleep function is doing , it is setting the bits in the register.
The only part I am not able to understand is
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= -_BV(bit))
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
There is nothing in the data sheet like _SFR_BYTE or _BV. I looked into AVR/libc headers, there I found _BV but not sure what is it doing.