Hi all,
The circuit is to sense brightness at pin 7 via LDR, light ON when dark, OFF when bright.
Pin 6, to ensure constant current.
When the circuit powered up, the LED stayed OFF, but once the LED ON, it ON all the way, even the LDR is uncovered.
I’m using mikroC version 6.0.0 and pickit2.
The codes is below, please any advice?
Thanks
My source code:
The circuit is to sense brightness at pin 7 via LDR, light ON when dark, OFF when bright.
Pin 6, to ensure constant current.
When the circuit powered up, the LED stayed OFF, but once the LED ON, it ON all the way, even the LDR is uncovered.
I’m using mikroC version 6.0.0 and pickit2.
The codes is below, please any advice?
Thanks
My source code:
Rich (BB code):
// internal oscillator, 8Mhz
//MCLR pin function disable
unsigned LDR, REF;
void initmain(){
ansel=0b00011;
trisio=0b00011; //pin 7&6 inputs.
cmcon0=0;
cmcon1=0;
}
void main() {
initmain();
PWM1_Init(1000000); //PWM F=1Mhz
while(1){
ADC_Init();
LDR= ADC_Read(0);
LDR/=4;
while(LDR<5){ // darkness value
//GPIO.B2=1;
ADC_Init();
REF= ADC_Read(1); // for constant current
REF/=4;
if (REF>12) {
PWM1_Set_Duty(120);
PWM1_Start(); }
else { PWM1_Set_Duty(200);
PWM1_Start();}
ADC_Init();
LDR= ADC_Read(0);
LDR/=4;
}
//PWM1_Stop();
GPIO.B2=0;
}
}
Attachments
-
5.3 KB Views: 52
Last edited by a moderator: