16F18877 Input not working

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Hi.,
My setup is not function, the input allways reads 0.
Here is code, have been struggeling for days.
Code:
#include <xc.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lcd.c"

#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#pragma config RSTOSC = HFINT1//32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 4000000
#define FOSC 1000000L

#define back_light RB0
#define led1_on RE0
#define KN1 RC3
#define KN2 RC2
#define KN3 RA6
#define KN4 RA7

void setup_init(void)
{
        // NOSC HFINTOSC; NDIV 4;
    OSCCON1 = 0x62;
    //O
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;
    // HFFRQ 4_MHz;
    OSCFRQ = 0x02;
    // HFTUN 0;
    OSCTUNE = 0x00;
   
     
      // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; NVMMD NVM enabled;
    PMD0 = 0x00;
    // TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; NCOMD DDS(NCO) enabled; TMR6MD TMR6 enabled;
    PMD1 = 0x00;
    // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x00;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; CCP5MD CCP5 enabled; PWM6MD PWM6 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled; MSSP1MD MSSP1 enabled; UART1MD EUSART enabled; MSSP2MD MSSP2 enabled;
    PMD4 = 0x00;
    // DSMMD DSM enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; SMT2MD SMT2 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD5 = 0x00;
      /*
       * WPUx registers
    */
      /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /*TRISx registers  */
    TRISE0=0; // LED
    TRISE1=0;
    TRISE2=0;
   // TRISE3=1; // reset,
    TRISA0=0;
    TRISA1=0;
    TRISA2=0;
    TRISA3=0;
    TRISA4=0;
    TRISA5=0;
    TRISA6=1; // KN3
    TRISA7=1; // KN4
    TRISB0=0; // bk light
    TRISB1=0;
    TRISB2=0;
    TRISB3=0; // RS
    TRISB4=0;
    TRISB5=0;
    TRISB6=0;
    TRISB7=0;
    TRISC0=0;
    TRISC1=0;
    TRISC2=1; // KN2
    TRISC3=1; // KN1
    TRISC4=0;
    TRISC5=0;
    TRISC6=0; // TX
    TRISC7=1; // RX
    TRISD0=0; // DATA
    TRISD1=0; //
    TRISD2=0; //
    TRISD3=0; // Data
    TRISD4=0;
    TRISD5=0;
    TRISD6=0;
    TRISD7=0; // Enable
   
    /**
    ANSELx registers
    */
    ANSELD = 0x00;
    ANSELC = 0x00;
    ANSELB = 0x00;
    ANSELE = 0x00;
    ANSELA = 0x00;
   
    /**/
    
    WPUD = 0x00;
    WPUE = 0x00;
    WPUB = 0x00;
    WPUA = 0x00;
    WPUC = 0x00;
 
    /**
    ODx registers
    */
    ODCONE = 0x00;
    ODCONA = 0x00;
    ODCONB = 0x00;
    ODCONC = 0x00;
    ODCOND = 0x00;

    /**
    SLRCONx registers
    */
    SLRCONA = 0xFF;
    SLRCONB = 0xFF;
    SLRCONC = 0xFF;
    SLRCOND = 0xFF;
    SLRCONE = 0x07;
  
      RC6PPS = 0x10;
   
lcd_init();  //lcd's init
lcd_goto(0);
} // end setup

void main(void)
{
setup_init();
RB0=1;  // turn on bk light
    lcd_goto(0x03);
    lcd_puts("STARTER OP");
    lcd_goto(0x43);
    lcd_puts("VENT VENLIGST!");

    while(1)
  {
             if (KN1)
             {
        lcd_goto(0x00);
                lcd_puts("Ikke     ");
             }
              else
             {
        lcd_goto(0x00);
                 lcd_puts("Trykket 1");
             }
 
             if (KN2)
             {
        lcd_goto(0x40);
                 lcd_puts("Ikke     ");
             }
              else
             {
        lcd_goto(0x40);
                lcd_puts("Trykket 2");
         }
            
             if (KN3)
             {
        lcd_goto(0x14);
                 lcd_puts("Ikke     ");
             }
              else
             {
        lcd_goto(0x14);
                 lcd_puts("Trykket 3");
         }
            
             if (KN4)
             {
        lcd_goto(0x54);
                 lcd_puts("Ikke     ");
             }
              else
             {
        lcd_goto(0x54);
                lcd_puts("Trykket 4");
             }
   

            } //end while
} // end main
Both Digital function and configured as input,
KN1 and KN2 on RC3 and RC2 is both working,
KN3 and KN4 on RA6 and RA7 both allways show 0,
Hardware is ok, both are high, and goes LOW when button pressed.
Reason to connect to RA6-7 is due to design of PCB,
 

Ian Rogers

Joined Dec 12, 2012
1,136
Please read my code. Ian Rogers

Trisa6=1 is that not input ?
Apologies…. I just saw 0's when I looked.... I read RC0 for KN1 for some reason...

When little things like this crop up I tend to focus on the if() condition itself But as PortC is okay and Port A isn't, I'm looking at the OSC pins.. You have to make sure that you set the primary OSC to internal if you need these pins..

What are you using as a clock.... You have XTAL_FREQ as 4Mhz Is this a crystal???
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
RC0 and RC1 are connected to a 32,768 xtal. to give me a second. Secondary osc.

the 4 MHz is from internal.

I moved the KN3 and KN4 to RC4+RC5, and changed them to input, now it works,
 
Top