Pages: [1]   Go Down
Author Topic: Adjust LCD/LED brightness by reading from an LED  (Read 360 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Full Member
***
Karma: 4
Posts: 218
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
//*********************************************************
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
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
« Last Edit: November 15, 2012, 04:36:02 am by april » Logged

Offline Offline
Full Member
***
Karma: 4
Posts: 187
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
ADCSRA |= (1<<ADSC); //Turn back on the ADC
To start the ADC measurements, the ADSC bit in ADCSRA needs to be set

Code:
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:
PORTB = PORTB & ~(1<<DAC_CS);
Code:
PORTB = PORTB | (1<<DAC_CS);  
Chip Selection signal for DAC

Code:
WriteByteSPI(0b011110000);
WriteByteSPI(0x00);

To send data on DAC

for more infohttp://www.avrfreaks.net/
« Last Edit: November 15, 2012, 08:26:21 am by Cybernetician » Logged

From Idea To Invention

Pages: [1]   Go Up
Jump to: