CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Simple source code problem for PIC16F628A

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Eric85
Guest







Simple source code problem for PIC16F628A
PostPosted: Thu Feb 28, 2008 8:46 am     Reply with quote

i have a problem in program PIC16F628A. The output result getting a strange result.
Below is my source code:

#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

void main()
{
set_tris_b(0x0F); //RB7-4 is output, RB3-0 is input port
while (true)
{
if( input(PIN_b0) ) //if input pin RB0 is high
output_high (pin_b6); //Only output pin RB6 is high
else
output_high (pin_b7); //output pin RB6 is low
}
}

From what i understanding,
when RB0 is low, RB7 is high and if RB0 is high, RB6 is high also. Which mean if RB0 is shifting from low to high, both RB6 and RB7 should be high right?

but the actual result is, if i shift RB0 to low, RB7 high and RB6 low. if i shift RB0 to high, RB6 high and RB7 low. Since i didn't set RB6 and RB7 to low, how come RB6 and RB7 go low??

Please give any suggestion. Thanks for your help.
Ttelmah
Guest







PostPosted: Thu Feb 28, 2008 9:31 am     Reply with quote

What is actually _connected_ to the pins?. What voltage are you defining as 'high'?.
This actually sounds like the PIC 'RMW' problem (a search here will find a lot of references to this). If (for instance), the lines are connected to LED's, without a suitable load resistor, though the signal will go 'on' if you turn a pin on, and enable the LED, the LED, will clamp the voltage 'seen' by the chip, so it never actually gets to a logic 'high'. Whenever you write a single pin, the chip internally performs a 'read modify write' cycle (hence the name), and if the other pins are not actually 'high', will turn them of!...

Best Wishes
Eric85
Guest







Simple source code problem for PIC16F628A
PostPosted: Thu Feb 28, 2008 10:46 am     Reply with quote

my RB0 is connected to a switch and RB6 and RB7 is LED light.
'High' here i mean LED light turn ON (or 5V).

ya, u r right. my LED light is without connected to any resistor, therefore 'RMW' happend.

after i connected two resistors to both RB6 and RB7, my problem solved.

Thanks for your help.
Txtx.
EricKoh1985



Joined: 03 Mar 2008
Posts: 8

View user's profile Send private message

PostPosted: Mon Mar 03, 2008 8:23 am     Reply with quote

i face another problems again.

my picture link is: http://www.imagehosting.com/show.php...6f628.JPG.html

this is my code:
Code:

#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)

Void Main()
{
Set_Tris_B(0x0F);

While(1)
  {
   if (Input(PIN_B0))
      Output_Bit(PIN_B6,1);

   else
      Output_Bit(PIN_B5,1);
  }
}

At default, before press RB0's switch, oni RB5's LED will "High" right?!
But why RB5 and RB7 will high oso??
Since i didn't declare RB7 to "High", why RB7 will "High"?
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Mon Mar 03, 2008 9:15 am     Reply with quote

Your image link does not work.
EricKoh1985



Joined: 03 Mar 2008
Posts: 8

View user's profile Send private message

PostPosted: Mon Mar 03, 2008 10:03 am     Reply with quote

Sorry,this is the link
Link: http://www.imagehosting.com/show.php/1610469_16f628.JPG.html
Ttelmah
Guest







PostPosted: Mon Mar 03, 2008 11:50 am     Reply with quote

Look in the data sheet. Look at what the _default_ values are for the port B register. Note that these are 'undefined'. The port pins will wake up in an indeterminate state. In fact the chip wakes up with the _TRIS_ bits all set as '1' (inputs), but the values in the data latch are undefined. As soon as you change the TRIS, these values will appear on the output pins.
Change your code to:
Code:

#include <16F628A.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use fast_io(B) //otherwise I/O operations will _override_ your
//tris settings.

void main(void) {
   output_b(0); //set all the latch bits to 0
   set_tris_b(0x0F); //RB7-4 is output, RB3-0 is input port
   while (true) {
       if( input(PIN_b0) ) //if input pin RB0 is high
           output_high (pin_b6); //Only output pin RB6 is high
       else
           output_high (pin_b7); //output pin RB6 is low
   }
}

Now the value will be defined as '0'...

Best Wishes
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Mon Mar 03, 2008 12:24 pm     Reply with quote

Until you are confident of what you are doing there is less grief if you use the CCS defaults. Unless you absolutely need fast_io don't mess with the tris bits. CCS has byte outputs and inputs and pin specific outputs and inputs.
The compiler will handle the TRIS stuff for you. If you are confident and believe you can improve on the compiler's way of doing things then jump in with fast_io and override the compiler. Now you may be asking what is faster about fast I/O. Well the compiler sets the correct TRIS each and every time for you. Now if you know that the TRIS hasn't changed between I/O then you will see the compiler as inserting redundant TRIS statements, so faster is less redundant TRIS statements. Most often this is a small improvement and not always worth bothering with especially since the consequence of leaving out a manual TRIS is onerous.
arunb



Joined: 08 Sep 2003
Posts: 492
Location: India

View user's profile Send private message Send e-mail

RE:
PostPosted: Tue Mar 04, 2008 12:43 am     Reply with quote

Hi,

Set the tris bits first then make the outputs low.
Make RB7 low, when RB0 is high and when RB0 goes low, make RB6 low

Hope this helps

thanks
arunb
Ttelmah
Guest







PostPosted: Tue Mar 04, 2008 3:03 am     Reply with quote

Actually, Arun, it is slightly _better_ practice, to set the output bits first, then set the TRIS (as I show). This way, the pins go straight from being floating (as inputs), to pulling low. If you set TRIS first, then set the output latch, you get floating, then undefined (pulling high/low, according to what is in the latches), then finally pulling low. In 'normal' use, this perhaps doesn't matter, for the few nSec involved, but if (for instance), the pins connect to power drivers, where having all enabled together, leads to a potential overload condition, it can be dangerous. The 'trick', in this case, is to have very large resistors on the pins (1MR), pulling them to the disabled state, when they are acting as inputs, then setup the 'safe' pattern on the output latch, and finally enable the TRIS. This avoids any nasty glitches when the processor starts. Smile

Best Wishes
EricKoh1985



Joined: 03 Mar 2008
Posts: 8

View user's profile Send private message

PostPosted: Sun Mar 09, 2008 11:12 am     Reply with quote

Thanks for all of your information.
With using the code given by Ttelmah, my circuit is able to function well.
Thanks.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group