Odliczanie czasu na zmiennej

Pytania, kody i porady dotyczące nie tylko Bascom.
demsy
Posty: 24
Rejestracja: 30 sie 2015, 22:06

Re: Odliczanie czasu na zmiennej

Post autor: demsy » 25 wrz 2017, 20:44

Nie mogłem wcześniej napisać, ale znalazłem chwilę więc wrzucam działający kod może się komuś przyda

  1. $regfile = "m8def.dat"
  2. $crystal = 8000000
  3.  
  4. Ddrb = &B00011100 : Portb = &B11100011
  5. Ddrc = &B00111000 : Portc = &B00000000
  6. Ddrd = &B11111111 : Portd = &B00000000
  7.  
  8. Config Timer2 = Timer , Prescale = 8 , Compare = Disconnect , Clear Timer = 1
  9. Enable Compare2 : On Compare2 Int0_isr : Compare2 = 124
  10.  
  11.  
  12. Config Adc = Single , Prescaler = Auto , Reference = Avcc
  13. Start Adc
  14.  
  15.  
  16. Enable Interrupts
  17.  
  18. '******************  Zmienne *******************
  19. '***********************************************
  20. Dim Licznik As Byte
  21. Dim Licz As Byte
  22. Dim Max_pwm As Byte
  23. Dim Pwm_led(17) As Byte
  24. Dim Speed As Word                                           '
  25. Dim Scenario As Byte
  26. Dim Z As Word
  27. Dim Czas As Word
  28. Dim Led_nr As Byte
  29. Dim Bit_1 As Bit
  30. Dim Leds_pwm As Byte
  31. Dim M As Byte
  32. Dim Old_val As Byte
  33. Dim L_d As Byte
  34. Dim Szybkosc As Word
  35. Dim Odlicz As Byte
  36. Dim Adc_read As Word
  37. Dim Result As Word
  38.  
  39.  
  40.  
  41. D1 Alias Portd.2 : D2 Alias Portd.3 : D3 Alias Portd.4 : D4 Alias Portd.5
  42.  
  43.  
  44. '***************   przyciski     *****************
  45. Sw1 Alias Pinb.0
  46. Sw2 Alias Pinb.1
  47. '*************************************************
  48.  
  49.  
  50.  
  51. '**************  Petla glowna ******************
  52.  
  53. Bit_1 = 0
  54. Szybkosc = 100
  55. Speed = 0
  56. Result = 0
  57. Led_nr = 1
  58.  
  59.  
  60. Do
  61.  
  62.  
  63. Adc_read = Getadc(2)
  64. Adc_read = Adc_read
  65.  
  66.  
  67.    If Sw1 = 0 Or Sw2 = 0 And Bit_1 = 0 Then
  68.  
  69.         Bit_1 = 1
  70.         Led_nr = 1
  71.        Scenario = 1
  72.  
  73.         End If
  74.        
  75.  
  76.  
  77. If Speed = 0 Then
  78.                                           '
  79. Select Case Scenario
  80.  
  81.  
  82.  
  83. Case 0
  84. '************************************************************************************
  85.  
  86.  
  87. Case 1
  88. ' * * * * * * * * * * * * * * * * * * * * * * * * *
  89.               If Led_nr < 100 Then Incr Led_nr
  90.  
  91.                If Old_val <> Led_nr Then
  92.                   Old_val = Led_nr
  93.  
  94.                   Speed = 127 - Led_nr
  95.                   Speed = Szybkosc
  96.  
  97.  
  98.                   Shift Speed , Left , 1
  99.  
  100.                   M = Memcopy(led_nr , Pwm_led(1) , 17 , 2)
  101.  
  102.                End If
  103.  
  104.                 If Pwm_led(4) = 100 Then
  105.                Scenario = 2
  106.                End If
  107.  
  108.  
  109.  
  110. Case 2
  111.  
  112.                     Incr Result
  113.                     Waitms 50
  114.                       If Sw1 = 0 Or Sw2 = 0 Then
  115.                       Result = 0
  116.                       End If
  117.  
  118.                       If Result >= Adc_read Then
  119.                       Result = 0
  120.                       Scenario = 3
  121.                    End If
  122.  
  123.  
  124.  
  125.  
  126. Case 3
  127.  
  128.  
  129.               If Led_nr > 0 Then Decr Led_nr
  130.  
  131.                If Old_val <> Led_nr Then
  132.                   Old_val = Led_nr
  133.  
  134.                   Speed = 127 - Led_nr
  135.                   Speed = Szybkosc
  136.  
  137.                   Shift Speed , Left , 1
  138.  
  139.                   M = Memcopy(led_nr , Pwm_led(1) , 17 , 2 )
  140.                End If
  141.  
  142.                  Bit_1 = 0
  143.  
  144.  
  145.  
  146. End Select
  147.  
  148. End If
  149.  
  150.  
  151. Loop
  152.  
  153.  
  154.  
  155. '************ Przerwanie timer 0 ***************
  156. '***********************************************
  157. Int0_isr:
  158.  Incr Licznik
  159.  
  160.  If Licznik = 100 Then Licznik = 0
  161.  
  162.  If Licznik < Pwm_led(1) Then Set D1 Else Reset D1
  163.  If Licznik < Pwm_led(2) Then Set D2 Else Reset D2
  164.  If Licznik < Pwm_led(3) Then Set D3 Else Reset D3
  165.  If Licznik < Pwm_led(4) Then Set D4 Else Reset D4
  166.  
  167.     If Speed > 0 Then Decr Speed
  168.  
  169.  
  170.  
  171. Return
ODPOWIEDZ