Pic uController Problem: reading input

Discussion in 'Embedded Systems and Microcontrollers' started by PRS, Nov 19, 2010.

  1. PRS

    Thread Starter Well-Known Member

    Aug 24, 2008
    I'm just learning how to program using PicKit3 and Microchip's C18. I'm trying to read a byte of data on the C port of a PIC18f45k20, but I'm not getting the byte I'm inputting to the port. Can someone tell me what I'm doing wrong? Here's my code: (I used number2 just to set a break-point; it has no useful function).

    /** C O N F I G U R A T I O N B I T S ******************************/

    #pragma config FOSC = INTIO67, FCMEN = OFF, IESO = OFF // CONFIG1H
    #pragma config PWRT = OFF, BOREN = SBORDIS, BORV = 30 // CONFIG2L
    #pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H
    #pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = ON, CCP2MX = PORTC // CONFIG3H
    #pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
    #pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
    #pragma config CPB = OFF, CPD = OFF // CONFIG5H
    #pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
    #pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
    #pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
    #pragma config EBTRB = OFF // CONFIG7H

    /** I N C L U D E S **************************************************/
    #include "p18f46k20.h"
    // include files go here....

    /** V A R I A B L E S *************************************************/
    #pragma udata // declare statically allocated uninitialized variables
    unsigned char number, number2;

    /** D E C L A R A T I O N S *******************************************/

    #pragma code // declare executable instructions
    void main (void)
    TRISC = 0B11111111; // Port C is input
    number = 0;
    number = LATC;
    number2 = 3;

    while (1)
  2. Markd77

    Senior Member

    Sep 7, 2009
    I think LATC is for output only, you need to read PORTC.
  3. mik3

    Senior Member

    Feb 4, 2008
    The LATx register is readable too, however, the compiler might not support that function.
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    If you have the PIC18f45k20 then you might have the demo board and lessons. Take a look at lesson 4.
  5. PRS

    Thread Starter Well-Known Member

    Aug 24, 2008
    You're right, Mark. Thank you very much! I got up this morning and, after reading your post, changed LATC to PORTC and experimentally proved it was reading the input correctly. I can now read and write data to a breadboard! My goal is to make an EEPROM programmer. The next step is to configure a single port with various inputs and outputs. Thanks again! :)