; Bargraph.asm ; 11/15/02 ; By Joel Jordan ; SigArch, ACM@UIUC ; http://www.acm.uiuc.edu/sigarch/ ; Displays 0-5V input magnitude on 6-LED bargraph ; For the PIC16F676 #include "p16f676.inc" LIST P=16f676, R=HEX ; Configuration Options: ; 1) _WDT_OFF Turn off Watchdog Timer ; 2) _INTRC_OSC_NOCLKOUT Use internal 4MHz oscillator, don't output clock signal ; 3) _CP_OFF Turn off code protect ; 4) _MCLRE_OFF Turn off /MCLR reset pin ; ; Because of #2 and #4, we have a total of 12 I/O pins if we need them __CONFIG _WDT_OFF & _INTRC_OSC_NOCLKOUT & _CP_OFF & _MCLRE_OFF ; declare a temporary variable for use later ; 0x20 is the first available address for variables temp equ 0x20 Start CLRF PORTC CLRW ; Set up peripherals ; See the PIC16F676 data sheet for information on ; what these configuration registers all do ; Bank 1 Accesses BSF STATUS,RP0 MOVWF TRISC ; Set PORTC to all outputs MOVLW b'00000001' MOVWF ANSEL ; turn on only analog input 0 BCF ADCON1,ADCS2 ; set ADCS<2:0> = 001 -> BCF ADCON1,ADCS1 ; Sample Time 8*Tosc BSF ADCON1,ADCS0 ; from Data Sheet ; Bank 0 Accesses BCF STATUS,RP0 MOVLW b'11100011' ; set A/D input channel 0 ANDWF ADCON0, f BCF ADCON0, ADFM ; set shift left BCF ADCON0, VCFG ; set reference voltage = Vdd BSF ADCON0, ADON ; turn on A/D converter Main_Loop BSF ADCON0, GO ; Fetch a sample Wait_Loop BTFSC ADCON0, GO ; wait for A/D conversion to finish GOTO Wait_Loop ; ADC will clear bit ADCON0, GO when done MOVF ADRESH, W ; get upper 8 bits of sampled data movwf temp ; need to use "temp" because we can't rotate ; things in the w register BCF STATUS,C ; clear status, so we don't rotate anything in ; Rotate to keep only last 3 bits rrf temp,f rrf temp,f rrf temp,f rrf temp,f rrf temp,f movf temp,w ANDLW b'00000111' ; clear all bits other than last 3 call Get_Bar ; convert 0-7 into bargraph display movwf PORTC ; move bargraph display into PORTC ; nop instructions just delay for awhile, I'm not sure if they're needed ; between A/D conversions or not. The number of nops was arbitrary here. nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop nop goto Main_Loop ; Get_Bar implements a table lookup to convert ; numbers 0-7 into 6-bit bargraph displays. It ; works by adding the index to the program counter, ; which jumps to the address that returns the ; correct bar graphic in w. Get_Bar addwf PCL,f retlw b'00000000' retlw b'00000001' retlw b'00000011' retlw b'00000111' retlw b'00001111' retlw b'00011111' retlw b'00111111' retlw b'00111111' END