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

Outport_B problem

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

Outport_B problem
PostPosted: Mon Jun 01, 2015 8:33 am     Reply with quote

Hai,

I have trouble with my pic. When the first time I entered number 12 after "Enter command: " display appear, output from port B0 and B1 will be high, but when the second time after "Enter command: " I enter 0, out B0 and B1 remain high, supposed B0 and B1 should be low. What cause this problem? Please help me to solve it.

code as below:
Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <stdio.h>
#int_rb


void detect_rb_change() {


while(input(PIN_B0)) ;     

   delay_ms(25);               

}

void main()
{

    char  x[8] ;
    char  ba[8]="0" ;
    char  bb[8]="1" ;
    char  bc[8]="12" ;
{

while(true)
{

enable_interrupts(int_rb);
enable_interrupts(global);


      printf("\rEnter command: \n");
    gets(x);
     
      if(strcmp(x,ba)==0)
         output_B(0b00000000);

      if(strcmp(x,bb)==0)
         output_B(0b00000001);

       if(strcmp(x,bc)==0)
        output_B(0b00000011);

}
}
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Jun 01, 2015 1:40 pm     Reply with quote

here is the TIP of your iceberg of trouble:

* need ERRORS in #use rs232
* NO DELAY() inside ISR
* ENABLE INTS on the wrong side of the while loop in main
* GETS() is a risk_OF_HANGING CALL and good programmers deprecate its use.
* possible interaction between YOUR OUTBUT_B() and ISR INPUT pin_B0,
RE: CCS auto- TRIS !!!

the concept you are following is unclear too, and i wonder if i am looking at Proteus ISIS school task...........
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 2:02 pm     Reply with quote

Quote:
When the first time I entered number 12 after "Enter command: " display appear, output from port B0 and B1 will be high

if(strcmp(x,bc)==0)
output_B(0b00000011);

I don't know what the initial state of PortB is. You did not initialize it.
But apparently, setting PortB to 0b00000011 causes an #int_rb interrupt.
So now you are in the following routine:
Quote:
#int_rb
void detect_rb_change() {

while(input(PIN_B0)) ;

delay_ms(25);

}

But you have a while() loop which runs as long as Pin B0 is at a high level.
Since your previous code set PortB to 0b00000011, Pin B0 is high.
Therefore, your program will stay in this loop forever:
Code:
while(input(PIN_B0));   // Always read 1 from Pin B0   

Your program can't execute any other code.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 9:20 pm     Reply with quote

I've change my code, but the result still the same. Which part is wrong?
Code:
Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <string.h>
#include <stdio.h>     


void main()
{

    char  x[8] ;
    char  ba[8]="0" ;
    char  bb[8]="1" ;
    char  bc[8]="12" ;

byte i;
for (i=1, i<=30;++i)
{


      printf("\rEnter command: \n");
    gets(x);
     
      if(strcmp(x,ba)==0)
         output_B(0b00000000);

      if(strcmp(x,bb)==0)
         output_B(0b00000001);

       if(strcmp(x,bc)==0)
        output_B(0b00000011);

}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 01, 2015 11:30 pm     Reply with quote

The problem is caused by gets() putting a 0x0d after the incoming
characters. To fix this, you can substitute the following line for the
gets(x) line:
Code:
get_string(x, 8);

Also add this line below your other #include lines:
Code:
#include <input.c>
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 3:30 am     Reply with quote

Thank you pcm programmer. I have test it , it can work but if first i send command 12, B0 and B1 will become high and then i send command 1, B0 and B1 still high. B0 and B1 will only become low when i send command 0.
If after that i send command 1, only B0 will become high. why is this happen?
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jun 02, 2015 7:41 am     Reply with quote

is this Proteus ISIS testing or testing with hardware?

you still lack the ERRORS command in your #use RS232 declaration.......
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 11:25 am     Reply with quote

Quote:
I have test it , it can work but if first i send command 12, B0 and B1 will
become high and then i send command 1, B0 and B1 still high. B0 and B1
will only become low when i send command 0.
If after that i send command 1, only B0 will become high. why is this happen?

It works OK for me. I am testing it in hardware, in a PicDem2-Plus board
(non-rohs version). This board has LEDs on pins B0 to B3. They behave
correctly when I type 0, 1, or 12 into TeraTerm and press the Enter key
to send the data to the board.

You may have a hardware problem. Or, you may not be using hardware.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 7:13 am     Reply with quote

Thank you for your reply. When i send command 0,1 or 12 the B0 and B1 it is working properly . The main problem here is when i send command 12 and then i send command 1, B0 and B1 still both high, suppose when i send command 1, B0 become high while B1is low. Did you encounter this problem? Kindly please reply.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 10:09 am     Reply with quote

Quote:

The main problem here is when i send command 12 and then i send
command 1, B0 and B1 still both high,
suppose when i send command 1,
B0 become high while B1is low. Did you encounter this problem? Kindly
please reply.


When I power-up the board, both LEDs are off. When I send 12 with
TeraTerm (type 12 and press enter), both LEDs go on. When I next
type 1 and press Enter, the LED on pin B1 goes off, but the LED on pin B0
stays on. This is correct.

If it doesn't work for you, either your hardware is incorrect, or you are
using Proteus (Isis) and it's incorrect.

The LED circuits on my PicDem2-Plus board look like this:
Code:

PIC      470 ohms      LED       
pin -----/\/\/\/------->|----
                            |
                            |
                          -----  Ground 
                           ---
                            -

Each pin (B0 to B3) has an identical circuit, as shown above.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 7:27 pm     Reply with quote

Thank you for your reply, i've compare the picdem2 hardware with my own hardware. My hardware is the problem.

Thank you very much. My problem already solve. You are a very good programmer and a nice teacher. Thanks again.
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