1. ;    Filename:      toto.asm                                          *
  2. ;    Date:          Jan 2014                                          *
  3. ;    File Version:         1.0                                        *
  4. ;                                                                     *
  5. ;    Author:        Rémi PUTHOMME-ESSAISSI aka gpunk                  *
  6. ;    Company:       Bad .    *
  7. ;                                                                     *
  8. ;                                                                     *
  9. ;**********************************************************************
  10. ;                                                                     *
  11. ;    Files required:  $0 + your assembler                             *
  12. ;                                                                     *
  13. ;                                                                     *
  14. ;                                                                     *
  15. ;**********************************************************************
  16. ;                                                                     *
  17. ;    Notes:       Thanks to:                                          *        
  18. ;       Microchip ofcourse                                            *
  19. ;       BigOnOff for his template/tutorial                            *
  20. ;                                                                     *
  21. ;    Copyright : GPL 2.0 & + ;)                                       *
  22. ;         money & time has been spent on this, so please repect it    *
  23. ;        
  24. ;**********************************************************************
  25.  
  26.  
  27.         list      p=16f877         ; list directive to define processor
  28.         #include <p16f877.inc>        ; processor specific variable definitions
  29.         ;ERRORLEVEL -302 ;remove message about using proper bank
  30.  
  31. ;  __CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_OFF & _WDT_OFF & _HS_OSC
  32. ;  __CONFIG    _CONFIG2, _WRT_OFF & _BOR21V
  33.     __CONFIG   _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_ON & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _PWRTE_OFF & _WDT_OFF & _XT_OSC & _BODEN_OFF & _WRT_ENABLE_ON
  34.  
  35.  
  36. ;*****************************************************************************
  37. ;                               ASSIGNATIONS SYSTEME                         *
  38. ;*****************************************************************************
  39.  
  40. ; REGISTRE OPTION_REG (configuration)
  41. ; -----------------------------------
  42. OPTIONVAL       EQU     B'10000111'
  43.  
  44.                                 ; RBPU                  b7      RB7 pull ups (inverted)
  45.                                 ; INTEDG                b6      RB0 int                         edge side
  46.                                 ; T0CS                  b5      TMR0                            source
  47.                                 ; T0SE                  b4      TMR0                            edge side
  48.                                 ; PSA                   b3      prescaler assignment            TMR0  / WDTMR
  49.                                 ; PS2:PS0               b2 , b1, b0  prescaler rate select      TMR0 or WDTMR
  50.  
  51.  
  52. ; REGISTRE INTCON (contrôle interruptions standard)
  53. ; -------------------------------------------------
  54. INTCONVAL       EQU     B'01100000'
  55.  
  56.                                 ; GIE                   b7  masque autorisation générale interrupt      ACTIVATION (prise en charge... )
  57.                                 ; ne pas mettre ce bit à 1 ici  , attendre fin des initialisations ...
  58.                                 ; PEIE                  b6  interruption perifs ... a/d usart ...               ACTIVATION
  59.                                 ; T0IE                  b5  timer0 overflow ... interrupt                               ACTIVATION
  60.                                 ; INTE                  b4  RB0 int                                                                             ACTIVATION
  61.                                 ; RBIE                  b3      RBIE RB port change      int                                    ACTIVATION
  62.                                 ; T0IF                  b2      timer0 interupt                                                         FLAG
  63.                                 ; INTF                  b1      RB0 int                                                                         FLAG
  64.                                 ; RBIF                  b0      RB port int                                                                     FLAG
  65.  
  66. ; REGISTRE PIE1 (contrôle interruptions périphériques)
  67. ; ----------------------------------------------------
  68. PIE1VAL         EQU     B'01110001'
  69.                                 ; PSPIE         b7 : Toujours 0 sur PIC 16F876 ... pas de port parallele, E ... :p
  70.                                 ; ADIE          b6 : masque interrupt convertisseur A/D         ACTIVATION
  71.                                 ; RCIE          b5 : masque interrupt réception USART           ACTIVATION
  72.                                 ; TXIE          b4 : masque interrupt transmission USART        ACTIVATION
  73.                                 ; SSPIE         b3 : masque interrupt port série synchrone             ACTIVATION
  74.                                 ; CCP1IE        b2 : masque interrupt CCP1                      ACTIVATION
  75.                                 ; TMR2IE        b1 : masque interrupt TMR2 = PR2                                ACTIVATION
  76.                                 ; TMR1IE        b0 : masque interrupt débordement tmr1          ACTIVATION
  77.  
  78. ; REGISTRE PIE2 (contrôle interruptions particulières)
  79. ; ----------------------------------------------------
  80. PIE2VAL         EQU     B'00000001'
  81.                                 ; UNUSED                b7 : inutilisé, laisser à 0
  82.                                 ; RESERVED      b6 : réservé,  laisser à 0
  83.                                 ; UNUSED                b5 : inutilisé, laisser à 0
  84.                                 ; EEIE                  b4 : masque  interrupt écriture EEPROM                 ACTIVATION
  85.                                 ; BCLIE                 b3 : masque  interrupt collision bus                    ACTIVATION
  86.                                 ; UNUSED                b2 : inutilisé, laisser à 0
  87.                                 ; UNUSED                b1 : inutilisé, laisser à 0
  88.                                 ; CCP2IE                b0 : masque  interrupt CCP2                                             ACTIVATION
  89.  
  90.  
  91. ; REGISTRE ADCON1 (ANALOGIQUE/DIGITAL)
  92. ; ------------------------------------
  93. ;ADCON1VAL      EQU     B'10000011'     ; PORTA en mode digital , ra0 1 2 4 analog, justifé a droite
  94. ;ADCON1VAL      EQU     B'10000010'     ; 2/2 ; vref + et - , 2 ADC
  95.  
  96. ;ADCON1VAL      EQU     B'10001101'     ; 2/2 ; vref + et - , 2 ADC
  97. ;ADCON1VAL      EQU     B'10000100'     ;
  98. ADCON1VAL       EQU     B'10000101'     ; 2 ana / 1 vref+, sur ra3
  99.  
  100.  
  101. CHAN0           EQU     b'00000000'
  102. CHAN1           EQU     b'00001000'
  103. CHAN2           EQU     b'00010000'
  104. CHAN3           EQU     b'00011000'
  105. CHAN4           EQU     b'00100000'
  106.  
  107. ADUP            EQU     b'00000001'
  108. ADGO            EQU     b'00000100'
  109. ADFR            EQU     b'10000000'             ; internal rc oscillator
  110.  
  111. ; DIRECTION DES PORTS I/O
  112. ; -----------------------
  113. DIRPORTA        EQU B'00001011'         ; RA6,7 non present, tous en entrées, RA3,4/5 en sortie
  114. DIRPORTB        EQU B'00000000'         ; Direction PORTB : sortie, RB6 et RB7 pour debeugeur ;)
  115. DIRPORTC        EQU B'00000010'         ; Direction PORTC : sortie
  116. DIRPORTD        EQU B'00000000'         ; Direction PORTD : sortie
  117. DIRPORTE        EQU B'00000000'         ; Direction PORTE : sortie
  118.  
  119.  
  120.  
  121. ;*****************************************************************************
  122. ;                             Serial port part                                          *
  123. ;*****************************************************************************
  124. ;----------------------------------------------------------------------------
  125. ;Constants
  126.  
  127. SPBRG_VAL   EQU         .10 ; for 115200   /// .25      ;set baud rate 9600 for 4Mhz clock
  128. TX_BUF_LEN  EQU         .80                             ;length of transmit circular buffer
  129. RX_BUF_LEN  EQU         TX_BUF_LEN                      ;length of receive circular buffer
  130.  
  131. ;----------------------------------------------------------------------------
  132. ;Bit Definitions
  133.  
  134. TxBufFull       EQU         0               ;bit indicates Tx buffer is full
  135. TxBufEmpty      EQU         1                           ;bit indicates Tx buffer is empty
  136. RxBufFull       EQU         2                   ;bit indicates Rx buffer is full
  137. RxBufEmpty      EQU         3                           ;bit indicates Rx buffer is empty
  138. ReceivedCR      EQU         4                           ;bit indicates <CR> character received
  139.  
  140.  
  141. ;*****************************************************************************
  142. ;                             Timer1 port part                                          *
  143. ;*****************************************************************************
  144. ;----------------------------------------------------------------------------
  145. ;       prescaler       XX110000                                ; 11 = 1/8
  146. ;       oscen           XXXX1000                                ; 1 osc enabled
  147. ;       extsunc         XXXXX1XX                                ; 0 ext sync
  148. ;       clksrc          XXXXXX1X                                ; 1 externzl clock , 0 internal clock , fosc/4
  149. ;       tmrena          XXXXXXX1                                ; 1 enable timer
  150.  
  151. TMR1FREQU       EQU         b'00110000'
  152. TMR1OSCEN       EQU         b'00000000'
  153. TMR1EXSYN       EQU         b'00000000'
  154. TMR1CKSRC       EQU         b'00000000'
  155. TMR1ENABL       EQU         b'00000001'
  156.  
  157.  
  158.  
  159.  
  160. ;*****************************************************************************
  161. ;                                  DEFINE                                    *
  162. ;*****************************************************************************
  163. #DEFINE     LEDPORT     PORTA
  164. #DEFINE     LED         LEDPORT,5   ; LED de sortie
  165. #DEFINE     LEDMASK     B'00100000'     ; Masque pour inversion de la LED (RA4)
  166. #DEFINE     CMPTVAL     D'1'            ; valeur de recharge du compteur
  167.  
  168. #DEFINE     LEDPORT2    PORTA
  169. #DEFINE     LED2        LEDPORT2,4      ; LED de sortie
  170. #DEFINE     LEDMASK2    B'00010000'     ; Masque pour inversion de la LED (RA4)
  171. #DEFINE     CMPTVAL2    D'1'            ; valeur de recharge du compteur
  172.  
  173.  
  174.  
  175. ;*****************************************************************************
  176. ;                                  DEFINES for lcd                                    *
  177. ;*****************************************************************************
  178. #DEFINE     LCDPORT     PORTD           ; le lcd est sur le port B en 4bit, a ORer avec 00001111
  179. #DEFINE     LCDSTAT     TRISD           ; idem
  180. #DEFINE     RS          LCDPORT,1       ; register select du lcd:data ou instructions:0 inst, 1 data
  181. #DEFINE     RW          LCDPORT,2       ; mode : lecture / ecriture : 0 write , 1 read
  182. #DEFINE     EN          LCDPORT,3       ; active l execution par le LCD lcd en question sur le bus
  183. #DEFINE     BZF         LCDPORT,7       ; busy flag pour le LCD
  184. #DEFINE     BZD         LCDSTAT,7       ; "busy port" BZF's direction
  185.  
  186. #DEFINE     IN          1
  187. #DEFINE     OUT         0
  188.  
  189.  
  190. ;*****************************************************************************
  191. ;                                  DEFINES for Graphic lcd                                    *
  192. ;*****************************************************************************
  193. #DEFINE     GLCDPORT    PORTB           ; le Glcd est sur le port B en 8bit
  194. #DEFINE     GLCDSTAT    TRISB           ; idem
  195.  
  196. #DEFINE     GRS         PORTE,2         ; register select du lcd:data ou instructions:0 inst, 1 data
  197. #DEFINE     GRW         PORTE,1         ; mode : lecture / ecriture : 0 write , 1 read
  198. #DEFINE     GEN         PORTE,0         ; active l execution par le LCD lcd en question sur le bus
  199.  
  200. #DEFINE     GCS1        PORTA,5         ; graphic pane select 1
  201. #DEFINE     GCS2        PORTA,2         ; // 2
  202. #DEFINE     GRST        PORTA,4         ; Reset the damn ting, would you?
  203.  
  204.  
  205. #DEFINE     GBZF        GLCDPORT,7      ; busy flag pour le LCD
  206. #DEFINE     GBZD        GLCDSTAT,7      ; "busy port" BZF's direction
  207.  
  208.  
  209. ;*****************************************************************************
  210. ;                        VARIABLES BANQUE 0                                  *
  211. ;*****************************************************************************
  212.  
  213. ; Zone de 80 bytes
  214. ; ----------------
  215.  
  216.         CBLOCK      0x20                                                        ; Début de la zone (0x20 à 0x6F)
  217.         cmpt            :1                                                              ; compteur de passage
  218.         cmpt2           :1
  219.         DIGI1           :1
  220.         DIGI2           :1
  221.         DIGI3           :1
  222.         DIGI4           :1
  223.         datal4lb        :1
  224.         datal4hb        :1
  225.         dig1            :1
  226.         dig2            :1
  227.         ADCCHAN         :1
  228.         ACTCHAN0        :1
  229.         ACTCHAN1        :1
  230.         ACTCHAN2        :1
  231.         ACTCHAN3        :1
  232.         ACTCHAN4        :1
  233.         ACTCHAN         :1
  234. ; GLCD stuff
  235.  
  236.         GCONTR          :1
  237.         octel           :1
  238.         Doctel          :1
  239.         DBIT            :1
  240.         DSHIFT          :1
  241.         DPAGE           :1
  242.        
  243.         gPos            :1
  244.  
  245.         gposx           :1
  246.         gposy           :1
  247.  
  248. ; serial stuff
  249.         Flags           :1                                                      ;byte to store indicator flags
  250.         TempData        :1                                                      ;temporary data in main routines
  251.         BufferData      :1                                                      ;temporary data in buffer routines
  252.         TxStartPtr      :1                                                      ;pointer to start of data in TX buffer
  253.         TxEndPtr        :1                                                      ;pointer to end of data in TX buffer
  254.         RxStartPtr      :1                                                      ;pointer to start of data in RX buffer
  255.         RxEndPtr        :1                                                      ;pointer to end of data in RX buffer
  256. ;delay routines
  257.         b1              :1
  258.         b2              :1
  259.         b3              :1
  260.         d1              :1
  261.         d2              :1
  262.         e1              :1
  263.         x1              :1
  264.         x2              :1
  265.         ENDC                                                                            ; Fin de la zone
  266.  
  267. ; more serial stuff
  268.  
  269.         CBLOCK          0xA0
  270.         TxBuffer        :TX_BUF_LEN                     ;transmit data buffer
  271.         ENDC
  272.  
  273.         CBLOCK          0x120
  274.         RxBuffer        :RX_BUF_LEN                     ;receive data buffer
  275.         ENDC
  276.  
  277. ;*****************************************************************************
  278. ;                      VARIABLES ZONE COMMUNE                                *
  279. ;*****************************************************************************
  280.  
  281. ; Zone de 16 bytes
  282. ; ----------------
  283.  
  284.         CBLOCK          0x70                                                    ; Début de la zone (0x70 à 0x7F)
  285.         W_TEMP          : 1                                                             ; Sauvegarde registre W
  286.         STATUS_TEMP     : 1                                                             ; sauvegarde registre STATUS
  287.         FSR_TEMP        : 1                                                             ; sauvegarde FSR (si indirect en interrupt)
  288.         PCLATH_TEMP     : 1                                                             ; sauvegarde PCLATH (si prog>2K)
  289.         datal           : 1
  290.         datah           : 1
  291.         datal_tmp       : 1
  292.         datah_tmp       : 1
  293.         char            : 1
  294.         char_temp       : 1
  295.         cmd             : 1
  296.         tmrl            : 1
  297.         tmrh            : 1
  298.         gYpos           : 1
  299.         gXpos           : 1
  300.         tmport          : 1
  301.         ENDC
  302.  
  303. ;*****************************************************************************
  304. ;                             MACRO                                          *
  305. ;*****************************************************************************
  306.                         ; Changement de banques
  307.                         ; ----------------------
  308.  
  309. BANK0           macro                                                                   ; passer en banque0
  310.         bcf     STATUS,RP0
  311.  
  312.         endm
  313.  
  314. BANK1           macro                                                                   ; passer en banque1
  315.         bsf     STATUS,RP0
  316.  
  317.         endm
  318.  
  319. BANK2           macro                                                                   ; passer en banque2
  320.         bcf     STATUS,RP0
  321.         bsf     STATUS,RP1
  322.         endm
  323.  
  324. BANK3           macro                                                                   ; passer en banque3
  325.         bsf     STATUS,RP0
  326.         bsf     STATUS,RP1
  327.         endm
  328.  
  329. NIBHEX          macro
  330.  
  331.         addlw   0-D'10'
  332.         btfsc   STATUS,C
  333.         addlw   'A'-'0'-d'10'
  334.         addlw   '0'+d'10'
  335.         endm
  336.  
  337. PRINTADC        macro
  338.  
  339.         movfw   datah                                                   ; XXXXXXYY
  340.         andlw   b'00000011'                                     ; remove the zeros      bit 3 to 7  000000YY
  341.         NIBHEX
  342.         movwf   char
  343.         call    LCDsendchar4
  344.  
  345.         swapf   datal,W
  346.         andlw   b'00001111'                                     ; 0000XXXX: remove the zeros
  347.         NIBHEX
  348.         movwf   dig2
  349.         movwf   char
  350.         call    LCDsendchar4
  351.  
  352.         movfw   datal
  353.         andlw   b'00001111'                                     ; 0000XXXX: remove the zeros
  354.         NIBHEX
  355.         movwf   char
  356.         call    LCDsendchar4
  357.         endm
  358.  
  359. GetPOS    macro
  360.  
  361.         swapf   datal,W
  362.         andlw   b'00001111'
  363.         movwf   datal
  364.  
  365.         swapf   datah,W
  366.         andlw   b'00110000'
  367.         iorwf   datal,W
  368.         movwf   gPos
  369.         endm
  370.  
  371. HM      macro
  372.         movlw   b'00000010'             ; home pos 0,0
  373.         movwf   cmd
  374.         call            LCDsendcmd4
  375.         endm
  376.  
  377. WAITCAP         macro
  378.         NOP                                             ; cant help nature !!! hein
  379.         NOP             ; this version uses 20Mhz xtal
  380.         NOP             ; more cycles are needed for the capacitor
  381.         NOP             ; to dicharge .
  382.         NOP             ; a next version, will force this .
  383.         NOP             ; depending on your "speed"
  384.         NOP             ; less NOOPs will be needed ;)
  385.         NOP
  386.         NOP
  387.         NOP
  388.  
  389.         NOP                                             ; cant help nature !!! hein
  390.         NOP
  391.         NOP
  392.         NOP
  393.         NOP
  394.         NOP
  395.         NOP
  396.         NOP
  397.         NOP
  398.         NOP
  399.  
  400.         NOP                                             ; cant help nature !!! hein
  401.         NOP
  402.         NOP
  403.         NOP
  404.         NOP
  405.         NOP
  406.         NOP
  407.         NOP
  408.         NOP
  409.         NOP
  410.         NOP                                             ; cant help nature !!! hein
  411.         NOP
  412.         NOP
  413.         NOP
  414.         NOP
  415.         NOP
  416.         NOP
  417.         NOP
  418.         NOP
  419.         NOP
  420.         NOP                                             ; cant help nature !!! hein
  421.         NOP
  422.         NOP
  423.         NOP
  424.         NOP
  425.         NOP
  426.         NOP
  427.         NOP
  428.         NOP
  429.         NOP
  430.  
  431.         endm
  432.  
  433.  
  434. ;
  435. ; //////////////////////////////
  436.  
  437. ;**********************************************************************
  438.         ORG         0x000                                                               ; processor reset vector
  439.         clrf            PCLATH                                                  ; ensure page bits are cleared
  440.         goto            init                                                                    ; go to beginning of program
  441.  
  442.         ORG         0x004                                                               ; interrupt vector location
  443.  
  444.         movwf           W_TEMP                                                  ;save WREG
  445.         movf            STATUS,W                                                        ;store STATUS in WREG
  446.         clrf            STATUS                                                  ;select file register bank0
  447.         movwf           STATUS_TEMP                                     ;save STATUS value
  448.         movf            PCLATH,W                                                        ;store PCLATH in WREG
  449.         movwf           PCLATH_TEMP                                     ;save PCLATH value
  450.         clrf            PCLATH                                                  ;select program memory page0
  451.         movf            FSR,W                                                           ;store FSR in WREG
  452.         movwf           FSR_TEMP                                                        ;save FSR value
  453.  
  454.  
  455.         BANK0                                                                                   ; passer en banque0
  456.         BTFSC           INTCON,T0IE                                             ; timer0 int configured?
  457.         BTFSC           INTCON,T0IF                                             ; timer0 int accured ?
  458.         GOTO            T0_INT
  459.  
  460.         BSF             STATUS,RP0
  461.         BTFSC           PIE1,TMR1IE
  462.         BANK0
  463.         BTFSC           PIR1,TMR1IF
  464.         GOTO            T1_INT
  465.  
  466.  
  467.         bsf             STATUS,RP0
  468.         BTFSC           PIE1,ADIE
  469.         BANK0
  470.         BTFSC           PIR1,ADIF
  471.         GOTO            AD_INT
  472.  
  473.         bsf             STATUS,RP0
  474.         BTFSC           PIE1,CCP1IE
  475.         BANK0
  476.         BTFSC           PIR1,CCP1IF
  477.         GOTO            CCP1_INT
  478.  
  479.         bsf             STATUS,RP0
  480.         BTFSC           PIE2,CCP2IE
  481.         BANK0
  482.         BTFSC           PIR2,CCP2IF
  483.         GOTO            CCP2_INT
  484.  
  485.  
  486. ; cereal removeer
  487.  
  488.         ; reception
  489.         BANK0                                                                                   ;select bank0
  490.         BTFSC           PIR1,RCIF                                                       ;test RCIF receive interrupt
  491.         BSF             STATUS,RP0                                              ;change to bank1 if RCIF set
  492.         BTFSC           PIE1,RCIE                                                       ;test if interrupt enabled if RCIF set
  493.         GOTO            GetData                                                 ;if RCIF and RCIE set, do receive
  494.         ; emission
  495.         BANK0                                                                                   ;select bank0
  496.         BTFSC           PIR1,TXIF                                                       ;test for TXIF transmit interrupt
  497.         BSF             STATUS,RP0                                              ;change to bank1 if TXIF set
  498.         BTFSC           PIE1,TXIE                                                       ;test if interrupt enabled if TXIF set
  499.         GOTO            PutData                                                 ;if TXIF and TCIE set, do transmit
  500.  
  501.  
  502.         GOTO            END_ISR
  503.  
  504.  
  505. ;------------------------------------
  506. ;Get received data and write into receive buffer.
  507.  
  508. GetData:
  509.         BANK0
  510.         btfsc   RCSTA,OERR                                              ;test overrun error flag
  511.         goto    ErrOERR                                                 ;handle it if error
  512.         btfsc   RCSTA,FERR                                              ;test framing error flag
  513.         goto    ErrFERR                                                 ;handle it if error
  514.  
  515.         btfsc   Flags,RxBufFull                                         ;check if buffer full
  516.         goto    ErrRxOver                                                       ;handle it if full
  517.  
  518.         movf    RCREG,W                                                 ;get received data
  519.         xorlw   0x0d                                                            ;compare with <CR>
  520.         btfsc   STATUS,Z                                                        ;check if the same
  521.         bsf     Flags,ReceivedCR                                        ;indicate <CR> character received
  522.         xorlw   0x0d                                                            ;change back to valid data
  523.         call    PutRxBuffer                                             ;and put in buffer
  524.         goto    END_ISR
  525.  
  526. ;error because OERR overrun error bit is set
  527. ;can do special error handling here - this code simply clears and continues
  528.  
  529. ErrOERR:
  530.         bcf     RCSTA,CREN                                              ;reset the receiver logic
  531.         bsf     RCSTA,CREN                                              ;enable reception again
  532.         goto    END_ISR
  533.  
  534. ;error because FERR framing error bit is set
  535. ;can do special error handling here - this code simply clears and continues
  536.  
  537. ErrFERR:
  538.         movf    RCREG,W                                                 ;discard received data that has error
  539.         goto    END_ISR
  540.  
  541. ;error because receive buffer is full and new data has been received
  542. ;can do special error handling here - this code simply clears and continues
  543.  
  544. ErrRxOver:
  545.         movf    RCREG,W                                                 ;discard received data
  546.         xorlw   0x0d                                                            ;but compare with <CR>
  547.         btfsc   STATUS,Z                                                        ;check if the same
  548.         bsf     Flags,ReceivedCR                                        ;indicate <CR> character received
  549.         goto    END_ISR
  550.  
  551. ;------------------------------------
  552. ;Read data from the transmit buffer and transmit the data.
  553.  
  554. PutData:
  555.         BANK0                                                                           ;select bank 0
  556.         btfss   Flags,TxBufEmpty                                        ;check if transmit buffer empty
  557.         goto    PutDat1                                                 ;if not then go get data and transmit
  558.         bsf     STATUS,RP0                                                                              ;select bank1
  559.         bcf     PIE1,TXIE                                                       ;disable TX interrupt because all done
  560.         goto    END_ISR
  561.  
  562. PutDat1:
  563.         call    GetTxBuffer                                             ;get data to transmit
  564.         movwf   TXREG                                                           ;and transmit
  565.         goto    END_ISR
  566.  
  567.  
  568. ;----------------------------------------------
  569. ;Add a byte (from WREG) to the end of the receive buffer
  570.  
  571. PutRxBuffer:
  572.         BANK0                                           ;select bank 0
  573.         btfsc   Flags,RxBufFull         ;check if buffer full
  574.         goto    ErrRxBufFull            ;and go handle error if full
  575.  
  576.         BANKISEL RxBuffer               ;bank bit for indirect addressing
  577.         movwf   BufferData                      ;save WREG into BufferData
  578.         movf    RxEndPtr,W              ;get EndPointer
  579.         movwf   FSR                             ;and place into FSR
  580.         movf    BufferData,W            ;get BufferData
  581.         movwf   INDF                            ;store in buffer
  582.  
  583. ;test if buffer pointer needs to wrap around to beginning of buffer memory
  584.  
  585.         movlw   LOW RxBuffer+RX_BUF_LEN-1 ;get last address of buffer
  586.         xorwf   RxEndPtr,W              ;and compare with end pointer
  587.         movlw   LOW RxBuffer            ;load first address of buffer
  588.         btfss   STATUS,Z                        ;check if pointer is at last address
  589.         incf    RxEndPtr,W              ;if not then increment pointer
  590.         movwf   RxEndPtr                        ;store new end pointer value
  591.  
  592. ;test if buffer is full
  593.  
  594.         subwf   RxStartPtr,W            ;compare with start pointer
  595.         btfsc   STATUS,Z                        ;check if the same
  596.         bsf     Flags,RxBufFull         ;if so then indicate buffer full
  597.         bcf     Flags,RxBufEmpty        ;buffer cannot be empty
  598.         return
  599.  
  600. ;error because attempting to store new data and the buffer is full
  601. ;can do special error handling here - this code simply ignores the byte
  602.  
  603. ErrRxBufFull:
  604.         return                          ;no save of data because buffer full
  605.  
  606. ;----------------------------------------------
  607. ;Remove and return (in WREG) the byte at the start of the transmit buffer
  608.  
  609. GetTxBuffer:
  610.         BANK0                                           ;select bank 0
  611.         btfsc   Flags,TxBufEmpty        ;check if transmit buffer empty
  612.         goto    ErrTxBufEmpty           ;and go handle error if empty
  613.  
  614.         BANKISEL TxBuffer                       ;bank bit for indirect addressing
  615.         movf    TxStartPtr,W            ;get StartPointer
  616.         movwf   FSR                             ;and place into FSR
  617.  
  618. ;test if buffer pointer needs to wrap around to beginning of buffer memory
  619.  
  620.         movlw   LOW TxBuffer+TX_BUF_LEN-1 ;get last address of buffer
  621.         xorwf   TxStartPtr,W            ;and compare with start pointer
  622.         movlw   LOW TxBuffer            ;load first address of buffer
  623.         btfss   STATUS,Z                        ;check if pointer is at last address
  624.         incf    TxStartPtr,W            ;if not then increment pointer
  625.         movwf   TxStartPtr                      ;store new pointer value
  626.         bcf     Flags,TxBufFull         ;buffer cannot be full
  627.  
  628. ;test if buffer is now empty
  629.  
  630.         xorwf   TxEndPtr,W              ;compare start to end
  631.         btfsc   STATUS,Z                        ;check if the same
  632.         bsf     Flags,TxBufEmpty        ;if same then buffer will be empty
  633.         movf    INDF,W                  ;get data from buffer
  634.  
  635.         return
  636.  
  637. ;error because attempting to read data from an empty buffer
  638. ;can do special error handling here - this code simply returns zero
  639.  
  640. ErrTxBufEmpty:
  641.         retlw   0                       ;tried to read empty buffer
  642.  
  643.  
  644.  
  645.  
  646. T0_INT
  647.         BCF     INTCON,T0IF
  648.  
  649.         goto ADUPDATE  ; zap LED for now  ; décrémenter compteur de passages
  650.  
  651.  
  652.         decfsz  cmpt , f
  653.         GOTO    ADUPDATE ;END_ISR                                                               ; pas 0, on ne fait rien
  654.         movlw   LEDMASK                                                         ; sélectionner bit à inverser
  655. ;       xorwf   LEDPORT,f                                                               ; inverser LED
  656.         movlw   CMPTVAL                                                         ; pour CMPTVAL nouveaux passages
  657.         movwf   cmpt                                                                    ; dans compteur de passages
  658.  
  659.         ;GOTO   END_ISR
  660.  
  661.  
  662. ;T1_INT
  663. ADUPDATE                                                                                        ; Routine when the Timer1 overflows
  664.  
  665.         ; get saved last channel, stick it in the pointer the zeros
  666.         movfw   ACTCHAN
  667.         movwf   FSR
  668.  
  669.         movfw   INDF
  670.         movwf   ADCCHAN
  671.  
  672. ;zap chan3, change here the number of chans to scan
  673.         movfw   ADCCHAN
  674.         xorlw   CHAN2
  675.         btfss   STATUS,2
  676.  
  677.         call    ADClunch
  678.  
  679. ;       BCF             ADCON0,0                ; stop AD
  680.  
  681.         movfw   ADCCHAN
  682.         xorlw   CHAN4                   ; the max channel configured
  683.         btfss   STATUS,2
  684.         goto    INC
  685.         goto    RST
  686.  
  687. INC
  688.         incf    FSR,1
  689.  
  690.         goto    END_AD
  691.  
  692. RST
  693.         movlw   ACTCHAN0
  694.         movwf   FSR
  695.  
  696.         goto    END_AD
  697.  
  698. END_AD
  699.         ;;BCF   PIR1,TMR1IF                                                     ; Clear the Timer1 overflow interrupt flag
  700.         ; save last channel used.
  701.         movfw   FSR
  702.         movwf   ACTCHAN
  703.         GOTO    END_ISR                                                                 ; Ready to leave ISR (for this request)
  704.  
  705. AD_INT                                                                                  ; Routine when the A/D completes
  706.  
  707.         BCF     ADCON0,0
  708.         BCF     PIR1, ADIF                                                              ; Clear the A/D interrupt flag
  709.  
  710.         call    ADCCrunch
  711.   ;  call        CCPCrunch
  712.  
  713.         ;check and print the H
  714. CHECKH
  715.         movfw   ADCCHAN
  716.         xorlw   CHAN0
  717.         btfsc   STATUS,2
  718.         goto    PRINTH
  719.         goto    CHECKV
  720. PRINTH
  721.         movlw   0x80+d'2'  ; b'10000000'                ; goto beginning line 1
  722.         movwf   cmd
  723.         call    LCDsendcmd4
  724.         PRINTADC
  725.         GetPOS
  726.         movfw   gPos
  727.         movwf   gposx
  728.         goto    PRINTDONE
  729.  
  730. ;check and print the V
  731. CHECKV
  732.         movfw   ADCCHAN
  733.         xorlw   CHAN1
  734.         btfsc   STATUS,2
  735.         goto    PRINTV
  736.         goto    PRINTDONE
  737.         ;goto   CHECKL
  738. PRINTV
  739.         movlw   0x80+d'8'   ;b'10000110'                ; goto mid line 1
  740.         movwf   cmd
  741.         call    LCDsendcmd4
  742.         PRINTADC
  743.         GetPOS
  744.         movfw   gPos
  745.         movwf   gposy
  746.         goto    PRINTDONE
  747.  
  748. CHECKL
  749.         movfw   ADCCHAN
  750.         xorlw   CHAN2
  751.         btfsc   STATUS,2
  752.         goto    PRINTL
  753.         goto    PRINTDONE
  754.         ;goto   CHECKL
  755. PRINTL
  756.         movlw   0x80+d'13'   ;b'10000110'               ; goto mid line 1
  757.         ;movlw  0xC0+d'13'  ; b'10000000'               ; goto beginning line 1
  758.         movwf   cmd
  759.         call    LCDsendcmd4
  760.         PRINTADC
  761.         goto    PRINTDONE
  762.  
  763. PRINTDONE
  764.  
  765.         BSF     ADCON0,0                                ; turn AD back on
  766.  
  767.         GOTO    END_ISR                                                                 ; Ready to leave ISR (for this request)
  768.  
  769. T1_INT
  770. graphpos:
  771.  
  772.         BCF     PIR1,TMR1IF
  773.         ;GSC2
  774.         movfw   gposx
  775.         movwf   gXpos
  776.         movfw   gposy
  777.         movwf   gYpos
  778.         call    GLCDsendPixOr
  779.  
  780. ;       decfsz  cmpt2,F
  781. ;       GOTO    END_ISR                                                         ; pas 0, on ne fait rien;
  782.  
  783. ;       btfss   PORTA,4
  784. ;       GOTO    LEDON
  785. ;       GOTO    LEDOFF
  786.  
  787. ;LEDON
  788. ;       bsf     PORTA,4
  789. ;       GOTO    END_LED
  790. ;LEDOFF
  791. ;       bcf     PORTA,4
  792. ;       GOTO    END_LED
  793. ;END_LED
  794. ;       movlw   CMPTVAL2                                                                ; pour CMPTVAL nouveaux passages
  795. ;       movwf   cmpt2
  796.                                                         ; dans compteur de passages
  797.     GOTO        END_ISR
  798.  
  799.  
  800.  
  801. CCP2_INT
  802.  
  803.         BANK1
  804.         BCF     PIE2,CCP2IE                     ; disable this interrupt
  805.         BANK0
  806.         BCF     PIR2,CCP2IF                     ; Clear the CCP interrupt flag
  807.    
  808.     ;stop timer
  809.         movfw   T1CON
  810.         andlw   b'11111110'                     ; whatever is at 1, le reste, mais met bit0 à Zéro
  811.         movwf   T1CON
  812.  
  813.         movfw   CCPR2H
  814.         movwf   tmrh
  815.         movfw   CCPR2L
  816.         movwf   tmrl
  817.  
  818.     ;restart timer
  819.         clrf    TMR1H
  820.         clrf    TMR1L
  821.         movfw   T1CON
  822.         iorlw   b'00000001'
  823.         movwf   T1CON
  824.  
  825.         BANK1
  826.         BSF     PIE2,CCP2IE                            ; reeable this interrupt
  827.         BANK0
  828.  
  829.         GOTO    END_ISR                                                                 ; Ready to leave ISR (for this request)
  830.  
  831. CCP1_INT                                                                                ; Routine when PortB has a change
  832.  
  833.     GOTO    END_ISR
  834.  
  835. END_ISR                                                                                         ;
  836.  
  837.         BANK0                                                                                   ;select bank 0
  838.         movf    FSR_TEMP,W                                                      ;get saved FSR value
  839.         movwf   FSR                                                                     ;restore FSR
  840.         movf    PCLATH_TEMP,W                                           ;get saved PCLATH value
  841.         movwf   PCLATH                                                          ;restore PCLATH
  842.         movf    STATUS_TEMP,W                                           ;get saved STATUS value
  843.         movwf   STATUS                                                          ;restore STATUS
  844.         swapf   W_TEMP,F                                                                ;prepare WREG to be restored
  845.         swapf   W_TEMP,W                                                        ;restore WREG without affecting STATUS
  846.         retfie                                                                  ;return from interrupt
  847.  
  848.  
  849.  
  850.  
  851. ; '__CONFIG' directive is used to embed configuration data within .asm file.
  852. ; The lables following the directive are located in the respective .inc file.
  853. ; See respective data sheet for additional information on configuration word.
  854.  
  855.  
  856.  
  857.  
  858. ; remaining code goes here
  859.  
  860. ;*****************************************************************************
  861. ;                          INITIALISATIONS                                   *
  862. ;*****************************************************************************
  863. init
  864.                         ; initialisation PORTS (banque 0 et 1)
  865.                         ; ------------------------------------
  866.         BANK0                           ; sélectionner banque0
  867.         clrf            PORTA           ; Sorties PORTA à 0
  868.         clrf            PORTB           ; sorties PORTB à 0
  869.         clrf            PORTC           ; sorties PORTC à 0
  870.         clrf            PORTD           ; sorties PORTD à 0
  871.         clrf            PORTE           ; sorties PORTE à 0
  872.  
  873.         clrf            ADCON0
  874.  
  875.         bsf     STATUS,RP0      ; passer en banque1
  876.         movlw   ADCON1VAL       ; PORTA en mode digital/analogique
  877.         movwf   ADCON1          ; écriture dans contrôle A/D
  878.  
  879.         movlw   DIRPORTA                ; Direction PORTA
  880.         movwf   TRISA           ; écriture dans registre direction
  881.         movlw   DIRPORTB                ; Direction PORTB
  882.         movwf   TRISB           ; écriture dans registre direction
  883.         movlw   DIRPORTC                ; Direction PORTC
  884.         movwf   TRISC           ; écriture dans registre direction
  885.         movlw   DIRPORTD                ; Direction PORTD
  886.         movwf   TRISD           ; écriture dans registre direction
  887.         movlw   DIRPORTE                ; Direction PORTE
  888.         movwf   TRISE           ; écriture dans registre direction
  889.  
  890.                 ; Registre d'options (banque 1)
  891.                         ; -----------------------------
  892.         movlw   OPTIONVAL       ; charger masque
  893.         movwf   OPTION_REG      ; initialiser registre option
  894.  
  895.                         ; registres interruptions (banque 1)
  896.                         ; ----------------------------------
  897.         movlw   INTCONVAL       ; charger valeur registre interruption
  898.         movwf   INTCON          ; initialiser interruptions
  899.         movlw   PIE1VAL         ; Initialiser registre
  900.         movwf   PIE1                    ; interruptions périphériques 1
  901.         movlw   PIE2VAL         ; initialiser registre
  902.         movwf   PIE2                    ; interruptions périphériques 2
  903.  
  904.                         ; Effacer RAM banque 0
  905.                         ; ---------------------
  906.         bcf             STATUS,RP0      ; sélectionner banque 0
  907.         movlw   0x20                    ; initialisation pointeur
  908.         movwf   FSR                     ; d'adressage indirect
  909. init1
  910.         clrf    INDF                    ; effacer ram
  911.         incf    FSR,f                   ; pointer sur suivant
  912.         btfss   FSR,7                   ; tester si fin zone atteinte (>7F)
  913.         goto    init1                   ; non, boucler
  914.  
  915.                         ; initialiser variable
  916.                         ; --------------------
  917.         movlw   CMPTVAL         ; charger valeur d'initialisation
  918.         movwf   cmpt                    ; initialiser compteur de passages
  919.  
  920.     movlw       CMPTVAL2                ; charger valeur d'initialisation
  921.         movwf   cmpt2                   ; initialiser compteur de passages
  922.  
  923.  
  924.                         ; autoriser interruptions (banque 0)
  925.                         ; ----------------------------------
  926.         clrf            PIR1                            ; effacer flags 1
  927.         clrf            PIR2                            ; effacer flags 2
  928.  
  929. ; init AD
  930.  
  931.         movlw   ADUP
  932.         movwf   ADCON0                  ; turn on
  933.         movlw   ADFR                    ; get freq
  934.         iorwf   ADCON0,1                ; set freq
  935.  
  936.  
  937.  
  938. ;init timer 1
  939.  
  940.  
  941.  
  942.         clrf            T1CON
  943.         clrf            TMR1H
  944.         clrf            TMR1L
  945.  
  946.         movlw   TMR1FREQU
  947.         movwf   T1CON
  948.  
  949.         movlw   TMR1OSCEN
  950.         iorwf   T1CON,1
  951.  
  952.         movlw   TMR1EXSYN
  953.         iorwf   T1CON,1
  954.  
  955.         movlw   TMR1CKSRC
  956.         iorwf   T1CON,1
  957.  
  958.         movlw   TMR1ENABL
  959.         iorwf   T1CON,1
  960.  
  961.  
  962.  
  963.  
  964. ; init CCP2CON ... test capture...
  965.  
  966.     movlw   b'00000110'
  967.     movwf   CCP2CON
  968.  
  969.  
  970. ; init CCP1CON ... test pwm ...
  971.  
  972.         movlw   b'00001100'
  973.         movwf   CCP1CON
  974.  
  975.     movlw       d'254'          ; frequency
  976.         movwf   PR2
  977.  
  978.         movlw   d'0'            ; duty cycle
  979.         movwf   CCPR1L
  980.  
  981.         movlw   b'00000100'
  982.         movwf   T2CON
  983.  
  984.  
  985.  
  986. ; init pointer
  987.  
  988.         movlw   CHAN0
  989.         movwf   ACTCHAN0
  990.  
  991.         movlw   ACTCHAN0
  992.         movwf   FSR
  993.  
  994.         incf    FSR,1
  995.  
  996.         movlw   CHAN1
  997.         movwf   ACTCHAN1
  998.  
  999.         movlw   ACTCHAN1
  1000.         movwf   FSR
  1001.  
  1002.         incf    FSR,1
  1003.  
  1004.         movlw   CHAN2
  1005.         movwf   ACTCHAN2
  1006.  
  1007.         movlw   ACTCHAN2
  1008.         movwf   FSR
  1009.  
  1010.         incf    FSR,1
  1011.  
  1012.         movlw   CHAN3
  1013.         movwf   ACTCHAN3
  1014.  
  1015.         movlw   ACTCHAN3
  1016.         movwf   FSR
  1017.  
  1018.         incf    FSR,1
  1019.  
  1020.         movlw   CHAN4
  1021.         movwf   ACTCHAN4
  1022.  
  1023.         movlw   ACTCHAN4
  1024.         movwf   FSR
  1025.  
  1026.         decf    FSR,1
  1027.         decf    FSR,1
  1028.         decf    FSR,1
  1029.         decf    FSR,1
  1030.         decf    FSR,1
  1031.  
  1032.  
  1033. ;
  1034. ;macros pour le LCD
  1035.  
  1036. lcdPlop         macro
  1037.         bsf             EN
  1038.         bcf             EN
  1039.         endm
  1040.  
  1041. lcdCmd          macro
  1042.         bcf             RS
  1043.         endm
  1044.  
  1045. lcdDat          macro
  1046.         bsf             RS
  1047.         endm
  1048.  
  1049. lcdRmode        macro
  1050.         bsf             RW
  1051.         endm
  1052.  
  1053. lcdWmode        macro
  1054.         bcf             RW
  1055.         endm
  1056.  
  1057. ; initialisation du LCD
  1058.  
  1059.         call            Delay1m
  1060.         call            Delay1m
  1061.         call            Delay1m
  1062.         call            Delay1m
  1063.         call            Delay1m
  1064.         call            Delay1m
  1065.  
  1066.  
  1067.  
  1068. ; setup  lcd
  1069. ;
  1070.  
  1071.  
  1072.         ; movlw b'00111100'             ; FONCTION SET
  1073.         movfw   LCDPORT
  1074.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1075.         iorlw   b'00100000'             ; FONCTION SET
  1076.         movwf   LCDPORT
  1077.         lcdPlop
  1078.         call    Delay1m
  1079.         call    Delay1m
  1080.         call    Delay1m
  1081.         call    Delay1m
  1082.         call    Delay1m
  1083.  
  1084.  
  1085.         movfw   LCDPORT
  1086.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1087.         iorlw   b'00100000'             ; FONCTION SET
  1088.         movwf   LCDPORT
  1089.         lcdPlop
  1090.         call    Delay1m
  1091.         call    Delay1m
  1092.         call    Delay1m
  1093.         call    Delay1m
  1094.         call    Delay1m
  1095.  
  1096.  
  1097.         movfw   LCDPORT
  1098.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1099.         iorlw   b'11000000'             ; FONCTION SET
  1100.         movwf   LCDPORT
  1101.         lcdPlop
  1102.         call    Delay1m
  1103.         call    Delay1m
  1104.         call    Delay1m
  1105.         call    Delay1m
  1106.         call    Delay1m
  1107.  
  1108.  
  1109.         movfw   LCDPORT
  1110.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1111.         iorlw   b'00000000'             ; FONCTION SET
  1112.         movwf   LCDPORT
  1113.         lcdPlop
  1114.         call    Delay1m
  1115.         call    Delay1m
  1116.         call    Delay1m
  1117.         call    Delay1m
  1118.         call    Delay1m
  1119.  
  1120.  
  1121.         movfw   LCDPORT
  1122.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1123.         ;iorlw  b'11110000'             ; FONCTION SET, cursor+blink
  1124.         iorlw   b'11000000'             ; FONCTION SET, no cur+blnk
  1125.         movwf   LCDPORT
  1126.         lcdPlop
  1127.         call    Delay1m
  1128.         call    Delay1m
  1129.         call    Delay1m
  1130.         call    Delay1m
  1131.         call    Delay1m
  1132.  
  1133.  
  1134.  
  1135.         movfw   LCDPORT
  1136.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1137.         iorlw   b'00000000'             ; FONCTION SET
  1138.         movwf   LCDPORT
  1139.         lcdPlop
  1140.         call    Delay1m
  1141.         call    Delay1m
  1142.         call    Delay1m
  1143.         call    Delay1m
  1144.         call    Delay1m
  1145.  
  1146.  
  1147.         movfw   LCDPORT
  1148.         andlw   b'00001111'             ; mask to preserve RB 0 1 2 3
  1149.         iorlw   b'01100000'             ; FONCTION SET
  1150.         movwf   LCDPORT
  1151.         lcdPlop
  1152.         call    Delay1m
  1153.         call    Delay1m
  1154.         call    Delay1m
  1155.         call    Delay1m
  1156.         call    Delay1m
  1157.  
  1158. ;init lcd END
  1159.  
  1160.  
  1161. ; graphic lcd ...
  1162. ; macros...
  1163.  
  1164. GSC1    macro
  1165.         BsF     GCS1
  1166.         BcF     GCS2
  1167.   ;  NOP
  1168.         endm
  1169. GSC2    macro
  1170.         BcF     GCS1
  1171.         BsF     GCS2
  1172.  ;   NOP
  1173.         endm
  1174. GSC0    macro
  1175.         BsF     GCS1
  1176.         BsF     GCS2
  1177.  ;   NOP
  1178.         endm
  1179. GSC3    macro
  1180.         BcF     GCS1
  1181.         BcF     GCS2
  1182.  ;   NOP
  1183.         endm
  1184.  
  1185. GWRTDAT macro
  1186.         BSF     GRS                     ; data incomming!
  1187.         BCF     GRW                     ; to be written
  1188.  ;   NOP
  1189.         endm
  1190.  
  1191. GWRTINS macro
  1192.         BCF     GRS                     ; instruction incomming!
  1193.         BCF     GRW                     ; to written to GLCD
  1194.  ;   NOP
  1195.         endm
  1196.  
  1197. GRRDDAT macro
  1198.         BSF     GRS                     ; data incoming !
  1199.         BSF     GRW                     ; to be read from the GLCD
  1200.  ;   NOP
  1201.         endm
  1202.  
  1203. GRRDINS macro
  1204.         BCF     GRS
  1205.         BSF     GRW
  1206.  ;   NOP
  1207.         endm
  1208.  
  1209. GPLOP   macro
  1210.         BSF     GEN
  1211.         NOP
  1212.         NOP
  1213.         NOP
  1214.         BCF     GEN
  1215.         NOP
  1216.         NOP
  1217.         NOP
  1218.         endm
  1219. GWAITerde   macro
  1220.  
  1221.     GSC0
  1222.     GRRDINS
  1223.     bsf     STATUS,RP0
  1224.         bsf     GBZD                                                    ; put on read mode en BF port,  TRISB7 as IN to read it
  1225.         BANK0
  1226.         bsf     GEN
  1227.     btfsc       GBZF                                                    ; LCD not busy ?                                read bit  PORTB7
  1228.         goto    $-1
  1229.         bcf GEN
  1230.     bsf     STATUS,RP0
  1231.         bcf     GBZD                                            ; write mode en BF port,                        clear bit TRISB7 of the LCD
  1232.         BANK0
  1233.     NOP
  1234.     ;BCF        GRW
  1235.         endm
  1236.  
  1237.  
  1238. GWAIT   macro
  1239.  
  1240. ;    bsf     STATUS,RP0
  1241. ;    movlw   b'11111111'
  1242. ;    movwf   TRISE
  1243. ;    BANK0
  1244. ;
  1245. ;    movfw   PORTE
  1246. ;    movwf   GCONTR
  1247. ;
  1248. ;    GRRDINS
  1249. ;
  1250. ; ;     bsf     STATUS,RP0
  1251. ;;      bsf     GBZD
  1252. ;;      BANK0
  1253. ;       bsf     GEN
  1254. ;    btfsc      GBZF
  1255. ;       goto    $-1
  1256. ;       bcf     GEN
  1257. ;;    bsf     STATUS,RP0
  1258. ;;      bcf     GBZD
  1259. ;;      BANK0
  1260. ;
  1261. ;    bsf     STATUS,RP0
  1262. ;    movlw   b'00000000'
  1263. ;    movwf   TRISE
  1264. ;    BANK0
  1265. ;
  1266. ;    movfw   GCONTR
  1267. ;    movwf   PORTE
  1268.    
  1269.         call    Delay10µ
  1270.         call    Delay10µ
  1271.         call    Delay10µ
  1272.   ;  call       Delay1m
  1273.   ;  call       Delay1m
  1274.   ;  call       Delay1m
  1275.   ;  call       Delay1m
  1276.  
  1277.  
  1278.         endm
  1279.  
  1280.  
  1281.         ;reset the glcd
  1282.         BCF     GRST                    ; pull down the reset pin,
  1283.         call    Delay1m
  1284.         BSF     GRST                    ; pull it back up,
  1285.         call    Delay1m
  1286.  
  1287.  
  1288.         ; turn it on ;
  1289.  
  1290.         ;BCF            GEN                             ;
  1291.         GSC1
  1292.         GPLOP
  1293.    ; call    Delay1m
  1294.  
  1295.         GSC2
  1296.         GPLOP
  1297.    ; call    Delay1m
  1298.  
  1299.         GWRTINS
  1300.     ;call    Delay1m
  1301.  
  1302.         GSC1                    ; chip 1 setup
  1303.         GPLOP
  1304.         call    Delay1m
  1305.         movlw   b'00111111'             ; DISP ON command
  1306.         movwf   GLCDPORT                ;
  1307.         GPLOP
  1308.     ;set page : x address
  1309.         movlw   b'10111000'
  1310.         movwf   GLCDPORT                ;
  1311.         GPLOP
  1312.         ; set adress : y address
  1313.         movlw   b'01000000'
  1314.         movwf   GLCDPORT                ;
  1315.         GPLOP
  1316.         ; set start line : z address
  1317.         GWAIT
  1318.         movlw   b'11000000'
  1319.         movwf   GLCDPORT                ;
  1320.         GPLOP
  1321.  
  1322.         GSC2            ; chip 2 setup
  1323.         call    Delay1m
  1324.         movlw   b'00111111'             ; DISP ON command
  1325.         movwf   GLCDPORT                ;
  1326.         GPLOP
  1327.         ;set page : x address
  1328.         movlw   b'10111000'
  1329.         movwf   GLCDPORT                ;
  1330.         GPLOP
  1331.         ; set adress : y address
  1332.         movlw   b'01000000'
  1333.         movwf   GLCDPORT                ;
  1334.         GPLOP
  1335.         ; set start line : z address
  1336.         movlw   b'11010000'
  1337.         movwf   GLCDPORT                ;
  1338.         GPLOP
  1339.  
  1340.         ; Clear screen .....
  1341.  
  1342.  
  1343. ;       for  (page(x) =0, page(x) +,  page(x)< 8)
  1344. ;       {
  1345. ;               for (segment(y)=0, segment(y)+, segment(y)< 64)
  1346. ;               {       clear OcTel }
  1347. ;       }
  1348. ;
  1349. ClearGscreen
  1350.  
  1351. SETPAGE         EQU             b'10111000'             ; Les liges / X ??
  1352. SETSEGM         EQU             b'01000000'             ; Les colonnes Y
  1353.  
  1354.  
  1355.         GSC1
  1356.         movlw   d'8'
  1357.         movwf   gXpos
  1358.         movlw   d'64'
  1359.         movwf   gYpos
  1360. _1cls0loop
  1361.         GWAIT
  1362.         GWRTINS
  1363.         movlw   SETPAGE
  1364.         iorwf   gXpos,W
  1365.         movwf   GLCDPORT
  1366.         GPLOP
  1367.         movlw   SETSEGM
  1368.         movwf   GLCDPORT
  1369.         GPLOP
  1370. _1cls1loop
  1371.         GWAIT
  1372.         GWRTDAT
  1373.         movlw   b'00000000'             ;       0x01                                            ; rempli de zeros
  1374.         movwf   GLCDPORT
  1375.         GPLOP
  1376.         decfsz  gYpos,F
  1377.         goto    _1cls1loop
  1378.         decfsz  gXpos,F
  1379.         goto    _1cls0loop
  1380.  
  1381.  
  1382.         GSC2
  1383.         movlw   d'8'
  1384.         movwf   gXpos
  1385.         movlw   d'64'
  1386.         movwf   gYpos
  1387. _2cls0loop
  1388.         GWAIT
  1389.         GWRTINS
  1390.         movlw   SETPAGE
  1391.         iorwf   gXpos,W
  1392.         movwf   GLCDPORT
  1393.         GPLOP
  1394.         movlw   SETSEGM
  1395.         movwf   GLCDPORT
  1396.         GPLOP
  1397. _2cls1loop
  1398.         GWAIT
  1399.         GWRTDAT
  1400.         movlw   b'00000000'             ;       0x01                                            ; rempli de zeros
  1401.         movwf   GLCDPORT
  1402.         GPLOP
  1403.         decfsz  gYpos,F
  1404.         goto    _2cls1loop
  1405.         decfsz  gXpos,F
  1406.         goto    _2cls0loop
  1407.  
  1408.        
  1409. Welcome
  1410. ; welcome screen
  1411.         GSC1
  1412.         GWRTINS
  1413.         movlw   SETPAGE+0x05
  1414.         movwf   GLCDPORT                ;
  1415.         GPLOP
  1416.         GWAIT
  1417.         movlw   SETSEGM+0x55
  1418.         movwf   GLCDPORT
  1419.         GPLOP
  1420.  
  1421.         ;test
  1422.  
  1423.     GWAIT
  1424.         GWRTDAT                         ; write data
  1425.  
  1426.     GWAIT
  1427.         movlw   b'01111111'             ; testing :p
  1428.         movwf   GLCDPORT                ;
  1429.         GPLOP
  1430.     GWAIT
  1431.         movlw   b'00001001'             ; testing :p
  1432.         movwf   GLCDPORT                ;
  1433.         GPLOP
  1434.     GWAIT
  1435.         movlw   b'00011001'             ; testing :p
  1436.         movwf   GLCDPORT                ;
  1437.         GPLOP
  1438.     GWAIT
  1439.         movlw   b'00101001'             ; testing :p
  1440.         movwf   GLCDPORT                ;
  1441.         GPLOP
  1442.     GWAIT
  1443.         movlw   b'01000110'             ; testing :p
  1444.         movwf   GLCDPORT                ;
  1445.         GPLOP
  1446.  
  1447.     GWAIT
  1448.         movlw   b'00111000'             ; testing :p
  1449.         movwf   GLCDPORT                ;
  1450.         GPLOP
  1451.     GWAIT
  1452.         movlw   b'01010100'             ; testing :p
  1453.         movwf   GLCDPORT                ;
  1454.         GPLOP
  1455.     GWAIT
  1456.         movlw   b'01010100'             ; testing :p
  1457.         movwf   GLCDPORT                ;
  1458.         GPLOP
  1459.     GWAIT
  1460.         movlw   b'01001000'             ; testing :p
  1461.         movwf   GLCDPORT                ;
  1462.         GPLOP
  1463.     GWAIT
  1464.         movlw   b'00000000'             ; testing :p
  1465.         movwf   GLCDPORT                ;
  1466.         GPLOP
  1467.  
  1468.         GSC2
  1469.  
  1470.         GWRTINS
  1471.         ;set page : x address
  1472.         movlw   SETPAGE+0x06
  1473.         movwf   GLCDPORT                ;
  1474.         GPLOP
  1475.     GWAIT
  1476.         movlw   SETSEGM+0x55
  1477.         movwf   GLCDPORT
  1478.         GPLOP
  1479.     GWAIT
  1480.  
  1481.         GWRTDAT
  1482.         movlw   b'01111111'             ; testing :p
  1483.         movwf   GLCDPORT                ;
  1484.         GPLOP
  1485.     GWAIT
  1486.         movlw   b'00001001'             ; testing :p
  1487.         movwf   GLCDPORT                ;
  1488.         GPLOP
  1489.     GWAIT
  1490.         movlw   b'00011001'             ; testing :p
  1491.         movwf   GLCDPORT                ;
  1492.         GPLOP
  1493.     GWAIT
  1494.         movlw   b'00101001'             ; testing :p
  1495.         movwf   GLCDPORT                ;
  1496.         GPLOP
  1497.     GWAIT
  1498.         movlw   b'01000110'             ; testing :p
  1499.         movwf   GLCDPORT                ;
  1500.         GPLOP
  1501.  
  1502.     GWAIT
  1503.         movlw   b'00111000'             ; testing :p
  1504.         movwf   GLCDPORT                ;
  1505.         GPLOP
  1506.     GWAIT
  1507.         movlw   b'01010100'             ; testing :p
  1508.         movwf   GLCDPORT                ;
  1509.         GPLOP
  1510.     GWAIT
  1511.         movlw   b'01010110'             ; testing :p
  1512.         movwf   GLCDPORT                ;
  1513.         GPLOP
  1514.     GWAIT
  1515.         movlw   b'00001001'             ; testing :p
  1516.         movwf   GLCDPORT                ;
  1517.         GPLOP
  1518.     GWAIT
  1519.         movlw   b'00000000'             ; testing :p
  1520.         movwf   GLCDPORT                ;
  1521.         GPLOP
  1522.  
  1523.     GWAIT
  1524.         movlw   b'01111100'             ; testing :p
  1525.         movwf   GLCDPORT                ;
  1526.         GPLOP
  1527.     GWAIT
  1528.         movlw   b'00001000'             ; testing :p
  1529.         movwf   GLCDPORT                ;
  1530.         GPLOP
  1531.     GWAIT
  1532.         movlw   b'00000100'             ; testing :p
  1533.         movwf   GLCDPORT                ;
  1534.         GPLOP
  1535.     GWAIT
  1536.         movlw   b'01111000'             ; testing :p
  1537.         movwf   GLCDPORT                ;
  1538.         GPLOP
  1539.     GWAIT
  1540.         movlw   b'00000100'             ; testing :p
  1541.         movwf   GLCDPORT                ;
  1542.         GPLOP
  1543.     GWAIT
  1544.         movlw   b'01111000'             ; testing :p
  1545.         movwf   GLCDPORT                ;
  1546.         GPLOP
  1547.  
  1548.     GWAIT
  1549.         movlw   b'00000000'             ; testing :p
  1550.         movwf   GLCDPORT                ;
  1551.         GPLOP
  1552.     GWAIT
  1553.         movlw   b'01111101'             ; testing :p
  1554.         movwf   GLCDPORT                ;
  1555.         GPLOP
  1556.  
  1557.     goto startg
  1558.  
  1559. GLCDsendoctel:
  1560.  
  1561.         movfw   octel
  1562.         GRRDINS
  1563.         bsf     GEN
  1564.         BANK1
  1565.         bsf     GBZD                    ; put on read mode en BF port,  TRISB7 as IN to read it
  1566.         BANK0
  1567.         btfsc   GBZF                    ; LCD not busy ?                                read bit  PORTB7
  1568.         goto    $-1
  1569.         bsf     STATUS,RP0
  1570.         bcf     GBZD                    ; write mode en BF port,                        clear bit TRISB7 of the LCD
  1571.         BANK0
  1572.         GWRTDAT
  1573.         movwf   GLCDPORT
  1574.         bcf     GEN
  1575.         GPLOP
  1576.         return
  1577.  
  1578.  
  1579. GLCDsendPixel:
  1580.     movlw   b'00000001'              ; the pixel to put, need to find its position "parmis" les 8 bit de l'octel
  1581.     movwf   DBIT                     ; 8 positions a calculer depuis XXXXXPPP, PPP 3 bits --> 8 positions
  1582.                                         ; Find "bit"
  1583.     movfw   gYpos
  1584.     andlw   b'00000111'
  1585.     addlw   d'1'
  1586.     movwf   DSHIFT                      ; to make the pixel , shift of of '10000000'
  1587.                                         ; Find Page, divide by 8, trash after Virgule
  1588.     rrf     gYpos,F
  1589.     rrf     gYpos,F
  1590.     rrf     gYpos,W
  1591.     andlw   b'00011111'
  1592.     movwf   DPAGE
  1593.     BCF     STATUS,C
  1594.  
  1595. shifting
  1596.     decfsz  DSHIFT,F
  1597.     goto    shiftIt
  1598.     goto    makePixel
  1599. shiftIt
  1600.     rlf     DBIT,F
  1601.     movfw   DBIT
  1602.     goto    shifting
  1603.  
  1604. makePixel
  1605.     GWRTINS
  1606.     movfw   DPAGE
  1607.     iorlw   SETPAGE
  1608.     movwf   GLCDPORT
  1609.     GPLOP
  1610.     GWAIT
  1611.     ;GWRTINS
  1612.     movfw   gXpos
  1613.     iorlw   SETSEGM
  1614.     movwf   GLCDPORT
  1615.     GPLOP
  1616.     GWAIT
  1617.     GWRTDAT
  1618.     movfw   DBIT
  1619.     movwf   octel
  1620.     call    GLCDsendoctel
  1621.     GPLOP
  1622.     return
  1623.  
  1624. GLCDsendPixOr:
  1625.     movlw   b'00000001'                                           ; the pixel to put, need to find its position "parmis" les 8 bit de l'octel
  1626.     movwf   DBIT                                                        ; 8 positions a calculer depuis XXXXXPPP, PPP 3 bits --> 8 positions
  1627. ; assign "bit"'s position
  1628.     movfw   gYpos
  1629.     andlw   b'00000111'
  1630.     addlw   d'1'
  1631.     movwf   DSHIFT      ; to make the pixel , shift of of '10000000'
  1632.     ; Find Page, divide by 8, trash after Virgule
  1633.     rrf     gYpos,F
  1634.     rrf     gYpos,F
  1635.     rrf     gYpos,W
  1636.     andlw   b'00011111'
  1637.     movwf   DPAGE
  1638.     BCF     STATUS,C
  1639.  
  1640. shiftingOr
  1641.     decfsz  DSHIFT,F
  1642.     goto    shiftItOr
  1643.     goto    makePixelOr
  1644. shiftItOr
  1645.     rlf     DBIT,F
  1646.     movfw   DBIT
  1647.     goto    shiftingOr
  1648.  
  1649. makePixelOr
  1650.  
  1651.     GWRTINS
  1652.     movfw   DPAGE
  1653.     iorlw   SETPAGE
  1654.     movwf   GLCDPORT
  1655.     GPLOP
  1656.     GWAIT
  1657.     ;GWRTINS
  1658.     movfw   gXpos
  1659.     iorlw   SETSEGM
  1660.     movwf   GLCDPORT
  1661.     GPLOP
  1662.     GWAIT
  1663.  
  1664. ;get the octel already in place
  1665.     bsf     STATUS,RP0
  1666.     movlw   b'11111111'
  1667.     movwf   GLCDSTAT
  1668.     BANK0
  1669.     GRRDDAT
  1670.     GPLOP
  1671.         GWAIT
  1672.         GWAIT
  1673.     movfw   GLCDPORT
  1674.     movwf   Doctel
  1675.     bsf     STATUS,RP0
  1676.     movlw   b'00000000'
  1677.     movwf   GLCDSTAT
  1678.     BANK0
  1679.     GWAIT
  1680.  
  1681. ; write the pixel
  1682. ;go back to my xPos, reading it, incremented it,the zeros man
  1683.     GWRTINS
  1684.     movfw   gXpos
  1685.     iorlw   SETSEGM
  1686.     movwf   GLCDPORT
  1687.     GPLOP
  1688.     GWAIT
  1689.  
  1690.     GWRTDAT
  1691.     movfw       DBIT
  1692.     iorwf       Doctel,0
  1693.     movwf       octel
  1694.     call        GLCDsendoctel
  1695.     GPLOP
  1696.  
  1697.     return
  1698.  
  1699.  
  1700.  
  1701. startg
  1702.  
  1703.     GSC1
  1704.    
  1705.     movlw   b'11001100'
  1706.     movwf   octel
  1707.     call    GLCDsendoctel
  1708.     movlw   b'11001100'
  1709.     movwf   octel
  1710.     call    GLCDsendoctel
  1711.  
  1712.     movlw   b'11001100'
  1713.     movwf   octel
  1714.     call    GLCDsendoctel
  1715.  
  1716.     movlw   b'00110011'
  1717.     movwf   octel
  1718.     call    GLCDsendoctel
  1719.  
  1720.     movlw   b'00110011'
  1721.     movwf   octel
  1722.     call    GLCDsendoctel
  1723.  
  1724.  
  1725.     movlw   b'11001100'
  1726.     movwf   octel
  1727.     call    GLCDsendoctel
  1728.  
  1729.     movlw   b'11001100'
  1730.     movwf   octel
  1731.     call    GLCDsendoctel
  1732.  
  1733.     movlw   b'00110011'
  1734.     movwf   octel
  1735.     call    GLCDsendoctel
  1736.  
  1737.     movlw   b'00110011'
  1738.     movwf   octel
  1739.     call    GLCDsendoctel
  1740.  
  1741.     GSC1
  1742.  
  1743.     movlw   b'11001100'
  1744.     movwf   octel
  1745.     call    GLCDsendoctel
  1746.  
  1747.     movlw   b'11001100'
  1748.     movwf   octel
  1749.     call    GLCDsendoctel
  1750.  
  1751.     movlw   b'00110011'
  1752.     movwf   octel
  1753.     call    GLCDsendoctel
  1754.  
  1755.     movlw   b'00110011'
  1756.     movwf   octel
  1757.     call    GLCDsendoctel
  1758.  
  1759.  
  1760.     movlw   b'11001100'
  1761.     movwf   octel
  1762.     call    GLCDsendoctel
  1763.  
  1764.     movlw   b'11001100'
  1765.     movwf   octel
  1766.     call    GLCDsendoctel
  1767.  
  1768.     movlw   b'00110011'
  1769.     movwf   octel
  1770.     call    GLCDsendoctel
  1771.  
  1772.     movlw   b'00110011'
  1773.     movwf   octel
  1774.     call    GLCDsendoctel
  1775.  
  1776.  
  1777.  
  1778.     GSC1
  1779.  
  1780.  
  1781.  
  1782.     movlw   d'1'
  1783.     movwf   gXpos
  1784.     movlw   d'5'
  1785.     movwf   gYpos
  1786.     call    GLCDsendPixOr
  1787.  
  1788.     movlw   d'5'
  1789.     movwf   gXpos
  1790.     movlw   d'10'
  1791.     movwf   gYpos
  1792.     call    GLCDsendPixOr
  1793.  
  1794.  
  1795.     movlw   d'20'
  1796.     movwf   gXpos
  1797.     movlw   d'45'
  1798.     movwf   gYpos
  1799.     call    GLCDsendPixOr
  1800.  
  1801.  
  1802.  
  1803.  
  1804. gline
  1805.     movlw   d'64'+.1
  1806.     movwf   tmport
  1807. gloop
  1808.     decfsz  tmport,F
  1809.     goto    gprint
  1810.     goto    gdone
  1811.  
  1812. gprint
  1813.     movfw   tmport
  1814.     movwf   gYpos
  1815.     movwf   gXpos
  1816.     call    GLCDsendPixOr
  1817.     goto    gloop
  1818. gdone
  1819.  
  1820.     GSC2
  1821.  
  1822.     movlw   d'56'
  1823.     movwf   gXpos
  1824.     movlw   d'44'
  1825.     movwf   gYpos
  1826.     call    GLCDsendPixOr
  1827.  
  1828.  
  1829. gline2
  1830.     movlw   d'64'+.1
  1831.     movwf   tmport
  1832. gloop2
  1833.     decfsz  tmport,F
  1834.     goto    gprint2
  1835.     goto    gdone2
  1836.  
  1837. gprint2
  1838.     movfw   tmport
  1839.     movwf   gYpos
  1840.     movwf   gXpos
  1841.     call    GLCDsendPixOr
  1842.     goto    gloop2
  1843. gdone2
  1844.  
  1845.  
  1846.     movlw   d'20'
  1847.     movwf   gYpos
  1848.     movlw   d'18'
  1849.     movwf   gXpos
  1850.     call    GLCDsendPixOr
  1851.  
  1852.     movlw   d'11'
  1853.     movwf   gYpos
  1854.     movlw   d'11'
  1855.     movwf   gXpos
  1856.     call    GLCDsendPixOr
  1857.  
  1858.     movlw   d'0'
  1859.     movwf   gYpos
  1860.     movlw   d'25'
  1861.     movwf   gXpos
  1862.     call    GLCDsendPixOr
  1863.  
  1864.     movlw   d'0'
  1865.     movwf   gYpos
  1866.     movlw   d'35'
  1867.     movwf   gXpos
  1868.     call    GLCDsendPixOr
  1869.  
  1870.  
  1871.         goto    start                           ; programme principal
  1872.  
  1873.  
  1874.  
  1875. ;*****************************************************************************
  1876. ;                      PROGRAMME PRINCIPAL                                   *
  1877. ;*****************************************************************************
  1878. ;
  1879. ;               macro applicatives ...
  1880.  
  1881.  
  1882.  
  1883.  
  1884. CLS     macro
  1885.         movlw   b'00000001'                     ; clear screen
  1886.         movwf   cmd
  1887.         call            LCDsendcmd4
  1888.         endm
  1889.  
  1890.  
  1891.  
  1892. ;   fin macro applicatives
  1893.  
  1894. start
  1895.  
  1896.         CLS                                                     ; clear screen
  1897.  
  1898.  
  1899.     movlw       0x80+d'00'  ; b'10000000'               ; goto beginning line 1
  1900.         movwf   cmd
  1901.         call            LCDsendcmd4
  1902.         movlw   A'V';    b'01001000'            ; put H unit ...
  1903.         movwf   char
  1904.         call            LCDsendchar4
  1905.         movlw   A':';    b'01001000'            ; put H unit ...
  1906.         movwf   char
  1907.         call            LCDsendchar4
  1908.  
  1909.     movlw       0x80+d'06'   ;b'10000110'               ; goto mid line 1
  1910.         movwf   cmd
  1911.         call            LCDsendcmd4
  1912.         movlw   A'I'    ;b'01010110'            ; put V unit ...
  1913.         movwf   char
  1914.         call            LCDsendchar4
  1915.     movlw       A':'    ;b'01010110'            ; put V unit ...
  1916.         movwf   char
  1917.         call            LCDsendchar4
  1918.  
  1919.     movlw       0x80+d'12'   ;b'10000110'               ; goto mid line 1
  1920.         movwf   cmd
  1921.         call            LCDsendcmd4
  1922.         movlw   A'C'    ;b'01010110'            ; put V unit ...
  1923.         movwf   char
  1924.         call            LCDsendchar4
  1925.     ;movlw      A':'    ;b'01010110'            ; put V unit ...
  1926.         ;movwf  char
  1927.         ;call           LCDsendchar4
  1928.  
  1929.  
  1930.     movlw       0xC0+d'10'; b'10001100'                 ; goto to 3/3 of line 1
  1931.         movwf   cmd
  1932.         call            LCDsendcmd4
  1933.         movlw   A'T'        ;b'01001000'                        ; put H unit ...
  1934.         movwf   char
  1935.         call            LCDsendchar4
  1936.     movlw       A':'        ;b'01001000'                        ; put H unit ...
  1937.         movwf   char
  1938.         call            LCDsendchar4
  1939.  
  1940.  
  1941.         movlw   0xC0+d'0'; b'10001100'                  ; goto to 3/3 of line 1
  1942.         movwf   cmd
  1943.         call            LCDsendcmd4
  1944.         movlw   A'S'        ;b'01001000'                        ; put H unit ...
  1945.         movwf   char
  1946.         call            LCDsendchar4
  1947.  
  1948.         call            SetupSerial                     ;set up serial port and buffers
  1949.  
  1950.  
  1951.     ; test GLCD :p
  1952.  
  1953.     ; write doctel
  1954.  
  1955.     movlw       0x80+d'13'   ;b'10000110'               ; goto mid line 1
  1956.         movwf   cmd
  1957.         call            LCDsendcmd4
  1958.  
  1959.     swapf       Doctel,0
  1960.     andlw       b'00001111'
  1961.         NIBHEX
  1962.     movwf       char
  1963.         call            LCDsendchar4
  1964.  
  1965.     movfw       Doctel
  1966.     andlw       b'00001111'
  1967.     NIBHEX
  1968.     movwf       char
  1969.         call        LCDsendchar4
  1970.  
  1971.    
  1972.  
  1973.         BANK0
  1974.         bsf             INTCON,GIE                              ; activer les interruptions
  1975.  
  1976. toto
  1977.  
  1978.         BANK0
  1979.         btfsc   Flags,ReceivedCR                ;check if <CR> character received
  1980.         call            CopyRxToTx                      ;if so then move received data
  1981.  
  1982.  
  1983. ;       call    GetRxBuffer
  1984. ;;
  1985. ;
  1986.         bcf             INTCON,GIE                              ; desactiver les interruptions
  1987.  
  1988.         movlw   0xC0+d'1'  ;b'10001101'                 ; goto to 3/3 of line 1
  1989.         movwf   cmd
  1990.         call            LCDsendcmd4
  1991. ;
  1992.  
  1993.         BANKISEL RxBuffer               ;bank bit for indirect addressing
  1994.         movf    RxStartPtr,W            ;get StartPointer
  1995.         movwf   FSR                             ;and place into FSR
  1996.         movlw   LOW RxBuffer+RX_BUF_LEN-1 ;get last address of buffer
  1997.         xorwf   RxStartPtr,W            ; and compare with start pointer
  1998.         movlw   LOW RxBuffer            ;load first address of buffer
  1999.  
  2000.         movf    INDF,W                  ;get data from buffer
  2001.         movwf   char
  2002.         call    LCDsendchar4
  2003.  
  2004.         incf    FSR,F
  2005.         movf    INDF,W                  ;get data from buffer
  2006.         movwf   char
  2007.         call    LCDsendchar4
  2008.  
  2009.         incf    FSR,F
  2010.         movf    INDF,W                  ;get data from buffer
  2011.         movwf   char
  2012.         call    LCDsendchar4
  2013.  
  2014.         incf    FSR,F
  2015.         movf    INDF,W                  ;get data from buffer
  2016.         movwf   char
  2017.         call    LCDsendchar4
  2018.  
  2019.         incf    FSR,F
  2020.         movf    INDF,W                  ;get data from buffer
  2021.         movwf   char
  2022.         call    LCDsendchar4
  2023.  
  2024.         incf    FSR,F
  2025.         movf    INDF,W                  ;get data from buffer
  2026.         movwf   char
  2027.         call    LCDsendchar4
  2028.  
  2029.         incf    FSR,F
  2030.         movf    INDF,W                  ;get data from buffer
  2031.         movwf   char
  2032.         call    LCDsendchar4
  2033.  
  2034.         incf    FSR,F
  2035.         movf    INDF,W                  ;get data from buffer
  2036.         movwf   char
  2037.         call    LCDsendchar4
  2038.  
  2039.  
  2040.         ;movlw  TempData
  2041.         ;movlw  b'01001000'                     ; put H unit ...
  2042.  
  2043.  
  2044.     ;timer the zeros ...
  2045. PRINTCCP:
  2046.  
  2047.     movlw       0xC0+d'12'  ;; goto to 3/3 of line 1
  2048.         movwf   cmd
  2049.         call    LCDsendcmd4
  2050.  
  2051.     swapf   tmrh,W
  2052.     andlw   b'00001111'
  2053.     NIBHEX
  2054.     movwf   char
  2055.     call    LCDsendchar4
  2056.  
  2057.     movfw   tmrh
  2058.     andlw   b'00001111'
  2059.     NIBHEX
  2060.     movwf   char
  2061.     call    LCDsendchar4
  2062.  
  2063.     swapf   tmrl,W
  2064.     andlw   b'00001111'
  2065.     NIBHEX
  2066.     movwf   char
  2067.     call    LCDsendchar4
  2068.  
  2069.     movfw   tmrl
  2070.     andlw   b'00001111'
  2071.     NIBHEX
  2072.     movwf   char
  2073.     call    LCDsendchar4
  2074.  
  2075.         bsf             INTCON,GIE                              ; reactiver les interruptions     ( sinon interferance avec ecriture LCD
  2076.  
  2077.  
  2078.         goto    toto                                    ; boucle principale ...si besoin de boucle ...
  2079.  
  2080.  
  2081. ; cereal routines
  2082. ;----------------------------------------------------------------------------
  2083. ;Set up serial port and buffers.
  2084.  
  2085. SetupSerial:
  2086.         bsf     STATUS,RP0                                                              ;select bank 1
  2087.         movlw   b'11000000'                             ;set tris bits for TX and RX
  2088.         iorwf   TRISC,F
  2089.         movlw   SPBRG_VAL                               ;set baud rate
  2090.         movwf   SPBRG
  2091.         movlw   0x24                                            ;enable transmission and high baud rate
  2092.         movwf   TXSTA
  2093.         BANK0                                           ;select bank0
  2094.         movlw   0x90                            ;enable serial port and reception
  2095.         movwf   RCSTA
  2096.         clrf    Flags                                   ;clear all flag bits
  2097.  
  2098.         call    InitTxBuffer                    ;initialize transmit buffer
  2099.         call    InitRxBuffer                    ;initialize receive buffer
  2100.  
  2101.         return
  2102. ;----------------------------------------------
  2103. ;Add a byte (from WREG) to the end of the transmit buffer
  2104.  
  2105. PutTxBuffer:
  2106.         bsf     STATUS,RP0                                              ;select bank 1
  2107.         bcf             PIE1,TXIE                       ;disable transmit interrupt
  2108.         BANK0                                           ;select bank 0
  2109.         btfsc   Flags,TxBufFull         ;check if buffer full
  2110.         goto    ErrTxBufFull            ;and go handle error if full
  2111.  
  2112.         BANKISEL TxBuffer                       ;bank bit for indirect addressing
  2113.         movwf   BufferData                      ;save WREG data into BufferData
  2114.         movf    TxEndPtr,W              ;get EndPointer
  2115.         movwf   FSR                             ;and place into FSR
  2116.         movf    BufferData,W            ;get BufferData
  2117.         movwf   INDF                            ;and store in buffer
  2118.  
  2119. ;test if buffer pointer needs to wrap around to beginning of buffer memory
  2120.  
  2121.         movlw   LOW TxBuffer+TX_BUF_LEN-1 ;get last address of buffer
  2122.         xorwf   TxEndPtr,W              ;and compare with end pointer
  2123.         movlw   LOW TxBuffer            ;load first address of buffer
  2124.         btfss   STATUS,Z                        ;check if pointer is at last address
  2125.         incf    TxEndPtr,W              ;if not then increment pointer
  2126.         movwf   TxEndPtr                        ;store new end pointer value
  2127.  
  2128. ;test if buffer is full
  2129.  
  2130.         subwf   TxStartPtr,W            ;compare with start pointer
  2131.         btfsc   STATUS,Z                        ;check if the same
  2132.         bsf             Flags,TxBufFull         ;if so then indicate buffer full
  2133.         bcf             Flags,TxBufEmpty        ;buffer cannot be empty
  2134.         bsf     STATUS,RP0                                              ;select bank 1
  2135.         bsf             PIE1,TXIE                       ;enable transmit interrupt
  2136.         BANK0                                           ;select bank 0
  2137.         return
  2138.  
  2139. ;error because attempting to store new data and the buffer is full
  2140. ;can do special error handling here - this code simply ignores the byte
  2141.  
  2142. ErrTxBufFull:
  2143.         bsf     STATUS,RP0                                              ;select bank 1
  2144.         bsf             PIE1,TXIE                       ;enable transmit interrupt
  2145.         BANK0                                           ;select bank 0
  2146.         return                                          ;no save of data because buffer full
  2147.  
  2148.  
  2149. ;----------------------------------------------
  2150. ;Remove and return (in WREG) the byte at the start of the receive buffer
  2151.  
  2152. GetRxBuffer:
  2153.         BANK0                                           ;select bank 0
  2154.         btfsc   Flags,RxBufEmpty        ;check if receive buffer empty
  2155.         goto    ErrRxBufEmpty           ;and go handle error if empty
  2156.  
  2157.         bsf     STATUS,RP0                                              ;select bank 1
  2158.         bcf             PIE1,RCIE                               ;disable receive interrupt
  2159.         BANK0                                           ;select bank 0
  2160.  
  2161.         BANKISEL RxBuffer               ;bank bit for indirect addressing
  2162.         movf    RxStartPtr,W            ;get StartPointer
  2163.         movwf   FSR                             ;and place into FSR
  2164.  
  2165. ;test if buffer pointer needs to wrap around to beginning of buffer memory
  2166.  
  2167.         movlw   LOW RxBuffer+RX_BUF_LEN-1 ;get last address of buffer
  2168.         xorwf   RxStartPtr,W            ; and compare with start pointer
  2169.         movlw   LOW RxBuffer            ;load first address of buffer
  2170.         btfss   STATUS,Z                        ;check if pointer is at last address
  2171.         incf    RxStartPtr,W            ;if not then increment pointer
  2172.         movwf   RxStartPtr                      ;store new pointer value
  2173.         bcf             Flags,RxBufFull         ;buffer cannot be full
  2174.  
  2175. ;test if buffer is now empty
  2176.  
  2177.         xorwf   RxEndPtr,W              ;compare start to end
  2178.         btfsc   STATUS,Z                        ;check if the same
  2179.         bsf             Flags,RxBufEmpty        ;if same then buffer will be empty
  2180.         movf    INDF,W                  ;get data from buffer
  2181.  
  2182.         bsf     STATUS,RP0                                              ;select bank 1
  2183.         bsf             PIE1,RCIE                       ;enable receive interrupt
  2184.         BANK0                                           ;select bank 0
  2185.         return
  2186.  
  2187. ;error because attempting to read data from an empty buffer
  2188. ;can do special error handling here - this code simply returns zero
  2189.  
  2190. ErrRxBufEmpty:
  2191.         bsf     STATUS,RP0                                              ;select bank 1
  2192.         bsf             PIE1,RCIE                       ;enable receive interrupt
  2193.         BANK0                                           ;select bank 0
  2194.         retlw   0                                       ;tried to read empty buffer
  2195.  
  2196.  
  2197.  
  2198. ;----------------------------------------------------------------------------
  2199. ;Move data from receive buffer to transmit buffer to echo the line back
  2200.  
  2201. CopyRxToTx:
  2202.         BANK0                                           ;select bank 0
  2203.         bcf             Flags,ReceivedCR        ;clear <CR> received indicator
  2204. Copy1:
  2205.         btfsc   Flags,RxBufEmpty        ;check if Rx buffer is empty
  2206.         return                                          ;if so then return
  2207.         call    GetRxBuffer             ;get data from receive buffer
  2208.         movwf   TempData                        ;save data
  2209.         call    PutTxBuffer             ;put data in transmit buffer
  2210.         movf    TempData,W              ;restore data
  2211.         xorlw   0x0d                            ;compare with <CR>
  2212.         btfss   STATUS,Z                        ;check if the same
  2213.         goto    Copy1           ;if not the same then move another byte
  2214.         return
  2215.  
  2216. ;----------------------------------------------------------------------------
  2217. ;Circular buffer routines.
  2218.  
  2219. ;----------------------------------------------------------------------------
  2220. ;Initialize transmit buffer
  2221.  
  2222. InitTxBuffer:
  2223.         BANK0
  2224.         movlw   LOW TxBuffer            ;take address of transmit buffer
  2225.         movwf   TxStartPtr                      ;and place in transmit start pointer
  2226.         movwf   TxEndPtr                        ;and place in transmit end pointer
  2227.         bcf             Flags,TxBufFull         ;indicate Tx buffer is not full
  2228.         bsf             Flags,TxBufEmpty        ;indicate Tx buffer is empty
  2229.         return
  2230.  
  2231. ;----------------------------------------------
  2232. ;Initialize receive buffer
  2233.  
  2234. InitRxBuffer:
  2235.         BANK0
  2236.         movlw   LOW RxBuffer            ;take address of receive buffer
  2237.         movwf   RxStartPtr                      ;and place in receive start pointer
  2238.         movwf   RxEndPtr                        ;and place in receive end pointer
  2239.         bcf             Flags,RxBufFull         ;indicate Rx buffer is not full
  2240.         bsf             Flags,RxBufEmpty        ;indicate Rx buffer is empty
  2241.         return
  2242.  
  2243.  
  2244. ADClunch:
  2245.  
  2246.         movlw   b'11000111'
  2247.         andwf   ADCON0  ,1                                      ; clean up previous ADCON0 state from CHAN / keep FR
  2248.                                                                                         ; ON state
  2249.         movfw   ADCCHAN                                         ; get CHAN to Convert
  2250.         iorwf   ADCON0  ,1                                      ; stick it in ADCON0
  2251.  
  2252.         movlw   ADGO
  2253.  
  2254.         WAITCAP                                                         ; wait for the capacitor to discharge ...
  2255.                                                                                 ; capacitor discharger after 9 cycles (µs) (@4Mhz) put ten to be safe
  2256.         iorwf   ADCON0  ,1                                      ; start conversion
  2257.  
  2258.         return
  2259.  
  2260. ADCCrunch:
  2261.  
  2262.    ; movfw      ADCCHAN
  2263. ;       xorlw   CHAN1
  2264. ;       btfsc   STATUS,2
  2265.  ;   return
  2266.  
  2267.         bsf     STATUS,RP0
  2268.         movfw   ADRESL
  2269.         movwf   datal
  2270.         movwf   datal_tmp
  2271.        
  2272.    ; movwf   CCPR1L
  2273.  
  2274.     BANK0
  2275.         movfw   ADRESH                                          ; XXXXXXDD
  2276.         movwf   datah
  2277.         movwf   datah_tmp
  2278.  
  2279.    ; movwf   CCPR1H
  2280.  
  2281.  
  2282.  
  2283.         movfw   datal
  2284.         movwf   dig2
  2285.         rlf             dig2,1
  2286.         rlf             dig2,1
  2287.         rlf             dig2,1
  2288.         rlf             dig2,0                                                  ; XXDDYYYY
  2289.         andlw   b'00110000'                                     ; remove the zeros
  2290.         iorlw   b'00001100'                                     ; and keep the state of ccp1con ...
  2291.         movwf   CCP1CON
  2292.  
  2293.         rrf             datal,1
  2294.         rrf             datal,0
  2295.         andlw   b'00111111'                                     ; remove the zeros top2
  2296.         movwf   datal
  2297.  
  2298.         rlf             datah,1
  2299.         rlf             datah,1
  2300.         rlf             datah,1
  2301.         rlf             datah,1
  2302.         rlf             datah,1
  2303.         rlf             datah,0
  2304.         andlw   b'11000000'                                     ; remove the zeros 6 bottoms for datal
  2305.         iorwf   datal,0
  2306.         movwf   CCPR1L
  2307.  
  2308.         movfw   datal_tmp
  2309.         movwf   datal
  2310.         movfw   datah_tmp
  2311.         movwf   datah
  2312.  
  2313.  
  2314.  
  2315.         return
  2316.  
  2317. ; fin adccrunch
  2318.  
  2319.  
  2320.  
  2321.  
  2322. LCDsendcmd4:
  2323.  
  2324.         movlw   b'00001111'
  2325.         andwf   LCDPORT,1
  2326.  
  2327.         movfw   cmd             ; the char to save
  2328.  
  2329.         lcdCmd                  ; cmd mode ,                                            clear bit RS / PORTB1
  2330.         lcdRmode                ; for reading status  ,                                 set bit RW   / PORTB2
  2331.         bsf     STATUS,RP0
  2332.         bsf     BZD             ; put on read mode en BF port,  TRISB7 as IN to read it
  2333.         BANK0
  2334.         bsf     EN              ; magic ,                                                       set bit   PORTB3
  2335.         btfsc   BZF             ; LCD not busy ?                                read bit  PORTB7
  2336.         goto    $-1
  2337.         bcf     EN              ; test if that helps
  2338.         bsf     STATUS,RP0
  2339.         bcf     BZD             ; write mode en BF port,                        clear bit TRISB7 of the LCD
  2340.         BANK0
  2341.  
  2342.         lcdWmode                ; clear bit PORTB2
  2343.         lcdCmd                  ; set bit   PORTB1
  2344.  
  2345.         movfw   cmd
  2346.         andlw   b'11110000'                                     ; remove bit 0 1 2 3 from the char to write
  2347.         iorlw   b'00000000'                                       ; mask for data+write mode
  2348.         movwf   LCDPORT                                         ; out to the LCD;:
  2349.  
  2350.         lcdPlop                                                                 ; execute
  2351.  
  2352.         swapf   cmd,W
  2353.    
  2354.         andlw   b'11110000'                                     ; remove bit 0 1 2 3 from the char to write
  2355.         iorlw   b'00000000'                                       ; mask for data+write mode
  2356.         movwf   LCDPORT
  2357.  
  2358.         lcdPlop                                                                 ; execute
  2359.  
  2360.         return                                                               ; command sending
  2361.  
  2362.  
  2363. LCDsendchar4:
  2364.        
  2365.         movlw   b'00001111'
  2366.         andwf   LCDPORT,1
  2367.  
  2368.         movfw   char                                                    ; the char to save
  2369.  
  2370.         lcdCmd                                                          ; cmd mode ,                                            clear bit RS / PORTB1
  2371.         lcdRmode                                                        ; for reading status  ,                                 set bit RW   / PORTB2
  2372.         bsf     STATUS,RP0
  2373.         bsf     BZD                                                     ; put on read mode en BF port,  TRISB7 as IN to read it
  2374.         BANK0
  2375.         bsf     EN                                                      ; magic ,                                                       set bit   PORTB3
  2376.         btfsc   BZF                                                     ; LCD not busy ?                                read bit  PORTB7
  2377.         goto    $-1
  2378.         bcf     EN                                                      ; test if that helps
  2379.         bsf     STATUS,RP0
  2380.         bcf     BZD                                                     ; write mode en BF port,                        clear bit TRISB7 of the LCD
  2381.         BANK0
  2382.  
  2383.         lcdWmode                                                                ;                                                                       clear bit PORTB2
  2384.         lcdDat                                                                  ;                                                                       set bit   PORTB1
  2385.  
  2386.         movfw   char
  2387.         andlw   b'11110000'                                     ; remove bit 0 1 2 3 from the char to write
  2388.         iorlw   b'00000010'                                     ; mask for data+write mode
  2389.         movwf   LCDPORT                                         ; out to the LCD;:
  2390.  
  2391.         lcdPlop                                                                 ; execute
  2392.  
  2393.         swapf   char,W
  2394.  
  2395.         andlw   b'11110000'                                     ; remove bit 0 1 2 3 from the char to write
  2396.         iorlw   b'00000010'                                     ; mask for data+write mode
  2397.         movwf   LCDPORT
  2398.  
  2399.         lcdPlop                                                                 ; execute
  2400.  
  2401.         return                                                                  ; caracter sending
  2402.  
  2403.  
  2404. ;
  2405. ;  routine de temporisations
  2406. ;
  2407.  
  2408. ; Delay = 1 seconds
  2409. ; Clock frequency = 20 MHz
  2410. ; Actual delay = 1 seconds = 5000000 cycles
  2411. ; Error = 0 %
  2412.  
  2413. ;       cblock;
  2414. ;       b1
  2415. ;       b2
  2416. ;       b3
  2417. ;       endc
  2418.  
  2419. Delay:
  2420.                                 ;4999993 cycles
  2421.         movlw   0x2C
  2422.         movwf   b1
  2423.         movlw   0xE7
  2424.         movwf   b2
  2425.         movlw   0x0B
  2426.         movwf   b3
  2427. Delay_0:
  2428.         decfsz  b1, f
  2429.         goto    $+2
  2430.         decfsz  b2, f
  2431.         goto    $+2
  2432.         decfsz  b3, f
  2433.         goto    Delay_0
  2434.  
  2435.                                 ;3 cycles
  2436.         goto    $+1
  2437.         nop
  2438.  
  2439.                                 ;4 cycles (including call)
  2440.         return
  2441.  
  2442.  
  2443.  
  2444. ; 1 milliseconde avec q 20Mhz
  2445.  
  2446. ; Delay = 0.001 seconds
  2447. ; Clock frequency = 20 MHz
  2448.  
  2449. ; Actual delay = 0.001 seconds = 5000 cycles
  2450. ; Error = 0 %
  2451.  
  2452. ;       cblock
  2453. ;       d1
  2454. ;       d2
  2455. ;       endc
  2456.  
  2457. Delay1m:
  2458.                                 ;4993 cycles
  2459.         movlw   0xE6
  2460.         movwf   d1
  2461.         movlw   0x04
  2462.         movwf   d2
  2463. Delay1m_0:
  2464.         decfsz  d1, f
  2465.         goto    $+2
  2466.         decfsz  d2, f
  2467.         goto    Delay1m_0
  2468.  
  2469.                         ;3 cycles
  2470.         goto    $+1
  2471.         nop
  2472.  
  2473.                                 ;4 cycles (including call)
  2474.         return
  2475.  
  2476.  
  2477. ; 100 micros
  2478.  
  2479. ; Delay = 0.0001 seconds
  2480. ; Clock frequency = 20 MHz
  2481.  
  2482. ; Actual delay = 0.0001 seconds = 500 cycles
  2483. ; Error = 0 %
  2484.  
  2485. ;       cblock
  2486. ;       e1
  2487. ;       endc
  2488.  
  2489. Delay100µ:
  2490.                                 ;496 cycles
  2491.         movlw   0xA5
  2492.         movwf   e1
  2493. Delay100µ_0:
  2494.         decfsz  e1, f
  2495.         goto    Delay100µ_0
  2496.  
  2497.                                 ;4 cycles (including call)
  2498.         return
  2499.  
  2500.  
  2501. Delay50µ:
  2502.                                 ;496 cycles
  2503.         movlw   0x53
  2504.         movwf   e1
  2505. Delay50µ_0:
  2506.         decfsz  e1, f
  2507.         goto    Delay50µ_0
  2508.  
  2509.                                 ;4 cycles (including call)
  2510.         return
  2511.  
  2512. Delay25µ:
  2513.                                 ;496 cycles
  2514.         movlw   0x29
  2515.         movwf   e1
  2516. Delay25µ_0:
  2517.         decfsz  e1, f
  2518.         goto    Delay25µ_0
  2519.  
  2520.                                 ;4 cycles (including call)
  2521.         return
  2522.  
  2523. Delay10µ:
  2524.                                 ;496 cycles
  2525.         movlw   0x0B
  2526.         movwf   e1
  2527. Delay10µ_0:
  2528.         decfsz  e1, f
  2529.         goto    Delay10µ_0
  2530.  
  2531.                                 ;4 cycles (including call)
  2532.         return
  2533.  
  2534. ; Delay = 0.025 seconds
  2535. ; Clock frequency = 20 MHz
  2536.  
  2537. ; Actual delay = 0.025 seconds = 125000 cycles
  2538. ; Error = 0 %
  2539.  
  2540. ;       cblock
  2541. ;       x1
  2542. ;       x2
  2543. ;       endc
  2544.  
  2545. Delay25m:
  2546.                                 ;124993 cycles
  2547.         movlw   0xA6
  2548.         movwf   x1
  2549.         movlw   0x62
  2550.         movwf   x2
  2551. Delay25m_0:
  2552.         decfsz  x1, f
  2553.         goto    $+2
  2554.         decfsz  x2, f
  2555.         goto    Delay25m_0
  2556.  
  2557.                                 ;3 cycles
  2558.         goto    $+1
  2559.         nop
  2560.  
  2561.                                 ;4 cycles (including call)
  2562.         return
  2563.  
  2564.         END                     ; directive fin de programme
  2565.  
  2566.