PDA

View Full Version : Pic16f877 output drop



nitinmathew2007
02-26-2011, 07:57 PM
I am surprised after seeing that the output of my PORTB is 3.24V,2.51V,1.78V,1.05V!!
I use MPLAB IDE with HiTEch Compiler. If I give
TRISB=0x00;
PORTB=0xFF; or anything
I get the output at corresponding pins almost = Vcc. But in the following case:

while(RC5==1)
{
if(PORTC==0x22)
{
PORTB=0x09;
}
}

I get the output at RB0 & RB3 as 3.24V and RB1 & RB2 as 0V which is not a big problem. But

while(RC5==1)
{
if(PORTC==0x26)
{
PORTB=0x0A;
}
}

here I get the output at RB1 & RB3 as 1.05V only :( and RB0 & RB2 as 0V. I don't know what is the problem. Please Help... Thanks in advance.. :)

allbits
02-26-2011, 11:24 PM
If you are getting a non logic output at any pin configured for digital output, It means that you are having a pulse/square wave at that pin(well, most of the times).

In your case, I guess the same thing is happening. You have not posted the rest of the code so cant make out where you are having a bug. Hope you have configured PORTC properly for I/O. And of course, check the fuse bits even though it does not look like a fuse bit error at the first hand.

nitinmathew2007
02-27-2011, 11:04 AM
Ok I paste the entire coding

#include<htc.h>
#define M1P RB0
#define M1N RB1
#define M2P RB2
#define M2N RB3

void main()
{
__CONFIG(0X193A);
unsigned int i,j;

TRISC=0xFF; //Define PORTC as input
TRISB=0x00; //Define PORTB as output
TRISD=0x00; //Define PORTD as output

PORTB=0x00;
PORTD=0x00;
PORTC=0x00;

while(RC5==1)
{
if((PORTC&0x22)==0x22)
{
do //I have tried even without do while, even then o/p is same!
{
PORTB=0x09;
PORTD=0x09;
}
while(PORTC==0x22);
}
if((PORTC&0x28)==0x28)
{
do
{
PORTB=0x06;
PORTD=0x06;
}
while(PORTC==0x28);
}
if((PORTC&0x24)==0x24)
{
do
{
PORTB=0x05;
PORTD=0x05;
}
while(PORTC==0x24);
}
if((PORTC&0x26)==0x26)
{
do //The output constant at 1.5V without do while. With do while loop it oscillates bet 0 to vcc!
{
PORTB=0x0A;
PORTD=0x0A;
}
while(PORTC==0x26);
}
if((PORTC&0x25)==0x25)
{
do
{
PORTB=0x00;
PORTD=0x00;
}
while(PORTC==0x25);
}
else
{
PORTB=0x00;
PORTD=0x00;
}
}
}

vikas
02-27-2011, 12:07 PM
Can you show us an oscilloscope trace ?

nitinmathew2007
02-27-2011, 12:12 PM
Er I am sorry, cuz i do it on breadboard not simulating!! I shall try if anything else can be done!

nitinmathew2007
02-27-2011, 12:29 PM
I tried the following code to test a single condition. I am again surprised to see another kind of output!

#include<htc.h>
#define M1P RB0
#define M1N RB1
#define M2P RB2
#define M2N RB3
void main()
{
__CONFIG(0X193A);
unsigned int i,j;
TRISC=0xFF; //Define PORTC as input
TRISB=0x00; //Define PORTB as output
TRISD=0x00; //Define PORTD as output
PORTB=0x00;
PORTD=0x00;
while(RC5==1)
{
if((PORTC&0x26)==0x26)
{
PORTB=0x0A;
PORTD=0x0A;
}
if((PORTC&0x25)==0x25)
{
PORTB=0x00;
PORTD=0x00;
}
else
{
PORTB=0x00;
PORTD=0x00;
}
}
}

EXPECTED OUTPUT:
When 5th Input of PortC is high, AND if the PORTC has input 0x26 (i.e. 00100110.. Rc7 to Rc0) then the Output PORTB & PORTD must have High i.e. = Vcc
RD0=RB0=0
RD1=RB1=1
RD2=RB2=0
RD3=RB3=1 where '1'=5V or atleast 3.V(Vcc)

But the OUTPUT I get is as follows:
When 5th Input of PortC is 4.93V, AND if the PORTC has input 0x26 (i.e. 00100110.. Rc7 to Rc0, 4.93V is given to pins) then the Output at PORTB & PORTD are got as:
RD0=0V
RB0=0V
RD1=1.97V
RB1=2.13V
RD2=0V
RB2=0V
RD3=1.97V
RB3=2.13V

But NOTE in program I have given PORTB & PORTD as outputs which must have a same output. I don know how I get such a different analog kindda output in Digital Circuit! I tried a pull up resister of 10K which is also of no use!

MohitM
02-27-2011, 03:25 PM
I'm no C expert but shouldn't the __config macro be before main() function. Does your program even compile without warnings? Shouldn't you initialise the various registers like OPTION - or does HiTech take care of all these initialisations (although I seriously doubt this)? There are peripherals multiplexed with various ports that should be enabled or disabled as needed... e.g. Pull-ups on PortB. Have you taken care of these? Have you tried a delay inside the loops where you write to the ports?

Is your PIC even working properly? Write simple program to toggle a pin continuously - does it work?

Also, use comments inline with code. It tells us what you are doing.

Finally, do not use hex like:
PORTD=0x0A;
Use:
PORTD=0b00001010;
This way you don't have to use mental maths to convert hex to binary values. One look and its enough for you (and others) to know what pins are you writing with what.

nitinmathew2007
02-27-2011, 06:08 PM
1. __CONFIG can be given inside main(), Cuz if I simply give PORTB or PORTD=0xFF I get 4.93V at all pins of PORTB & PORTD hence working perfect. But in my case, I use a condition. I mean if i say
while(RC5==1)
{
if((PORTC&0x26)==0x26)
{
PORTB=0xFF;
PORTD=0xFF;
}
}
And if I measure voltage at port pins of PORTB & PORTD I get it as 1.70V at PORTB and 1.53V at PORTD. So Its clear that The output statements are clear. Only the input scanning part of code is mal functioning.

2. I even tried OPTION register. I included RBPU=0 even then i get same output. I even tried a pull up resistor. But I don think there is any use working with OUTPUT Part. Cuz the O/P is correct if I give without the if conditions [like: if((PORTC&0x26)==0x26))].

3. PORT C has multiple features like CCP1,CCP2, SDO,T1OSO etc But as far as i know by default the PORTC is used as I/O pins only. And TRIS register is used to Mention whether its an I/P Or an O/P pin.

4. I tried Delay too. May be I will try once more. I used do-while as shown in 2nd post by me. Pl refer my 2nd post in this page..!

5. My PIC works correctly I tried to make PORTB ON & OFF after a delay which is working well.

6. I shall use comments next time onwards. What I want is to make the outputs on a PORT to go High or Low based on the binary input given at PORTC.

nitinmathew2007
02-27-2011, 09:25 PM
On further analysis i guess the output is error if the input has more than two high bits. I mean for the following combination at PORTC

PORTC==0x22 or PORTC==0x24 or PORTC==0x28 has only two bits that is = Vcc (I/P given to PORTC). Here I get the expected output properly. But for

PORTC==0x26 or PORTC==0x25 has more than 3 I/P bits=High/Vcc [00100110, 00100101], Now the O/P i get is random! Can anyone help me in correcting it and also say the reason. My current code is as follows...

#include<htc.h>
void main()
{
__CONFIG(0X193A);
unsigned int i,j;
unsigned char IP;
TRISC=0xFF; //Define PORTC as input
TRISB=0x00; //Define PORTB as output
TRISD=0x00; //Define PORTD as output

PORTB=0x00;
PORTD=0x00;
while((PORTC&0x20)==0x20) //When RC5 is high enter the loop to check further conditions
{
if((PORTC&0x24)==0x24) //Check if RC7 to RC0 I/P = 00100100
{
PORTB=0x05; //Output PORTB: RB7 to RB0 as 00000101
PORTD=0x05;
}
if((PORTC&0x22)==0x22) //Check if RC7 to RC0 I/P = 00100010
{
PORTB=0x09;
PORTD=0x09;
}
if((PORTC&0x28)==0x28) //Check if RC7 to RC0 I/P = 00101000
{
PORTB=0x06;
PORTD=0x06;
}
if((PORTC&0x26)==0x26) //Check if RC7 to RC0 I/P = 00100110
{
PORTB=0x0A; //Output PORTB: RB7 to RB0 as 00001010
PORTD=0x0A;
}
if((PORTC&0x25)==0x25) //Check if RC7 to RC0 I/P = 00100101
{
PORTB=0x00; //Output PORTB: RB7 to RB0 as 00000000
PORTD=0x00;
}
}
}

Pl help.. I hope the solution to this will help others like me in future! :P

nitinmathew2007
03-01-2011, 09:42 PM
I found the error myself!

#include<htc.h>
#define M1P RB0
#define M1N RB1
#define M2P RB2
#define M2N RB3

void main()
{
__CONFIG(0X193A);
unsigned char i,j;
TRISC=0xFF; //Define PORTC as input
TRISB=0x00; //Define PORTB as output
TRISD=0x00; //Define PORTD as output
PORTB=0x00;
PORTD=0x00;
i=0;
while(1)
{
if(RC5==1)
{
i=PORTC;
if(i==0x24)
{
PORTB=0x05;
PORTD=0x05;
}
else if(i==0x22)
{
PORTB=0x09;
PORTD=0x09;
}
else if(i==0x28)
{
PORTB=0x06;
PORTD=0x06;
}
else if(i==0x26)
{
PORTB=0x0A;
PORTD=0x0A;
}
else if(i==0x25)
{
PORTB=0x00;
PORTD=0x00;
}
else
{
PORTB=0x00;
PORTD=0x00;
}
}
else
{
PORTB=0x00;
PORTD=0x00;
}
}
}

U pl compare this correct program with the above one! This can also be done using switch case! This program is used to control a RC using DTMF IC CM8870. I use PIC16F877A...