I came across this bit of code and am struggling to understand it
Can anyone explain the registers involved and what is being done to them ?
I would like to get it to compile and run in something
//*********************************************************
void adjustBrightness(void) {
char brightness ;
unsigned char adc_reading = ADCH;
ADCSRA |= (1<<ADSC); //Turn back on the ADC
if (adc_reading < 25) brightness = 16;
else if (adc_reading < 70) brightness = 15;
else if (adc_reading < 125) brightness = 14;
else if (adc_reading < 170) brightness = 13;
else if (adc_reading < 200) brightness = 12;
else brightness = 11;
PORTB = PORTB & ~(1<<DAC_CS);
WriteByteSPI(0b011110000);
WriteByteSPI(0x00);
PORTB = PORTB | (1<<DAC_CS);
_delay_us(5);
PORTB = PORTB & ~(1<<DAC_CS);
WriteByteSPI(0b11110000 | ((0xA - (brightness >> 3)) & 0x000F));
WriteByteSPI(~(brightness & 7) << 5);
PORTB = PORTB | (1<<DAC_CS);
}
You might like to know what this is in see
Brightness Auto Adjust
In running state, the program will adjust the brightness of the seven-segment LEDs automatically according to the environment brightness. If the room is bright, the program will light up the LEDs more. If the room is dark, the program will dim the LEDs. The following function is called:
void adjustBrightness(void): This function reads the high bits value of the Analog-Digital Converter. Then it adjusts the value of the brightness global variable according to the ADC value. The calibrated brightness will be written to the DAC using WriteByteSPI. The DAC will power the LED drivers with the calibrated voltage and thus control brightness.
at this site
http://people.ece.cornell.edu/land/courses/ece4760/FinalProjects/s2012/aab85_tz54/aab85_tz54/index.html