启用端口 RA0 作为输入并从 LED 检索值 LDR
我使用 picdem 18F4550 和 microchip v8.63 以及 C18 编译器。
我将启用 PortA 设置为输入,我将在端口 RA0 上连接 LDR。 如下(我认为)
TRISAbits.TRISA0 = 1; <= set RA0 as input
现在我想要 LDR 的值(如果 LED 打开的话电压/值),我可以说:
int colorLed = PortAbits.RA0;
现在在 int 类型的变量中有我的 Led 的值/电压。
如果我错了请纠正我。
I'm using the picdem 18F4550 with microchip v8.63 with the C18 compiler.
I will enable PortA to set as input, I will connect a LDR on port RA0.
Which is as following (I think)
TRISAbits.TRISA0 = 1; <= set RA0 as input
Now I want the value of the LDR (voltage/value if a led is on), can I say:
int colorLed = PortAbits.RA0;
And now in the variable of type int there is the value/voltage of my Led.
Correct me if i'm wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来您想测量模拟电压,在这种情况下,您需要将引脚用作
AN0
,而不是RA0
。您应该阅读数据表的第21节,但总而言之,您需要配置AD使用寄存器ADCON0
、ADCON1
和ADCON2
进行转换器,并读取ADRESH:ADRESL
上的结果。请注意,您可以使用数字输入读取LDR,但无法对其进行校准。使用模拟输入稍微复杂一些,但更加灵活。
It sounds like you want to measure an analogue voltage, in which case you will need to use the pin as
AN0
, rather thanRA0
. You should read section 21 of the datasheet, but in summary, you will need to configure the A-D converter using registersADCON0
,ADCON1
andADCON2
, and read the result that's present onADRESH:ADRESL
.Note that you could potentially read the LDR using a digital input, but you will be unable to calibrate it. Using the analogue input is slightly more complicated, but much more flexible.
默认情况下,PORTA 可以用作数字输入或模拟输入。
要使用 LDR,您很可能需要将 PIN 配置为模拟输入。
然而,这仍然取决于你想做什么。也许您应该提供有关您的项目的更多详细信息。
检查此链接以获取 PIC18F4550 数据表
您只能在以下位置找到更详细的资源: PIC184550 这里
再次,您可能需要提供有关您的应用的更多详细信息所以我们可以更好地帮助您。
PORTA can function as a digital input or analog input, by default.
To use an LDR you most likely need to to configure the PIN as analog input.
HOwever it still depends on what you want to do. Maybe you should give a bit mroe details as to your project.
check this link for the PIC18F4550 datasheet
You can only find more detailed resources on the PIC184550 here
again, you might want to give more details on your application so we can help you better.