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.

How to output a nibble on PORT C using PIC-C

Status
Not open for further replies.

techie

Advanced Member level 3
Joined
Feb 5, 2002
Messages
839
Helped
58
Reputation
116
Reaction score
9
Trophy points
1,298
Location
Pakistan
Activity points
7,805
pic 18 port registers latch

PIC-C
PIC16F873
RC7,RC6,RC4,RC3 connected to LCD port (4-bit)
RC3,RC2 connected to 2 relays
RC1,RC0 connected to 2 Buttons (Input)

How do I output 4-bit data on RC7-RC4 without disturbing the rest of the port pins
 

c nibble

First read a Portb, then modify your bits, then make a write. In this way, only your bits get modified, others stay the same. Can somebody else also confirm this?
 

nibble output

Code:
Temp = PORTC;    /* Read port */
Temp &= 0x0f;    /* Clear high bits, keep low bits */
Temp |= (newvalue << 4);  /* Shift 4-bit new value to hi nibble and and set/clear bits */
PORTC = Temp;  /* Write new value to port */
 

    techie

    Points: 2
    Helpful Answer Positive Rating
how to output a nibble in pic

this code asummes that you set the PORTC as "FAST_IO" so it will not change your TRISC configuration
Code:
#use FAST_IO(C)

void portc_init(void){
 set_tris_c(0x03);//lower bits are inputs...
}

void lcd_out4(unsigned char nibble){
  output_c((nibble<<4)|(input_c()&0x0F));
}

it will output the lower nibble of the argument..
could be also a macro...

for a lcd command... [pseudo code...] asumes
Code:
void lcd_cmd(unsigned char cmd){
 lcd_rs(0):
 lcd_out4(cmd>>4); //higher nibble
 lcd_en_pulse();
 lcd_out4(cmd); //lower nibble could be (cmd&0x0F)
 lcd_en_pulse();
 lcd_wait();
}

hope it helps...
 

read port pic

There is a problem with the technique suggestd by "btbass". It changes direction of the port before reading it so it returns the status on the pins which is always zero. So the RC2 and RC3 go back to low state.

I am a little unsure about the method suggested by Kurenai. It gives different result than expected.
 

clean output registers with picc

Hi,
Never believe what you read on the port pins unless they are configured as INPUTS.

Port Pin read-after-writes can cause misery and problems - especially if there is any capacitive or significant load on the output pins.

If you want robust and reliable code use a port-clone-register.
Do all your read-modify-write actions on the clone-register THEN write it to the port (pins).

This applies to all programming languages and processors.

This situation is helped by processors which have a port LATCH register (e.g. 18FXXXX series) where the Latch register acts as the output-clone.

However - as the port latch if often part of the port silicon structure it is not unknown nor uncommon for external circuitry to cause an output pin to glitch into a changed latched state.

I am sure you all backup you important design files - then why not do the same for important and essential data files in a micro?

Polymath
 

how to read an output port in pic

Ended up using bit by bit update of 4 port pins. The code looks ugly but works.
 

#DEFINE S0 PIN_B4
#DEFINE S1 PIN_B5
#DEFINE S2 PIN_B6
#DEFINE S3 PIN_B7
/////////change pin as required///////
void out(int x) {
output_bit(S0, x%2); ////////////out(x); function in the code to output the nibble////////////////////
output_bit(S1, (x/2)%2);
output_bit(S2, (x/4)%2);
output_bit(S3, (x/8)%2);
delay_ms(100);

}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top