Mikrobasic, Interrupts en multiplexen

Bavelt

Golden Member

Is best wel lastig, het signaal op CLKout is slechts ca 50mv toptop.

Versterken is inmiddels gelukt. Een 74HC02 (NOR gate) werkt gewoon.
Hij maakt er een mooie spanning van. Nu kan ik de frequentie op CLKOUT meten.
Deze geeft aan 10,001 Mhz met de 22pf / 12pf consensatoren.

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Zal best hoger zijn, hier is het ongeveer 1v (3.3v voeding)
Frequentieteller via klein condensatortje van 10-20pF. (als dat werkt, weet niet hoe de ingang daarvan in elkaar zit)

Met een offset werkt ook prima, minder als een halve seconde per week afwijking mag geen probleem zijn

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

weet niet hoe de ingang daarvan in elkaar zit

.
Die zit rechtstreeks op poort A3 van de 628A zo te zien.

Fouten zijn het bewijs dat je het probeert..
Bavelt

Golden Member

Met een offset werkt ook prima, minder als een halve seconde per week afwijking mag geen probleem zijn

Dat ga ik zeker ook uitproberen. Ik probeer echter wel eerst de oscillator helemaal goed te krijgen. En kijken wat de waarde van de condensatortjes doen met de frequentie.

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Om goed te kunnen meten heb je wel 7 of 8 digits nodig, 5 is te grof...
Bij 2 seconden te snel zou je iets van 10.000.250 Hz moeten meten.
Ingangscondensatortje groter dan wordt 'ie langzamer...

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Om goed te kunnen meten heb je wel 7 of 8 digits nodig, 5 is te grof...
Bij 2 seconden te snel zou je iets van 10.000.250 Hz moeten meten.

Ook misschien een leuke oefening om een nauwkeurige frequentieteller te maken met behulp van MikroBasic.

Dan ben je natuurlijk ook wel afhankelijk van een juiste klokfrequentie
van de schakeling lijkt me. Kip en ei?

Wat zou een handige methode zijn om pulsen te tellen binnen 1 seconde?
Uiteraard gebruik ik dan nu de timerfunctie. Het detecteren van een puls zou dan kunnen met iets als

pic basic code:

 if portc.6 = not portc.6

, waarbij de pulsen dan binnenkomen op Poort C.6?

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Op die manier ga je nooit hoge frequenties meten...
Als de clock 10MHz is, dan is de instruction cycle clock 10/4=2.5MHz. Sneller kan nooit.
Met alle code om pinnen te testen en counters bij te houden mag je met 250kHz al heel blij zijn...

Hoge frequenties 'tellen' wordt meestal met Timer 1 en de interrupt gedaan.
Voorbeeld: https://www.best-microcontroller-projects.com/pic-frequency-counter.ht…

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Ik had deze gezien bij mijn speurtocht. Voor deze keer had ik maar even MikroC geïnstalleerd.
Met het idee Kijken of ik het aan de praat krijg en dan proberen het om te zetten naar iets begrijpelijks, zoals MikroBasic dat wel biedt.

Maar deze taal (C) zal voor mij nooit wennen. Daarbij mist het project een "stdlib.h en string.h".

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Die standaard headerfiles horen bij de compiler te zitten...

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Ondertussen even weer terug naar de interrupt.

Ik ben het klokprogramma aan het ombouwen van 7-Segment naar MatrixLeds,
8 * 8 met MAX7219-en aan boord.

Nu wil ik in de interrupt routine de Led's aansturen.

dus ipv

pic basic code:


  LATA = DigTab[DigCount]
  LATB = SegTab[Digit[DigCount]]  

Wordt het dan UPDATE_LED().

In die Update_Led wordt mbv het SPI protocol de LED's aangestuurd.

Maar de compiler vindt het niet goed als je in de interrupt Procedure een andere procedure aanroept waar een SPI1_Write in zit.

Je krijgt dan als melding:

89 365 Reentrancy is not allowed: function 'SPI1_write' called from two threads __Lib_SPI_c345.mpas

Waarom mag dat niet en hoe los ik het dan op?

Fouten zijn het bewijs dat je het probeert..
Bavelt

Golden Member

Terwijl ik het post heb ik al wel een idee: De SPI1_write kan natuurlijk op zijn beurt weer worden onderbroken door dezelfde interrupt, en dan wordt het een oneindige lus, niet?

Fouten zijn het bewijs dat je het probeert..
Bavelt

Golden Member

Ik heb het zo opgelost door een vlaggetje te maken (Update as boolean)
en hem op TRUE te zetten als er moet worden geupdate.

in de MAIN Procedure vang ik dan af of de update vlag aan staat.
Ik weet niet of dit de juiste of een nette methode is.

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Trage zaken als i2c/spi/uart doe je sowieso niet in een interrupt. Dan zet je inderdaad een flag en handelt het in main af...

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Het is nog wel even puzzelen om de SPI matrix Leds erin te zetten ipv de 7 segmenten.
Er lijkt iets mis te gaan met de timer. Morgen verder puzzelen.

Fouten zijn het bewijs dat je het probeert..
Bavelt

Golden Member

Het is gelukt!

Programma met de PIC16F887, en 4 Matrix LEDS 8*8.
Draait op 10Mhz extern kristal.
Even zonder gelijkstel button.

De Matrixen zitten met 4 aan elkaar vast, daardoor is het even puzzelen om de juiste cijfers te presenteren.
Maar het werkt.

pic basic code:

Program MatrixKlok
Dim DigCount   As Byte
    Digit      As Byte[4]
    Seccount   As Byte
    MinCount   As Byte
    KeyCount   As Byte
    Flags      As Byte
    fMinute    As sBit At Flags.0
    fKeyRpt    As sBit At Flags.1
    fUpdate    As sBit At Flags.2
    Button     As sBit At PORTC.2      'Pin 17

Const Maxdigits      = 4               'Displayed digits
      Keytime        = 120             '0.6sec
      Keydebounce    = 10              '40mS
      Keyrpttime     = 10              '40mS

Dim ClockPin as sbit at PORTC.3        'pin 18
Dim Latch as sbit at PORTC.7           'pin 26
Dim Datapin as sbit at PORTC.5         'pin 24

Dim Kolom as Byte
Dim Inhoud as Byte[4]
Dim tdx as byte[4]
Dim dx as byte[4]

const font as byte[80] =               'Character tabel, 90 Degrees turn left
(
  0x3c, 0x66, 0x6e, 0x76, 0x66, 0x66, 0x3c, 0x00,
  0x7e, 0x18, 0x18, 0x18, 0x1c, 0x18, 0x18, 0x00,
  0x7e, 0x06, 0x0c, 0x30, 0x60, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x38, 0x60, 0x66, 0x3c, 0x00,
  0x30, 0x30, 0x7e, 0x32, 0x34, 0x38, 0x30, 0x00,
  0x3c, 0x66, 0x60, 0x60, 0x3e, 0x06, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3e, 0x06, 0x66, 0x3c, 0x00,
  0x18, 0x18, 0x18, 0x30, 0x30, 0x66, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3c, 0x66, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x7c, 0x66, 0x66, 0x3c, 0x00
)

  '===============================================================================
Sub Procedure Max_Write_2
'===============================================================================
 Latch = 0
 SPI1_Write(Kolom)
 SPI1_Write(Inhoud[0])
 SPI1_Write(Kolom)
 SPI1_Write(Inhoud[1])
 SPI1_Write(Kolom)
 SPI1_Write(Inhoud[2])
 SPI1_Write(Kolom)
 SPI1_Write(Inhoud[3])
 Latch = 1
 delay_ms(1)
End sub

'===============================================================================
Sub Procedure Max7219_Init()
'===============================================================================

Kolom = $09                           'Decode Mode Register:
Inhoud[0] = $00
Inhoud[1] = $00                       'Code B for digits
Inhoud[2] = $00
Inhoud[3] = $00                       'Code B for digits
Max_Write_2 ()

Kolom = $0B                           'Scan-Limit Register:
Inhoud[0] = $07
Inhoud[1] = $07                       'Digit 0,1,2,3,4,5,6,7 ($00-$07)
Inhoud[2] = $07
Inhoud[3] = $07                       'Digit 0,1,2,3,4,5,6,7 ($00-$07)
Max_Write_2()

Kolom = $0C                           'Shutdown Register:
Inhoud[0] = $01
Inhoud[1] = $01                       'Normal Operation
Inhoud[2] = $01
Inhoud[3] = $01                       'Normal Operation
Max_Write_2()

Kolom = $0A                           'Intensity Register
Inhoud[0] = $01
Inhoud[1] = $01
Inhoud[2] = $01
Inhoud[3] = $01
Max_Write_2()
End sub

'===============================================================================
Sub Procedure Display_Testen()      'Testen van het display; alle LEDS aan
'===============================================================================

Kolom = $0F
Inhoud[0] = $01
Inhoud[1] = $01
Inhoud[2] = $01
Inhoud[3] = $01
Max_Write_2()
DELAY_MS(500)

Kolom = $0F                         'Weer terugzetten naar normal operation
Inhoud[0] = $00
Inhoud[1] = $00
Inhoud[2] = $00
Inhoud[3] = $00
Max_Write_2()
DELAY_MS(500)
end sub

'===============================================================================
Sub Procedure Uitzetten_Leds()      'Leds uitzetten per kolom
'===============================================================================
FOR Kolom = 1 TO 8
  Inhoud[0] = 0
  Inhoud[1] = 0
  Inhoud[2] = 0
  Inhoud[3] = 0
  Max_Write_2()
  delay_ms(50)
NEXT Kolom
end sub

'===============================================================================
Sub Procedure Aanzetten_Leds()      'Leds aanzetten per kolom
'===============================================================================
Inhoud[1] = 0
  Inhoud[2] = 0
  Inhoud[3] = 0

FOR Kolom = 1 TO 8
  Inhoud[0] = 0xff
  Inhoud[1] = 0xff
  Inhoud[2] = 0xff
  Inhoud[3] = 0xff
  Max_Write_2()
  delay_ms(50)
NEXT Kolom
end sub

'===============================================================================
Sub Procedure Update_LED            'Update LED
'===============================================================================
   dx[0] = Digit[0] * 8
     dx[1] = Digit[1] * 8
     dx[2] = Digit[2] * 8
     dx[3] = Digit[3] * 8

     For Kolom = 1 to 8
        Inhoud[0] = font[dx[0]]
        Inhoud[1] = font[dx[1]]
        Inhoud[2] = font[dx[2]]
        Inhoud[3] = font[dx[3]]
        dx[3] = dx[3] + 1
        dx[2] = dx[2] + 1
        dx[1] = dx[1] + 1
        dx[0] = dx[0] + 1
        Max_write_2()
    Next Kolom
end sub

'----------------------------------------------------------------------------------------
sub procedure Interrupt() iv 0x0004 ics ICS_AUTO
'----------------------------------------------------------------------------------------
  If TMR2IF_bit Then                                                'See if timer2 irq
    Inc(Seccount)                                                   'If so, update coun-
    If Seccount = 250 Then                                          'ters.
      Seccount = 0                                                  'Set flag if minute
      Inc(MinCount)                                                 'passed.
      If MinCount = 60 Then 
         MinCount = 0 
         fMinute = 1 
         fUpdate = 1                                                'Update LED
      End If
    End If
    TMR2IF_bit = 0
  End If
end sub

'========================================================================================
Main:

'----------------------------------------------------------------------------------------
  T2CON      = %00100110                                            'Post:5 - Pre:16
  PR2        = 124                                                  'Timer load value
  ANSEL      = %00000000                                            'All ports digital
  ANSELh     = %00000000                                            '
  TRISC      = %00011101
  TRISB      = %00000000
  
  fUpdate    = 0
  GIE_bit    = 1                                                    'Enable timer2 irq
  PEIE_bit   = 1                                                    '
  TMR2IE_bit = 1                                                    '
  DigCount   = 0                                                    'Clear digits and
  Digit[0]   = 0                                                    'digitcounter
  Digit[1]   = 0                                                    '
  Digit[2]   = 0                                                    '
  Digit[3]   = 0  
  
  SPI1_Init()
  Max7219_Init()
  Display_Testen()
  Uitzetten_Leds()
  Update_LED()                                                      'Start with '0000'
                                            '
  '--------------------------------------------------------------------------------------
  While True  
  if fUpdate = 1 then
     Inc(Digit[0])
     If Digit[0] = 10 Then                                          'Update all 4 Digit-
        Digit[0] = 0                                                'values
        Inc (Digit[1])
        If Digit[1] = 6 Then
           Digit[1] = 0
           Inc (Digit[2])
           If Digit[2] = 10 Then
              Digit[2] = 0
              Inc(Digit[3])

           End If
         End If
      End If 
      If (Digit[3] = 2) And (Digit[2] = 4) Then                     'If hour '24', reset
          Digit[0]=0    Digit[1]=0    Digit[2]=0    Digit[3]=0      'to 00:00
      End If
    Update_LED()                                                    'Update LED Matrix
    fMinute = 0                                                     'Reset flag
    fUpdate = 0                                                     'Reset flag
    End if  
    
  Wend

  End.
Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Mooi!

fMinute kun je weglaten, doet hetzelfde als fUpdate...

Volgens mij doet dit hetzelfde, alleen wat ingekort, niet getest... ;)

pic basic code:


Program MatrixKlok
Dim DigCount   As Byte
    Digit      As Byte[4]
    Seccount   As Byte
    MinCount   As Byte
    KeyCount   As Byte
    Flags      As Byte
    fKeyRpt    As sBit At Flags.0
    fUpdate    As sBit At Flags.1
    Button     As sBit At PORTC.2      'Pin 17

Const Maxdigits      = 4               'Displayed digits
      Keytime        = 120             '0.6sec
      Keydebounce    = 10              '40mS
      Keyrpttime     = 10              '40mS

Dim ClockPin as sbit at PORTC.3        'pin 18
Dim Latch as sbit at PORTC.7           'pin 26
Dim Datapin as sbit at PORTC.5         'pin 24

Dim Kolom as Byte

const font as byte[80] =               'Character tabel, 90 Degrees turn left
(
  0x3c, 0x66, 0x6e, 0x76, 0x66, 0x66, 0x3c, 0x00,
  0x7e, 0x18, 0x18, 0x18, 0x1c, 0x18, 0x18, 0x00,
  0x7e, 0x06, 0x0c, 0x30, 0x60, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x38, 0x60, 0x66, 0x3c, 0x00,
  0x30, 0x30, 0x7e, 0x32, 0x34, 0x38, 0x30, 0x00,
  0x3c, 0x66, 0x60, 0x60, 0x3e, 0x06, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3e, 0x06, 0x66, 0x3c, 0x00,
  0x18, 0x18, 0x18, 0x30, 0x30, 0x66, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3c, 0x66, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x7c, 0x66, 0x66, 0x3c, 0x00
)

  '===============================================================================
Sub Procedure Max_Write(Dim pCol, pDig0, pDig1, pDig2, pDig3 As Byte)
'===============================================================================
  Latch = 0
  SPI1_Write(pCol)
  SPI1_Write(pDig0)
  SPI1_Write(pCol)
  SPI1_Write(pDig1)
  SPI1_Write(pCol)
  SPI1_Write(pDig2)
  SPI1_Write(pCol)
  SPI1_Write(pDig3)
  Latch = 1
  delay_ms(1)
End sub

'===============================================================================
Sub Procedure Max7219_Init()
'===============================================================================
  Max_Write(0x09,0x00,0x00,0x00,0x00)
  Max_Write(0x0B,0x07,0x07,0x07,0x07)
  Max_Write(0x0C,0x01,0x01,0x01,0x01)
  Max_Write(0x0A,0x01,0x01,0x01,0x01)
End sub

'===============================================================================
Sub Procedure Display_Testen()      'Testen van het display; alle LEDS aan
'===============================================================================
  Max_Write(0x0F,0x01,0x01,0x01,0x01)
  DELAY_MS(500)
  Max_Write(0x0F,0x00,0x00,0x00,0x00)
  DELAY_MS(500)
end sub

'===============================================================================
Sub Procedure Uitzetten_Leds()      'Leds uitzetten per kolom
'===============================================================================
FOR Kolom = 1 TO 8
  Max_Write(Kolom,0x00,0x00,0x00,0x00)
  delay_ms(50)
NEXT Kolom
end sub

'===============================================================================
Sub Procedure Aanzetten_Leds()      'Leds aanzetten per kolom
'===============================================================================
  For Kolom = 1 TO 8
    Max_Write(Kolom,0xFF,0xFF,0xFF,0xFF)
    delay_ms(50)
  Next Kolom
end sub

'===============================================================================
Sub Procedure Update_LED            'Update LED
'===============================================================================
  For Kolom = 1 to 8
    Max_write(Kolom,
              Font[(Digit[0]<<3)+(Kolom-1)],
              Font[(Digit[1]<<3)+(Kolom-1)],
              Font[(Digit[2]<<3)+(Kolom-1)],
              Font[(Digit[3]<<3)+(Kolom-1)])
  Next Kolom
end sub

'----------------------------------------------------------------------------------------
sub procedure Interrupt() iv 0x0004 ics ICS_AUTO
'----------------------------------------------------------------------------------------
  If TMR2IF_bit Then                                                'See if timer2 irq
    Inc(Seccount)                                                   'If so, update coun-
    If Seccount = 250 Then                                          'ters.
      Seccount = 0                                                  'Set flag if minute
      Inc(MinCount)                                                 'passed.
      If MinCount = 60 Then
         MinCount = 0
         fUpdate = 1                                                'Update LED
      End If
    End If
    TMR2IF_bit = 0
  End If
end sub

'========================================================================================
Main:


'----------------------------------------------------------------------------------------
  T2CON      = %00100110                                            'Post:5 - Pre:16
  PR2        = 124                                                  'Timer load value
  ANSEL      = %00000000                                            'All ports digital
  ANSELh     = %00000000                                            '
  TRISC      = %00011101
  TRISB      = %00000000

  fUpdate    = 0
  GIE_bit    = 1                                                    'Enable timer2 irq
  PEIE_bit   = 1                                                    '
  TMR2IE_bit = 1                                                    '
  DigCount   = 0                                                    'Clear digits and
  Digit[0]   = 0                                                    'digitcounter
  Digit[1]   = 0                                                    '
  Digit[2]   = 0                                                    '
  Digit[3]   = 0

  SPI1_Init()
  Max7219_Init()
  Display_Testen()
  Uitzetten_Leds()
  Update_LED()                                                      'Start with '0000'
                                            '
  '--------------------------------------------------------------------------------------
  While True
  if fUpdate = 1 then
     Inc(Digit[0])
     If Digit[0] = 10 Then                                          'Update all 4 Digit-
        Digit[0] = 0                                                'values
        Inc (Digit[1])
        If Digit[1] = 6 Then
           Digit[1] = 0
           Inc (Digit[2])
           If Digit[2] = 10 Then
              Digit[2] = 0
              Inc(Digit[3])

           End If
         End If
      End If
      If (Digit[3] = 2) And (Digit[2] = 4) Then                     'If hour '24', reset
          Digit[0]=0    Digit[1]=0    Digit[2]=0    Digit[3]=0      'to 00:00
      End If
    Update_LED()                                                    'Update LED Matrix
    fUpdate = 0                                                     'Reset flag
    End if

  Wend

  End.

[Bericht gewijzigd door Arco op dinsdag 10 december 2019 14:34:59 (98%)

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Het is inderdaad beter om de routines te voorzien van parameters. Scheelt toch weer een hoop coderegels.
Ik had het geprogrammeerd vanuit recht-toe-recht aan, om makkelijk te kunnen testen.
Maar dit is beter.

Hier heb ik wel even op lopen turen:

pic basic code:


 Font[(Digit[0]<<3+(Kolom -1)] 

.

Weet nog niet helemaal wat er gebeurt.

Intussen heb ik ook de gelijkstel-knop erbij ingezet. Die werkt, echter hij gaat niet snel als je hem ingedrukt houdt.

pic basic code:

Program MatrixKlok

Dim DigCount   As Byte
    Digit      As Byte[4]
    Seccount   As Byte
    MinCount   As Byte
    KeyCount   As Byte
    Flags      As Byte
    fKeyRpt    As sBit At Flags.0
    fUpdate    As sBit At Flags.1
    Button     As sBit At PORTC.2      'Pin 17

Const Maxdigits      = 4               'Displayed digits
      Keytime        = 120             '0.6sec
      Keydebounce    = 10              '40mS
      Keyrpttime     = 10              '40mS

Dim ClockPin as sbit at PORTC.3        'pin 18
Dim Latch as sbit at PORTC.7           'pin 26
Dim Datapin as sbit at PORTC.5         'pin 24

Dim Kolom as Byte
const font as byte[80] =               'Character tabel, 90 Degrees turn left
(
  0x3c, 0x66, 0x6e, 0x76, 0x66, 0x66, 0x3c, 0x00,
  0x7e, 0x18, 0x18, 0x18, 0x1c, 0x18, 0x18, 0x00,
  0x7e, 0x06, 0x0c, 0x30, 0x60, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x38, 0x60, 0x66, 0x3c, 0x00,
  0x30, 0x30, 0x7e, 0x32, 0x34, 0x38, 0x30, 0x00,
  0x3c, 0x66, 0x60, 0x60, 0x3e, 0x06, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3e, 0x06, 0x66, 0x3c, 0x00,
  0x18, 0x18, 0x18, 0x30, 0x30, 0x66, 0x7e, 0x00,
  0x3c, 0x66, 0x66, 0x3c, 0x66, 0x66, 0x3c, 0x00,
  0x3c, 0x66, 0x60, 0x7c, 0x66, 0x66, 0x3c, 0x00
)

'===============================================================================
Sub Procedure Max_Write(Dim pCol, PDig0, pDig1, pDig2, Pdig3 as Byte)
'===============================================================================
 Latch = 0
 SPI1_Write(pCol)
 SPI1_Write(pDig0)
 SPI1_Write(pCol)
 SPI1_Write(pDig1)
 SPI1_Write(pCol)
 SPI1_Write(pDig2)
 SPI1_Write(pCol)
 SPI1_Write(pDig3)
 Latch = 1
 delay_ms(1)
End sub

'===============================================================================
Sub Procedure Max7219_Init()
'===============================================================================

Max_Write(0x09, 0x00, 0x00, 0x00, 0x00)
Max_Write(0x08, 0x00, 0x07, 0x07, 0x07)
Max_Write(0x0C, 0x01, 0x01, 0x01, 0x01)
Max_Write(0x0A, 0x01, 0x01, 0x01, 0x01)
End sub

'===============================================================================
Sub Procedure Display_Testen()      'Testen van het display; alle LEDS aan
'===============================================================================
Max_Write(0x0F, 0x01, 0x01, 0x01, 0x01)
DELAY_MS(500)
Max_Write(0x0F, 0x00, 0x00, 0x00, 0x00)
DELAY_MS(500)
End sub

'===============================================================================
Sub Procedure Aanzetten_Leds()      'Leds aanzetten per kolom
'===============================================================================
  For Kolom = 1 TO 8
    Max_Write(Kolom,0xFF,0xFF,0xFF,0xFF)
    delay_ms(100)
  Next Kolom
End sub

'===============================================================================
Sub Procedure Uitzetten_Leds()      'Leds uitzetten per kolom
'===============================================================================
FOR Kolom = 1 TO 8
   Max_Write(Kolom, 0x00, 0x00, 0x00, 0x00)
   delay_ms(100)
NEXT Kolom
End sub

'===============================================================================
Sub Procedure Update_LED            'Update LED
'===============================================================================
     For Kolom = 1 to 8
        Max_Write(Kolom, Font[(Digit[0]<<3) + (Kolom - 1)],
                         Font[(Digit[1]<<3) + (Kolom - 1)],
                         Font[(Digit[2]<<3) + (Kolom - 1)],
                         Font[(Digit[3]<<3) + (Kolom - 1)])
     Next Kolom
End sub

'===============================================================================
sub procedure Interrupt() iv 0x0004 ics ICS_AUTO
'===============================================================================
  If TMR2IF_bit Then                                                'See if timer2 irq
    Inc(Seccount)                                                   'If so, update coun-
    If Seccount = 250 Then                                          'ters.
      Seccount = 0                                                  'Set flag if minute
      Inc(MinCount)                                                 'passed.
      If MinCount = 60 Then 
         MinCount = 0 
         fUpdate = 1                                                'Update LED
      End If
    End If
   '------------------------------------------------------------------------------------
    If Button = 0 Then                                              'Button pressed?
      Inc(KeyCount)                                                 'Yes, count + 1
      Select Case KeyCount                                          '
        Case KeyTime                                                'See if it's a nor-
          fKeyRpt  = 1                                              'mal or a repeated
          KeyCount = 0                                              'keypress.
        Case KeyDebounce                                            '
          fUpdate = 1                                               '
          SecCount  = 0                                             '
          Mincount = 0                                              '
        Case KeyRptTime                                             '
          If fKeyRpt Then                                           '
            fUpdate  = 1                                            '
            SecCount  = 0
            MinCount = 0                                             '
            KeyCount = 0                                            '
          End If                                                    '
      End Select                                                    '
    Else                                                            'No key, clear count
      fKeyRpt   = 0                                                 'and flag
      KeyCount  = 0                                                 '
    End If                             '
    TMR2IF_bit = 0
  End If
End sub

'========================================================================================
Main:

'----------------------------------------------------------------------------------------
  T2CON      = %00100110                                            'Post:5 - Pre:16
  PR2        = 124                                                  'Timer load value
  ANSEL      = %00000000                                            'All ports digital
  ANSELh     = %00000000                                            '
  TRISC      = %00011101
  TRISB      = %00000000
  fUpdate    = 0
  GIE_bit    = 1                                                    'Enable timer2 irq
  PEIE_bit   = 1                                                    '
  TMR2IE_bit = 1                                                    '
  DigCount   = 0                                                    'Clear digits and
  Digit[0]   = 0                                                    'digitcounter
  Digit[1]   = 0                                                    '
  Digit[2]   = 0                                                    '
  Digit[3]   = 0  
  
  SPI1_Init()
  Max7219_Init()
  Display_Testen()
  Uitzetten_Leds()
  Update_LED()                                                      'Start with '0000'
                                                                    '
  '--------------------------------------------------------------------------------------
  While True  
  if fUpdate = 1 then
     Inc(Digit[0])
     If Digit[0] = 10 Then                                          'Update all 4 Digit-
        Digit[0] = 0                                                'values
        Inc (Digit[1])
        If Digit[1] = 6 Then
           Digit[1] = 0
           Inc (Digit[2])
           If Digit[2] = 10 Then
              Digit[2] = 0
              Inc(Digit[3])
           End If
         End If
      End If 
      If (Digit[3] = 2) And (Digit[2] = 4) Then                     'If hour '24', reset
          Digit[0]=0    Digit[1]=0    Digit[2]=0    Digit[3]=0      'to 00:00
      End If
    Update_LED()                                                    'Reset flag
    fUpdate = 0                                                     'Reset flag
    End if
  Wend

  End. 
Fouten zijn het bewijs dat je het probeert..
Bavelt

Golden Member

Ik zie het al, komt omdat hij de leds update als je de knop vasthoudt.
En dat gaat met ca 1 seconde

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

code:

 Font[(Digit[3]<<3)+(Kolom-1)]) 

is hetzelfde als:

code:

 Font[(Digit[3]*8)+(Kolom-1)]) 
Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Ik kan de klok bij het gelijkstellen sneller laten lopen als je de button houdt ingedrukt door PR2 een andere waarde te geven. Bij loslaten neemt hij weer de juiste waarde aan (124).

Ik weet alleen niet of dit een 'nette' methode is.

pic basic code:


While True
  if fUpdate = 1 then
     if Button = 0 then
        PR2 = 30
     else
        PR2 = 124
     end if 
Fouten zijn het bewijs dat je het probeert..
Lambiek

Special Member

Kijk, die Bavelt doet tenminste iets met de informatie die hij toegespeeld krijgt van Arco.

Al is Mikrobasic niet mijn ding, maar ik vind het mooi dat Bavelt er zo mee aan de gang gaat. :)

Als je haar maar goed zit, GROETEN LAMBIEK.
Bavelt

Golden Member

Het is toch wel heel erg leuk, hoor...

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Het makkelijkst is de Select Case zoals ik gebruikte.
Je test dan of de toets langer als een bepaalde tijd is ingedrukt, en schakelt dan een tandje hoger... ;)

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com
Bavelt

Golden Member

Die had ik er ook ingezet (gelaten).

pic basic code:

 If Button = 0 Then                                              'Button pressed?
      Inc(KeyCount)                                                 'Yes, count + 1
      Select Case KeyCount                                          '
        Case KeyTime                                                'See if it's a nor-
          fKeyRpt  = 1                                              'mal or a repeated
          KeyCount = 0                                              'keypress.
        Case KeyDebounce                                            '
          fUpdate = 1                                               '
          SecCount  = 0                                             '
          Mincount = 0                                              '
        Case KeyRptTime                                             '
          If fKeyRpt Then                                           '
            fUpdate  = 1                                            '
            SecCount  = 0
            MinCount = 0                                             '
            KeyCount = 0                                            '
          End If                                                    '
      End Select                                                    '
    Else                                                            'No key, clear count
      fKeyRpt   = 0                                                 'and flag
      KeyCount  = 0                                                 '
    End If                         

Ik kan het 'versnellen' ook daar in de Select Case plaatsen'. Ik weet alleen niet of het wijzigen van PR2 is aan te bevelen.

[Bericht gewijzigd door Bavelt op dinsdag 10 december 2019 17:32:06 (83%)

Fouten zijn het bewijs dat je het probeert..
Arco

Special Member

Nee, je hoeft niks te doen.

Bij iedere toets wordt fUpdate geset, zodat in main() de tijd wordt opgehoogd en fUpdate weer gereset.
PR2 veranderen is niet aan te raden en niet nodig, omdat de post en prescaler dan niet worden gereset.

Arco - "Simplicity is a prerequisite for reliability" - hard-, firm-, en software ontwikkeling: www.arcovox.com