Jump to content

Recommended Posts

OK, I give up! I must have missed something in the manual and have this simple problem that I don't understand relating to writing to bits in a port.

 

The code below works fine and shows a 100ms square wave on pin A0;

 

void main()

{

trisa = 0;

while( 1 )

{

porta.0=1;

delay_ms(50);

porta.0=0;

delay_ms(50)

;

}

}

 

The code below doesn't work and is making me crazy. Now I see is the squarewave on A1 and just short (us) pulses on A0.

 

void main()

{

trisa = 0;

while( 1 )

{

porta.0=1;

porta.1=0; // I added this

delay_ms(50);

porta.0=0;

porta.1=1; //and this

delay_ms(50);

}

}

 

It almost looks like the porta.1 operation is resetting A0.

 

What am I missing? Is this part of the much feared read modify write beast? I am sure this is a common problem, but I haven't had much luck searching the forum --mostly because I can't seem to define the problem.

 

In the larger problem, I have a mix of input and output pins on port a -- so I want to write to specific pins as well as read from specific pins.

 

du-uh! :P

 

Rye

Link to post
Share on other sites
OK, I give up! I must have missed something in the manual and have this simple problem that I don't understand relating to writing to bits in a port.

 

The code below works fine and shows a 100ms square wave on pin A0;

 

void main()

{

trisa = 0;

while( 1 )

{

porta.0=1;

delay_ms(50);

porta.0=0;

delay_ms(50)

;

}

}

 

The code below doesn't work and is making me crazy. Now I see is the squarewave on A1 and just short (us) pulses on A0.

 

void main()

{

trisa = 0;

while( 1 )

{

porta.0=1;

porta.1=0; // I added this

delay_ms(50);

porta.0=0;

porta.1=1; //and this

delay_ms(50);

}

}

 

It almost looks like the porta.1 operation is resetting A0.

 

What am I missing? Is this part of the much feared read modify write beast? I am sure this is a common problem, but I haven't had much luck searching the forum --mostly because I can't seem to define the problem.

 

In the larger problem, I have a mix of input and output pins on port a -- so I want to write to specific pins as well as read from specific pins.

 

du-uh! :P

 

Rye

 

Hi ryeg,

First try it on portb, see if it works then use porta.

What chip are you using ? chips such as PIC12C67x, PIC16C7xx, PIC16F87x porta pins will come up as analog pins. to use as digital write adcon1 = 7;

Some chips like 16C620, 621, 16F627,628, have analog comparators on porta & you need to disable them to use them as digital pins, so use comcon = 7;

 

Regards

Raghunathan.

Link to post
Share on other sites

I am using a 16F877 so it probably is the fact that port A can be an analog input. I will read up on ADCON and see if I can make sense of it. It seems to me that I did try port b with success, but didn't follow the clue in the correct direction.

 

I now see the paragraph in the manual about PORTA and ADCON1 / CMCON. Now to sort thru it.

 

The frustrating thing is that I have been successfully using A0 for and output with A1 and A2 set to analog, but adding the second output (A3) screwed things up.

 

Thanks so much for your help.

 

Regards

Rye

 

 

 

Hi ryeg,

First try it on portb, see if it works then use porta.

What chip are you using ? chips such as PIC12C67x, PIC16C7xx, PIC16F87x porta pins will come up as analog pins. to use as digital write adcon1 = 7;

Some chips like 16C620, 621, 16F627,628, have analog comparators on porta & you need to disable them to use them as digital pins, so use comcon = 7;

 

Regards

Raghunathan.

Link to post
Share on other sites

Sorry for the short answers, first time the phone went and second time I got called away then got distracted.

 

Anyhow, it is almost certainly Read Modify Write, the fast A.0 blips on momentarily implies it is being written to and then overwritten by th enext instruction.

 

How does this work?

A PIC has rather crude ports compared to other processors, a read always reads the state of the port pins a write always writes to the output register.

Instructions that modify SFRs read the SFR into a hidden reg, modify the hidden reg and write back to the SFR,

when this happens with a port it is the pins that are read and the result written back to the out put which causes the error you see.

 

PIC 18 have an extra register per port to help with this problem.

 

A solution is to keep an image of the output, modify the image and write that out to the port

EG

char aImage;

aImage = 0;

porta = aImage;

 

...

..

aImage.0 = 1;

aImage.2 = 1;

porta = aImage;

...

aImage | = 3;

porta = aImage;

 

of course PORTA has further complications due to being set to analogue at reset.

 

Another solution is to use a proper processor... whoops...how did that slip out?

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...