http://www.nxp.com/documents/data_sheet/PCA9551.pdf
Wbudowane timery pozwalają na efektywniejsze wykorzystanie magistrali.
Uwaga Biblioteka przygotowana do używania w tzw. Project mode wraz z Config Submode = New.
Jak używać ??
W pliku main.bas wklejamy naszą nazwę wraz z nazwą folderu zamkniętą w cudzysłów, zaraz po ustawieniach portów, deklaracji zmiennych globalnych, stałych itp.
W moim przypadku folder nazywał się Subs.
$include "Subs\Sub Pca9551_led.inc"
$include "Subs\function Pca9551_get_PWM_0_1_dutycy.inc"
$include "Subs\Function Pca9551_get_PWM_0_1_Freq.inc"
Teraz aby włączyć np. led number 6 piszemy:
Call Pca9551_ledv(6 , 1) 'turn led ON
Wyłaczyć Led 6:
Call Pca9551_ledv2(6 , 0) 'turn led Off
Jeśli chcemy wybrać tryb PWM_0:
Call Pca9551_ledv2(6 , 2) 'led6 PWM0
Jelsi chcemy wybrać PWM_1
Call Pca9551_ledv2(6 , 3) 'led6 PWM1
W podobny sposób ustawiamy wypełnienie i częstotliwość:
Call Pca9551_pwm(pwm_mode 0 lub 1 , Freq 0 do 255, Duty 0 do 255)
Wybór led oraz trybu pracy :
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $nocompile
- '*****************************************************************************************
- '**********PCA9551 ver 1.0 by Pawel Labedz aka Pikczu 2014*******************************
- '*********************Pawel.labedz@gmail.com**********************************************
- '*****************************************************************************************
- '-----------------------------------------------------------------------------------------
- 'Name to include: "Sub Pca9551_led.inc"
- 'Syntax: Call Pca9551_ledv2(led number , mode 0 or 1 or 2 or 3 )
- 'Make sure you add constant in to main.bas
- 'Const Led0_on = &B11111100
- 'Const Led0_off = &B00000001
- 'Const Led0_pwm0 = &B00000010 ' make sure you send first off mask then
- 'Const Led0_pwm1 = &B00000011 'use OR after default condition 01010101 to change to PWM1
- 'Const Led1_on = &B11110011
- 'Const Led1_off = &B00000100
- 'Const Led1_pwm0 = &B00001000
- 'Const Led1_pwm1 = &B00001100 'use OR after default condition 01010101 to change to PWM1
- 'Const Led2_on = &B11001111
- 'Const Led2_off = &B00010000
- 'Const Led2_pwm0 = &B00100000
- 'Const Led2_pwm1 = &B00110000 'use OR after default condition 01010101 to change to PWM1
- 'Const Led3_on = &B00111111
- 'Const Led3_off = &B01000000
- 'Const Led3_pwm0 = &B10000000
- 'Const Led3_pwm1 = &B11000000 'use OR after default condition 01010101 to change to PWM1
- 'Const Led4_on = &B11111100
- 'Const Led4_off = &B00000001
- 'Const Led4_pwm0 = &B00000010
- 'Const Led4_pwm1 = &B00000011 'use OR after default condition 01010101 to change to PWM1
- 'Const Led5_on = &B11110011
- 'Const Led5_off = &B00000100
- 'Const Led5_pwm0 = &B00001000
- 'Const Led5_pwm1 = &B00001100 'use OR after default condition 01010101 to change to PWM1
- 'Const Led6_on = &B11001111
- 'Const Led6_off = &B00010000
- 'Const Led6_pwm0 = &B00100000
- 'Const Led6_pwm1 = &B00110000 'use OR after default condition 01010101 to change to PWM1
- 'Const Led7_on = &B00111111
- 'Const Led7_off = &B01000000
- 'Const Led7_pwm0 = &B01000000
- 'Const Led7_pwm1 = &B11000000 'use OR after default condition 01010101 to change to PWM1
- 'Const 0to3led_off = &B01010101
- 'Const 0to3led_on = &B00000000
- 'Const 4to7led_off = &B01010101
- 'Const 4to7led_on = &B00000000
- '-----------------------------------------------------------------------------------------
- 'PCA9551 Led driver with PWM
- Sub Pca9551_led(byval Led As Byte , Byval Led_mode As Byte )
- Dbg
- 'Const Pca9551rd = 193
- 'Const Pca9551wr = 192
- Local Bufor_1 As Byte
- Local Bufor_2 As Byte
- Bufor_1 = 0
- Bufor_2 = 0
- I2cstart
- I2cwbyte Pca9551wr
- I2cwbyte &B00010101
- I2crepstart
- I2cwbyte Pca9551rd
- I2crbyte Bufor_1 , Ack 'led 0 to 3
- I2crbyte Bufor_2 , Nack 'led 4 to 7
- I2cstop
- I2cstart
- I2cwbyte Pca9551wr
- If Led > 3 Then
- I2cwbyte 6
- Else
- I2cwbyte 5
- End If
- Select Case Led
- Case 0
- Select Case Led_mode
- Case 0 ' led0_off
- Bufor_1 = Bufor_1 And Led0_on 'AND zerowanie
- Bufor_1 = Bufor_1 Or Led0_off ' OR ustwianie
- Case 1 'led0_on
- Bufor_1 = Bufor_1 And Led0_on 'AND zerowanie
- Case 2 'led0 PWM0
- Bufor_1 = Bufor_1 And Led0_on
- Bufor_1 = Bufor_1 Or Led0_pwm0
- Case 3 'led0 pwm1
- Bufor_1 = Bufor_1 Or Led0_pwm1
- End Select
- Case 1 'led 1
- Select Case Led_mode
- Case 0 ' led1_off
- Bufor_1 = Bufor_1 And Led1_on 'AND zerowanie
- Bufor_1 = Bufor_1 Or Led1_off ' OR ustwianie
- Case 1 'led1_on
- Bufor_1 = Bufor_1 And Led1_on 'AND zerowanie
- Case 2 'led1 PWM0
- Bufor_1 = Bufor_1 And Led1_on
- Bufor_1 = Bufor_1 Or Led1_pwm0
- Case 3 'led1 pwm1
- Bufor_1 = Bufor_1 Or Led1_pwm1
- End Select
- Case 2 'led 2
- Select Case Led_mode
- Case 0 ' led2_off
- Bufor_1 = Bufor_1 And Led2_on 'AND zerowanie
- Bufor_1 = Bufor_1 Or Led2_off ' OR ustwianie
- Case 1 'led2_on
- Bufor_1 = Bufor_1 And Led2_on 'AND zerowanie
- Case 2 'led2 PWM0
- Bufor_1 = Bufor_1 And Led2_on
- Bufor_1 = Bufor_1 Or Led2_pwm0
- Case 3 'led2 pwm1
- Bufor_1 = Bufor_1 Or Led2_pwm1
- End Select
- Case 3 'led3
- Select Case Led_mode
- Case 0 ' led3_off
- Bufor_1 = Bufor_1 And Led3_on 'AND zerowanie
- Bufor_1 = Bufor_1 Or Led3_off ' OR ustwianie
- Case 1 'led3_on
- Bufor_1 = Bufor_1 And Led3_on 'AND zerowanie
- Case 2 'led3 PWM0
- Bufor_1 = Bufor_1 And Led3_on
- Bufor_1 = Bufor_1 Or Led3_pwm0
- Case 3 'led3 pwm1
- Bufor_1 = Bufor_1 Or Led3_pwm1
- End Select
- Case 4 'led 4
- Select Case Led_mode
- Case 0 ' led4_off
- Bufor_2 = Bufor_2 And Led4_on 'AND zerowanie
- Bufor_2 = Bufor_2 Or Led4_off ' OR ustwianie
- Case 1 'led4_on
- Bufor_2 = Bufor_2 And Led4_on 'AND zerowanie
- Case 2 'led4 PWM0
- Bufor_2 = Bufor_2 And Led4_on
- Bufor_2 = Bufor_2 Or Led4_pwm0
- Case 3 'led4 pwm1
- Bufor_2 = Bufor_2 Or Led4_pwm1
- End Select
- Case 5 'led 5
- Select Case Led_mode
- Case 0 ' led5_off
- Bufor_2 = Bufor_2 And Led5_on 'AND zerowanie
- Bufor_2 = Bufor_2 Or Led5_off ' OR ustwianie
- Case 1 'led5_on
- Bufor_2 = Bufor_2 And Led5_on 'AND zerowanie
- Case 2 'led5 PWM0
- Bufor_2 = Bufor_2 And Led5_on
- Bufor_2 = Bufor_2 Or Led5_pwm0
- Case 3 'led3 pwm1
- Bufor_2 = Bufor_2 Or Led5_pwm1
- End Select
- Case 6 'led 6
- Select Case Led_mode
- Case 0 ' led6_off
- Bufor_2 = Bufor_2 And Led6_on 'AND zerowanie
- Bufor_2 = Bufor_2 Or Led6_off ' OR ustwianie
- Case 1 'led6_on
- Bufor_2 = Bufor_2 And Led6_on 'AND zerowanie
- Case 2 'led6 PWM0
- Bufor_2 = Bufor_2 And Led6_on
- Bufor_2 = Bufor_2 Or Led6_pwm0
- Case 3 'led6 pwm1
- Bufor_2 = Bufor_2 Or Led6_pwm1
- End Select
- Case 7 'led 7
- Select Case Led_mode
- Case 0 ' led7_off
- Bufor_2 = Bufor_2 And Led7_on 'AND zerowanie
- Bufor_2 = Bufor_2 Or Led7_off ' OR ustwianie
- Case 1 'led7_on
- Bufor_2 = Bufor_2 And Led7_on 'AND zerowanie
- Case 2 'led7 PWM0
- Bufor_2 = Bufor_2 And Led7_on
- Bufor_2 = Bufor_2 Or Led7_pwm0
- Case 3 'led7 pwm1
- Bufor_2 = Bufor_2 Or Led7_pwm1
- End Select
- End Select
- If Led > 3 Then
- I2cwbyte Bufor_2
- Else
- I2cwbyte Bufor_1
- End If
- I2cstop
- End Sub
- '-----------------------------------------------------------------------------------------
Konfiguracja PWM-ów
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $nocompile
- '*****************************************************************************************
- '**********PCA9551 ver 1.0 by Pawel Labedz aka Pikczu 2014*******************************
- '*********************Pawel.labedz@gmail.com**********************************************
- '*****************************************************************************************
- '-----------------------------------------------------------------------------------------
- ' Name to include : "Sub pca9551_pwm.inc"
- 'syntax: PCA9551_PWM(0 or 1 , 0 to 255, 0 to 255)
- Sub Pca9551_pwm(byval Pwm_0_1 As Byte , Byval Freq_pres As Byte , Byval Duty_cyc As Byte)
- Dbg
- If Pwm_0_1 > 1 Then Pwm_0_1 = 1
- I2cstart
- I2cwbyte Pca9551wr
- Select Case Pwm_0_1
- Case 0
- I2cwbyte &B00010001 '10001 sub adres plus auto increment bit 4
- I2cwbyte Freq_pres
- I2cwbyte Duty_cyc
- Case 1
- I2cwbyte &B00010011 '10001 sub adres plus auto increment bit 4
- I2cwbyte Freq_pres
- I2cwbyte Duty_cyc
- End Select
- I2cstop
- End Sub
Funkcja zwracająca wartość wypełnienia PWM:
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $nocompile
- '*****************************************************************************************
- '**********PCA9551 ver 1.0 by Pawel Labedz aka Pikczu 2014*******************************
- '*********************Pawel.labedz@gmail.com**********************************************
- '*****************************************************************************************
- 'name to include : "function Pca9551_get_PWM_0_1_dutycy.inc"
- 'syntax: yourvariable= Pca9551_get_pwm_0_1_dutycy(0 or 1)
- '-----------------------------------------------------------------------------------------
- 'PCA9551 Led driver with PWM reading dutycycle for pwm 0 or 1
- Function Pca9551_get_pwm_0_1_dutycy(byval Pwms As Byte ) As Byte
- Dbg
- 'Const Pca9551rd = 193'this is address
- 'Const Pca9551wr = 192' this is address setup in main code
- Local Bufor_1 As Byte
- 'Local Bufor_2 As Byte
- Bufor_1 = 0
- 'Bufor_2 = 0
- If Pwms > 1 Then Pwms = 1
- I2cstart
- I2cwbyte Pca9551wr
- Select Case Pwms
- Case 0
- I2cwbyte &B00000010
- I2crepstart
- I2cwbyte Pca9551rd
- I2crbyte Bufor_1 , Nack 'led 0 to 3
- Case 1
- I2cwbyte &B00000100
- I2crepstart
- I2cwbyte Pca9551rd
- I2crbyte Bufor_1 , Nack 'led 0 to 3
- End Select
- I2cstop
- Pca9551_get_pwm_0_1_dutycy = Bufor_1
- End Function
Funkcja zwracająca wartość częstotliwości:
Code: [Zaznacz cały] [Rozwiń/Zwiń]
- $nocompile
- '*****************************************************************************************
- '**********PCA9551 ver 1.0 by Pawel Labedz aka Pikczu 2014*******************************
- '*********************Pawel.labedz@gmail.com**********************************************
- '*****************************************************************************************
- '-----------------------------------------------------------------------------------------
- 'name to include : "function Pca9551_get_PWM_0_1_Freq.inc"
- ' syntax: yourvariable=Pca9551_get_pwm_0_1_freq(0 or 1 )
- '-----------------------------------------------------------------------------------------
- 'PCA9551 Led driver with PWM reading frequency for fwm 0 or 1
- Function Pca9551_get_pwm_0_1_freq(byval Pwm_number As Byte ) As Byte
- Dbg
- 'Const Pca9551rd = 193'this is address
- 'Const Pca9551wr = 192' this is address setup in main code
- Local Bufor_1 As Byte
- 'Local Bufor_2 As Byte
- Bufor_1 = 0
- 'Bufor_2 = 0
- If Pwm_number > 1 Then Pwm_number = 1
- I2cstart
- I2cwbyte Pca9551wr
- Select Case Pwm_number
- Case 0
- I2cwbyte &B00000001
- I2crepstart
- I2cwbyte Pca9551rd
- I2crbyte Bufor_1 , Nack 'led 0 to 3
- Case 1
- I2cwbyte &B00000011
- I2crepstart
- I2cwbyte Pca9551rd
- I2crbyte Bufor_1 , Nack 'led 0 to 3
- End Select
- I2cstop
- Pca9551_get_pwm_0_1_freq = Bufor_1
- End Function
Edit------------------------- poprawiłem wyświetlanie bo się wszystko rozjechało.
Pikczu