News:

;) This forum is the property of Proton software developers

Main Menu

Help with TMR0 interrupt and RX serial reception

Started by jam19857, Dec 06, 2022, 01:55 AM

Previous topic - Next topic

jam19857

Hello everyone, I am trying to add to my code (preb example) serial receive interrupt rx, this code already has tmr0 overflow interrupt, it works physically very well, but adding the serial receive interrupt does nothing.

I am also attaching an example of serial reception interrupt code (serial interrupt rx_ok); it works very well physically and in proteus.

Could you help me with this and indicate how i can place both interruptions: tmr0 overflow and serial reception.

Attached some images of the 16x32 pixel p10 matrix

TimB


Can you rephrase your question please

What issue are you trying to achieve? I presume the Tmr0 interrupt is to drive the leds. And the Usart TX is to receive data.

If you are using a 16f device then your limited to 1 interrupt channel. So to add the USART you need to:-

1 Set up the USART to interrupt on data
2 In the interrupt check if the interrupt source the Tmr0 roll over or the usart. Just check the flags and decide.

Remember to loop back to the start of the interrupt to check if an interrupt occurred while you were servicing the other one and remember to only clear the interrupt being  serviced.

Lastly you can post the code out side of rar file. More people will look at it and comment.


Pepe

if you include the .inc file within the main program you can debug it step by step, and I delete trisc = 0 in library

jam19857

#3
Thank you very much for your answer, The led card works very well with tmro overflow interruption and I need to add the serial communication as you indicated.
I use a PIC18F26K22 at 40Mhz, the main code is the following:

Device = 18F26K22
Declare Xtal = 40
Declare Optimiser_Level = 3
Declare Create_Coff On
Declare Watchdog Off

'Declare HSerout_Pin = PORTC.6 ; Tx
'Declare HSerin_Pin = PORTC.7  ; Rx
Declare Hserial_Baud = 9600
Declare Hserial_Clear = On

Declare CCP1_Pin PORTC.2

On_Hardware_Interrupt GoTo Isr_INTERRUP_TMRO
On_Low_Interrupt GoTo Isr_INTERUP_RX_UART

TRISB.0 = 0
TRISB.1 = 0
TRISB.2 = 0
TRISC.2 = 0
TRISC.6 = 0
TRISC.7 = 1
PORTB = 0
PORTC = 0

Include "Multi_RAM_Byte_Array.inc"
Include "Panel P10.inc"

; T0CON: TIMER 0 CONTROL REGISTER
T0CONBITS_TMR0ON = 1  ; "1" HABILITA TIMER0, "0" DETIENE EL TIMER0
T0CONBITS_T08BIT = 1  ; "1" CONFIGURA EL TIMER0 A 8 BITS; "0" CONFIGURA EL TIMER0 A 16 BITS
T0CONBITS_T0CS   = 0  ; "1" TRANASISICON DEL PIN EXTERNO T0CKI; "0" CICLO DE RELOJ INTERNO (CLKOUT)
T0CONBITS_T0SE   = 1  ; "1" INCREMENTA EN FLANCO DE ALTO A BAJO SOBRE EL PIN T0CKI; "0" INCREMENTA EN FLANCO DE BAJO A ALTO SOBRE EL Pin T0CKI
T0CONBITS_PSA    = 0  ; "1" PRESCALER NO ASIGNADO, EL TIEMPO DE RELOJ ES UN PUENTE; "0" SE ASIGNA PREESCALER A LA SALIDA.
T0CONBITS_T0PS2  = 1  ;PRESCALER
T0CONBITS_T0PS1  = 1  ; 111 = 1:256 ; 110 = 1:128 ; 101 = 1:64 ; 100 = 1:32 ; 011 = 1:16 ; 010 = 1:8 ; 001 = 1:4 ; 000 = 1.2
T0CONBITS_T0PS0  = 0

;SLRCON: SLEW RATE CONTROL REGISTER
SLRCONbits_SLRC = 0  ;  "1" todo el puerto C ets con una velocidad limitada; "0" to el puerto C esta una velocidad standar

RCSTAbits_SPEN = 1     ; SPEN: Serial port Enable bit
RCSTAbits_CREN = 1     ; Continuous Receive Enable bit - Enables receiver
PIR1bits_RC1IF = 0     ; El búfer de recepción de EUSART1 está vacío
PIE1bits_RC1IE = 1     ; PIE1: PERIPHERAL INTERRUPT ENABLE (FLAG) REGISTER 1
IPR1BITS_RC1IP = 0     ; "1" ALTA PRIORIDAD DE INTERRUPICON DE RX; "0" BAJA PRIORIDAD
TXSTABITS_TXEN = 1     ; TRANSMISION SRIA HABILITADA
TxSTABITS_SYNC = 0     ; "1" MODO SINCRONO ; "0" MODO ASINCRONO
'SPBRGH1 = 0    'EUSARTx Baud Rate Generator Register High Byte
'SPBRG1 = 25    'EUSARTx Baud Rate Generator Register Low Byte

;RCON: RESET CONTROL REGISTER
RCONBITS_IPEN    = 1   ; "1" HABILITA LA PRIORIDAD DE LOS NIVELES CATIVOS DE INTERRUPCION;"0" DESHABILITA LA PRIORIDAD DE LOS NIVELES CATIVOS DE INTERRUPCION
;CUANDO IPEN (PRIORIDAD DE INTERRUPCIONES)
;INTCON: INTERRUP CONTROL REGISTER
INTCONBITS_GIE   = 1   ; IPEN = 0
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES NO ENMASCARADAS; "0" DESHABILITA TODAS LAS INTERRUPCIONES INCLUYENDO PERIFERICOS
                       ; IPEN = 1
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES DE ALTA PRIORIDAD; "0" DESHABILITA TODAS LAS INTERRUPCIONES INCLUYENDO LAS DE BAJA PRIRORIDAD
INTCONBITS_PEIE  = 1   ; IPEN = 0
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES NO ENMASCARADAS DE PERIFERICOS; "0" DESHABILITA TODAS LAS INTERRUPCIONES DE PERIFERICOS
                       ; IPEN = 1
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES DE BAJA PRIORIDAD; "0" DESHABILITA TODAS LAS INTERRUPCIONES DE BAJA PRIORIDAD
INTCONBITS_TMR0IE  = 1 ; "1" HABILITA LA INTERRUPCION DE DESBORDAMIENTO DEL TMR0; "0" DESHABILITA LA INTERRUPCION DE DESBORDAMIENTO DEL TMR0

;INTCON2 : INTERRUP CONTROL 2 REGISTER
INTCON2BITS_TMR0IP = 1 ; " 1" HABILITA ALTA PRIORIDAD DE LA INTERRUPCION DE DESBORDAMIENTO TMR0; "0" HABILITA BAJA PRIORIDAD DEL DESBORDAMIENTO DEL TMR0

TMR0L = 0
TMR0H = 0

; para el calculo de DEL TIMER 0 PARA DESBORDAMIENTO
Dim TIEMPO As Word
Dim TIMER_INT As Float
Dim FOSC As Dword
Dim PRESCALER As Word
Dim VALOR_1 As Float
Dim VALOR_2 As Float
Dim CARGA_TIMER As Word

TIEMPO = 133;138; 800             ; TIEMPO DE INTERRUPCION DESORDAMIENTO
TIMER_INT = TIEMPO/1000000  ; PARA MILISEGUNDOS (1000); PARA MICROSEGUNDOS (1000000)
FOSC = 40000000           ; 20 MhZ
PRESCALER = 128         ; PRESCALADOR ASIGNADO

VALOR_1 = FOSC*TIMER_INT
VALOR_2 = 4*PRESCALER

CARGA_TIMER = 256 - (VALOR_1 / VALOR_2) ; VALOR CALCULADOR PARA DESBORDAMIENTO DE TIMER 8 BITS
'CARGA_TIMER = 65536 - (VALOR_1 / VALOR_2) ; VALOR CALCULADOR PARA DESBORDAMIENTO DE TIMER 16 BITS

TMR0L = CARGA_TIMER.LowByte ;CARGA_TIMER
'TMR0H = CARGA_TIMER.HighByte
'HSerOut [Hex CARGA_TIMER,13]
'HSerOut [Hex CARGA_TIMER.HighByte,13]
'HSerOut [Hex CARGA_TIMER.LowByte,13]

; VARIABLES DEL PROGRAMA
Dim int_syc As Byte Access = 0
Dim i As Byte
Dim w As Byte
Dim x3 As Byte
Dim x As Byte
Dim x0 As Word
Dim z As Byte
'Dim myarray As Byte
'Dim Text As Flash8 = "", 0
Dim Text As STRING*255
'Dim Text[255] As byte
Dim DATO_RX As Byte

Symbol LED = PORTB.0
Symbol LED2 = PORTB.1

int_syc = 0
i  = 0
w  = 0
x3 = 0

INICIO:

For x = 1 To 4
      LED = 1 : LED2 = 1 ;  // enciende led
      DelayMS 125
      LED = 0 : LED2 = 0
      DelayMS 125
Next x

High LED : Low LED2 ;  // enciende led
HRSOut "INICIO",13



Set_Brightness(5);   //Da brillo.
clear_display(0)

Text ="P10      "
ShowString(3,0,1,0)
Text ="Test     "
ShowString(3,8,1,0)

DelayMS 1500

For i = 0 To 31 Step 1
    For w = 0 To 15 Step 1
       LineScroll(w,0);
    Next w
    DelayMS 50;
Next i

clear_display(0)
DelayMS 100
Clear Text

Do
      FontTest()
      clear_display(0)
      DelayMS 1000
      NumberTest()
      DelayMS 1000
      clear_display(0)
      ScrollTest()
      DelayMS 1000
      clear_display(0)
      BrightTest()
      DelayMS 1000
      clear_display(0)
      GeoShapes()
      DelayMS 1000
      clear_display(0)
      SineWawe()
      DelayMS 1000
      clear_display(0)
      BarTest()
      DelayMS 2000
      clear_display(0)
      Text="FUNCIONAMIENTO DE PLACA P10 16 X 32        "
      TextToLeftScroll(4,1,9,0)
      DelayMS 2000
      clear_display(0)
      Writeimage()
      DelayMS 2000
      clear_display(0)
      x = 159
      Text ="               "
      ShowString(0,0,1,0)
      Text = Str$(Dec3 x)+"   "
      ShowString(0,0,1,0)
      x0 = 500
      Text ="               "
      ShowString(0,8,1,0)
      Text = Str$(Dec3 x0)+"   "
      ShowString(11,8,1,0)
      DelayMS 3000
      clear_display(0)
Loop
End


Isr_INTERRUP_TMRO:
        ; VALOR DE TIMER 0
                TMR0L = CARGA_TIMER.LowByte
        '        TMR0H = CARGA_TIMER.HighByte
        Context Save   
        ;INTERRUPCION POR DESBORDAMIENTO DE TMR0           
                    If INTCONBITS_TMR0IF = 1 Then ; "1" BANDERA DE INTERRUPCION DEL REGISTRO TMR0 HA DESBORDADO; "0" EL REGISTRO DE TMR0 NO SE HA DESBORDADO
       
                            Select int_syc  ; Cada 800us Saltar a la siguiente línea
           
                                    Case 0
                                            write_data(int_syc) : PinA = 0 : PinB  = 0 : LATCH = 1 : DelayCS 4 : LATCH = 0
                                    Case 1
                                            write_data(int_syc) : PinA = 1 : PinB  = 0 : LATCH = 1 : DelayCS 4 : LATCH = 0
                                    Case 2
                                            write_data(int_syc) : PinA = 0 : PinB  = 1 : LATCH = 1 : DelayCS 4 : LATCH = 0
                                    Case 3
                                            write_data(int_syc) : PinA = 1 : PinB  = 1 : LATCH = 1 : DelayCS 4 : LATCH = 0
                             EndSelect
           
                             int_syc = int_syc + 1
           
                             If int_syc > 3 Then
                                 int_syc = 0
                             EndIf
                             INTCONBITS_TMR0IF = 0
                    EndIf       
      Context Restore             
                 
Isr_INTERUP_RX_UART:
        Context Save
;INTERRUPCION DE RECEPCION RX 
                   
            If PIR1BITS_RC1IF = 1 Then
                     DATO_RX = RCREG1 ;HRSIn

                     If DATO_RX = "1" Then
                          RCREG1 = 0
                          HRSOut " LED ENCENDIDO",13
                          LED2 = 1
                     EndIf
   
                     If DATO_RX = "0" Then
                          RCREG1 = 0
                          HRSOut " LED APAGADO",13
                          LED2 = 0
                     EndIf
                     PIR1BITS_RC1IF = 0
            EndIf
        Context Restore

Proc write_data(row_adr As Byte)
        Dim i0 As Byte
        Dim J0 As Byte
        Dim k0 As Byte
        Dim cont As Byte
        Dim myarray As Byte

        i0 = 0
        j0 = 0
        k0 = 3

        While k0 <> 255
               j0 = 15 - k0                    ; Carga hacia la izquierda.
               For i0 = 0 To 3 Step 1           ; Se están llenando las líneas.
                   myarray = Read_MultiByteArray display_ram,[j0, row_adr]
                   SHOut PinData, PinSCK, MsbFirst,[myarray\8]
                   j0 = j0 - 4;
               Next i0
               k0 = k0 - 1
        Wend
EndProc

'';**** Added by Fuse Configurator ****
''; Use the Fuse Configurator plug-in to change these settings

Config_Start
  FOSC = HSHP;INTIO67;HSMP    ;Internal oscillator block
  PLLCFG = On    ;Oscillator multiplied by 4
  PRICLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
  IESO = OFF    ;Oscillator Switchover mode disabled
  PWRTEN = On    ;Power up timer enabled
  BOREN = OFF    ;Brown-out Reset disabled in hardware and software
  BORV = 190    ;VBOR set to 1.90 V nominal
  WDTEN = OFF    ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 256    ;1:256
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF    ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0    ;T3CKI is on RC0
  P2BMX = PORTB5    ;P2B is on RB5
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disabled
  STVREN = OFF    ;Stack full/underflow will not cause Reset
  LVP = On    ;Single-Supply ICSP enable MCLRE a 1
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Disabled
  Cp0 = OFF    ;Block 0 (000800-003FFFh) not code-protected
  CP1 = OFF    ;Block 1 (004000-007FFFh) not code-protected
  CP2 = OFF    ;Block 2 (008000-00BFFFh) not code-protected
  CP3 = OFF    ;Block 3 (00C000-00FFFFh) not code-protected
  CPB = OFF    ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF    ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 (000800-003FFFh) not write-protected
  WRT1 = OFF    ;Block 1 (004000-007FFFh) not write-protected
  WRT2 = OFF    ;Block 2 (008000-00BFFFh) not write-protected
  WRT3 = OFF    ;Block 3 (00C000-00FFFFh) not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  EBTR0 = OFF    ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF    ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF    ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
  EBTRB = OFF    ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

'';**** End of Fuse Configurator Settings ****
'';-------------------------------------------------------------------------------

The library with the procedures is the following, its name is "Panel P10.Inc"

Include "Multi_RAM_Byte_Array.inc"
Include "Font1_1.inc"
Include "Font2.inc"
Include "Font3.inc"
Include "Font4.inc"
Include "pictogramas.inc"


TRISC = 0
PORTC = 0

Symbol PinA    =  PORTC.0
Symbol PinB    =  PORTC.1
'Symbol PinOE   =  PORTC.4                    ;//OE=0 Display OFF, OE=1 Display On
Symbol LATCH   =  PORTC.4                    ;//
Symbol PinSCK  =  PORTC.3                   ;//Spi Clock Hattý
Symbol PinData =  PORTC.5                     ;//Spi Data Hattý
'Symbol PWM_Pin =  PORTC.2                     ;//


''//  P10 Led Board
''//     1     2
''// OE  --|oo|-- A
''// GND --|oo|-- B
''// GND --|oo|--
''// GND --|oo|-- SCK
''// GND --|oo|-- LATCH
''// GND --|oo|-- DATA
''// GND --|oo|--
''// GND --|oo|--
''//     15    16
''//
Symbol Font1H = 8
Symbol Font2H   12
Symbol Font3H   14
Symbol Font4H   16
'Symbol Panel     1                        //P10 Panel Sayýsý
'Symbol RIGHT     0                        //
'Symbol LEFT      1                        //
Symbol WIDTH = 31       ;31
Symbol DobleFila = 0             ;//"1" para doble fila y "0" para una sola fila
Symbol ModulosWidth = 1        ;         //numero de modulos que tiene a lo largo de su Letrero
Symbol HEIGHT = 15      ;15
Symbol BytePorMod = 4
Symbol TotalDeModulos = 1;
Symbol BytesTotal =  BytePorMod * TotalDeModulos  ;                // numero total de modulos de su letrero
Symbol BytesFila = BytePorMod * ModulosWidth
'Dim bit_test As Byte
Symbol GrupFila = BytePorMod * TotalDeModulos  ;// cada grupo pertence al IC 74HC595


Create_MByteArray(display_ram,16,4)                    ' Create a 2 dimensional Byte array 16 columns by 4 rows
'Dim Text[100] As Byte =""
Dim imageBuffer[64] As Byte

''/*******************************************************************************
''*   Función de limpieza de pantalla                                            *
''*   Parameter:    fill                                                         *
''*   Return:                                                                    *
''*******************************************************************************/
Proc clear_display (fillx As Bit)
    Dim i0 As Byte
    Dim j0 As Byte
    Dim fdata As Byte

    i0 = 0
    j0 = 0
    fdata = 0

    If fillx = 1 Then
       fdata = 0x00
    Else
       fdata = 0xFF
    EndIf

    For i0 = 0 To 3 Step 1
        For j0 = 0 To 15 Step 1
            Write_MultiByteArray display_ram, [j0,i0], fdata
        Next j0
    Next i0
    Clear
EndProc

''/*******************************************************************************
''*   Función de ajuste de brillo                                                *
''*   Parameter:    value (0-255)                                                *
''*   Return:                                                                    *
''*******************************************************************************/
Proc Set_Brightness(value As Byte)
        If value > 127 Then
            value = 127
        EndIf

        HPWM 1, value, 1000
EndProc


''/*******************************************************************************
''*   Función de píxel                                                           *
''*   Parameter:    Xpos, Ypos, fill                                             *
''*   Return:                                                                    *
''*******************************************************************************/
Proc PutPixel(Xpos As Byte, Ypos As Byte, fillx As Bit)
    Dim y1 As Byte
    Dim x1 As Byte
    Dim y2 As Byte
    Dim x2 As Byte
    Dim temp As Byte
    Dim Xran As Word
    Dim Yran As Word

    Xran = Xpos
    Yran = Ypos
    temp = 0

    y1 = Yran //4
    y2 = Yran / 4
    x1 = Xran //8;
    x2 = Xran / 8;

    If fillx = 1 Then  ; iluminar punto

        temp = Read_MultiByteArray display_ram, [(y2*4) + x2, y1]   ; El valor en RAM se almacena en caché.
        Write_MultiByteArray display_ram, [(y2*4) + x2, y1],(temp & (255-(128 >> x1)))    ; El bit x se establece en 0

    Else     ;//Eliminar o apagar punto

        temp = Read_MultiByteArray display_ram, [(y2*4)+x2, y1]       ; El valor en RAM se almacena en caché.
        Write_MultiByteArray display_ram, [(y2*4) + x2, y1],(temp | (128 >> x1))          ; El bit x se establece en 1

    EndIf

EndProc

''/*******************************************************************************
''*   Función de inversión de pantalla                                                 *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/
Proc invert_screen ()
     Dim i0 As Byte
     Dim j0 As Byte
     Dim myarray As Byte

     i0 = 0
     j0 = 0

     For i0 = 0 To 3 Step 1
           For j0 = 0 To 15 Step 1
             myarray = Read_MultiByteArray display_ram, [j0, i0]
             myarray = ~myarray
             Write_MultiByteArray display_ram, [j0, i0],myarray;  //El valor en el caché no se vuelve a escribir
           Next j0
     Next i0

EndProc

''''/*******************************************************************************
''''*   Función de lectura de píxeles                                              *
''''*   Parameter:    Xpos, Ypos                                                   *
''''*   Return: value                                                              *
''''*******************************************************************************/
Proc Getpixel(Xpos As Byte, Ypos As Byte),Bit
       Dim y1 As Byte
       Dim x1 As Byte
       Dim y2 As Byte
       Dim x2 As Byte
       Dim temp As Byte
       Dim value As Byte

       y1 = 0
       x1 = 0
       y2 = 0
       x2 = 0
       temp = 0
       value = 0

       y1 = Ypos//4;
       y2 = Ypos/4;
       x1 = Xpos//8; 8
       x2 = Xpos/8;  8

       Write_MultiByteArray display_ram, [(y2*4)+ x2, y1], temp; //El valor en RAM se almacena en caché.
       value = 1-(0x80 & temp << x1 )/128;   //El bit en la dirección Ram se está devolviendo
       Result = value

EndProc


''/*******************************************************************************
''*   Función de dibujo lineal                                                     *
''*   Parameter:    x1, x2, y1, y2, fill                                         *
''*   Return:                                                                    *
''*******************************************************************************/

Proc Linea(x1 As Byte, y1 As Byte, x2 As Byte, y2 As Byte, fillx As Bit)

        Dim addx As Word
        Dim addy As Word
        Dim P0 As Word
        Dim i0 As Word
        Dim dy As Word
        Dim dx As Word
        Dim diff As Word

        addx = 1
        addy = 1

            If x2 > x1 Then
                    dx = x2 - x1;
                Else
                    dx = x1 - x2;
                    addx = -1;
            EndIf

            If y2 > y1 Then
                   dy = y2 - y1;
                Else
                    dy = y1 - y2;
                    addy = -1;
            EndIf

            If dx >= dy Then
                     dy   = dy*2
                      P0   = dy - dx
                     diff =  P0 - dx

                      For i0 = 0 To dx Step 1

                         PutPixel(x1, y1, fillx)

                         If P0 < 0 Then
                            P0 = P0 + dy
                            x1 = x1 + addx
                         Else
                            P0  = P0 + diff
                            x1 = x1 + addx
                            y1 = y1 + addy
                        EndIf
                      Next i0
            Else
                     dx = dx*2
                     P0 = dx - dy
                     diff = P0 - dy

                    For i0 = 0 To dy Step 1

                        PutPixel(x1, y1, fillx);

                        If P0 < 0 Then
                            P0 = P0 + dx
                            y1 = y1 + addy
                        Else
                            P0 = P0 + diff
                            x1 = x1 + addx
                            y1 = y1 + addy
                        EndIf
                    Next i0
            EndIf
EndProc

''/*******************************************************************************
''*   Función de dibujo de rectángulo                                               *
''*   Parameter:    x1, x2, y1, y2, fill                                         *
''*   Return:                                                                    *
''*******************************************************************************/
Proc Rectangle(x1 As Byte, y1 As Byte, x2 As Byte, y2 As Byte, fillx As Bit)
      Dim i1    As Word
      Dim i2    As Word
      Dim xmin As Word
      Dim xmax As Word
      Dim ymin As Word
      Dim ymax As Word

      If fillx = 1 Then

              If x1 < x2 Then
                 xmin = x1
                 xmax = x2
              Else
                 xmin = x2
                 xmax = x1
              EndIf

              If y1 < y2 Then
                 ymin = y1;
                 ymax = y2;
              Else
                 ymin = y2;
                 ymax = y1;
              EndIf

    ''      for(; xmin <= xmax; ++xmin)
              For i1 = xmin To xmax Step 1
                     For i2 = ymin To ymax Step 1
                        PutPixel(i1, i2, 1)
                     Next i2
              Next i1
      Else
              Linea(x1, y1, x2, y1, 1)
              Linea(x2, y1, x2, y2, 1)
              Linea(x2, y2, x1, y2, 1)
              Linea(x1, y2, x1, y1, 1)
      EndIf
EndProc

''/*******************************************************************************
''*   Función de dibujo circular                                                    *
''*   Parameter:    x, y, Size, fill                                             *
''*   Return:                                                                    *
''*******************************************************************************/
Proc Circulo(x0 As Byte, y0 As Byte, size As Byte, fillx As Bit)
        Dim a0 As SByte
        Dim b0 As SByte
        Dim P0 As SByte

        a0 = 0
        b0 = size
        P0 = 1 - size

        Do
              If fillx = 1 Then
                     Linea(x0 - a0, y0 + b0, x0 + a0, y0 + b0, 1)
                     Linea(x0 - a0, y0 - b0, x0 + a0, y0 - b0, 1)
                     Linea(x0 - b0, y0 + a0, x0 + b0, y0 + a0, 1)
                     Linea(x0 - b0, y0 - a0, x0 + b0, y0 - a0, 1)
              Else
                     PutPixel(a0 + x0, b0 + y0, 1)
                     PutPixel(b0 + x0, a0 + y0, 1)
                     PutPixel(x0 - a0, b0 + y0, 1)
                     PutPixel(x0 - b0, a0 + y0, 1)
                     PutPixel(b0 + x0, y0 - a0, 1)
                     PutPixel(a0 + x0, y0 - b0, 1)
                     PutPixel(x0 - a0, y0 - b0, 1)
                     PutPixel(x0 - b0, y0 - a0, 1)
              EndIf

              If P0 < 0 Then
                  P0 = P0 + 3 + 2*a0
                  a0 = a0 + 1
              Else
                  P0 = P0 + 5 + 2*(a0 - b0)
                  a0 = a0 + 1
                  b0 = b0 - 1
              EndIf
        Loop While a0 <= b0

EndProc

''/*******************************************************************************
''*   Función de dibujo de gráfico de barras                                                  *
''*   Parameter:    x1, y1, x2, y2, Mode, Value                                  *
''*   Return:                                                                    *
''*******************************************************************************/
Proc Bargraph (x1 As Byte, y1 As Byte, x2 As Byte, y2 As Byte, hor As Bit, value As Byte)
        Dim w0 As Word
        Dim h0 As Word
        Dim a0 As Word
        Dim b0 As Word
        Dim barval As Word

        w0 = 0
        h0 = 0
        a0 = 0
        b0 = 0
        barval = 0

        If x1 > x2 Then
            w0 = x1 - x2
        EndIf

        If x1 < x2 Then
            w0 = x2 - x1
        EndIf

        If y1 > y2 Then
            h0 = y1 - y2
        EndIf

        If y1 < y2 Then
            h0 = y2 - y1
        EndIf

        If value > 100 Then
            value = 100
        EndIf

        If hor = 1 Then ; La barra se creará verticalmente
                barval=(value*h0)/100;
                For a0 = 0 To h0 Step 1
                      For b0 = 0 To w0 Step 1
                            If barval >= a0 Then
                                PutPixel(x1 + b0, 15 - a0 + y1, 1)
                            Else
                                PutPixel(x1 + b0, 15 - a0 + y1, 0)
                            EndIf
                      Next b0
                Next a0
        Else            ; La barra se creará horizontalmente
                barval=(value*w0)/100
                For a0 = 0 To w0 Step 1
                      For b0 = 0 To h0 Step 1
                            If barval >= a0 Then
                               PutPixel(x1 + a0, y1 + b0, 1)
                            Else
                               PutPixel(x1 + a0, y1 + b0, 0)
                            EndIf
                      Next b0
                Next a0
        EndIf

EndProc

''/*******************************************************************************
''*   Función de dibujo de test gráfico de barras                                                  *
''*   Parameter:    x1, y1, x2, y2, Mode, Value                                  *
''*   Return:                                                                    *
''*******************************************************************************/

Proc BarTest()
    Dim xstart As Byte
    Dim xend As Byte
    Dim w0 As Byte
    Dim i0 As Byte


    Text="Bar    "
    ShowString(0,0,1,0)
    Text="Test   "
    ShowString(0,8,1,0)
    DelayMS 1000
    clear_display(0)
    DelayMS 100

    xstart = 0
    xend = 3

    For w0 = 0 To 7 Step 1
          For i0 = 0 To 100 Step 1
            Bargraph(xstart, 0, xend - 1, 15, 1, i0)
          Next I0
          DelayUS 5;

          xstart = xstart + 4
          xend = xend + 4
   Next W0

    xstart = 0
    xend = 3

    For w0 = 0 To 7 Step 1
      For i0 = 0 To 100 Step 1
        Bargraph(xstart, 0, xend - 1, 15, 1, 100 - i0);
      Next I0
      DelayUS 5;
      xstart = xstart + 4
      xend = xend + 4
    Next W0

    clear_display(0);

    xstart = 0
    xend = 3

    For w0 = 0 To 3 Step 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, xstart, 31, xend - 1, 0, i0)
      Next I0
      DelayUS 5;
      xstart = xstart + 4
      xend = xend + 4
    Next W0

    xstart = 0
    xend = 3

    For  w0 = 0 To 3 Step 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, xstart, 31, xend - 1, 0, 100 - i0);
      Next I0
      DelayUS 5;
      xstart = xstart + 4
      xend = xend + 4
    Next W0

    clear_display(0);

    For w0 = 0 To 1 Step 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, 0, 31, 15, 1, i0);
      Next I0
      DelayUS 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, 0, 31, 15, 1, 100 - i0);
      Next I0
      DelayUS 1
    Next W0

    clear_display(0);

    For w0 = 0 To 1 Step 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, 0, 31, 15, 0, i0);
      Next I0
      DelayUS 1
      For i0 = 0 To 100 Step 1
        Bargraph(0, 0, 31, 15, 0, 100 - i0);
        DelayUS 1
      Next I0
      DelayUS 1
    Next W0
    clear_display(0);

EndProc

''/*******************************************************************************
''*   Función de cambio de línea                                                 *
''*   Parameter:    Line, ZeroBit                                                *
''*   Return:    OwerFlow                                                        *
''*******************************************************************************/
Proc LineScroll( Lines As Word, ZeroBit As Bit),Bit

    Dim Tam As Dword = 0
    Dim y1 As Word = Lines // 4
    Dim y2 As Word = Lines / 4
    Dim Temp As Byte = 0

    Dim OwerFlow As Bit

    ZeroBit = 1 - Zerobit; ~ Zerobit ;

    temp = Read_MultiByteArray display_ram, [(y2*4),y1]
    OwerFlow=(temp /128)&0x01

    Tam = Read_MultiByteArray display_ram, [(y2*4),y1]
    Tam = Tam << 8
    temp = Read_MultiByteArray display_ram, [(y2*4)+1,y1]
    Tam = Tam + temp
    Tam = Tam << 8
    temp = Read_MultiByteArray display_ram, [(y2*4)+2,y1]
    Tam = Tam + temp
    Tam = Tam << 8
    temp = Read_MultiByteArray display_ram, [(y2*4)+3,y1]
    Tam = Tam + temp
    Tam = Tam << 1
    Tam = Tam + ZeroBit

    Write_MultiByteArray display_ram,[(y2*4) ,y1],(Tam>>24)&0xFF
    Write_MultiByteArray display_ram,[(y2*4)+1 ,y1],(Tam>>16)&0xFF
    Write_MultiByteArray display_ram,[(y2*4)+2 ,y1],(Tam>>8)&0xFF
    Write_MultiByteArray display_ram,[(y2*4)+3 ,y1],Tam&0xFF

    Result = OwerFlow
EndProc

''/*******************************************************************************
''*   Función de impresión de texto Font1                                        *
''*   Parameter:    x, y, Fill                                                   *
''*   Return:                                                                    *
''*******************************************************************************/

Proc DrawCharFont1 (x0 As Byte, y0 As Byte, FILLX As Bit)
        Dim a0, b0, i0,clm, row, cdato, d0, bFontBits, cant As Byte
        Dim wCharOffset As Word
        Dim c0 As Bit = 0

         a0 = 0
         b0 = 0
         i0 = 0
         clm = 0
         row = 0
         cdato = 0
         c0 = 0
         d0 = 0

         clm = x0
         row = y0
         cant = Len(Text)

         While Text <>0

                  If cant > 255 Then
                         ExitProc
                  EndIf

                  If clm + 5 > WIDTH Then ;//¿Ha llegado al final de la línea? Saltar a una sublínea
                        clm = 0;
                        row = row + 8

                        If row + 8 > HEIGHT + 1 Then
                                ExitProc
                        EndIf
                  EndIf

                  bFontBits = Text[i0] - 32         ' Get a character from the string

                  For b0 = 0 To 4 Step 1                                              ' Get a character from the string
                          wCharOffset = bFontBits * 5                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + b0                              '
                          cdato = CRead8 Font_5x7[wCharOffset]

                          For a0 = 0 To 7 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + b0,row + a0,c0)
                          Next a0
                  Next b0

                  i0 = i0 + 1
                  clm = clm + 6
          Wend
EndProc

''/*******************************************************************************
''*  Función de impresión de texto Font2                                               *
''*   Parameter:    x, y, Fill                                                   *
''*   Return:                                                                    *
''*******************************************************************************/

Proc DrawCharFont2 (x0 As Byte, y0 As Byte, FILLX As Bit)
        Dim a0, b0, i0,clm, row, wCharOffset As Word
        Dim cdato, d0, bFontBits, cant As Byte
        Dim c0 As Bit = 0

         a0 = 0
         b0 = 0
         i0 = 0
         clm = 0
         row = 0
         cdato = 0
         c0 = 0
         d0 = 0

         clm = x0
         row = y0
         cant = Len(Text)

         While Text <>0

                  If cant > 255 Then
                         ExitProc
                  EndIf

                  If clm + 8 > WIDTH + 1 Then ;//¿Ha llegado al final de la línea? Saltar a una sublínea
                        clm = 0;
                        row = row + 12;

                        If row + 12 > HEIGHT + 1 Then
                                ExitProc
                        EndIf
                  EndIf

                  bFontBits = Text[i0] - 32         ' Get a character from the string

                  For b0 = 0 To 15 Step 2                                              ' Get a character from the string

                          wCharOffset = bFontBits * 16                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + b0                              '
                          cdato = CRead8 Font_8x12[wCharOffset]

                          For a0 = 0 To 7 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + a0,c0)
                          Next a0

                          wCharOffset = bFontBits * 16                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + (b0+1)                              '
                          cdato = CRead8 Font_8x12[wCharOffset]

                          For a0 = 0 To 3 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + 8 + a0,c0)
                          Next a0

                  Next b0

                  clm = clm + 8
                  i0 = i0 + 1
          Wend
EndProc

'''/*******************************************************************************
'''*   Función de impresión de texto Font3                                              *
'''*   Parameter:    x, y, Fill                                                   *
'''*   Return:                                                                    *
'''*******************************************************************************/

Proc DrawCharFont3 (x0 As Byte, y0 As Byte, FILLX As Bit)
        Dim a0, b0, i0,clm, row, wCharOffset As Word
        Dim cdato, d0, bFontBits, cant As Byte
        Dim c0 As Bit = 0

         a0 = 0
         b0 = 0
         i0 = 0
         clm = 0
         row = 0
         cdato = 0
         c0 = 0
         d0 = 0

         clm = x0
         row = y0
         cant = Len(Text)

         While Text <>0

                  If cant > 255 Then
                         ExitProc
                  EndIf

                  If clm + 11 > WIDTH + 1  Then ;//¿Ha llegado al final de la línea? Saltar a una sublínea
                        clm = 0;
                        row = row + 14;

                        If row + 14 > HEIGHT + 1 Then
                                ExitProc
                        EndIf
                  EndIf

                  bFontBits = Text[i0] - 32         ' Get a character from the string

                  For b0 = 0 To 23 Step 2                                              ' Get a character from the string

                          wCharOffset = bFontBits * 24                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + b0                              '
                          cdato = CRead8 Font_12x14[wCharOffset]

                          For a0 = 0 To 7 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + a0,c0)
                          Next a0

                          wCharOffset = bFontBits * 24                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + (b0+1)                              '
                          cdato = CRead8 Font_12x14[wCharOffset]
                                       ;5
                          For a0 = 0 To 5 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + 8 + a0,c0)
                          Next a0

                  Next b0

                  clm = clm + 11
                  i0 = i0 + 1
          Wend
EndProc

'''/*******************************************************************************
'''*   Función de impresión de texto Font4                                               *
'''*   Parameter:    x, y, Fill                                                   *
'''*   Return:                                                                    *
'''*******************************************************************************/

Proc DrawCharFont4 (x0 As Byte, y0 As Byte, FILLX As Bit)
        Dim a0, b0, i0,clm, row, wCharOffset As Word
        Dim cdato, d0, bFontBits, cant As Word
        Dim c0 As Bit = 0

         a0 = 0
         b0 = 0
         i0 = 0
         clm = 0
         row = 0
         cdato = 0
         c0 = 0
         d0 = 0

         clm = x0
         row = y0
         cant = Len(Text)

         While Text <>0

                  If cant > 255 Then
                         ExitProc
                  EndIf

                  If clm + 11 > WIDTH + 1 Then ;//¿Ha llegado al final de la línea? Saltar a una sublínea
                        clm = 0;
                        row = row + 16;

                        If row + 16 > HEIGHT + 1 Then
                                ExitProc
                        EndIf
                  EndIf

                  bFontBits = Text[i0] - 32         ' Get a character from the string

                  For b0 = 0 To 23 Step 2                                              ' Get a character from the string

                          wCharOffset = bFontBits * 24                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + b0                              '
                          cdato = CRead8 Font_12x16[wCharOffset]

                          For a0 = 0 To 7 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + a0,c0)
                          Next a0

                          wCharOffset = bFontBits * 24                     '  poner 5 para usar font1_1 archivo original
                          wCharOffset = wCharOffset + (b0+1)                              '
                          cdato = CRead8 Font_12x16[wCharOffset]

                          For a0 = 0 To 6 Step 1
                                         d0 = (cdato >> a0) & 0x01
                                         c0 = fillx - d0
                                         PutPixel(clm + (b0/2),row + 8 + a0,c0)
                          Next a0

                  Next b0

                  clm = clm + 11
                  i0 = i0 + 1
          Wend
EndProc

''/*******************************************************************************
''*   Función de impresión de texto                                                      *
''*   Parameter:    x, y, size, fill                                             *
''*   Return:                                                                    *
'*******************************************************************************/
Proc ShowString (x0 As Byte, y0 As Byte, size As Byte, fillx As Bit)
    Select size
        Case 1
                DrawCharFont1(x0,y0,fillx)
        Case 2
                DrawCharFont2(x0,y0,fillx)
        Case 3
                DrawCharFont3(x0,y0,fillx)
        Case 4
                DrawCharFont4(x0,y0,fillx)

        Case Else
                ExitProc
    EndSelect
EndProc

''/*******************************************************************************
''*   Función TEST  DE FUENTES                                                   *
''*   Parameter:    x, y, size, fill                                             *
''*   Return:                                                                    *
'*******************************************************************************/
Proc FontTest ()
      Dim i0 As Byte
      Dim X0 As Byte
'      For X0 = 1 To 2
           Text ="Font     "
           ShowString(3,0,1,0)
           Text ="Test     "
           ShowString(3,8,1,0)
           DelayMS 1500
           clear_display(0)
           DelayMS 100
           Clear Text

           Text="A              ";
           For i0 = 1 To 4 Step 1
             ShowString(0,0,i0,0)
             DelayMS 400
             clear_display(0)
             DelayMS 50
           Next i0


           Text="B             ";
           For i0 = 1 To 4 Step 1
             ShowString(0,0,i0,0)
             DelayMS 400
             clear_display(0)
             DelayMS 50
           Next i0


           Text="C             ";
           For i0 = 1 To 4 Step 1
             ShowString(0,0,i0,0)
             DelayMS 400
             clear_display(0)
             DelayMS 50
           Next i0

           Text="D            ";
           For i0 = 1 To 4 Step 1
             ShowString(0,0,i0,0)
             DelayMS 400
             clear_display(0)
             DelayMS 50
           Next i0

'       Next X0
EndProc


''/*******************************************************************************
''*   Función de scroll onda senoidal                                            *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/
Proc SineWawe ()
    Dim ax As Float
    Dim i0 As Byte
    Dim xpos As Word
    Dim ypos As Byte
    Dim sayac As Byte
    Symbol pi = 3.1416

    ax = 0
    xpos = 0
    ypos = 0
    sayac = 0

    clear_display(0);
'    DelayMS 100

    While 1 = 1

        For xpos = 0 To 18 Step 1
            For i0 = 0 To 15 Step 1
              LineScroll(i0,0);
            Next i0
            ax = (pi*xpos)/180;
            ypos = 8 + (7*Sin (19 * ax))
            PutPixel(31, ypos, 1)
            DelayMS 100
        Next xpos

        sayac = sayac + 1

        If sayac = 10 Then
            Break;
        EndIf
    Wend
    clear_display(0);

EndProc

''''/*******************************************************************************
''''*   Función de desplazamiento de texto Font1                                            *
''''*   Parameter:    Line, Speed, Fill                                            *
''''*   Return:                                                                    *
''''*******************************************************************************/

Proc ScrollTextFont1 (lines As Word, speed As Word, fillx As Bit)
        Dim a0, b0, i0, d0, delay, cant, wCharOffset, sayac As Word
        Dim cdato, bFontBits, bIndex As Byte
        Dim ZeroBit As Bit

        a0 = 0
        b0 = 0
        i0 = 0
        d0 = 0
        cdato = 0
        delay = speed * 20
        ZeroBit = 0
        cant = 0

        cant = Len(Text)

        While Text <> 0

                 If cant > 255 Then
                        ExitProc
                 EndIf

                 If i0 = cant Then
                        ExitProc
                 EndIf

                 If Lines + Font1H - 1 > HEIGHT Then
                        Break
                        ExitProc
                 EndIf

                 bFontBits = Text[i0]-32

                 For b0=0 To 4 Step 1
                        wCharOffset = bFontBits * 5
                        wCharOffset = wCharOffset + b0                              '
                        cdato = CRead8 Font_5x7[wCharOffset]
    '
                        For a0 = 0 To 7 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + a0,ZeroBit);
                        Next a0

                        DelayMS 230 - delay

                Next b0

                For a0 = 0 To 7 Step 1
                     LineScroll(Lines + a0,0);
                Next a0

                DelayMS 230 - delay

                i0 = i0 + 1
        Wend
EndProc


'''/*******************************************************************************
'''*   Función de desplazamiento de texto Font2                                            *
'''*   Parameter:    Line, Speed, Fill                                            *
'''*   Return:                                                                    *
'''*******************************************************************************/
Proc ScrollTextFont2 (lines As Word, speed As Word, fillx As Bit)
        Dim a0, b0, i0, d0, delay, cant, wCharOffset, sayac As Word
        Dim cdato, bFontBits, bIndex As Byte
        Dim ZeroBit As Bit

        a0 = 0
        b0 = 0
        i0 = 0
        d0 = 0
        cdato = 0
        delay = speed * 20
        ZeroBit = 0
        cant = 0
        sayac = 0

        cant = Len(Text)

        While Text <> 0

                 If cant > 255 Then
                        ExitProc
                 EndIf

                 If i0 = cant Then
                        ExitProc
                 EndIf

                 If Lines + Font2H - 1 > HEIGHT Then
                        Break
                        ExitProc
                 EndIf

                 bFontBits = Text[i0]-32

                 For b0 = 0 To 15 Step 2
                        wCharOffset = bFontBits * 16
                        wCharOffset = wCharOffset + b0
                        cdato = CRead8 Font_8x12[wCharOffset]

                        For a0 = 0 To 7 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + a0,ZeroBit);
                        Next a0

                        wCharOffset = bFontBits * 16
                        wCharOffset = wCharOffset + (b0+1)
                        cdato = CRead8 Font_8x12[wCharOffset]

                        For a0 = 0 To 3 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + 8 + a0,ZeroBit);
                        Next a0

                        DelayMS 230 - delay

                Next b0

                i0 = i0 + 1
        Wend
EndProc

'''/*******************************************************************************
'''*   Función de desplazamiento de texto Font3                                           *
'''*   Parameter:    Line, Speed, Fill                                            *
'''*   Return:                                                                    *
'''*******************************************************************************/

Proc ScrollTextFont3 (lines As Word, speed As Word, fillx As Bit)
        Dim a0, b0, i0, d0, delay, cant, wCharOffset, sayac As Word
        Dim cdato, bFontBits, bIndex As Byte
        Dim ZeroBit As Bit

        a0 = 0
        b0 = 0
        i0 = 0
        d0 = 0
        cdato = 0
        delay = speed * 20
        ZeroBit = 0
        cant = 0
        sayac = 0

        cant = Len(Text)

        While Text <> 0

                 If cant > 255 Then
                        ExitProc
                 EndIf

                 If i0 = cant Then
                        ExitProc
                 EndIf

                 If Lines + Font3H - 1 > HEIGHT Then
                        Break
                        ExitProc
                 EndIf

                 bFontBits = Text[i0]-32

                 For b0 = 0 To 23 Step 2
                        wCharOffset = bFontBits * 24
                        wCharOffset = wCharOffset + b0
                        cdato = CRead8 Font_12x14[wCharOffset]

                        For a0 = 0 To 7 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + a0,ZeroBit);
                        Next a0

                        wCharOffset = bFontBits * 24
                        wCharOffset = wCharOffset + (b0+1)
                        cdato = CRead8 Font_12x14[wCharOffset]

                        For a0 = 0 To 6 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + 8 + a0,ZeroBit);
                        Next a0

                        DelayMS 230 - delay

                Next b0

                i0 = i0 + 1
        Wend
EndProc

'' /*******************************************************************************
''*   Función de desplazamiento de texto Font4                                            *
''*   Parameter:    Line, Speed, Fill                                            *
''*   Return:                                                                    *
''*******************************************************************************/

Proc ScrollTextFont4 (lines As Word, speed As Word, fillx As Bit)
        Dim a0, b0, i0, d0, delay, cant, wCharOffset, sayac As Word
        Dim cdato, bFontBits, bIndex As Byte
        Dim ZeroBit As Bit

        a0 = 0
        b0 = 0
        i0 = 0
        d0 = 0
        cdato = 0
        delay = speed * 20
        ZeroBit = 0
        cant = 0
        sayac = 0

        cant = Len(Text)

        While Text <> 0

                 If cant > 255 Then
                        ExitProc
                 EndIf

                 If i0 = cant Then
                        ExitProc
                 EndIf

                 If Lines + Font4H - 1 > HEIGHT Then
                        Break
                        ExitProc
                 EndIf

                 bFontBits = Text[i0]-32

                 For b0 = 0 To 23 Step 2
                        wCharOffset = bFontBits * 24
                        wCharOffset = wCharOffset + b0
                        cdato = CRead8 Font_12x16[wCharOffset]

                        For a0 = 0 To 7 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + a0,ZeroBit);
                        Next a0

                        wCharOffset = bFontBits * 24
                        wCharOffset = wCharOffset + (b0+1)
                        cdato = CRead8 Font_12x16[wCharOffset]

                        For a0 = 0 To 6 Step 1
                                d0 = (cdato >> a0) & 0x01
                                ZeroBit = fillx - d0
                                LineScroll(Lines + 8 + a0,ZeroBit);
                        Next a0

                        DelayMS 230 - delay

                Next b0

                i0 = i0 + 1
        Wend
EndProc

''/*******************************************************************************
''*   Función de desplazamiento de texto                                                   *
''*   Parameter:    Line, Size, Speed, Fill                                      *
''*   Return:                                                                    *
''*******************************************************************************/
Proc TextToLeftScroll(lines As Byte,size As Byte, speed As Byte, fillx As Bit)
      Select size
            Case 1
                  ScrollTextFont1(Lines,Speed,fillx)
            Case 2
                  ScrollTextFont2(Lines,Speed,fillx)
            Case 3
                  ScrollTextFont3(Lines,Speed,fillx)
            Case 4
                  ScrollTextFont4(Lines,Speed,fillx)
            Case Else
                    ExitProc
      EndSelect
'      Clear
EndProc


''/*******************************************************************************
''*   Función de mostrar imagen                                                  *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/

Proc Writeimage()
        Dim a0 As Byte
        Dim b0 As Byte
        Dim c0 As Byte
        Dim i0 As Byte
        Dim cdato As Byte
        Dim Xpos    As Byte
        Dim Ypos    As Byte
        Dim Addr    As Word;Byte
        Dim AUX_0   As Byte
        Dim i1      As Byte

        a0 = 0
        b0 = 0
        c0 = 0
        i0 = 0
        i1 = 0
        cdato = 0

'        leo el mapa de bits

        For Ypos = 0 To 31  ; en vertical
             For Xpos = 0 To 1     ; en horizontal
                 Addr = (Ypos*2)+ Xpos      ; posicion dato
                 imageBuffer [i1]= CRead8 img_1 [Addr]
                 i1 = i1 + 1
             Next Xpos
        Next Ypos

'        escribo mapa de bits
        For a0 = 0 To 31
              cdato = imageBuffer[i0]
              For b0 = 0 To 7
                     c0 = (cdato >> b0) & 0x01
                     PutPixel(a0,b0,c0)
              Next
              Inc i0

              cdato = imageBuffer[i0]
              For b0 = 0 To 7
                     c0 = (cdato >> b0) & 0x01
                     PutPixel(a0,8+b0,c0)
              Next
              Inc i0
         Next
EndProc

''Proc Writeimage2()
''        Dim a0 As Byte
''        Dim b0 As Byte
''        Dim c0 As Byte
''        Dim i0 As Byte
''        Dim cdato As Byte
''        Dim Xpos    As Byte
''        Dim Ypos    As Byte
''        Dim Addr    As Word;Byte
''        Dim AUX_0   As Byte
''        Dim i1      As Byte

''        a0 = 0
''        b0 = 0
''        c0 = 0
''        i0 = 0
''        i1 = 0
''        cdato = 0

'''        leo el mapa de bits

''        For Ypos = 0 To 31  ; en vertical
''             For Xpos = 0 To 1     ; en horizontal
''                 Addr = (Ypos*2)+ Xpos      ; posicion dato
''                 imageBuffer [i1]= CRead8 img_1 [Addr]
''                 i1 = i1 + 1
''             Next Xpos
''        Next Ypos

'''        escribo mapa de bits
''        For a0 = 0 To 31
''              cdato = imageBuffer[i0]
''              For b0 = 0 To 7
''                     c0 = ~(cdato >> b0) & 0x01
''                     PutPixel(a0,b0,c0)
''              Next
''              Inc i0

''              cdato = imageBuffer[i0]
''              For b0 = 0 To 7
''                     c0 = (cdato >> b0) & 0x01
''                     PutPixel(a0,8+b0,c0)
''              Next
''              Inc i0
''         Next
''EndProc

''/*******************************************************************************
''*   Función de prueba de numeros                                               *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/

Proc NumberTest()
   Dim i, w As Byte

   Text="Numbr ";
   ShowString(0,0,1,0);
   Text="Test  ";
   ShowString(0,8,1,0);
   DelayMS 1000
   clear_display(0);
   DelayMS 100

    For i = 0 To 99 Step 1
      text = Str$(Dec2  i)
      DelayMS 50
      ShowString(4,0,4,0);
      DelayMS 200
    Next i

    For i = 0 To 2 Step 1
     Text="--"
     ShowString(4,0,4,1)
     DelayMS 700
     clear_display(1)
     DelayMS 500
   Next i

   DelayMS 500
      For i = 0 To 31 Step 1
        For w = 0 To 15 Step 1
           LineScroll(w,0)
        Next w
           DelayMS 15
      Next i

EndProc

''/*******************************************************************************
''*   Función de prueba de los tipos de scroll                                   *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/

Proc ScrollTest()
       Text="Scroll"
       ShowString(0,0,1,0)
       Text="Test"
       ShowString(0,8,1,0)
       DelayMS 1000
       clear_display(0)
       DelayMS 100

       Text="FONT 1     "
       TextToLeftScroll(0,1,9,0)
       DelayMS 800
       Text="FONT 2     "
       TextToLeftScroll(0,2,9,0)
       DelayMS 700
       Text="FONT 3     "
       TextToLeftScroll(0,3,9,0)
       DelayMS 600
       Text="FONT 4     "
       TextToLeftScroll(0,4,9,0)
       DelayMS 500
       clear_display(1)
       Text="Pic18F26K22 con POSITRON IDE    "
       TextToLeftScroll(0,4,9,1)
       clear_display(0);
EndProc

''/*******************************************************************************
''*   Función de prueba de figuras                                               *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/
Proc GeoShapes()

          PutPixel(0,0,1)
          PutPixel(0,15,1)
          PutPixel(31,0,1)
          PutPixel(31,15,1)
          PutPixel(15,8,1)
          DelayMS 1000
          clear_display(0)
          Rectangle(14,6,17,9,0)
          DelayMS 600
          clear_display(0)
          Rectangle(11,3,20,12,0)
          DelayMS 600
          clear_display(0)
          Rectangle(8,0,23,15,0)
          DelayMS 600
          clear_display(0)
          Rectangle(14,6,17,9,1)
          DelayMS 600
          clear_display(0)
          Rectangle(11,3,20,12,1)
          DelayMS 600
          clear_display(0)
          Rectangle(8,0,23,15,1)
          DelayMS 1000
          clear_display(0)
          Circulo(16,8,2,0)
          DelayMS 600
          clear_display(0)
          Circulo(16,8,4,0)
          DelayMS 600
          clear_display(0)
          Circulo(16,8,7,0)
          DelayMS 600
          clear_display(0)
          Circulo(16,8,2,1)
          DelayMS 600
          clear_display(0)
          Circulo(16,8,4,1)
          DelayMS 600
          clear_display(0)
          Circulo(16,8,7,1)
          DelayMS 1000
          clear_display(0)
          DelayMS 100
          Linea(0,0,31,15,1)
          DelayMS 600
          Linea(0,15,31,0,1)
          DelayMS 600
          Linea(5,0,8,15,1)
          DelayMS 600
          Linea(31,8,0,4,1)
          DelayMS 600
          Linea(29,0,10,15,1)
          DelayMS 600
          Linea(31,13,0,8,1)
          DelayMS 1000
          clear_display(0)
EndProc

''/*******************************************************************************
''*   Función de prueba de brillo                                                *
''*   Parameter:                                                                 *
''*   Return:                                                                    *
''*******************************************************************************/

Proc BrightTest()
   Dim i0 As Byte

    For i0 = 1 To 100 Step 1
       Text="Brigt  "
       ShowString(0,0,1,0)
       Text="             "
       ShowString(8,8,1,0)
       text = Str$(Dec3 i0)+" %"
       ShowString(1,8,1,0)
       Set_Brightness(i0)
       DelayMS 250
       If i0 = 100 Then
          i0 = 100
          text = Str$(Dec3 i0)+" %"
          ShowString(1,8,1,0)
          DelayMS 2000
       EndIf
    Next i0

    Set_Brightness(100);
    clear_display(0);

    Text="MAX ";

    For i0 = 0 To 3 Step 1
        ShowString(4,3,2,0);
        DelayMS 800
        clear_display(0)
        DelayMS 500
   Next i0
   Set_Brightness(5);

EndProc


Make an independent test code only with the serial reception interrupt and if it works for me but I require that it work with the two interrupts: TMR0 and RX reception.

This is the test code: RX interrupt and it works.

Device = 18F26K22
Declare Xtal = 40
Declare Optimiser_Level = 3
Declare Dead_Code_Remove = 1        ' Remove dead code
Declare Create_Coff On
Declare Watchdog Off

'Declare HSerout_Pin = PORTC.6
'Declare HSerin_Pin = PORTC.7  ;rx
Declare Hserial_Baud = 9600 ' Set Baud rate to 9600 for HSerout
Declare Hserial_Clear = On

On_Hardware_Interrupt GoTo INTERRUP


RCSTAbits_SPEN = 1     ; SPEN: Serial port Enable bit
RCSTAbits_CREN = 1     ; Continuous Receive Enable bit - Enables receiver
PIR1bits_RC1IF = 0     ; El búfer de recepción de EUSART1 está vacío
PIE1bits_RC1IE = 1     ; PIE1: PERIPHERAL INTERRUPT ENABLE (FLAG) REGISTER 1
IPR1BITS_RC1IP = 1     ; "1" ALTA PRIORIDAD DE INTERRUPICON DE RX; "0" BAJA PRIORIDAD
TXSTABITS_TXEN = 1     ; TRANSMISION SERIAL HABILITADA
TxSTABITS_SYNC = 0     ; "1" MODO SINCRONO ; "0" MODO ASINCRONO
'SPBRGH1 = 0    'EUSARTx Baud Rate Generator Register High Byte
'SPBRG1 = 64    'EUSARTx Baud Rate Generator Register Low Byte


;RCON: RESET CONTROL REGISTER
RCONBITS_IPEN    = 1   ; "1" HABILITA LA PRIORIDAD DE LOS NIVELES CATIVOS DE INTERRUPCION;"0" DESHABILITA LA PRIORIDAD DE LOS NIVELES CATIVOS DE INTERRUPCION
;CUANDO IPEN (PRIORIDAD DE INTERRUPCIONES)
;INTCON: INTERRUP CONTROL REGISTER
INTCONBITS_GIE   = 1   ; IPEN = 0
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES NO ENMASCARADAS; "0" DESHABILITA TODAS LAS INTERRUPCIONES INCLUYENDO PERIFERICOS
                       ; IPEN = 1
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES DE ALTA PRIORIDAD; "0" DESHABILITA TODAS LAS INTERRUPCIONES INCLUYENDO LAS DE BAJA PRIRORIDAD
INTCONBITS_PEIE  = 1   ; IPEN = 0
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES NO ENMASCARADAS DE PERIFERICOS; "0" DESHABILITA TODAS LAS INTERRUPCIONES DE PERIFERICOS
                       ; IPEN = 1
                       ; "1" HABILITA TODAS LAS INTERRUPCIONES DE BAJA PRIORIDAD; "0" DESHABILITA TODAS LAS INTERRUPCIONES DE BAJA PRIORIDAD               
TRISB.0 = 0
TRISB.1 = 0
TRISB.2 = 0
TRISC.2 = 0
TRISC.6 = 0
TRISC.7 = 1
PORTB = 0

; VARIABLES DEL PROGRAMA
Dim DATO_RX As Byte
Dim X As Byte
Symbol LED = PORTB.0
Symbol LED2 = PORTB.1
Symbol LED3 = PORTB.2

INICIO:
      For X = 1 To 4
            LED = 1 ;  // enciende led
            DelayMS 250
            LED = 0
            DelayMS 250
      Next X
'      LED = 1 ;  // enciende led

HRSOut "LED ENCENDIDO ==>  1 ",13
HRSOut "LED APAGADO   ==>  0 ",13,13

PRINCIPAL:

Do
      LED = 1 ;  // enciende led
      DelayMS 100
      LED = 0
      DelayMS 100
      LED3 = 1 ;  // enciende led
      DelayMS 100
      LED3 = 0
      DelayMS 100
Loop
End


INTERRUP:
        Context Save
'           INTERRUPCION RECEPCION SERIAL
            If PIR1BITS_RC1IF = 1 Then
                 DATO_RX = RCREG1 ;HRSIn

                 If DATO_RX = "1" Then
                      RCREG1 = 0
                      HRSOut " LED ENCENDIDO",13
                      LED2 = 1
                 EndIf

                 If DATO_RX = "0" Then
                      RCREG1 = 0
                      HRSOut " LED APAGADO",13
                      LED2 = 0
                 EndIf
                 PIR1BITS_RC1IF = 0
            EndIf
        Context Restore

Config_Start

  FOSC = HSHP;INTIO67;HSMP    ;Internal oscillator block
  PLLCFG = On    ;Oscillator multiplied by 4
  PRICLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
  IESO = OFF    ;Oscillator Switchover mode disabled
  PWRTEN = On    ;Power up timer enabled
  BOREN = OFF    ;Brown-out Reset disabled in hardware and software
  BORV = 190    ;VBOR set to 1.90 V nominal
  WDTEN = OFF    ;Watch dog timer is always disabled. SWDTEN has no effect.
  WDTPS = 256    ;1:256
  CCP2MX = PORTC1    ;CCP2 input/output is multiplexed with RC1
  PBADEN = OFF    ;PORTB<5:0> pins are configured as digital I/O on Reset
  CCP3MX = PORTB5    ;P3A/CCP3 input/output is multiplexed with RB5
  HFOFST = On    ;HFINTOSC output and ready status are not delayed by the oscillator stable status
  T3CMX = PORTC0    ;T3CKI is on RC0
  P2BMX = PORTB5    ;P2B is on RB5
  MCLRE = EXTMCLR    ;MCLR pin enabled, RE3 input pin disabled
  STVREN = OFF    ;Stack full/underflow will not cause Reset
  LVP = On    ;Single-Supply ICSP enable MCLRE a 1
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Disabled
  Cp0 = OFF    ;Block 0 (000800-003FFFh) not code-protected
  CP1 = OFF    ;Block 1 (004000-007FFFh) not code-protected
  CP2 = OFF    ;Block 2 (008000-00BFFFh) not code-protected
  CP3 = OFF    ;Block 3 (00C000-00FFFFh) not code-protected
  CPB = OFF    ;Boot block (000000-0007FFh) not code-protected
  CPD = OFF    ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 (000800-003FFFh) not write-protected
  WRT1 = OFF    ;Block 1 (004000-007FFFh) not write-protected
  WRT2 = OFF    ;Block 2 (008000-00BFFFh) not write-protected
  WRT3 = OFF    ;Block 3 (00C000-00FFFFh) not write-protected
  WRTC = OFF    ;Configuration registers (300000-3000FFh) not write-protected
  WRTB = OFF    ;Boot Block (000000-0007FFh) not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  EBTR0 = OFF    ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
  EBTR1 = OFF    ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
  EBTR2 = OFF    ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
  EBTR3 = OFF    ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
  EBTRB = OFF    ;Boot Block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End


I hope you can help me with this!!

Pepe

See the previous answer where it says delete in Panel P10.Inc the instruction Trisc = 0

Pepe

modify this in the program

Include "Panel P10.inc"
Include "Multi_RAM_Byte_Array.inc"
TRISB.0 = 0
TRISB.1 = 0
TRISB.2 = 0

TRISC = $80

PORTB = 0
PORTC = 0


and

delete TRISC = 0 in library 'Panel P10.inc'

Pepe

Otherwise the Rx pin remains as an output instead of an input

jam19857

Thank you very much Pepe, the problem was the TRISC= 0, I made some adjustments and both interrupts work very well in the real circuit. The interrupt code works fine.

John Drew

Hello Jam19857,
Glad you've got it working.
Just a gentle reminder that writing a post in capital letters is considered shouting and bad manners. Mixed case is also easier to read.
Something to remember for your next post.
John

jam19857


John Drew


HAL

Hello jam19857

I did the same thing (using all caps) when I first joined the forum.  I write notes to myself in caps because my vision is not what it once was.  So the key board is normally caps locked.  Don't be upset, the members here are really nice people and will not be too concerned with a minor mistake from those of us who are new to forums. 

Regards
Hal