Beginner PIC I/O (16F887, MPLAB, Hi-Tech C)

Discussion in 'Embedded Systems and Microcontrollers' started by Recon, Jan 9, 2012.

  1. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    I am trying to write a basic program to turn on an LED when an input is high. I managed to make an LED flash thanks to people on this forum :), but I'm having trouble with my next simple program.

    LED Flashing Program (works)
    Code ( (Unknown Language)):
    1. #include "htc.h"
    2. #define _XTAL_FREQ 4000000
    3.  
    4. __CONFIG(FOSC_INTRC_NOCLKOUT & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF & BOR4V_BOR40V & WRT_OFF);
    5.  
    6. int main(void) {
    7.  
    8.     TRISA = 0b11111110; //RA0 is output, others are inputs
    9.  
    10.     for(;;) {
    11.             RA0 = 1;    
    12.             __delay_ms(100);
    13.         __delay_ms(100);
    14.             RA0 = 0;
    15.         __delay_ms(100);
    16.         __delay_ms(100);
    17.     }
    18. }
    LED I/O Program (doesn't work)
    Code ( (Unknown Language)):
    1. #include "htc.h"
    2. #define _XTAL_FREQ 4000000
    3.  
    4. __CONFIG(FOSC_INTRC_NOCLKOUT & WDTE_OFF & PWRTE_ON & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_OFF & IESO_OFF & FCMEN_OFF & LVP_OFF & DEBUG_OFF & BOR4V_BOR40V & WRT_OFF);
    5.  
    6. int main(void) {
    7.  
    8.     TRISA = 0b11111110; //RA0 is output, others are inputs
    9.  
    10.     for(;;) {
    11.         if(RA1 == 1){
    12.         RA0 = 1;
    13.         }
    14.         else {
    15.         RA0 = 0;
    16.         }
    17.     }
    18. }
    I am making RA1 "high" by connecting it to +v through a 10k resistor. Is this correct?
     
  2. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
    Now I think its time for a schematic....
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    On every PIC with analog functions on pins. These pins will default to analog functions after power up. And here comes the downside of this. Then using PORTA
    Then working with PICs it is very important to have the datasheet at hand all the time. You do not have to read from start to finish. But then you use any functions. As in your case PORTA. You must confer with the datasheet. To be sure everything is set up correct. If you move to PORTD or PORTE your setup should work as is. If you use PORTA. You must insert the line​
    Code ( (Unknown Language)):
    1.  
    2. [LEFT]ANSEL=0; [/LEFT]
    3.  
    Before the endless loop​
     
    Recon likes this.
  4. Recon

    Thread Starter Member

    Oct 16, 2009
    30
    0
    Thanks, t06afre, that worked. :D

    I use the datasheet as much as I can, but admittedly a lot of it is still over my head. :rolleyes:
     
Loading...