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.

[PIC] port b is not stable

Status
Not open for further replies.

GirishRaj

Newbie level 5
Newbie level 5
Joined
Nov 1, 2015
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
68
hi,
i am using pic16f72 &
i am trying to blink some led on port c using port b as an input ,its not functioning properly.some times it is stable and some times it is not.can any one please help i am new to this pic.

additional information
port b high =5
port b low = 2.9v

if i dont use port c leds for blinking i e just make high according to sense .it is functioning properly.
 

See Table 14.2 on the data sheet. You are not meeting the specification for logic low thresholds.

Brian.
 

If the input is from an external circuit either use a comparator with ref voltage or use PICs internal comparator.
 

if i don't use port c leds for blinking i.e just make high or low according to sensors input of b its working fine .

the problem is only when i use delay function for blinking leds,its not stable.
 

Hi,

the problem is only when i use delay function for blinking leds,its not stable.
Then you should show us your code.

Klaus
 

Not working code,
all leds will blink randomly ,
Code:
#include <htc.h>
#include <pic.h>


__CONFIG(WDTEN & PWRTDIS & UNPROTECT \
  & UNPROTECT & BORDIS  );

#define FOSC 8000000L


void delay_ms(unsigned int cnt)
{
  unsigned int i;
  for(i=0;i<cnt;i++);
}




//output
#define LEDTF1 RC4
#define LEDTF2 RC5
#define LEDTF3 RC6
#define LEDTL1 RC0
#define LEDTL2 RC1
#define LEDTL3 RC2
#define LEDTL4 RC3

//INPUT
//#define SENS1 RB0
//#define SENS2 RB1
//#define SENS3 RB2
#define SENTL1 RB4
#define SENTL2 RB5
#define SENTL3 RB6
#define SENTL4 RB7
#define SENTF1 RB3


void main(void)
{

 // ADCON1=0b00000111;

  //TRISA=0X18;//porta 0th bit output
  TRISB=0b11111111;//portb has input
  TRISC=0b00000000;//portc as output
  
 LEDTL1=0;
 LEDTL2=0;
 LEDTL3=0;
 LEDTL4=0;
 LEDTF1=0;
 LEDTF2=0;
 LEDTF3=0;



 SENTL1=1;
 SENTL2=1;
 SENTL3=1;
 SENTL4=1;
 SENTF1=1;
 

while(1)
{
   if(SENTL1==0)
   {LEDTL1=1;}
   else
   {LEDTL1=0;}
   
   if(SENTL2==0)
   {LEDTL2=1;}
   else
   {LEDTL2=0;}

   if(SENTL3==0) 
   {LEDTL3=1;}
   else
   {LEDTL3=0;}
   
   if(SENTL4==0)
   {LEDTL4=1;}
   else
   {LEDTL4=0;}
     


if(SENTF1==1)
{ 
LEDTF1=1; LEDTF2=1; LEDTF3=1;
NOP();
delay_ms(55000);

LEDTF1=0; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);


 }
else 
{
LEDTF1=0; LEDTF2=0; LEDTF3=1;
NOP();
delay_ms(55000);
LEDTF1=0; LEDTF2=1; LEDTF3=0;
NOP();
delay_ms(55000);
LEDTF1=1; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);
}



}//while
}//main

working code
in this code i have disabled blinking function, it works properly according to code
Code:
#include <htc.h>
#include <pic.h>


__CONFIG(WDTEN & PWRTDIS & UNPROTECT \
  & UNPROTECT & BORDIS  );

#define FOSC 8000000L


void delay_ms(unsigned int cnt)
{
  unsigned int i;
  for(i=0;i<cnt;i++);
}




//output
#define LEDTF1 RC4
#define LEDTF2 RC5
#define LEDTF3 RC6
#define LEDTL1 RC0
#define LEDTL2 RC1
#define LEDTL3 RC2
#define LEDTL4 RC3

//INPUT
//#define SENS1 RB0
//#define SENS2 RB1
//#define SENS3 RB2
#define SENTL1 RB4
#define SENTL2 RB5
#define SENTL3 RB6
#define SENTL4 RB7
#define SENTF1 RB3


void main(void)
{

 // ADCON1=0b00000111;

  //TRISA=0X18;//porta 0th bit output
  TRISB=0b11111111;//portb has input
  TRISC=0b00000000;//portc as output
  
 LEDTL1=0;
 LEDTL2=0;
 LEDTL3=0;
 LEDTL4=0;
 LEDTF1=0;
 LEDTF2=0;
 LEDTF3=0;



 SENTL1=1;
 SENTL2=1;
 SENTL3=1;
 SENTL4=1;
 SENTF1=1;
 

while(1)
{
   if(SENTL1==0)
   {LEDTL1=1;}
   else
   {LEDTL1=0;}
   
   if(SENTL2==0)
   {LEDTL2=1;}
   else
   {LEDTL2=0;}

   if(SENTL3==0) 
   {LEDTL3=1;}
   else
   {LEDTL3=0;}
   
   if(SENTL4==0)
   {LEDTL4=1;}
   else
   {LEDTL4=0;}
     


/*if(SENTF1==1)
{ 
LEDTF1=1; LEDTF2=1; LEDTF3=1;
NOP();
delay_ms(55000);

LEDTF1=0; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);


 }
else 
{
LEDTF1=0; LEDTF2=0; LEDTF3=1;
NOP();
delay_ms(55000);
LEDTF1=0; LEDTF2=1; LEDTF3=0;
NOP();
delay_ms(55000);
LEDTF1=1; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);
}
*/

}//while
}//main
 

You enable WDT, but you don't clear it. So your PIC is reseting...
Eigther disable WDT in the config, or use CLRWDT to reset it before it resets ... you
 

Thanks,
but it didn't work.issue remains same.
since,i am using RC oscillator( R-10K AND C-10PF)I have declared oscillator as 10 MHz according to calculation.
is there any thing i have to change in oscillator declaration.
 

hello,

With internal RC oscillator, you must use Cext > 20pF
and limit the FOSC to a lower value <= 4MHz
else you have unstable oscillator and "hands effects" !

best is to use a quartz !
 

if i don't use port c leds for blinking i.e just make high or low according to sensors input of b its working fine .

the problem is only when i use delay function for blinking leds,its not stable.

Thank you,
but i have tried with 8,12 and 20 Mhz crystal ,
the problem remains same
 

did you follow advise of Xenos post #7
concerne WDTEN : <- to cancel !
else during big delay you can get Watchdog reset ...
 

yes, i did it .The problem remains same
 

why did you add a NOP(); just before a delay of more than 55000 cycles ?
is NOP(); a classic & normal C instruction ?
or use of something like asm nop;

is delay_ms a reserved word ?

i don't have your PIC for test it..
your program run OK with MikroC and a PIC18..
except i used MikroC Delay_ms(500);
and asm nop; even not usefull !
 

I think it's time for you to post a fresh copy of your code.
A schematics would be useful also.
 

NOP is just for alting.Even if i remove the NOP ,problem remains same.
Code:
#include <htc.h>
#include <pic.h>


__CONFIG(WDTDIS & PWRTDIS & UNPROTECT \
  & UNPROTECT & BORDIS  );

#define FOSC 10000000L


void delay_ms(unsigned int cnt)
{
  unsigned int i;
  for(i=0;i<cnt;i++);
}




//output
#define LEDTF1 RC4
#define LEDTF2 RC5
#define LEDTF3 RC6
#define LEDTL1 RC0
#define LEDTL2 RC1
#define LEDTL3 RC2
#define LEDTL4 RC3

//INPUT
//#define SENS1 RB0
//#define SENS2 RB1
//#define SENS3 RB2
#define SENTL1 RB4
#define SENTL2 RB5
#define SENTL3 RB6
#define SENTL4 RB7
#define SENTF1 RB3


void main(void)
{

 // ADCON1=0b00000111;

  //TRISA=0X18;//porta 0th bit output
  TRISB=0b11111111;//portb has input
  TRISC=0b00000000;//portc as output
  
 LEDTL1=0;
 LEDTL2=0;
 LEDTL3=0;
 LEDTL4=0;
 LEDTF1=0;
 LEDTF2=0;
 LEDTF3=0;



 SENTL1=1;
 SENTL2=1;
 SENTL3=1;
 SENTL4=1;
 SENTF1=1;
 

while(1)
{
   if(SENTL1==0)
   {LEDTL1=1;}
   else
   {LEDTL1=0;}
   
   if(SENTL2==0)
   {LEDTL2=1;}
   else
   {LEDTL2=0;}

   if(SENTL3==0) 
   {LEDTL3=1;}
   else
   {LEDTL3=0;}
   
   if(SENTL4==0)
   {LEDTL4=1;}
   else
   {LEDTL4=0;}
     


if(SENTF1==1)
{ 
LEDTF1=1; LEDTF2=1; LEDTF3=1;
NOP();
delay_ms(55000);

LEDTF1=0; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);


 }
else 
{
LEDTF1=0; LEDTF2=0; LEDTF3=1;
NOP();
delay_ms(55000);
LEDTF1=0; LEDTF2=1; LEDTF3=0;
NOP();
delay_ms(55000);
LEDTF1=1; LEDTF2=0; LEDTF3=0;
NOP();
delay_ms(55000);
}



}//while
}//main

SCHEMATIC is just bc547 transistor collector is connected to port pin b and emitter is connected to ground and for base 10 k resistor is connected with sensing signal.
 

Hi,

SCHEMATIC is just bc547 transistor collector is connected to port pin b and emitter is connected to ground and for base 10 k resistor is connected with sensing signal.

I wonder how you can store your program into the bjt, or do you store it into the 10k?


I think there is at least a microcontroller, power supply, some capacitors.....


Klaus
 

@GirishRaj

In my humble opinion you have a classic RMW (Read-Modify-Write) problem (usual issue when using a PIC16).

My advice (sorry but I don't know the CCS syntax):

- use a port-shadow char variable (in this case for example: char Portc_Shadow;)

- define your leds as its bits (eg #define LEDTL1 4 //RC4

- manipulate the so defined shadow bits (instead of the PORTC bits), eg setbit(Portc_Shadow, LEDTL1);

- after every led manipulation group modify once the real port (eg PORTC = Portc_Shadow;)


This means that you have to use only five PORTC writings in your main loop:
- first immediately before the flashing (after the first group),
- then (in the flashing) before every delaying (instead of the NOPs :|)

Of course you have to initialize the port and also its shadow var (eg PORTC = 0; Portc_Shadow = 0;) at begin of the main.

Hope this helps.


@paulfjujo
On a PIC18 the RMW issue was eliminated, using the LATx bits for output. But if you are using the PORTx bits instead, then the problem also exists.
 
Last edited:

@GirishRaj

@paulfjujo
On a PIC18 the RMW issue was eliminated, using the LATx bits for output.
But if you are using the PORTx bits instead, then the problem also exists.

:thumbsup: you are right !

so i understand now, why it was a NOP(); before the big delay_ms(55000);

and the countermeasure could be , juts to insert a nop(); between each access of an output port.

Code:
if(SENTF1==1)
{ 
LEDTF1=1;NOP();
 LEDTF2=1;NOP();
 LEDTF3=1;NOP();
delay_ms(55000);
LEDTF1=0;NOP();
 LEDTF2=0;NOP(); 
LEDTF3=0;NOP();
delay_ms(55000);
 }
.. etc ....
 

... insert a nop(); between each access of an output port ...

May be, but this solution is very clock-dependent: it may working with 4 MHz or slower clock but does not with 12 MHz and beyond, especially when the output pins have more capacitive load eg due to the long wires.

All (non-enhanced PIC16) cases use a port-shadow variable as I wrote above, this is the real, sure solution.

Larger delays (5-15 us, after each output port access) also can help but this method slows (and increases) the program.
 
Last edited:

Hi ,
Thank you all for your support and concern regarding this issue.
i got this issue cleared by one of my guru.
There was some problem with my code and he corrected it.

Thank you all once again.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top