Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Pic 16f876a Prog. Problem

Status
Not open for further replies.

JEFFxxx

New Member
Hi,
I am a newbie in this wonderful world of PIC programming and having some problems, so I'm hoping some one out there can help me out.
I have written a program for the pic 16f876a microcontroller, this uses RA0 as an analogue input. I have tested it with MPLAB simulator and seems to work ok. Unfortunately MPLAB does not seem to support analogue input simulation, I could be wrong here.
I have programmed the PIC with no problems encountered, but when I breadboard, it will not function. All the external components are in the correct locations, using 100k pull-up resistor on the MCLR pin and using a 10k pot for analogue input on pin 2 of the PIC.

Also I would appreciate any good links, especially a simulator that allows analogue input.
Hope someone can help me out there, before I pull out all my hair.
Many thanks,
Jeff

The programme listing:-


;CHARGE CONTROLER

PROCESSOR 16F876a
INCLUDE <p16f876a.inc>
__config _HS_OSC & _WDT_OFF & _PWRTE_ON


;CONSTANTS

STATUS EQU 03H
TRISA EQU 85H ;BANK 1
TRISB EQU 86H ;BANK 1
PORTA EQU 05H
PORTB EQU 06H
ADCON0 EQU 1Fh
ADCON1 EQU 9FH ;BANK 1
ADRES EQU 1EH
GODONE EQU 02H
CARRY EQU 00H

;SET UP PORTS

BSF STATUS,5 ;TO BANK 1
MOVLW H'ff'
errorlevel -302
MOVWF TRISA ;PORT A INPUT
MOVLW 00H
MOVWF TRISB ;PORT B OUTPUT
MOVLW B'00000100'
MOVWF ADCON1 ;SET RA0,1,3 ANALOGUE INPUTS
BCF STATUS,5 ;TO BANK 0
errorlevel +302
MOVLW B'01000001'
MOVWF ADCON0 ;CLOCK 4Mz
CLRF PORTA
CLRF PORTB
GOTO START

;24V ROUTINE
ROUTINE24V BSF PORTB,1 ;24V LED ON
BEGIN2 BSF ADCON0,2
WAIT2 BTFSC ADCON0,2
GOTO WAIT2
MOVF ADRES,W ;READ ANALOGUE VALUE
SUBLW .179 ;3.5V Used for test purpose
BTFSC STATUS,CARRY
GOTO CHARGE24V
BCF PORTB,0 ;BIT 0 CLEAR
BCF PORTB,4 ;BIT 4 CLEAR
BSF PORTB,1 ;BIT 1 SET
BSF PORTB,5 ;BIT 5 SET
GOTO BEGIN2

;CHARGING ROUTINES


CHARGE12V BSF PORTB,0 ;BAT TR ON
BSF PORTB,4 ;BAT LED ON
BCF PORTB,1 ;DUMP TR OFF
BCF PORTB,5 ;DUMP LED OFF
GOTO BEGIN

CHARGE24V BSF PORTB,0 ;BAT TR ON
BSF PORTB,4 ;BAT LED ON
BCF PORTB,1 ;DUMP TR OFF
BCF PORTB,5 ;DUMP LED OFF
GOTO BEGIN2

START
BTFSS PORTA,2 ;CHECK SW FOR 12V OR 24V
GOTO ROUTINE24V
BSF PORTB,0 ;TURN ON 12V LED
BEGIN BSF ADCON0,2 ;TURN ON ANALOGUE INPUT
WAIT BTFSC ADCON0,2
GOTO WAIT ;WAIT UNTILL ANALOGUE CONVERTION COMPLET
MOVF ADRES,W
SUBLW .128 ;2.5V. TEST, CHECK IF REQUIRES CHARGING
BTFSC STATUS,CARRY
GOTO CHARGE12V
BCF PORTB,0 ;TURN OFF BAT TRANS
BCF PORTB,4 ;BAT LED OFF
BSF PORTB,1 ;DUMP TR ON
BSF PORTB,5 ;DUMP LED ON
GOTO BEGIN
end
 
Hi there,

If your PIC won't function at all then that would suggest that there is something wrong with either you config of your circuit.

Try checking you connections like you clock, Power ect, ect.

Also have a look at your config for you PIC device. It may be something as simple as having LVP (Low Voltage Programming) Enabled.

Good look.

P.S As far as the A/D simulator I can't really help you there. I don't think Mplab does support A/D simulation but I may be wrong.
 
Unfortunately MPLAB does not seem to support analogue input simulation, I could be wrong here.

Too bad... I'm having problem with that.. Hope that there will be a new version with analog input simulator! :)

I have programmed the PIC with no problems encountered, but when I breadboard, it will not function. All the external components are in the correct locations, using 100k pull-up resistor on the MCLR pin and using a 10k pot for analogue input on pin 2 of the PIC.

I'm currently doing a project with ADC too but i'm using 877A. Though I'm not so good in this and maybe we could make some ideas exchange. Why are you using a 10k pot for your analogue input? From what i'd read in the datasheet:

The maximum recommended impedance for analog sources is 2.5 kΩ. This is required to meet the pin leakage specification.

I wonder if the statement work for you. Good luck!:)
 
Zane83 said:
The maximum recommended impedance for analog sources is 2.5 kΩ. This is required to meet the pin leakage specification.

It's not so much 'pin leakage', as the time it takes to switch channels - the PIC A2D inputs include a 'sample and hold' capacitor, and this requires time to charge. If you use too high a source impedance this can take a considerable time - so you have to add delays when switching channels.

My PIC analogue tutorial explains about using the analogue inputs, in a somewhat simpler way than the datasheets do.
 
Hello,
I am trying to paste a circuit diagram that is in abode reader format, any ideas how to do this.
Someone has given me a link for a simulator that allows analogue input simulation, it seems to work well
https://www.oshonsoft.com/pic.html
Tried this sim with my prog. and seemed to work ok, so possibly it could be a hardware problem. Iwill get back to you when I have discovered how to get circuit diagram in here. "No rest for the wicked" as the expression goes.
Many thanks for all your help.
Regards,
Jeff
 
Last edited:
eblc1388 said:
What didn't work?

Does the LEDs work when you change the 12/24V switch?
Hello,
Thanks for your reply
When I use the switch there is no change in any way. I have found a simulator that has analogue input simulation, and tested my prog. and it seems to work ok. So there is possibly a problem with my hardware or the breadboard i am using. I would paste my circuit here but it is in abode reader format and I do not know how to change the format.
https://www.electro-tech-online.com/ This link is for the simulator I was

given, hope you find it useful.
Regards,
Jeff
 
JEFFxxx said:
When I use the switch there is no change in any way.

My purpose of asking that question is to see whether there is any activities shown in the behaviour of your PIC. Usually problem is hardware related, perhaps your PIC clock is not running or you are using a 20MHz crystal with your circuit on a breadboard and wirings length become critical.

I would suggest to you not to troubleshoot a large trunk of the code but instead try initially to flash a LED and build gradually from that. This way, you can at least prove that all the hardware (PIC, PIC code downloading and clock oscillator) is working good, before moving on to the AD experiment.
 
Hi,
Sorry for delay in responding.
I found two problems:-
First the two caps. for the crystal were faulty, replaced and ran OK.

Second I had set the wrong bit for turning on the 12V led and the 24V
led, they should have been bit 2 and 3.
It now works perfectly.
Many thanks for all your input.
Regards,
Jeff
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top