PDA

View Full Version : Configuring 16F916 for digital I/O



tekart
- 19th October 2011, 20:41
Hi, I'm using a 16F916 for the first time. Using A0 and A1 for analog inputs, but everything else as digital I/O.
Problem is that PortC.0 is locked LOW and C.1 is locked HIGH. Can't see why.
Also Port A.3 and A.4 read as LOW all the time, but A2 works correctly.
Been trolling the data sheet and trying various registers with no luck.
I really need to use Port A2-4 as inputs, and Port C4-7 as outputs (and 0-3 for LCD data).

Here's the setups I have so far:
define LCD_DREG PORTC ' LCD data port is C
DEFINE LCD_DBIT 0 ' LCD start bit = 0

DEFINE LCD_RSREG PORTA ' LCD register port A
DEFINE LCD_RSBIT 7 ' LCD register bit 0

DEFINE LCD_EREG PORTA ' LCD enable port B
DEFINE LCD_EBIT 6 ' LCD enable bit 1

DEFINE LCD_BITS 4 ' LCD 4 bit drive
DEFINE LCD_LINES 2 ' LCD 2 line mode

DEFINE OSC 8 ' define clock speed for code
OSCCON = %01110001 ' set clock 8 Mhz
'+1 = Internal oscillator is used for system clock
' set up on chip ADC
DEFINE ADC_BITS 10 ' Set number of bits in result ( 8 or 10 bits )
DEFINE ADC_CLOCK 3 ' Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS
ADCON0 = %10100001 ' bit0 AD converter is ON, BIT5 1 = VREF+ pin, bit7 1 = Right justified
ADCON1 = %00000000 ' AD conversion rate clock (default)
ANSEL = %00000011 ' PortA0 and A1 used by ADC (0=digital 1=analog)

LCDCON = 0 ' bit 7 = 0 disable LCD
OPTION_REG = %10000000 ' bit 7 = 1 disables portB pull-ups
INTCON = 0 ' disable all interrupts

TRISA = %00011111 ' set I/O directions (0 = output, 1 = input)
TRISB = %11100000 '
TRISC = %00000000 ' all outputs


Any help would be much appreciated.

tekart
- 19th October 2011, 22:35
Found the PortC output issue - was a dumb typo! AAGH!
Still trying to configure PortA3 and 4 as input. They still read as 0...

Darrel Taylor
- 20th October 2011, 16:15
Try adding ...

CMCON0 = 7

tekart
- 20th October 2011, 17:45
OH yeah, forgot about that one! Tried it, but Port A.3 is still reading as zero.
I'm using a push button to ground and 10K pull-up.
I have spent the usual HOURS reading the spec sheet, and this is making me nuts.

tekart
- 20th October 2011, 17:51
NEVERMIND!

I had set the voltage ref to use A.3 accidentally. DUH. It's always something stupid isn't it.

Thanks Darrell