'*************************************
'&sim
$regfile = "m8def.dat"
$crystal = 16000000
$hwstack = 64
$swstack = 32
$framesize = 128
'----- KONFIGURACJA WYŚWIETLACZA -----------------------------------------------
Config Lcd = 20 * 4
Config Lcdpin = Pin , Db4 = Portc.2 , Db5 = Portc.3 , Db6 = Portc.4 , Db7 = Portc.5 , E = Portc.1 , Rs = Portc.0
Cursor Off
Cls
'----- KONFIGURACJA PORTÓW WEJŚCIA I WYJŚCIA / SCL i SDA / 1 WIRE / ALIASY -----
Config Pinb.0 = Input : Set Portb.0 : F_d Alias Pinb.0
Config Portb.1 = Output 'OC1A
Config Pinb.2 = Input : Set Portb.2 : Dn Alias Pinb.2
Config Pinb.3 = Input : Set Portb.3 : Up Alias Pinb.3
Config Pinb.4 = Input : Set Portb.4 : Plus Alias Pinb.4 'zaminna wanted + 10
Config Pinb.5 = Input : Set Portb.5 : Minus Alias Pinb.5 'zmienna wanted - 10
'Config Portb.5 = Input : Reset Portb.5 'nieprzypisany - na razie jako wejście
'Config Portc.0 = Input : Reset Portc.0 'nieprzypisany - na razie jako wejście
'Config Portd.0 = Input : Set Portd.0 : Led Alias Portd.0
'Config Portd.1 = Input : Reset Portd.1 'nieprzypisany - na razie jako wejście
'Config Pind.2 = Input : Set Portd.2 'nieprzypisany - na razie jako wejście
'Config Pind.3 = Input : Set Portd.3 'nieprzypisany - na razie jako wejście
'Config Pind.5 = Input : Set Portd.5 : nieprzypisany - na razie jako wejście
'Config Pind.6 = Input : Set Portd.6 : X100 Alias Pind.6
'Config Pind.7 = Input : Set Portd.7 : Run Alias Pind.7
'TCCR1A -> |COM1A1| COM1A0 COM1B1 COM1B0 FOC1A FOC1B WGM11 WGM10
'TCCR1B -> | ICNC1| ICES1 -- WGM13 WGM12 CS12 CS11 CS10
' WGM13-|
'----- DEKLARACJA STAŁYCH I ZMIENNYCH ------------------------------------------
Const Pwm_presc1 = &B00010001 'for TCCR1B
Const Pwm_presc8 = &B00010010
Const Pwm_presc64 = &B00010011
Const Pwm_presc256 = &B00010100
Const Pwm_presc1024 = &B00010101
Const Set_output_mode = &B10100000 'for TCCR1A
Dim Presc As Byte , Wartosc_dla_tccr1b As Byte
Dim Value As Dword , Help_d As Dword , Wanted As Dword
Dim Ustaw As Word , Wypelnienie As Word , Proc As Word
Dim Pos As Byte , Helpb As Byte
Dim Wanted_str As String * 20
dim licznik as single ' zmienna dla licznika PWM
Const Freq_ = _xtal / 2
Const Freq = Freq_ * 10 'z 8 Mega zrobi 80 Mega
Proc = 50 'wypełnienie początkowa wartośc, można wpisać 50
Wanted = 100 'Hz początkowa 10,0 Hz
Locate 1 , 3 : Lcd "FREQ = "
Locate 2 , 3 : Lcd "DUTY = "
Gosub Przelicz
Gosub Menu_sub
Do
If Plus = 0 Then
Wanted = Wanted + 10
Gosub Przelicz
End If
If Minus = 0 Then
Wanted = Wanted - 10
If Wanted < 10 Then : Wanted = 10 : End If
Gosub Przelicz
End If
If Up = 0 Then
Waitms 50
If Up = 0 Then
Select Case Pos
Case 0
If Wanted < 100000 Then
Incr Wanted
Gosub Przelicz
End If
Case 1
If Proc < 99 Then
Incr Proc
Gosub Calc_duty
Waitms 50
End If
End Select
End If
End If
If Dn = 0 Then
Waitms 50
If Dn = 0 Then
Select Case Pos
Case 0
If Wanted > 10 Then
Decr Wanted
Gosub Przelicz
End If
Case 1
If Proc > 1 Then
Decr Proc
Gosub Calc_duty
Waitms 50
End If
End Select
End If
End If
Debounce F_d , 0 , Menu_sub , Sub
Loop
End
Menu_sub:
If Pos = 0 Then
Pos = 1
Locate 1 , 1 : Lcd " "
Locate 2 , 1 : Lcd ">"
Else
Pos = 0
Locate 1 , 1 : Lcd ">"
Locate 2 , 1 : Lcd " "
End If
Return
Przelicz:
Wanted_str = Str(wanted)
Wanted_str = Format(wanted_str , "0.0")
Locate 1 , 10 : Lcd Wanted_str ; " Hz "
Value = Freq 'przepisz Freq procka do zmiennej
Help_d = Wanted
Select Case Help_d
Case 1 To 9
'256
Wartosc_dla_tccr1b = Pwm_presc256
Shift Value , Right , 7
Presc = 255
Case 10 To 152
'64
Wartosc_dla_tccr1b = Pwm_presc64
Shift Value , Right , 6
Presc = 64
Case 153 To 1220 '16000000Hz/131072 = 15Hz '
'8
Wartosc_dla_tccr1b = Pwm_presc8
Shift Value , Right , 3
Presc = 8
Case Else '16000000Hz/131072 = 122Hz
'1
Wartosc_dla_tccr1b = Pwm_presc1
Presc = 1
End Select
Value = Value \ Help_d 'rzeczywistą po preskalerze podziel przez żądaną
Decr Value : Ustaw = Value 'odejmij jeden
Gosub Calc_duty
Icr1 = Ustaw
Tccr1a = Set_output_mode 'set count up, reset count down
Tccr1b = Wartosc_dla_tccr1b 'ustaw prescaler i WGM13
Return
Calc_duty:
Help_d = Value * Proc
Help_d = Help_d / 100
Wypelnienie = Help_d
Ocr1a = Wypelnienie
Locate 2 , 10 : Lcd Proc ; " % "
Return