Go Down

### Topic: Adjust LCD/LED brightness by reading from an LED (Read 540 times)previous topic - next topic

#### april

##### Nov 15, 2012, 10:19 amLast Edit: Nov 15, 2012, 10:36 am by april Reason: 1
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

Code: [Select]
`//*********************************************************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
Quote

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

#### Cybernetician

#1
##### Nov 15, 2012, 02:07 pmLast Edit: Nov 15, 2012, 02:26 pm by Cybernetician Reason: 1
Code: [Select]
`ADCSRA |= (1<<ADSC); //Turn back on the ADC`

Code: [Select]
`unsigned char adc_reading = ADCH;`
ADC has a resolution of 10 bits, this 10 bit value is split across two 8 bit registers, ADCH and ADCL. The highest 8 bits of the measurement in the ADCH register, with the rest in the ADCL register.

Code: [Select]
`PORTB = PORTB & ~(1<<DAC_CS);`
Code: [Select]
`PORTB = PORTB | (1<<DAC_CS);  `
Chip Selection signal for DAC

Code: [Select]
`WriteByteSPI(0b011110000);WriteByteSPI(0x00);`

To send data on DAC

From Idea To Invention

Go Up

Please enter a valid email to subscribe