If switch 1 (RA1) as active low input and LED (RA6) as active high output

Discussion in 'Embedded Systems and Microcontrollers' started by CenaRock, Mar 25, 2016.

  1. CenaRock

    Thread Starter New Member

    Mar 25, 2016
    4
    0
    If switch 1 (RA1) as active low input and LED (RA6) as active high output, when switch 1 pressed, the LED on; but when switch 1 released, the LED will off.
    Can anyone help me for the program?
     
  2. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    You will have to tell people what uC you are using.
     
  3. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    In addition to what uC we would also need to know what you are writing it in? Assembler? Compiler? Which?

    The code would follow a scheme such as this:


    Code (Text):
    1. { Set up hardware }
    2.  
    3. Begin:
    4.  
    5.     { Set RA1 as input }
    6.     { Set RA6 as output }
    7.  
    8. Loop:
    9.  
    10.     { Set RA6 = (not) RA1}
    11.  
    12.     goto Loop
    Note to Moderators: this is NOT C
     
    Last edited: Mar 25, 2016
  4. CenaRock

    Thread Starter New Member

    Mar 25, 2016
    4
    0
    For embedded programming guys
    Problem is the RA6 also as OSC2/CLK0
    When I execute the program it say 'undefined identifier RA6'
     
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    Then you just need to get it defined.
     
  6. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    Sounds like you're using a PIC of some sort. If you tell me which chip and which IDE you're using I can help you further. You need to include a header file that defines constants such as RA6. Also, you'll have to setup the configuration bits, in addition to setting the TRISA register, to define RA6 as a GPIO pin and not a clock pulse.
     
  7. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    I wouldn't recommend a goto statement.
     
  8. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    Could you quote another possibility guaranteed to be in every language?
     
  9. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    You are being highly unfair. It is obvious that odm can read Cena's mind.
     
    ErnieM likes this.
  10. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    Given the names of the MCU pins and the error encountered, I'm pretty sure the OP is using a PIC chip and a C environment. A goto statement in C is usually a last resort option, I wasn't bashing anyone just stating I would not recommend using a goto in this case.
     
    Last edited: Mar 25, 2016
  11. CenaRock

    Thread Starter New Member

    Mar 25, 2016
    4
    0
    PIC16F877A
    And I use MPLAB IDE v8.83 as program the solution above
    And I'm just 17... so that's many things I don't know about too much...
     
  12. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    Use the wizard to start a new project and make sure you have the correct chip and compiler selected. In your main.c you need #include <pic16f877a.h>, then you will not get the "undefined identifier error".

    Also, as far as I can tell one the datasheet you do not have a RA6. http://ww1.microchip.com/downloads/en/DeviceDoc/39582C.pdf
     
  13. Picbuster

    Member

    Dec 2, 2013
    376
    50
    Hi, please provide schematic. reason: to find out how you connect the lot and more are you using an external xtal?
    Microchip has a lot of demo programs like setting a led on/off for this chip.
    download it and study how things work. Pay attention to Tris defining the in and outputs., the clock: how things are moved around in pic's belly.
    Please start with the microchip mplab plus C compiler (xc8) forget all the other brand compilers for the pic. (not because they are bad but stick to one until you understand all ins and outs and are able create good working programs)
    Yes, it will take a while but it is not difficult and even more..... good fun.
    GOOD LUCK
    Picbuster
     
  14. CenaRock

    Thread Starter New Member

    Mar 25, 2016
    4
    0
    But how about the OSC2 which also mean as RA6?
     
  15. odm4286

    Active Member

    Sep 20, 2009
    155
    5
    I don't see a RA6 on that pin. What makes you think OSC2 means RA6?
    [​IMG]
     
    • pic.png
      pic.png
      File size:
      20.1 KB
      Views:
      3
    Last edited: Mar 27, 2016
  16. bertus

    Administrator

    Apr 5, 2008
    15,648
    2,348
    Hello,

    There are no portnumbers given at OSC1 and OSC2:

    16F877A_intern.jpeg

    RA6 is NOT in the map.

    Bertus
     
  17. dannyf

    Well-Known Member

    Sep 13, 2015
    1,825
    364
    Is it possible that his PIC is made by Micr0chip? :)

    I think the OP will benefit far more if you teach him how to ask for help instead.
     
  18. JohnInTX

    Moderator

    Jun 26, 2012
    2,347
    1,029
    As the others have observed, the '877A does not have the ability to use one of the OSC pins as RA6. The 16F887A and others do, but not this one. It's important to look carefully at the datasheet for the exact PIC you are using. Many features are exactly the same from PIC to PIC but those subtle differences are why the part numbers are different.

    When you DO have a PIC that shares the oscillator pins with IO, you select between IO and OSC with the Configuration Bits register described in the '877 datasheet in chapter 14. The CONFIG register also controls other, non-optional things as well so take a close look at it. MPLABX makes setting the various CONFIG options easy if you open Window->Pic Memory Views -> Configuration Bits. All options for the specific PIC will be shown in a window. Select what you want then click Generate Source to Output and paste the resulting text into your program. The CONFIG bits will be programmed when you blast the chip.

    Be sure to visit the tables at the end of each PORT's description in the datasheet to see what alternate pin functions you may have to deal with in your firmware. For example most PORTA pins are assigned to the ADC on power up and must be explicitly set to be digital in your code by writing to ADCON1. Chapters 4 (IO Ports) and 11 (ADC) cover these things.

    If you are starting out with the 877, why not use PORTD? Its the easiest one to work with since its all digital from the get-go.

    Good luck.
     
Loading...