I'm using the picdem 18F4550 with microchip v8.63 and the c compiler. components that i'm use are: - 3 leds (red, green, blue); - a LDR (327700 NORPS-12 farnell).
I have connected 3 leds (red, green and blue), on RB4, RB5 and RB6 as output. I also connect the LDR on RB1.
My question is, when the green of another led lights up, how can I retrieve the value in my code that the LDR measures?
I included my code:
#include "p18开发者_StackOverflow中文版cxxx.h"
#pragma config WDT = OFF
void main(void)
{
// turn off all LED latches
LATDbits.LATD0 = 0;
LATDbits.LATD1 = 0;
LATDbits.LATD2 = 0;
// make port d bits which drive LEDs outputs
TRISDbits.TRISD0 = 0;
TRISDbits.TRISD1 = 0;
TRISDbits.TRISD2 = 0;
TRISB = 0;
// RB port output.
PORTB = 0;
PORTB = 0b10001111; // 0b01011010
if(PORTBbits.RB4 == 0) {
LATDbits.LATD0 = 1;
}
if(PORTBbits.RB5 == 0) {
LATDbits.LATD1 = 1;
}
if(PORTBbits.RB6 == 0) {
LATDbits.LATD2 = 1;
}
if(PORTBbits.RB1 == 1) {
}
while(1) {
;
}
}
Assuming you are connecting an LDR to a pin configured as GPIO. The voltage threshold for the GPIO pin applies. The value read on the pin is dependent on the resistance of the LDR swinging wide enough to generate both logic low and logic high. Most likely you want to connect the LDR to the PIC A/D pin instead of GPIO. Use ADCON to configure the A/D. Set up the A/D for conversion, and wait for the A/D completion interrupt. Then read the ADRESH and ADRESL which yields the analog voltage of the LDR.
精彩评论