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

Thread Starter

CenaRock

Joined Mar 25, 2016
4
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?
 

ErnieM

Joined Apr 24, 2011
8,377
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:
{ Set up hardware }

Begin:

    { Set RA1 as input }
    { Set RA6 as output }

Loop:

    { Set RA6 = (not) RA1}

    goto Loop
Note to Moderators: this is NOT C
 
Last edited:

Thread Starter

CenaRock

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

odm4286

Joined Sep 20, 2009
265
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.
 

odm4286

Joined Sep 20, 2009
265
In addition to what uC we would also need to know what you are3 writing it in? Assembler? Compiler? Which?

The code would follow a scheme such as this:


C:
{ Set up hardware }

Begin:

    { Set RA1 as input }
    { Set RA6 as output }

Loop:

    { Set RA6 = (not) RA1}

    goto Loop
Moderators note: used code tags for C
I wouldn't recommend a goto statement.
 

odm4286

Joined Sep 20, 2009
265
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:

Thread Starter

CenaRock

Joined Mar 25, 2016
4
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...
 

Picbuster

Joined Dec 2, 2013
1,047
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...
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
 

dannyf

Joined Sep 13, 2015
2,197
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.
 

JohnInTX

Joined Jun 26, 2012
4,787
But how about the OSC2 which also mean as RA6?
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.
 
Top