Arduino光电电阻器(在黑暗时点亮LED并在有光照时使LED变暗)

时间:2018-11-11 01:17:37

标签: assembly arduino embedded adc

sbi DDRB, 5 //led output
cbi DDRC, 0 //LDR input
sbi PORTC, 0 //pull up resistor 

//---- Conversion starts here ----//
LDI R16 , 0x87
STS ADCSRA, R16
LDI R16 , 0x11

STS ADMUX, R16
//configurations done

READ_ADC:
//CBI PORTB,5
LDS R18, ADCSRA
SBR R18,0b01000000
STS ADCSRA,R18

KEEP_POLING :
LDS R19, ADCSRA
SBRS R19, 4
//
RJMP KEEP_POLING // loop bc conversion not complegted ADIGF =0+


LDS R16, ADCSRA
SBR R16,0b00010000
STS ADCSRA,R16

// above is setting bit 4 to 1 indicating that conversion over

//below is reading the high and low byte of the ADC
LDS R16,ADCL //8 bits
LDS R17,ADCH // read 2 bits


LDI R20, 0Xf4
CP R16,R20 //IF R16>=R20
BRGE greateq // The low bits are greater than 244 so if ADCH has a value of one which is 2^8=256+244=500
CP R20,R16 //IF R20> R16 (It's dim)
BRGE check

greateq:
LDI R21,0X01 //Here we are checking if it is greater than or equal to one (2^8 on or more)
CP R17,R21
brge light
CP R21, R17
brge dim

check: // The ADCL has a decimal value lower than 244 so we need either 2^9 or both 2^8 and 2^9 but ( 2^8 alone is not enough )
LDI R23,0x02 // Greater than or equal to 2 aka either (2^9 is on) or (2^8 and 2^9 together are on)
CP R17,R23
brge light
CP R23, R17
brge dim

light:
CBI PORTB, 5 // Turns off the LED

dim:
SBI PORTB, 5 // Turns on the LED

RJMP READ_ADC

问题是LED一直亮着。 该代码应继续检查光强度是否超过500,并且如果这样做,则应清除与LED连接的位,但是如果光强度大于500,则应将该位设置为1。点亮LED。 是什么弄乱了我代码中的值?

0 个答案:

没有答案