MCU Examples.com
PIC Microcontroller Project Examples, free source codes and resources collection.
;****************************************************************************** ; Main Program code for the automated greenhouse ; Date 2nd September 2010 ;****************************************************************************** PROCESSOR 16F877a #include P16F877a.inc __config _CP_OFF & _DEBUG_ON & _LVP_OFF & _WDT_OFF & _XT_OSC LINE1 equ 0x00 ; location address of Line 1 of the LCD display LINE2 equ 0x40 ; location address of Line 2 of the LCD display LINE3 equ 0x14 ; location address of Line 3 of the LCD display LINE4 equ 0x54 ; location address of Line 4 of the LCD display E equ 0 ; LCD Enable/Disable pin connected to PORTB,0 RS equ 2 ; R/S pin in LCD (treating data as instructions or characters) connected to ;PORTB,2 RW equ 1 ; Read/Write pin of LCD connected to PORTB,1 LCD8bitPORT equ PORTD ; Data pins of LCD connected to PORTD LCDctrlPORT equ PORTB ; Control Pins connected to pins 0,1 and 2 of PORTB
cblock 0x20 HIcnt LOcnt LOOPcnt D1 D2 toASCII_tmp temp_adc temp_adc_user ls_adc sm_adc endc org 0x0 goto main org 0x10 ;**************************************************** main lcd_init ; Initialising LCD using the lcd_init macro banksel TRISC clrf TRISC ; PORTC made Output from the microcontroller for heater, ;cooler fan, sprinklers, light bulb banksel PORTC clrf PORTC ; Initially heater, cooler fan, sprinklers, light bulb are off banksel TRISA movlw b'11111111' movwf TRISA ; PORTA set as INPUT to the microcontroller ;**************************************************** lcd_txt ; Displaying characters Temp,User/Temp,Light Intensity,Soil ;Moisture in LCD using lcd_txt macro ;***************************************************************************** ;Checking if the system is in user mode user_mode banksel TRISB ; User mode switch is connected to pin 5 of PORTB so ;the pin is made as input bsf TRISB,5 banksel PORTB btfss PORTB,5 ; Checking if user mode switch is pressed goto sensor_mode ; If not pressed program jumps to sensor_mode pausems .10 ; If pressed switch is debounced btfss PORTB,5 ; Checking if user mode switch is still pressed goto sensor_mode ; If not pressed program jumps to sensor_mode ;***************************************************************************** ;Initialising AD Conversion user_input ;Initialising AD conversion. Clock conversion 4MHz/32, AN4 channel selected ;AN4, AD conversion OFF,AD converter module powered up banksel ADCON0 movlw b'10100001' movwf ADCON0 ; Initialising AD conversion. Left justified, All PORTA pins set as analogue input. banksel ADCON1 movlw b'01000000' movwf ADCON0 ;***************************************************************************** ;Starting AD conversion and saving the converted value in the EEPROM register ;0x10 banksel ADCON0 ; start AD conversion bsf ADCON0,2 check_user_adc btfsc ADCON0,2 ; Checking whether AD conversion is over. goto check_user_adc ; if conversion is in progress program goes back to ;check_user_adc banksel ADRESH movf ADRESH,0 ; After AD conversion is complete value in ADRESH is ;taken to W Reg. and value in ADRESL is omitted since ;much accuracy is not needed. banksel temp_adc_user movwf temp_adc_user ; value in working register is moved to the ;temp_adc_user variable movf temp_adc_user,W ; value in temp_adc_user variable is moved to the ;working register call write_rom ; value in working register is written to the EEprom by ;calling the ;write_rom function ; Displaying ?C gotoLCDline .53+.6 ;location for displaying ASCII character 'C' is ;selected by using gotoLCDline macro pausems .15 ;a delay needs to be given for the LCD to process ;instruction send_char 'C' ; character 'C' is displayed in the LCD using ;send_char macro pausems .1 ; again a delay needs to be given for the LCD to ;process data ;***************************************************************************** gotoLCDline .52+.6 ; location for displaying character 'degree' is ;selected by using gotoLCDline macro pausems .15 ; a delay needs to be given for the LCD to process ;instruction send_char b'11011111' ; character '?' is displayed in the LCD using ;send_char macro pausems .1 ; again a delay needs to be given for the LCD to ;process data ;***************************************************************************** ; Displaying the user set temperature value in the LCD movf temp_adc_user,0 ; value in temp_adc_user file is moved to W Reg. since ;toASCII macro converts the value in the W Reg. to a ;two digit number. call toASCII ; Convert value in W Reg.to a two digit number by ;calling the function toASCII gotoLCDline .50+.6 ; location for displaying D1 digit is selected by ;using gotoLCDline macro pausems .100 ; a delay needs to be given for the LCD to process ;instruction movf D1,W ; putting D1 value to the W Reg. since send_char_02 ;macro ;displays the bit pattern in the W Reg. call send_char_02 ; D1 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data movf D2,W ; putting D2 value to the W Reg. since send_char_02 ;macro ;displays the bit pattern in the W Reg. call send_char_02 ; D2 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data goto user_mode ;***************************************************************************** sensor_mode ;***************************************************************************** ;Initialising AD conversion. Clock conversion 4MHz/32,AN4 channel selected as AN0,AD ;conversion OFF,AD converter module powered up banksel ADCON1 movlw b'11000000' movwf ADCON1 ; Initialising AD conversion. Right justified, All PORTA pins set as analogue ;input. banksel ADCON0 movlw b'10000001' movwf ADCON0 banksel ADCON0 ; Starting AD conversion bsf ADCON0,2 ;***************************************************************************** check_adc_value ;***************************************************************************** gotoLCDline LINE1+8+.11 ; location for displaying ASCII character 'C' is ;selected by using gotoLCDline macro pausems .2 ; a delay needs to be given for the LCD to ;process instruction send_char 'C' ; character 'C' is displayed in the LCD using ;send_char macro pausems .1 ; a delay needs to be given for the LCD to ;process data ;***************************************************************************** gotoLCDline LINE1+7+.11 ; location for displaying character 'degree' is ;selected by using gotoLCDline macro pausems .2 ; a delay needs to be given for the LCD to ;process instruction send_char b'11011111' ; character 'degree' is displayed in the LCD using ;send_char macro pausems .1 ; a delay needs to be given for the LCD to process ;data ;***************************************************************************** btfsc ADCON0,2 ; Checking whether AD conversion is over. goto check_adc_value ; if conversion is in progress program goes back to ;check_user_adc banksel ADRESL movf ADRESL,0 ; After AD conversion is complete value in ADRESL is ;taken to W Reg. and value in ADRESH is omitted since ;much accuracy is ;not needed. banksel temp_adc movwf temp_adc ; value in working register is moved to the temp_adc ;variable ; calibrating temperature value decf temp_adc,F ; -1 from temp adc value bcf STATUS,C ; settring carry bit of the STATUS register rrf temp_adc,F ; and rotating temp_adc (simillar to dividing by two ;arithmetically) movf temp_adc,F ; after rotating temp_adc the result is saved in temp_adc. ;***************************************************************************** ; Displaying the sensor temperature value in the LCD movf temp_adc,W ; temp_adc value is saved to W Reg. call toASCII ; Converting value in W Reg.to a two digit ;number by calling the function toASCII gotoLCDline LINE1+5+.11 ; location for displaying D1 digit is selected ;by using gotoLCDline macro pausems .10 ; a delay needs to be given for the LCD to ;process instruction movf D1,W ; putting D1 value to the W Reg. since ;send_char_02 function displays the bit pattern ;in the W Reg. call send_char_02 ; D1 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data movf D2,W ; puting D2 value to the W Reg. since send_char_02 ;function displays the bit pattern in the W Reg. call send_char_02 ; D2 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data ;***************************************************************************** ; Comparing the user set temperature value and current temperature value. read_rom 0x10 ; eeprom value is read from 0x10 register using the read_rom ;macro. The value is in the W Reg. movwf temp_adc_user ; value from W Reg.is moved to temp_adc_user movf temp_adc,0 ; value from temp_adc is moved to the W Reg. subwf temp_adc_user,0 ; subtracting temp_adc from temp_adc_user and ;saving the result in W Reg. ; resulting value after subtracting is a two's complement so it needs to be ;converted to a normal binary value comf W,0 ; complement of W Reg is taken and result is saved in W Reg incf W,0 ; W Reg is incremented by one to get the correct binary ;value after subtracting ; checking whether the difference between temp_adc_user and temp_adc is zero btfsc STATUS,Z ; Checking whether the result is zero by checking the ;Z bit of the status register goto off_h_and_f ; If the difference is zero program jumps to ;off_h_and_f goto test_neg_pos ; If the difference is not zero program jumps to ;test_neg_pos off_h_and_f banksel PORTC bcf PORTC,1 ; cooler fan off bcf PORTC,2 ; heater off goto sensor_mode ; checking whether the difference between temp_adc_user and temp_adc is ;positive or negative test_neg_pos btfss STATUS,C ; Checking whether the difference is +ve or -ve by ;checking the C bit of the status register goto positive ; If the difference is a +ve value program jumps to ;positive goto negative ; If the difference is a -ve value program jumps to ;negative negative banksel PORTC bsf PORTC,1 ; cooler fan on bcf PORTC,2 ; heater off goto soil_mode positive banksel PORTC bcf PORTC,1 ; cooler fan off bsf PORTC,2 ; heater on goto soil_mode ;***************************************************************************** soil_mode ; Soil Moisture monitoring and controlling code ; Initialising AD conversion. Clock conversion 4MHz/32, AN4 channel selected ;as AN2,AD conversion OFF,AD converter module powered up banksel ADCON0 movlw b'10010001' movwf ADCON0 ; Initialising AD conversion. Left justified, All PORTA pins set as analogue ;input. banksel ADCON1 movlw b'01000000' movwf ADCON0 ; starting AD conversion banksel ADCON0 bsf ADCON0,2 ; waiting for AD conversion to finish chk_sm_adc_again btfsc ADCON0,2 goto chk_sm_adc_again ; saving the vale after AD conversion in a variable sm_adc. banksel ADRESH movf ADRESH,0 ; After AD conversion is complete value in ADRESH is taken ;to W Reg. and value in ADRESL is omitted since much ;accuracy is not needed. banksel sm_adc movwf sm_adc ; value in W Reg. is written to sm_adc variable ; displaying soil moisture level movf sm_adc,W ; sm_adc value is saved to W Reg. call toASCII ; Converting value in W Reg.to a two digit number by ;calling the function toASCII gotoLCDline 0x54+.17 ; location for displaying D1 digit is selected by ;using gotoLCDline ;macro pausems .100 ; a delay needs to be given for the LCD to process ;instruction movf D1,W ; putting D1 value to the W Reg. since send_char_02 ;macro displays the bit pattern in the W Reg. call send_char_02 ; D1 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process data movf D2,W ; putting D2 value to the W Reg. since send_char_02 ;macro displays the bit pattern in the W Reg. call send_char_02 ; D2 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data gotoLCDline 0x54+.19 ; location for displaying D2 digit is selected by ;using gotoLCDline macro pausems .2 ; a delay needs to be given for the LCD to process ;instruction send_char '%' ; character '%' is displayed in the LCD using ;send_char macro pausems .1 ; a delay needs to be given for the LCD to process ;data ;***************************************************************************** ; Regulating soil moisture level around 70% movlw .70 ; literal value 70 is moved to W Reg. subwf sm_adc,0 ; subtracting literal value 70 from sm_adc and saving the ;result in W Reg. ; Resulting value after subtracting is a two's complement so it needs to be ;converted to a normal binary value comf W,0 ; complement of W Reg is taken and result is saved in W Reg incf W,0 ; W Reg is incremented by one to get the correct binary ;value after subtracting ; checking whether the difference between sm_adc and literal .70 is zero btfsc STATUS,Z ; Checking whether the result is zero by checking the ;Z bit of the status register goto zero_soil ; If the difference is zero program jumps to zero_soil goto test_neg_soil ; If the difference is not zero program jumps to ;test_neg_soil zero_soil banksel PORTC bcf PORTC,3 ; solenoid valve off goto light ; checking whether the difference between sm_adc and literal .70 is positive ;or negative test_neg_soil btfss STATUS,C ; Checking whether the difference is +ve or -ve by ;checking the C bit of the status register goto positive_soil ; If the difference is a +ve value program jumps to ;positive_soil goto negative_soil ; If the difference is a -ve value program jumps to ;negative_soil negative_soil banksel PORTC bcf PORTC,3 ; solenoid valve off goto light positive_soil banksel PORTC bsf PORTC,3 ; solenoid valve on goto light ;***************************************************************************** light ; Regulating the Light level ; Initialising AD conversion. Clock conversion 4MHz/32,AN4 channel selected as ;AN1,AD conversion OFF,AD converter module powered up banksel ADCON0 movlw b'10001001' movwf ADCON0 ; Initialising AD conversion. Left justified, All PORTA pins set as analogue ;input. banksel ADCON1 movlw b'01000000' movwf ADCON0 ; starting AD conversion banksel ADCON0 bsf ADCON0,2 ; waiting AD conversion to finish chk_ls_adc_again btfsc ADCON0,2 goto chk_ls_adc_again ; saving the vale after AD conversion in a variable ls_adc. banksel ADRESH movf ADRESH,0 ; After AD conversion is complete value in ADRESH is taken ;to W Reg. and value in ADRESL is omitted since much ;accuracy is not needed. banksel ls_adc movwf ls_adc ; value in W Reg. is written to ls_adc variable ; displaying light intensity level movf ls_adc,W ; sm_adc value is saved to W Reg. call toASCII ; Converting value in W Reg.to a two digit number by ;calling the function toASCII gotoLCDline 0x14+.17 ; location for displaying D1 digit is selected by ;using gotoLCDline macro pausems .100 ; a delay needs to be given for the LCD to process ;instruction movf D1,W ; putting D1 value to the W Reg. since send_char_02 ;macro displays the bit pattern in the W Reg. call send_char_02 ; D1 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data movf D2,W ; putting D2 value to the W Reg. since send_char_02 ;macro displays the bit pattern in the W Reg. call send_char_02 ; D2 is displayed on LCD using send_char_02 function pausems .100 ; a delay needs to be given for the LCD to process ;data gotoLCDline 0x14+.19 ; location for displaying D1 digit is selected by ;using gotoLCDline macro pausems .2 ; a delay needs to be given for the LCD to process ;instruction send_char '%' ; character '%' is displayed in the LCD using ;send_char macro pausems .1 ; a delay needs to be given for the LCD to process ;data ;***************************************************************************** ; Regulating light level around 50% movlw .50 ; literal value .50 is moved to W Reg. subwf ls_adc,0 ; subtracting literal value .50 from ls_adc and saving ;the result in W Reg. ; resulting value after subtracting is a two's complement so it needs to be ;converted to a normal binary value comf W,0 ; complement of W Reg is taken and result is saved in W Reg incf W,0 ; W Reg is incremented by one to get the correct binary ;value after subtracting ; checking whether the difference between ls_adc and literal .50 is zero btfsc STATUS,Z ; Checking whether the result is zero by checking the ;Z bit of the status register goto zero_light ; If the difference is zero program jumps to zero_light goto test_neg_light ; If the difference is not zero program jumps to test_neg_light zero_light banksel PORTC bcf PORTC,4 ; bulbs off goto user_mode test_neg_light btfss STATUS,C ; Checking whether the difference is +ve or -ve by ;checking the C bit of the status register goto positive_light ; If the difference is a -ve value program jumps to ;positive_light goto negative_light ; If the difference is a -ve value program jumps to ;negative_light negative_light banksel PORTC bcf PORTC,4 ;switch ON the light goto user_mode positive_light banksel PORTC bsf PORTC,4 ;switch off the bulbs goto user_mode ;***************************************************************************** end
;Program Macros and Functions ;This is a macro that gives the desired delay when used in the main program pausems macro par1 ; defining variables which are in the macro only local Loop1 local dechi local Delay1ms local Loop2 local exit movlw HIGH par1 ;High part of par1 is taken to WREG movwf HIcnt ;High part of par1 moved from WREG to HIcnt movlw LOW par1 ;Low part of par1 is taken to WREG movwf LOcnt ;Low part of par1 moved from WREG to HIcnt Loop1 movf LOcnt, 1 ;LOcnt value taken to WREG btfsc STATUS, Z ;Z is checked to find WREG value is zero or not goto dechi ;If Z was set as 1 program proceeds to dechi since LOcnt value is ; zero call Delay1ms ;If Z is set as 0 1ms delay is called decf LOcnt, f ;LOcnt value is decreased by one goto Loop1 ;goes back to Loop1 to check LOcnt value dechi movf HIcnt, f ;HIcnt value taken to HIcnt variable btfsc STATUS, Z ;Z is checked to find HIcnt value is zero or not goto exit ;If Z is sets as 1, that is HIcnt value is 0 so ;program exits from the macro call Delay1ms ;If Z is set as 0 1ms delay is called decf HIcnt, f ;HIcnt value is decreased by one decf LOcnt, f ;LOcnt value is decreased by one goto Loop1 ;goes back to Loop1 to check LOcnt value Delay1ms ;.100 value is moved to LOOPcnt and decreased its value one ;by one therefore when one Loop2 cycle is reached time ;taken would be 0.01s movlw .100 movwf LOOPcnt Loop2 ; decreases one from LOOPcnt and save it in the same ;variable and returns when the value is zero decfsz LOOPcnt, f goto Loop2 return exit endm ;The code, in the datasheet of PIC16F877A, for reading data from an address of the EEPROM is used and modified as a macro to be used anywhere in the program. When the program exits from this macro the read value is saved in the working register. read_rom macro add ; the macro for reading data from an EEPROM address ;‘add’ where ‘add’ is a parameter and it is changed in ;the main program to desired EEPROM address from ;which data is read movlw add ; add value is moved to the working register banksel EEADR ; bank containing EEADR register is selected (bank 2) MOVWF EEADR ; Address to be read is moved to EEADR banksel EECON1 ; bank containing EECON1 register is selected (bank 3) BCF EECON1,EEPGD ; Point to Data BSF EECON1,RD ; EE Read banksel EEDATA MOVF EEDATA,W ; value in EEDATA is moved to Working Register endm ;A function is written which can be called at any place in the main program in order to convert a value in the Working Register to a two digit number in the format D1D2 (where D1 is the high part and D2 is the low part). It is to be noted that before calling to this function the value that needs to be converted to D1D2 format must be moved to the Working Register. toASCII ; D1D2 local nextD local incAscii local incD2 local endascii clrf D1 ;Initially D1 and D2 are cleared. clrf D2 banksel toASCII_tmp ;toASCII_tmp is a variable and the proper bank ;is selected. movwf toASCII_tmp ;value in W Reg. is moved to toASCII_tmp variable. movlw '0' ; literal value zero is moved to W Reg. first ;and then to both D1 ;and D2 banksel D1 movwf D1 banksel D2 movwf D2 incf toASCII_tmp,F ; ‘toASCII_tmp’ is increment by one because if the ;original value ;of ‘toASCII_tmp’ was zero(value ;before incrementing by one), after decreasing it by ;one in the next instruction of the program will save ;another value instead of 0. nextD decfsz toASCII_tmp,F ;value in ‘toASCII_tmp ’is decremented by one and ;result is saved in ‘toASCII_tmp’ and checked whether ;it is zero goto incAscii ;If the value in ‘toASCII_tmp’ is not zero program ;macro jumps to ‘incAscii’. goto endascii ;If the value in ‘toASCII_tmp’ is zero program macro ;jumps to ‘endascii’ and exits from the macro. incAscii movf D2,W ;D2 is taken to the W Reg. xorlw '9' ;The value in W Reg. is xor with the literal value 9 btfss STATUS,Z ;by checking the Z bit of the STATUS Register it checks ;whether the ;result of xor is zero or not. goto incD2 ;if the result is not zero Z=0 therefore the program jumps ;to ‘incD2’ movlw '0' ;If the result is zero Z=1 it means that the value in D2 is ;9 and therefore ;needs to set as zero and the value of D1 ;needs to be incremented by one movwf D2 incf D1,1 ;D1 value is incremented by one and result is saved to D1 goto nextD incD2 incf D2,F ;D2 value is incremented by one and result is saved to D2 goto nextD endascii nop return ;After writing date or commands to the LCD the following function is called in the main program or in any other macro. PULSE bcf PORTB,RW ; to write to the LCD RW pin in LCD needs to be made low ;(0). nop bsf PORTB,E ; a pulse is given by making the E pin in the LCD high ;thereby enabling LCD nop nop bsf PORTB,E return ;The macro is used to print the following characters in the LCD in the manner as shown below with the aid of the send_char macro and gotoLCDline macro ;Temp ;User/Temp ;Light Intensity ;Soil Moisture lcd_txt macro gotoLCDline LINE1 pausems .10 send_char 'T' send_char 'e' send_char 'm' send_char 'p' gotoLCDline LINE2 pausems .10 send_char 'U' send_char 's' send_char 'e' send_char 'r' send_char '/' send_char 'T' send_char 'e' send_char 'm' send_char 'p' gotoLCDline LINE3 pausems .10 send_char 'L' send_char 'i' send_char 'g' send_char 'h' send_char 't' send_char ' ' send_char 'I' send_char 'n' send_char 't' send_char 'e' send_char 'n' send_char 's' send_char 'i' send_char 't' send_char 'y' gotoLCDline LINE4 pausems .10 send_char 'S' send_char 'o' send_char 'i' send_char 'l' send_char ' ' send_char 'M' send_char 'o' send_char 'i' send_char 's' send_char 't' send_char 'u' send_char 'r' send_char 'e' nop endm ;This macro is used to initialise the LCD and the LCD is used in 8 bit mode lcd_init macro banksel TRISC clrf LCD8bitPORT ;All data and control pins in LCD are cleared bcf LCDctrlPORT,E bcf LCDctrlPORT,RS bcf LCDctrlPORT,RW pausems .15 ;a delay needs to be given after clearing data and control pins bcf PORTB,E ;LCD disabled send_cmd b'00111000' ;Function set pausems .1 send_cmd b'00001100' ;Display ON pausems .1 send_cmd b'00000001' ;Clear Display pausems .1 send_cmd b'00000110' ;Entry mode pausems .1 send_cmd b'00000011' ;Cursor home pausems .1 nop endm ; This macro is used to send commands to the LCD send_cmd macro x movlw x ;x a parameter is moved to the W Reg. movwf LCD8bitPORT ;value in W Reg. is moved to LCD8bitPORT ;(LCD8bitPORT=PORTD) for PORTD data pins of LCD are ;connected bcf LCDctrlPORT,RS ;RS pin in LCD is cleared to write instruction call PULSE ;a pulse needs to be given every time when writing ;data to LCD and it enables LCD endm ;This macro is used to print a character to the LCD screen send_char macro c movlw c ;c a parameter is moved to the W Reg movwf LCD8bitPORT ;value in W Reg is moved to LCD8bitPORT ;(LCD8bitPORT=PORTD) for PORTD data pins of LCD ;are connected bsf LCDctrlPORT,RS ;RS pin in LCD is cleared to write instruction call PULSE ;a pulse needs to be given every time when ;writing data to LCD as it enables LCD call pause1ms ;a small delay is given as LCD takes time to ;process data endm ;This macro is used to select the LCD line where the data needs to be printed. gotoLCDline macro u movlw u iorlw b'10000000' ; or operation for W Reg and literal banksel LCD8bitPORT movwf LCD8bitPORT ;value in W Reg is moved to LCD8bitPORT ;(LCD8bitPORT=PORTD) for PORTD data pins of LCD are ;connected bcf LCDctrlPORT,RS ;writing instruction to LCD call PULSE ;a pulse needs to be given every time when writing data to LCD as ;it enables LCD endm ;This is a function written to be called in the main program to display the bit pattern in the W Reg in the LCD. send_char_02 movwf LCD8bitPORT ; moving the value in W Reg. to LCD8bitPORT=PORTD bsf LCDctrlPORT,RS ; write data since RS is set call PULSE ; giving pulse return ;The code given in the datasheet to write data to a specific address in EEPROM is modified as a function to write the data in the working register to the 0x10 address in the EEPROM write_rom banksel EECON1 BTFSC EECON1,WR ;Wait for write GOTO $-1 ;to complete movlw 0x10 ;;; data adress banksel EEADR MOVWF EEADR ;Address to write banksel temp_adc_user movf temp_adc_user,W ; taking the AD converted value banksel EEDATA MOVWF EEDATA ;to write banksel EECON1 BCF EECON1,EEPGD ;Point to DATA ;memory BSF EECON1,WREN ;Enable writes bcf INTCON,GIE MOVLW 0x55 ; MOVWF EECON2 ;Write 55h MOVLW 0xAA ; MOVWF EECON2 ;Write AAh BSF EECON1,WR ;Set WR bit to;begin write nop nop nop nop BCF EECON1,WREN ;Disable writes return ;This function is used in the send_char macro to give a 1ms time delay. pause1ms pausems .1 return