;**************************************************************************** ;* Converts an one Channel Analog signal betwenn 0 and Vdd to 8 * ;* parallel Digital signal. * ;* * ;* On startup a hello sequence, going trough all 8 bits is performed. The * ;* hello sequence is completed by blinking up twice on all 8 bits. Then the* ;* ADC function is started. * ;* (for details see function hello) * ;* * ;* Date: 27.02.2007 * ;* Author: Stefan Bracher * ;* Contact: http://stefan.bracher.info * ;* * ;**************************************************************************** ;---PORT DESIGNATION--------------------------------------------------------- ; RA0: Analog Input ; RA1: Digital output: Least significant bit ; RA2: Digital output: Bit 1 ; RC0: Digital output: Bit 2 ; RC1: Digital output: Bit 3 ; RC2: Digital output: Bit 4 ; RC3: Digital output: Bit 5 ; RC4: Digital output: Bit 6 ; RC5: Digital output: Mist significant bit ; RA3: unused ; RA4: unused ; RA5: unused ;----------------------------------------------------------------------------- ;---CODE BEGIN---------------------------------------------------------------- list p=16f684 ; list directive to define processor #include ; processor specific variable definitions __CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF ; '__CONFIG' directive is used to embed configuration data within .asm file. ; The labels following the directive are located in the respective .inc file. ; See respective data sheet for additional information on configuration word. ;***VARIABLE DEFINITIONS******************************************************* delay_tmp1 equ 0x21 ; Variable used for delay function delay_tmp2 equ 0x22 ; Variable used for delay function delay_tmp3 equ 0x23 ; Variable used for delay function makro_tmp equ 0x24 ; Variable used to call macros temp equ 0x25 ; Temporary Variable temp2 equ 0x26 ; Temporary Variable2 Ch0_8bit equ 0x27 ; The variable to store the 8Bit result of Channel 0 ADC ;******************************************************************************* ;***MACROS********************************************************************** BANK1 MACRO ; MACRO to select BANK 1 BSF STATUS, RP0 ENDM BANK0 MACRO ; MACRO to select BANK 0 BCF STATUS, RP0 ENDM DELAY MACRO A ; MACRO used to generate a delay of MOVLW A ; "A*A loops", see delay_sub for details MOVWF delay_tmp1 MOVWF makro_tmp CALL DELAY_SUB ; Call of the actual Delay function ENDM ;****************************************************************************** ;***Interrupts***************************************************************** ORG 0x000 ; processor reset vector call port_con ; excecute Port Configuration call hello ; hello message to test succesful configuration goto MAIN ; Start Main function ;******************************************************************************* ;***Functions******************************************************************* ;###port_con############################################################## port_con ; Port Configuration BANK0 ; Change to Bank0 (where PORTA and CMCONO are stored) CLRF PORTA ; Clear PORTA CLRF PORTC ; Clear PORTC MOVLW B'00000111' ; w=B'00000111' MOVWF CMCON0 ; CMCONO=w (=B'00000111' -> COMPARATORS ARE OFF) ;...ADCON0 Register: A/D Control Register (in Bank0)................... ; Bit 7: 1=Right justified, 0=left justified ; -> Result saved as 10 Bits in ADRESH, ADRESL from the right/left ; Bit 6: Voltage Reference: 1=Vref pin, 0=Vdd ; Bit 5: - ; Bit 4-2: Analog channel selection: B'xxx'=Channel ; Bit 1: 1=Start conversion, 0=conversion is completed ; Bit 0: 1=Conversion is operating, 0=Conversion is shut off MOVLW B'00000001' ; Selected Channel: RA0, left justified, Vref=Vff MOVWF ADCON0 ;..................................................................... BANK1 ; Change to Bank1 (where ANSEL, ADCON1 and TRISA are stored) ;...ADCON1 Register: select AD conversion frequency................... ; Bit 7: - ; Bit 6-4: 000=Fosc/2 ; 001=Fosc/8 ; 002=Fosc/32 ; x11=Frc (internal oscilator, max 500kHz ; 100=Fosc/4 ; 101=Fosc/16 ; 110=Fosc/64 ; Bit 4-0: - MOVLW B'01110000' ; Internal osculator used MOVWF ADCON1 ;..................................................................... ;...TRISA Register: Select Input/Output A............................. ; Bit n: 1=input, 0=output MOVLW B'11111001' MOVWF TRISA ;..................................................................... ;...ANSEL Register: Analog channel selection......................... ; Bit n: 1=Analog, 0=digital MOVLW B'0000001' ; Channel 0 selected MOVWF ANSEL ;..................................................................... ;...TRISC Register: Select Input/Output C............................. ; Bit n: 1=input, 0=output MOVLW B'00000000' ; -> all output MOVWF TRISC; ;..................................................................... BANK0 ; Change to Bank0 DELAY B'1111111' ; Wait a bit retlw 0 ;######################################################################### ;###hello################################################################# hello ; Function saying hello by putting out ; "10000000" -> "01000000" -> "00100000" -> ; "00010000" -> "00001000" -> "00000100" -> ; "00000010" -> "00000001" -> "00000000" ; "11111111" -> "00000000" -> "11111111" ; "00000000" MOVLW B'10000000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'01000000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00100000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00010000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00001000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000100' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000010' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000001' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'11111111' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'11111111' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit MOVLW B'00000000' call w2output ; Putting the workspace on the output pins DELAY B'11111111' ; Wait a bit RETURN ;######################################################################### ;###DELAY_SUB############################################################# DELAY_SUB ; Function implemening a delay of macro_tmp^2 MOVFW makro_tmp MOVWF delay_tmp2 call DELAY2_SUB DECFSZ delay_tmp1, F GOTO DELAY_SUB RETURN ;######################################################################### ;###DELAY2_SUB############################################################ DELAY2_SUB ; Function implemening a delay_temp DECFSZ delay_tmp2, F GOTO DELAY2_SUB RETURN ;######################################################################### ;###ADCH0###################################\\\\<######################### ADCH0 ; AD conversion on channel 0 BSF ADCON0, GO ; Start conersion BTFSC ADCON0, GO ; Is conversion done? GOTO $-1 ; No -> test again MOVFW ADRESH ; The higher 8 bits are moved to workspace MOVWF Ch0_8bit ; And stored in CH0_0bit ; Note: The converion is in fact 10 bit ; The remaining bits are found in ADRESL RETURN ;######################################################################### ;###ADCH0################################################################# w2output ; Puts the current workspace on the output pins MOVWF temp ; Save entry to temp MOVWF temp2 ; Save entry to temp2 RLF temp2, 1 ; Rotate left (we want LSB to be on bit1 BCF temp2, 7 ; Clear all unused bits BCF temp2, 6 ; Clear all unused bits BCF temp2, 5 ; Clear all unused bits BCF temp2, 4 ; Clear all unused bits BCF temp2, 3 ; Clear all unused bits BCF temp2, 0 ; Clear all unused bits MOVFW temp2 ; Load to workspace MOVWF PORTA ; Output it on PORTA ; Note: Just the last two bits are actually output RRF temp, 1 ; Rotate right 2 times (for the output bits in PORTC) RRF temp, 1 ; BCF temp, 7 ; Clear all unused bits BCF temp, 6 ; Clear all unused bits MOVFW temp ; Load the result to workspace MOVWF PORTC ; write result to C RETURN ;######################################################################### ;******************************************************************************* ;***MAIN************************************************************************ MAIN call ADCH0 ; AD-conversion on Channel 0 MOVFW Ch0_8bit ; Load the result to workspace call w2output ; Putting the workspace on the output pins goto MAIN ;******************************************************************************* END ; directive 'end of program'