Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Programming outputs in C PIC16F877

Status
Not open for further replies.

WStevens_sa

Member level 2
Joined
Jan 5, 2011
Messages
47
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
South Africa
Activity points
1,695
Hi
Can somebody please explain the following to me. I found a small program which basically turns an LED on and off.

I know the code below sends an output to PORT A0 with a delay. I know that
0x00 is an address, but what part of the code below actually tells it to send output to PORTA0 and how do I calculate the address for the others for example PORTA 1,2,3,4,5 or PORTB 1,2,3,4,5 and so on or is there a list according to which MCU use use. I have looked through the datasheet but cannot find it.


Thanks

void main()
{
int x = 1;
PORTA = 0x00; /*set RA0-RA5 low */
TRISA = 0x00; /*set PORTA to output*/

while(1)
{
PORTA = !PORTA;
Delay_ms(500);

}
}
 

Hi

The "PORTA=0x00" set the output low. Just as the comment says.
The "TRISA=0x00" set all the ports as outputs.

You can set the individual bits in the port like this:
PORTA=0b10101010

Check out page 39 in the datasheet.

Hope it helps
 
Last edited:

Hi,
PORTA = 0x00;
This clears the initial PORTA state, basically, sending 0 to PORTA register. 0x00 is not a register, it is the value.
TRISA = 0x00;
This tells the microcontroller that PORTA is to be output. You initially have 0x00 at PORTA as you declared that in the last line.
PORTA = !PORTA;
Use
PORTA = ~PORTA;
This tells the microcontroller to invert the state of all PORTA pins. Thus when a PORTA pin is 0, it is turned to 1 and vice versa.
Delay_ms(500);
This is the delay between changing states on PORTA (inversion) so that it is slow enough to be detected by the eye.

Please read the datasheet to understand more.

In the program, all TRISA bits are set to 0, so all PORTA bits are output. Setting a TRISA bit to 1 sets the corresponding PORTA bit to input, and setting it to 0 causes the PORTA bit to be output.
Firstly, all PORTA bits are 0. Then they are all 1. After 500ms, they are all 0 and they keep inverting every 500ms.

Hope this helps.
Tahmid.
 
Last edited:
Hi WStevens_sa in your code one line is incorrect
PORTA = !PORTA; //this line you use "!" operator
PORTA = ~PORTA; //change it to "~" operator
 
Hi,
Also add this line:
Code:
ADCON1 = 7;
Add this line, if you use 16F877A:
Code:
CMCON = 7;
This is because 16F877A has a comparator which 16F877 does not.

PORTA has analog inputs multiplexed, you need to disable them to use the digital functions.
Code:
void main()
{
int x = 1;
ADCON1 = 7; //Disable ADC
//CMCON = 7; //Disable comparator --------------> Only for 16F877A, not 16F877
PORTA = 0x00; /*set RA0-RA5 low */
TRISA = 0x00; /*set PORTA to output*/

while(1)
{
PORTA = ~PORTA;
Delay_ms(500);

}
}

Hope this helps.
Tahmid.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top