0
\$\begingroup\$

I coded an assembly script to take the input values from two LDRs in parallel. Then my microcontroller was supposed to convert this analogue input into an 8-bit digital value (omitting the 2 LSBs). Next, this output was represented by 8 LEDs. I used pic16f877a microcontroller for this purpose.

But, instead of getting a digital output from LEDs, all of my 8 LEDs lit up and started to increase/decrease their brightness depending on the light intensity falling on my LDRs. No ADC was visible.

Here is my code:

PROCESSOR 16F876A
INCLUDE <P16F876A.INC>

ANVAL   EQU 0x20 ;ANALOG VALUE OF AN0 INPUT
CNT0    EQU 0x21 ;COUNTER IN BANK 00
CNT1    EQU 0x22 ;COUNTER IN BANK 01
PIN_C   EQU 0x23 ;PIN CONFIG FOR ADCON0

    ORG 0x00 ;WE MAKE USE OF INTERUPTS
INIT:

    CLRWDT
    BSF STATUS, 5 ;SELECT BANK 01

    ;PORTA IS SET TO INPUT MODE
    MOVLW B'11111111'
    MOVWF TRISA

    ;PORTB IS SET TO OUTPUT MODE
    MOVLW B'00000000'
    MOVWF TRISB

    BCF STATUS, 5 ;SELECT BANK 00

MAIN:
    CALL ADC_INIT ;INIT ADC MODULE

    CALL ADC_READ ;READ ANALOG VALUE AND STORE TO ANVAL REGISTER (AN0=Repeat this step)

    ;OUTPUT ANVAL TO PORTB
    MOVF ANVAL, W
    MOVWF PORTB

    GOTO MAIN


;;;;ADC INIT PROC;;;;
ADC_INIT:
    BSF STATUS, 5 ;SELECT BANK 01

    ;RIGHT JUSTIFIED RESULT, +VREF=VDD, -VREF=VSS, AN0-AN7=ANALOG
    MOVLW B'10000000'
    MOVWF ADCON1
    CLRWDT

    RETURN

;;;;ADC READ PROC;;;;
ADC_READ:
    BCF STATUS, 5 ;SELECT BANK 00

    ;FOSC/8, ADON=1
    MOVLW B'01000001'
    MOVWF ADCON0

    BSF ADCON0, 2 ;START CONVERTION PROCESS (WE SET THE GO BIT)
WAIT:
    CLRWDT
    BTFSC ADCON0, 2
    GOTO WAIT ;WAIT FOR CONVERTION TO FINISH (WAIT FOR GO BIT TO CLEAR)

    ;;;WE SAVE RESULT INTO AN 8BIT REGISTER TO USE FOR OUTPUT (WE DROP THE TWO LESS SIGNIFICANT BITS);;;

    ;FIRST WE PROCESS THE HIGH BYTE OF RESULT
    MOVLW 0x06
    MOVWF CNT0
AGAIN_0:
    BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
    RLF ADRESH, 1 ;SHIFT LEFT 6 BITS A/D RESULT HIGH BYTE
    CLRWDT
    DECFSZ CNT0
    GOTO AGAIN_0

    MOVF ADRESH, W ;MOVE ADRESH TO W
    MOVWF ANVAL ;MOVE W TO ANVAL

    ;NOW WE PROCESS THE LOW BYTE OF RESULT
    BSF STATUS, 5 ;SELECT BANK 01
    MOVLW 0x02
    MOVWF CNT1
AGAIN_1:
    BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
    RRF ADRESL, 1 ;SHIFT RIGHT 2 BITS A/D RESULT LOW BYTE
    CLRWDT
    DECFSZ CNT1
    GOTO AGAIN_1

    MOVF ADRESL, W ;MOVE ADRESL TO W
    BCF STATUS, 5 ;SELECT BANK 00
    ADDWF ANVAL, 1 ;ADD W TO ANVAL

    RETURN

    END

The simulation ran as it should be on proteus. But when I actually implemented it, A to D conversion did not happen.

Here is a screenshot of my proteus simulation. (You can see that ADC is happening smoothly. Some LEDs are ON, some are OFF)

Proteus Simulation

NOTE: I have used an RC oscillator for this microcontroller. Only a resistor of 10k OHMS has been used, I didn't use a capacitor because from what I've found online, it was not necessary (0 uF would work).

Here are two instances of my actual circuit implementation.

When the light level is high

When the light level is high

When the light level is low

When the light level is low

Highly appreciate your feedback on this.

UPDATE: I pulled out the ADC_INIT call from my Main subroutine and placed it in the INIT subroutine because calling ADC_INIT every time is not necessary. Yet, the issue remains the same.

PROCESSOR 16F876A
INCLUDE <P16F876A.INC>

ANVAL   EQU 0x20 ;ANALOG VALUE OF AN0 INPUT
CNT0    EQU 0x21 ;COUNTER IN BANK 00
CNT1    EQU 0x22 ;COUNTER IN BANK 01
PIN_C   EQU 0x23 ;PIN CONFIG FOR ADCON0

    ORG 0x00 ;WE MAKE USE OF INTERUPTS
INIT:

    CLRWDT
    BSF STATUS, 5 ;SELECT BANK 01

    ;PORTA IS SET TO INPUT MODE
    MOVLW B'11111111'
    MOVWF TRISA

    ;PORTB IS SET TO OUTPUT MODE
    MOVLW B'00000000'
    MOVWF TRISB

    BCF STATUS, 5 ;SELECT BANK 00
    CALL ADC_INIT ;INIT ADC MODULE

MAIN:

    CALL ADC_READ ;READ ANALOG VALUE AND STORE TO ANVAL REGISTER (AN0=Repeat this step)

    ;OUTPUT ANVAL TO PORTB
    MOVF ANVAL, W
    MOVWF PORTB

    GOTO MAIN


;;;;ADC INIT PROC;;;;
ADC_INIT:
    BSF STATUS, 5 ;SELECT BANK 01

    ;RIGHT JUSTIFIED RESULT, +VREF=VDD, -VREF=VSS, AN0-AN7=ANALOG
    MOVLW B'10000000'
    MOVWF ADCON1
    CLRWDT

    RETURN

;;;;ADC READ PROC;;;;
ADC_READ:
    BCF STATUS, 5 ;SELECT BANK 00

    ;FOSC/8, ADON=1
    MOVLW B'01000001'
    MOVWF ADCON0

    BSF ADCON0, 2 ;START CONVERTION PROCESS (WE SET THE GO BIT)
WAIT:
    CLRWDT
    BTFSC ADCON0, 2
    GOTO WAIT ;WAIT FOR CONVERTION TO FINISH (WAIT FOR GO BIT TO CLEAR)

    ;;;WE SAVE RESULT INTO AN 8BIT REGISTER TO USE FOR OUTPUT (WE DROP THE TWO LESS SIGNIFICANT BITS);;;

    ;FIRST WE PROCESS THE HIGH BYTE OF RESULT
    MOVLW 0x06
    MOVWF CNT0
AGAIN_0:
    BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
    RLF ADRESH, 1 ;SHIFT LEFT 6 BITS A/D RESULT HIGH BYTE
    CLRWDT
    DECFSZ CNT0
    GOTO AGAIN_0

    MOVF ADRESH, W ;MOVE ADRESH TO W
    MOVWF ANVAL ;MOVE W TO ANVAL

    ;NOW WE PROCESS THE LOW BYTE OF RESULT
    BSF STATUS, 5 ;SELECT BANK 01
    MOVLW 0x02
    MOVWF CNT1
AGAIN_1:
    BCF STATUS, C ;WE WANT SHIFT (NOT ROTATE), SO WE CLEAR CARRY
    RRF ADRESL, 1 ;SHIFT RIGHT 2 BITS A/D RESULT LOW BYTE
    CLRWDT
    DECFSZ CNT1
    GOTO AGAIN_1

    MOVF ADRESL, W ;MOVE ADRESL TO W
    BCF STATUS, 5 ;SELECT BANK 00
    ADDWF ANVAL, 1 ;ADD W TO ANVAL

    RETURN

    END

UPDATE 2:

I figured out that I have not connected VDD and VSS pins. So I connected them. Now, nothing lit up. Therefore, I created this simple program to test the MCU. This program simply lights up all the LEDs connected to PORTB.

This is my code.

PROCESSOR 16F876A
INCLUDE <P16F876A.INC>


ORG 0x00 ;WE MAKE USE OF INTERUPTS

INIT:

CLRWDT
BSF STATUS, 5 ;SELECT BANK 01

;PORTB IS SET TO OUTPUT MODE
MOVLW B'00000000'
MOVWF TRISB

BCF STATUS, 5 ;SELECT BANK 00

MAIN:

MOVLW B'11111111'
MOVWF PORTB

GOTO MAIN


END 

And here is a proteus scetch of my circuit as it is. I used the RC oscillator with 10k resistor and 1uF cap.

Proteus

In addition to the above connections, I connected the VDD (Pin 11, Pin 32) and VSS (Pin 12, Pin 33) respectively to 5v and GND. The LEDs did not light up. But when I removed the Pins 12 and 32 (VSS Pins) the circuit worked well (LEDs lit up). Next, when I removed the VDD pins leds lit up but they were dim. Is it possible to work with the MCU without connecting the GND pins? Is there a fix?

Update 3

Trying to figure out whether my circuit works when the VSS is pulled out, I coded a simple LED Flash program.

Here is the code:

PROCESSOR 16F877A
INCLUDE <p16F877A.INC>


CNT0    EQU 21h ;COUNTER 0
CNT1    EQU 22h ;COUNTER 1

ORG 0x00 ;

INIT:

BSF STATUS, 5 ;SELECT BANK 01

;PORTB IS SET TO OUTPUT MODE
MOVLW B'00000000'
MOVWF TRISB
BCF STATUS, 5 ;SELECT BANK 00


MAIN:

MOVLW B'11111111'
MOVWF PORTB
CALL DELAY
MOVLW B'00000000'
MOVWF PORTB
CALL DELAY
GOTO MAIN




DELAY:  

BCF STATUS, 5
MOVLW B'11111111'
MOVWF CNT0
MOVLW B'11111111'
MOVWF CNT1

LOOP1
DECFSZ CNT0,1
GOTO LOOP2
CLRWDT
LOOP2
DECFSZ CNT1,1
GOTO LOOP2
RETURN

END 

The circuit is dead as long as the VSS pins are connected to ground. But when I pull them out, LEDs don't flash! They only light up and remain stable. Once again, when I implement this in proteus it works fine.

Update 4

This is my complete schematic. The two Vdd and the two Vss pins are missing in the proteus microcontroller. However, I have attached the Vdds to the 5v power supply that I'm getting out of the potential divider. And the Vss pins have been connected to the ground. I have used a 9v battery to get 5v.

Update

\$\endgroup\$
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. Any conclusions reached should be edited back into the question and/or any answer(s). \$\endgroup\$ – Dave Tweed Oct 11 '19 at 21:14
2
\$\begingroup\$

The microcontroller requires a steady DC supply. LED current is sourced from the microcontroller's Vdd pin. If no LED are lit, Vdd current is small. One LED lit draws about 10mA, which is sourced through Vdd. All eight LED will pull about 80mA from Vdd (a bit much). This current cannot be sourced from a DC supply having 670 ohms equivalent resistance that your voltage divider provides.

A 5V linear regulator can drop +9V to +5V. Its +5V output will not change much as current varies. A capacitor across this +5V supply is always added to smooth glitchy current pulses. A weak 9V battery may sag below 7V under heavy current load. Below 7V, the 7805 cannot properly regulate its +5V output. basic PIC circuit
This chip default to RC oscillator, connected to OSC1 pin 13 (on the 40-pin DIP package). Your breadboard may provide enough stray capacitance to sustain oscillation. Data sheet recommends a minimum of 20pF, and a resistance between 3.3k - 100k. 10k is within that range.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ I removed all 8 LEDs and tried with a single LED. Still, the issue remains the same. I tried the oscillator with no capacitor attached, with a capacitor of 1uF and with 100nF. There is nothing wrong with the oscillator I guess. "A capacitor across this +5V supply is always added to smooth glitchy current pulses". I may try this as well \$\endgroup\$ – slhulk Oct 11 '19 at 18:48
  • \$\begingroup\$ I did as you instructed and connected a 1uF capacitor across both Vdd , Vss pin pairs. Now the LED bulb lit up. I didn't have to remove the Vss pin from GND. But the bulb remains steady without flashing. \$\endgroup\$ – slhulk Oct 11 '19 at 19:35
  • 1
    \$\begingroup\$ (1) Vss must always be connected to ground. (2) A voltage regulator is required to stabilize Vdd (I've recommended a 7805). You could get by with a 4.7V Zener diode with series resistor, if current variations are small...driving one LED, OK...driving 8 LEDs would likely overheat a Zener. (3) If your simple "blink one LED" test circuit fails, I'd suspect a blown processor, incorrect config settings, or non-working RC oscillator \$\endgroup\$ – glen_geek Oct 12 '19 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.