• 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.

Light a different LED with changing A/D input

Status
Not open for further replies.
Is there an easy way to light one LED when the ADC reads a certain value on the input, light first and second LEDs when a higher level is reached and light three LEDs in the last part of the ADC?
I've managed to get it to read the ADC but it only lights the LED when it is exactly one value and not above that.

"If ADC_RESULT > HighLED then light LED" but I can't see how to put this into assembly. Is there a less than / greater than, equal to instruction?

Code:
;**********************************************************************
;   This file is a basic code template for object module code         *
;   generation on the PICmicro PIC16F688. This file contains the      *
;   basic code building blocks to build upon.  As a project minimum   *
;   the 16F688.lkr file will also be required for this file to        *
;   correctly build. The .lkr files are located in the MPLAB          *
;   directory.                                                        *
;                                                                     *
;   If interrupts are not used all code presented between the         *
;   code section "INT_VECTOR and code section "MAIN" can be removed.  *
;   In addition the variable assignments for 'w_temp' and             *
;   'status_temp' can be removed.                                     *
;                                                                     *
;   If interrupts are used, as in this template file, the 16F688.lkr  *
;   file will need to be modified as follows: Remove the lines        *
;     CODEPAGE   NAME=vectors  START=0x0      END=0x4      PROTECTED  *
;   and                                                               *
;     SECTION    NAME=STARTUP  ROM=vectors                            *
;   and change the start address of the page0 section from 0x5 to 0x0 *
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler and linker (Document DS33014).          *
;                                                                     *
;   Refer to the respective PICmicro data sheet for additional        *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename: Flashing LED1.asm                                      *
;    Date: 2009/04/20                                                 *
;    File Version: 1.0                                                *
;                                                                     *
;    Author: Jules Lewarne                                            *
;    Company:                                                         *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                    16F688.lkr                                       *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************

	list      p=16F688           	; list directive to define processor
	#include <p16F688.inc>        	; processor specific variable definitions

	errorlevel  -302              	; suppress message 302 from list file

; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.

	__CONFIG    _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF



;***** VARIABLE DEFINITIONS 

; FORMAT: [name]	RES	[number of bytes reserved]
INT_VAR		UDATA_SHR	0x71   
DVAR		RES 1
DVAR2		RES 1
DVAR3		RES 1
ADC_RESULT	RES 1
LowLED		RES 1
MedLED		RES 1
HighLED		RES 1

;**********************************************************************
RESET_VECTOR	CODE	0x000		; processor reset vector
		goto    Main            ; go to beginning of program


INT_VECTOR	CODE	0x004		; interrupt vector location
		retfie                  ; return from interrupt

;**********************************************************************
;	Function Name: MAIN
;	
;	Parameters:
;
;	Description: This is the main program which is executed once the processor
;		has powered up.
;		The code within this routine performs some device initialisation then
;		flashes an LED. This routine makes use of another routine which causes
;		a short delay by sending the prcessor into a loop which decrements from
;		an arbitrary value until it reaches zero.
;**********************************************************************

Main
		
		;		
		
		
		;Device Initialisation
		
        bsf     STATUS,RP0  ; set file register bank to 1
        bsf    	ANSEL, 0       ; switch on ADC on AN0
        movlw	B'01110000'	; Set Conversion Clock frequency - xx11xxxx = internal clock
        movwf	ADCON1
        movlw   B'11001111' ; Set Ports to I/O, TRISA register: [X][X][RA5][RA4][RA3][RA2][RA1][RA0]
        movwf   TRISA    	;									Don't  I/O	I/O	  I	  I/O  I/O  I/O
							;									Care    
							
		
        bcf     STATUS,RP0  ; set file register bank to 0

        movlw   B'00000111' ; switch off the comparator, enable digital i/o
        movwf   CMCON0      ;
        
        movlw	B'00000001'	; Enable ADC, left justify, Vdd ref, AN0 input,
		movwf	ADCON0		; 
		
		clrf PORTA ; Set all PORTA outputs to zero
					
;**********************************************************************************
;						EUSART Setup
;**********************************************************************************
	
		bsf		TXSTA, TXEN 	; Enable EUSART
		bcf		TXSTA, SYNC 	; Disable synchronous communication
		bsf		TXSTA, BRGH 	; Select Baud Rate Generator high range
		bsf		RCSTA, SPEN 	; Serial Port Enable
		bcf		BAUDCTL, BRG16	; 16 bit baud rate generator disabled
		movlw	B'00011001' ; setup baudrate generator
		movwf	SPBRG

Loop  
		;btfsc PORTA, 3 ; Wait for button on RA3
        ;goto Loop

				; PORTA register: [X][X][RA5][RA4][RA3][RA2][RA1][RA0]
     			;				   Don't  I/O	I/O	  I	  I/O  I/O  I/O
				;				   Care    
				;						   D0/D1   D2/D3  D6/D7
				;						  o-!>!-o---!>!--o-!>!-o
				;						  o-!<!-o---!<!--o-!<!-o
				;							   D4/D5
				;						  o-----!>!------o
				;						  o-----!<!------o


		;movlw B'00010000' ; PORTA Register
		;movwf PORTA

;**********************************************************************************	
;							Flash the LED
;**********************************************************************************

		;bsf 	PORTA,5
		;call 	Delay
		;bcf 	PORTA,5 ; set BIT 5 of PORTA to zero
		;call 	Delay
		;call	Delay
		;call	Delay
		;call	Delay
		;call	Delay

;**********************************************************************************
;							EUSART Transmission
;**********************************************************************************
		
		;movlw	0x41 ; = 'A' in ASCII
		;movwf	TXREG
		;call 	Delay
		;movlw 	0x42 ; = 'B' in ASCII
		;movwf	TXREG
		;call	Delay
		;movlw	0x43 ; = 'C' in ASCII
		;movwf	TXREG
		;call	Delay
		
;**********************************************************************************
;							Analogue to Digital Conversion
;**********************************************************************************		

ADCcon		bsf		ADCON0, GO	; Start conversion
ADCTEST	btfsc	ADCON0, GO	; Is conversion done?
		goto	ADCTEST			; No, Test again
		bcf     STATUS,RP0  ; set file register bank to 0
		movf	ADRESH, W	; read upper 8 bits (Most significant bits)
		movwf	ADC_RESULT	; Place conversion into ADC_RESULT register
		;movf	ADC_RESULT, W ; dont need to do this as the result is still in W register
		movwf	TXREG		; Send A/D Conversion on serial port
		
		movlw	0xAB
		movwf	HighLED ; set HighLed register to upper threshold value
		movf	ADC_RESULT, W ; place ADC_RESULT into W register
		subwf	HighLED	; subtract ADC_RESULT from HighLED
		decfsz	HighLED	; Decrement HighLED value, if equal to zero skip next instruction
		goto	ADCcon	; perform another ADC conversion
		bsf		PORTA, 4
		call 	Delay
		bcf		PORTA, 4
		call 	Delay
		
Goto Loop
;**********************************************************************************
;							Delay Subroutine
;**********************************************************************************
Delay
    	movlw		0xff	; set outer delay loop (change these hex number to change the frequency)
    	movwf		DVAR2	; memory location
Delay0
    	movlw		0xff	; set inner delay loop  (change these hex number to change the frequency)
    	movwf		DVAR	; incremental memory
 							; location
Delay1	decfsz		DVAR,1
		goto		Delay1
    	decfsz		DVAR2,1
    	goto		Delay0
		
    	return



; initialize eeprom locations

EE		CODE	0x2100
		DE	0x00, 0x01, 0x02, 0x03


		END                       ; directive 'end of program'
 

Nigel Goodwin

Super Moderator
Most Helpful Member
"If ADC_RESULT > HighLED then light LED" but I can't see how to put this into assembly. Is there a less than / greater than, equal to instruction?
Check the PICList, it has an entire section about all types of conditional tests.

EDIT:

Incidently, my first post here was asking something vaguely similar.

I built a PIC based stereo replacement for the LM391x series LED display chips, it used a 16F876, two rows of 16 LED's (multiplexed), two analogue inputs, and two pins for switches. The switches selected dot or bar, and lin or log.

The question I asked was about calculating the switching points for the log scale.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top