Badcaps.net Forum
Go Back   Badcaps Forums > General Topics > General Capacitor Questions & Issues
Register FAQ Calendar Search Today's Posts Mark Forums Read

 
Thread Tools Display Modes
Old 09-11-2018, 01:11 AM   #1
Leonardori
New Member
 
Join Date: Jul 2016
City & State: Hong Kong
My Country: China
I'm a: Knowledge Seeker
Posts: 7
Default The troubles of PIC16F877A

Good day, I am in an electronic project and I made some troubles now.
I am currently developing a circuit that 16 LDRs, 4 LM339N ICs and 1 PIC16F877A. PIC16F877A datasheet is given. I compare outputs of LDRs with an adjustable reference voltage. I achieved getting 0-5 V from LM339N. Now I send these values to 16F877A as digital inputs to check if one of them is HIGH. If I check only one value, it works well and I can get voltages between 0.12-5.01 V from digital output pin. But when I increase digital inputs to two or more, digital output gives 2.1 when it is low and 2.5V when it is high and it is not enough for me. I have tried defining;
TRISB = 0xFF;
TRISC = 0xF0;
TRISD = 0xF0;
reading values between RB0-7, RC4-7, RD4-7 and writing output to RD0. I have also tried neglecting ADC module and;
TRISB = 0xFF;
TRISA = 0x00;
reading values from RB0 and RB1 and writing to RA0. But no luck yet.
I am using 8 MHz HS crystal with two 22 pF capacitors. Pin 1 has a resistor with push button. PIC is supplied with 5.1 V from 11-32 pins and ground is connected to 12-31 pins. I don't have any prepared circuit diagram but if I cannot find any support from here, I can build it. I am using MPLAB X IDE v3.45 and PicKit2 for programming. The code is included below.
#define _XTAL_FREQ 8000000#include // BEGIN CONFIG#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)//END CONFIGvoid main(){
ADCON1 = 6;
CMCON = 7;
TRISB = 0xFF;
TRISA = 0x00;
RA0 = 0;
while(1){
if(RB0 == 1){RA0 = 1;}else{RA0 = 0;}
__delay_us(5);
if(RB1 == 1){RA0 = 1;}else{RA0 = 0;} // Works well when I comment this line.
}}
Also tried building if/else if/else structure, not working too. Thanks in advance.

Last edited by Leonardori; 09-11-2018 at 01:13 AM..
Leonardori is offline   Reply With Quote
Old 09-11-2018, 11:47 AM   #2
eccerr0r
Solder Sloth
 
eccerr0r's Avatar
 
Join Date: Nov 2012
City & State: CO
My Country: USA
Line Voltage: 120VAC 60Hz
I'm a: Hobbyist Tech
Posts: 3,410
Default Re: The troubles of PIC16F877A

Are you reading the pins when they are completely disconnected or do you have something else hooked up to them?

It's been a while since I worked with PICs but it seems that you have a circuit issue of some sort, how are you hooking up the pins? You shouldn't be getting 2.5 volts when TRISxBy = 0 and PORTxBy = 1 ?
eccerr0r is online now   Reply With Quote
Old 09-12-2018, 03:38 AM   #3
momaka
Badcaps Veteran
 
momaka's Avatar
 
Join Date: May 2008
City & State: VA (NoVA)
My Country: U.S.A.
Line Voltage: 120 VAC, 60 Hz
I'm a: Hobbyist Tech
Posts: 8,294
Default Re: The troubles of PIC16F877A

I've only programmed on a PIC16F84 for a university class, and it was in Assembly, so I can't tell you anything about your code. All I can tell you is that in Assembly, you have to manually choose the Memory banks of the PIC16F84/87x chips in your code. You do this in order to access certain memory registers. For example, TRISA and TRISB memory registers of the PIC16F84 (and also TRISC for PIC16F87x) are located in memory bank 1.

Thus, make sure your code changes to the proper memory banks when defining the inputs and outputs on TRISA, TRISB, and TRISC.

In Assembly code, normally I do it like this:

Code:
BSF	STATUS,RP0	;Change to Data Memory (DM) bank 1
MOVLW	b'00000000'	;Port A config...
MOVWF	TRISA		;RA0-RA4 -> outputs 
MOVLW	b'00100001'	;Port B config...
MOVWF	TRISB		;RB1-RB4, RB6-RB7 -> outputs; RB0,RB5 -> inputs 
BCF	STATUS,RP0	;Change back to Data Memory (DM) bank 0
Also, note that the PIC16F84 has only two data memory banks, but the PIC16F87x has four. Thus, you also might need to change RP1 in STATUS register if you want to access memory banks 2 and 3.

I am not sure how you're going to implement this into your code (or if you even have to), but in Assembly, you do.

In any case, how this pertains to your problem is that it appears you program starts switching between the various input and output pins only temporarily - that is, when the program code is not using a certain input/output, it looks like the pin is oscillating or just not holding the value it should be (if set to an output). Hence the 2.1-2.5V output voltage you're getting.

Last edited by momaka; 09-12-2018 at 03:48 AM..
momaka is offline   Reply With Quote
Old 09-13-2018, 10:20 PM   #4
eccerr0r
Solder Sloth
 
eccerr0r's Avatar
 
Join Date: Nov 2012
City & State: CO
My Country: USA
Line Voltage: 120VAC 60Hz
I'm a: Hobbyist Tech
Posts: 3,410
Default Re: The troubles of PIC16F877A

So sad that Microchip bought out Atmel

Atmel AVR is sane. Microchip PIC is not sane.

Still nice that there's a C compiler for PIC finally, though avr-gcc still wins it for me.
eccerr0r is online now   Reply With Quote
Old 09-13-2018, 10:56 PM   #5
Curious.George
Badcaps Veteran
 
Join Date: Nov 2011
Posts: 765
Default Re: The troubles of PIC16F877A

Quote:
Originally Posted by eccerr0r View Post
Microchip PIC is not sane.
Actually, if you look at GI's history, you'd be impressed that they've managed to milk this cow for so long!
Curious.George is offline   Reply With Quote
Old 09-14-2018, 02:19 AM   #6
stj
Great Sage 齊天大聖
 
stj's Avatar
 
Join Date: Dec 2009
City & State: Europe
My Country: some shithole run by Israeli agents
I'm a: Professional Tech
Posts: 18,969
Default Re: The troubles of PIC16F877A

the pic is great when you consider how cheap some are,
and most code is compact enough to avoid banking.
(because avoiding banking is always wise. )
stj is offline   Reply With Quote
Old 09-14-2018, 02:21 AM   #7
stj
Great Sage 齊天大聖
 
stj's Avatar
 
Join Date: Dec 2009
City & State: Europe
My Country: some shithole run by Israeli agents
I'm a: Professional Tech
Posts: 18,969
Default Re: The troubles of PIC16F877A

Quote:
Originally Posted by Curious.George View Post
Actually, if you look at GI's history, you'd be impressed that they've managed to milk this cow for so long!
most meople dont know they used to be General Instruments.
they must have made a fortune in the 70's from all the pong/tennis home tv-game chips and the 80's was music/sound generators.
stj is offline   Reply With Quote
Old 09-14-2018, 05:43 AM   #8
Curious.George
Badcaps Veteran
 
Join Date: Nov 2011
Posts: 765
Default Re: The troubles of PIC16F877A

Quote:
Originally Posted by stj View Post
most meople dont know they used to be General Instruments.
they must have made a fortune in the 70's from all the pong/tennis home tv-game chips and the 80's was music/sound generators.
Actually, I think their big "win" was CATV boxes -- before all of that.
Curious.George is offline   Reply With Quote
Old 09-14-2018, 06:53 AM   #9
stj
Great Sage 齊天大聖
 
stj's Avatar
 
Join Date: Dec 2009
City & State: Europe
My Country: some shithole run by Israeli agents
I'm a: Professional Tech
Posts: 18,969
Default Re: The troubles of PIC16F877A

i forgot about those,
did they own "Jerrold Instruments" or was it just a customer??
stj is offline   Reply With Quote
Old 09-14-2018, 12:52 PM   #10
redwire
Badcaps Veteran
 
Join Date: Dec 2010
City & State: Alberta
My Country: Canada
Posts: 996
Default Re: The troubles of PIC16F877A

Microchip was losing to Atmel, so they bought them. It's sad because Atmel was making good stuff and Microchip has to tame (lame?) down the parts. Example is new MegaAVR like ATMEGA4809 has very little to offer but it's $2.

The 16F877A is $8 though, very expensive now. For 368 bytes of RAM?
The PIC 16 has many fanboys but it is a primitive state-machine. Not much of a microcontroller (for memory, registers, op-codes) and assembly-language with it was fun back in the day. Memory bank-switching was murder.

General Instrument may have made the part for VideoCipher or pong games and then General Semiconductor sold them.
The days are long gone where dollars can be saved with a small die.

OP should ensure he has hysteresis resistor on LM339 LDR circuit.
redwire is offline   Reply With Quote
Old Today, 12:20 PM   #11
Curious.George
Badcaps Veteran
 
Join Date: Nov 2011
Posts: 765
Default Re: The troubles of PIC16F877A

Quote:
Originally Posted by redwire View Post
The days are long gone where dollars can be saved with a small die.
"Small die in its own package", perhaps.

But, we're seeing (and soon will be seeing considerably MORE of) tiny "cores" being available to low volume users. Things like the 14500 are more than adequate for many tasks. Add a mosfet to an I/O and you've got a dandy "smart XMAS light... BULB!"

Folks get distracted by all the "big" processors in their PCs and phones and ignore the 10X - 1000X *more* "little" processors that they interact with every day.

Start counting the processors in your "computer" and see what sort of number you come up with... you'll be surprised!
Curious.George is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



Badcaps.net Technical Forums 2003 - 2018
Powered by vBulletin ®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
All times are GMT -6. The time now is 10:32 PM.

Did you find this forum helpful?