Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

4 digit 7 sepments display counter with 74hc595 micrkobasic

Status
Not open for further replies.

fred1966

New Member
program counter_74hc595 'wonking good

Code:
dim portd_index as byte ' Variables shifter y portd_index son de tipo byte
     portd_array as word[4]  ' Matriz portd_array tiene 4 miembros de tipo word
     oldstate as byte
     oldstate1 as byte
     J AS WORD
     CONTADOR AS WORD
'===============================================================================
   SUB FUNCTION NUMERO(DIM NUM AS BYTE)AS BYTE
      SELECT CASE NUM
       CASE 0
       NUMERO = 0xFC
       CASE 1
       NUMERO = 0x60
       CASE 2
       NUMERO = 0xDA
        CASE 3
       NUMERO = 0xF2
        CASE 4
       NUMERO = 0x66
        CASE 5
       NUMERO = 0xB6
        CASE 6
       NUMERO = 0xBE
        CASE 7
       NUMERO = 0xE0
        CASE 8
       NUMERO = 0xFE
        CASE 9
       NUMERO = 0xF6
       END SELECT
   END SUB
'===============================================================================
 sub procedure Output1(dim hex_value as word,dim port_data as byte, dim port_clock as byte,dim port_lach as byte )
        dim x as byte
       portb.port_clock = 0                       'GENERAR LACH
        for x = 0 to  7                        ' loops for a count of eight
            portb.port_data = hex_value.x      'SACAR DATA
             portb.port_lach = 1              'portb.1 is high
             nop
            portb.port_lach = 0                'portb.1 is low                  'LLAMA PROCEDIMIETO PARA GENERAR RELOJ
        next x
       portb.port_clock = 1                      'GENERAR LACH     Latch
    end sub

sub procedure interrupt ' Inicio de la rutina de interrupción
   PORTA = 0
   OUTPUT1(portd_array[portd_index],7,5,4)
   PORTA.portd_index = 1 ' Encender el visualizador apropiado de 7 segmentos
   Inc(portd_index)          ' Incrementar portd_index
     if (portd_index > 3) then    ' Encender el primero, apagar el cuarto visualizador de 7 segmentos
     if (CONTADOR > 0)and (CONTADOR < 10) then portd_index = 3 end if
     if (CONTADOR > 9)and (CONTADOR < 100) then portd_index = 2 end if
     if (CONTADOR > 99)and (CONTADOR < 1000) then portd_index = 1 end if
     if (CONTADOR > 999) then portd_index = 0 end if
   end if
     TMR0 = 0                  ' Reiniciar el valor del TIMER0
     T0IF_bit = 0              ' Borrar la bandera de interrupción del Timer0

end sub                       ' Final de la rutina de interrupción
'===============================================================================
 SUB PROCEDURE DISPLAY(DIM NUMBER AS WORD)
 DIM digit as word
   digit = number / 1000          ' Extraer miles
  portd_array[0] = NUMERO(digit)  ' y almacenarlo en la matriz Del 74hc595
  digit = (number / 100) mod 10   ' Extraer centenas
  portd_array[1] = NUMERO(digit)  ' y almacenarlo en la matriz Del 74hc595
  digit = (number / 10) mod 10    ' Extraer decenas
  portd_array[2] = NUMERO(digit)  ' y almacenarlo en la matriz Del 74hc595
  digit = number mod 10           ' Extraer unidades
  portd_array[3] = NUMERO(digit)  ' y almacenarlo en la matriz Del 74hc595
 END SUB
 '==============================================================================
 sub procedure EePromWriteVariable(dim Dat as ^byte, dim Number, EEpromAddress as byte)
 dim I as byte
   for I = 0 to Number -1
    EEProm_Write(EEPromAddress, Dat^)'  escribir en la eeprom
    Delay_ms(5)
   Inc(Dat)
    Inc(EEpromAddress)
   next I
end sub
 '==============================================================================
sub procedure EePromReadVariable(dim Dat as ^byte, dim Number, EEpromAddress as byte)
 dim I as byte
   for I = 0 to Number -1
    Dat^ = EEProm_Read(EEPromAddress)' leer valor de eeprom
     Delay_ms(5)
    Inc(Dat)
    Inc(EEpromAddress)
   next I
end sub
 '==============================================================================
   SUB PROCEDURE INICIO
OPTION_REG = $80 ' Configuración del Timer0 (Timer0 funciona como temporizador ' con el pre-escalador)
portd_index = 0  ' Encender el primer visualizador LED
TMR0 = 0         ' Borrar el Timer0
INTCON = $A0     ' Habilitar interrupción con los bits GIE y T0IE
PORTA = 0        ' Borrar el PORTA
TRISA = %11110000        ' Configurar el PORTA como salida
OPTION_REG.7 = 0 ' Enable WPUs by individual pin
PORTB = 0        ' Borrar el PORTD
TRISB = 0x07      ' Configurar el PORTB como salida
END SUB
main:
     INICIO   ' Inicio del programa
     CONTADOR = 0
     EePromReadVariable(@CONTADOR , 4, 10)

   while TRUE

    if Button(PORTB, 0, 1, 1) then
    oldstate = 255
    end if
    if oldstate and Button(PORTB, 0, 1, 0) then
        INC(CONTADOR)
         IF CONTADOR > 9999 THEN CONTADOR = 0 END IF
         EePromWriteVariable(@CONTADOR,4, 10)
    oldstate = 0
    end if

       if Button(PORTB, 1, 1, 1) then
    oldstate1 = 255
    end if
    if oldstate1 and Button(PORTB, 1, 1, 0) then
        CONTADOR = 0
      EePromWriteVariable(@CONTADOR,4, 10)
       oldstate1 = 0
    end if
     INC(J)
     IF J >= 3 THEN
        J =  0
       INC(CONTADOR)
         IF CONTADOR > 9999 THEN CONTADOR = 0 END IF
             DISPLAY(CONTADOR)
             EePromWriteVariable(@CONTADOR,4, 10)

       END IF
   wend

end. ' Final de programa

[MODNOTE]Please use the code tags.. Symbol '#'[/MODNOTE]
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top