Reading state of input pins on a PIC18

房东的猫 提交于 2019-12-06 05:24:19

The ADC pins on the PICs unfortunately are configured as analog inputs on powerup. You need to disable the ADC functionality on that pin to use it as a digital input.

Clearing bit 0 of ANSELH will set RC6 to digital input. Then your code will work.

This is documented in the datasheet in section 9.4 : Port Analog Control

PIC18F/LF1XK50 Datasheet

Setting...

TRISC = 1; // input

...should set only pin RC0 of PORTC as input pin, all other pins are defined as output. So PORTCbits.RC6 == 0 should not return correct input state of in RC6 pin.

TRISC = 255 should set all pins of PORTC as input.

It is worth mentioning that some other PIC18 variants are set up differently:

On PIC18F4520 and PIC18F4680 you need to configure pins as digital it trough a ADCON1 register instead of ANSELH.

Setting a bit to 1 will make a pin digital.

Besides this you need to make them input pins by setting a bit to 1 in the corresponding TRIS register.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!