logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

Sterowanie diody RGB Atmega32 - programy, rozwązania

WCHPikus 10 Paź 2012 13:57 1530 1
REKLAMA
  • #1 11396982
    WCHPikus
    Poziom 14  
    Posty: 196
    Ocena: 10
    Witam!
    Dopiero zaczynam zabawę w Bascom-ie, wiec proszę o wyrozumiałość.
    Zbudowałem układ na Atmega32, 4 kanały PWM do sterowania RGB+biały.
    Jeden klawisz do zmiany programów, LCD (do sprawdzenia co się dzieje) oraz czujnik oświetlenia na fototranzystorze.

    Co chciałbym zrobić:
    -klawiszem zmieniamy programy (różne kombinacje kolorów)
    -czujnik oświetlenia włącza układ lub reguluje jasnością LED-ów.

    Znalazłem jakiś gotowiec i nie do końca rozumiem jak działa.
    Szczególnie funkcje:
    Szybkość i Status.
    Nie wiem tez jak teraz podłączyć pod to ADC.
    Ogólnie działa, ale nie wiem jak zastosować wartość ADC do PWM..
    ADC w programie jeszcze nie ma.

    Podłączenie:

    ADC portA.0
    Klawisz portB.7
    LCD widać, diody tez są ok.

    Do tego jeszcze na LCD wartości Dana_x, jeśli są mniejsze od 0 wyświetlane są w postaci XX0, np 28 - 280, 9 - 900.
    Jak wykasować te 0-ra?

    Kod:

    $regfile = "m32def.dat"
    $crystal = 1000000

    Config Lcd = 16 * 2
    Config Lcdpin = Pin , Db4 = Portc.7 , Db5 = Portc.6 , Db6 = Portc.5 , Db7 = Portc.4 , E = Portc.2 , Rs = Portc.3
    Config Lcdmode = Port
    Initlcd
    Cls

    Config Timer0 = Pwm , Prescale = 8 , Compare = Toggle , Pwm = On , , Compare Pwm = Clear Down
    Config Timer1 = Pwm , Pwm = 8 , Compare A Pwm = Clear Down , Compare B Pwm = Clear Down , Prescale = 8
    Config Timer2 = Pwm , Prescale = 8 , Compare = Toggle , Pwm = On , Compare Pwm = Clear Down
    Stop Timer0

    On Timer0 Przerw_timer0
    On Timer1 Przerw_timer0
    On Timer2 Przerw_timer0

    'Konfiguracja portow /0-input /1-output
    Config Pinb.3 = Output
    Config Pind.5 = Output
    Config Pind.4 = Output
    Config Pind.7 = Output
    Config Portc = Output

    Config Portc.2 = Output
    Config Portc.3 = Output
    Config Portc.4 = Output
    Config Portc.5 = Output
    Config Portc.6 = Output
    Config Portc.7 = Output

    Config Portb.7 = Input 'konfiguracja przycisku
    Sw Alias Pinb.7
    Portb.7 = 1

    Dim Status As Byte
    Dim Program As Byte
    Dim Dana_a As Byte
    Dim Dana_b As Byte
    Dim Dana_c As Byte
    Dim Dana_d As Byte
    Dim Dana_ost_a As Byte
    Dim Dana_ost_b As Byte
    Dim Dana_ost_c As Byte
    Dim Dana_ost_d As Byte
    Dim Licznik As Byte
    Dim Szybkosc As Byte

    Pwm1a = 0
    Pwm1b = 0
    Pwm0 = 0
    Ocr2 = 0

    Status = 0
    Program = 0
    Dana_a = 0
    Dana_b = 0
    Dana_c = 0
    Dana_d = 0
    Dana_ost_a = 0
    Dana_ost_b = 0
    Licznik = 250
    Szybkosc = 40 'okresl szybkosc zmiany wyswietlania

    Restore Program1

    Enable Timer0
    Enable Timer1
    Enable Timer2

    Start Timer0
    Start Timer1
    Start Timer2
    Enable Interrupts


    Do

    Cursor Off Noblink
    Locate 1 , 1
    Lcd "prog. " ; Program
    Locate 1 , 10
    Lcd "G"
    Locate 1 , 12
    Lcd "B"
    Locate 1 , 14
    Lcd "W"
    Locate 1 , 16
    Lcd "R"

    Locate 2 , 1
    Lcd ; Dana_a

    'green
    Locate 2 , 5
    Lcd ; Dana_b

    Locate 2 , 9
    Lcd ; Dana_c

    'white
    Locate 2 , 13
    Lcd ; Dana_d 'red


    Loop





    Przerw_timer0:

    If Licznik > Szybkosc Then
    Licznik = 0

    If Sw = 1 And Status = 50 Then
    Status = 100
    Incr Program
    If Program = 8 Then Program = 0 'ilosc programow
    End If
    If Sw = 0 And Status = 0 Then Status = 50 ' do czego jest status? byl 50

    If Dana_a = Dana_ost_a And Dana_b = Dana_ost_b And Dana_c = Dana_ost_c And Dana_d = Dana_ost_d Then
    Read Dana_a
    Read Dana_b
    Read Dana_c
    Read Dana_d
    End If

    If Dana_a = 255 And Dana_b = 255 And Dana_c = 255 And Dana_d = 255 Or Status = 100 Then 'status byl 100
    Status = 0
    Select Case Program
    Case 0 : Restore Program1
    Case 1 : Restore Program2
    Case 2 : Restore Program3
    Case 3 : Restore Program3

    End Select
    Read Dana_a
    Read Dana_b
    Read Dana_c
    Read Dana_d
    End If

    If Dana_a > Dana_ost_a Then Incr Dana_ost_a
    If Dana_a < Dana_ost_a Then Decr Dana_ost_a
    If Dana_b > Dana_ost_b Then Incr Dana_ost_b
    If Dana_b < Dana_ost_b Then Decr Dana_ost_b
    If Dana_c > Dana_ost_c Then Incr Dana_ost_c
    If Dana_c < Dana_ost_c Then Decr Dana_ost_c
    If Dana_d > Dana_ost_d Then Incr Dana_ost_d
    If Dana_d < Dana_ost_d Then Decr Dana_ost_d
    Pwm1a = Dana_ost_a
    Pwm1b = Dana_ost_b
    Pwm0 = Dana_ost_c
    Ocr2 = Dana_ost_d

    End If
    Incr Licznik

    Return

    'w liniach data dane musza byc parzyste, tzn sa zawsze podane wartosci dla 2 kanalow pwm
    'program swietlny musi sie zawsze konczyc wartosciami 255,255
    'jesli chcesz wlaczyc led na max to uzywaj wartosci max np 250

    Program1:

    'Data y , b , w , r
    Data 0 , 0 , 0 , 250 , 0 , 0 , 250 , 0 , 0 , 250 , 0 , 0 , 250 , 0 , 0 , 0 , 250 , 200 , 150 , 100 , 0 , 50 , 10 , 60

    Program2:
    Data 0 , 20 , 0 , 0 , 250 , 255 , 255 , 255

    Program3:
    Data , 0 , 10 , 10 , 80 , 40 , 40 , 150 , 150 , 30 , 70 , 50 , 255 , 255 , 255 , 255 , 255

    Program4:
    Data 0 , 30 , 0 , 30 , 0 , 30 , 0 , 30 , 0 , 30 , 90 , 150 , 30 , 200 , 70 , 50 , 100 , 170 , 50 , 60 , 255 , 255 , 255 , 255

    Program5:
    Data 10 , 10 , 10 , 10 , 10 , 20 , 30 , 40 , 50 , 60 , 70 , 80 , 100 , 150 , 200 , 255 , 255 , 255 , 255 , 255


    Program6:
    Data 0 , 0 , 0 , 0 , 255 , 0 , 255 , 0 , 0 , 255 , 0 , 255 , 100 , 200 , 50 , 100 , 255 , 255 , 255 , 255


    Program7:
    Data 10 , 50 , 100 , 200 , 255 , 255 , 255 , 255

    Proszę zapoznać z zasadami obowiązującymi w naszym dziale i dostosować do nich zawartość tematu.[zumek]
  • REKLAMA
  • #2 11405597
    zbychmg
    Poziom 31  
    Posty: 1169
    Pomógł: 183
    Ocena: 159
    Możesz dodać linię Lcd space(16) kasującą drugą linię


    Locate 2 , 1 : Lcd " " ' space(16)
    ..........................................................................

    lub dodać 2 spacje za Daną

    Locate 2 , 1: Lcd Dana_a; " " 'green
    Locate 2 , 5: Lcd Dana_b; " " 'blue
    Locate 2 , 9: Lcd ; Dana_c " " 'white
    Locate 2 , 13 Lcd ; Dana_d " " 'red
REKLAMA