View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
Outport_B problem |
Posted: Mon Jun 01, 2015 8:33 am |
|
|
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
|
|
Posted: Mon Jun 01, 2015 1:40 pm |
|
|
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
|
|
Posted: Mon Jun 01, 2015 2:02 pm |
|
|
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
|
|
Posted: Mon Jun 01, 2015 9:20 pm |
|
|
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
|
|
Posted: Mon Jun 01, 2015 11:30 pm |
|
|
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:
Also add this line below your other #include lines:
|
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Tue Jun 02, 2015 3:30 am |
|
|
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
|
|
Posted: Tue Jun 02, 2015 7:41 am |
|
|
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
|
|
Posted: Tue Jun 02, 2015 11:25 am |
|
|
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
|
|
Posted: Wed Jun 03, 2015 7:13 am |
|
|
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
|
|
Posted: Wed Jun 03, 2015 10:09 am |
|
|
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
|
|
Posted: Wed Jun 03, 2015 7:27 pm |
|
|
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. |
|
|
|