| | |
Pic 16f876a Programming Problem
![]() |
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
Hi,
I am a newbie in this wonderful world of PIC programming and having some problems, so
I'm hopeing 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. I would appreciated
it if someone could have a look at my programe
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
I am a newbie in this wonderful world of PIC programming and having some problems, so
I'm hopeing 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. I would appreciated
it if someone could have a look at my programe
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,I think I can help you but can I see the circuit you are working on.And I also think you should use PROTEUS for simulations....you can actually add analog input and test your program from there...
"By the data to date, there is only one animal in the Galaxy dangerous to man—man himself. So he must supply his own indispensable competition. He has no enemy to help him."
From Time Enough for Love by Robert A. Heinlein
From Time Enough for Love by Robert A. Heinlein
Hi Jefxxx,
This simulator is one the bests:
http://www.oshonsoft.com/pic.html
Also,I'll give you anything I found about your code and any othe related things I found.And also I'll help you as much as I can.
Good luck.
This simulator is one the bests:
http://www.oshonsoft.com/pic.html
Also,I'll give you anything I found about your code and any othe related things I found.And also I'll help you as much as I can.
Good luck.
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
Hi Jefxxx,
This simulator is one the bests:
http://www.oshonsoft.com/pic.html
Also,I'll give you anything I found about your code and any othe related things I found.And also I'll help you as much as I can.
Good luck.
Many thanks for your help.
t tried the simulator with my program and it seems
to work perfectly, Inact seems a great simulator, thanks
, so possibly it is a hardware problem .
I will try and inclute a circuit diagram
Regards
Jeff
•
•
Join Date: Sep 2006
Posts: 3
Reputation:
Solved Threads: 0
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:p
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:p
![]() |
Similar Threads
- PIC Basic programming with RF modules (Assembly)
- Anyone knows what PIC chip programming about? (Computer Science)
Other Threads in the Assembly Forum
- Previous Thread: Where can i learn about assembly language
- Next Thread: beep codes, and frequency
| Thread Tools | Search this Thread |





