Mikrokontrollerga svetofornni dasturlash ; Portlarni belgilash



Yüklə 25,02 Kb.
səhifə1/4
tarix27.09.2023
ölçüsü25,02 Kb.
#149571
  1   2   3   4
Mikrokontrollerga svetofornni dasturlash


; Mikrokontrollerga svetofornni dasturlash

; Portlarni belgilash


PORT equ 0x01 ; Portlar uchun manzil
DDRB equ 0x03 ; Port B to'g'ri yo'naltirish registri

; Konstantalar


RED equ 0x01 ; Qizil rang
GREEN equ 0x02 ; Yashil rang
YELLOW equ 0x03 ; Sariq rang

; Boshlang'ich holat sozlash


RESET:
ldi R16, (1<
out DDRB, R16 ; DDRB registriga qiymat yozish

; Asosiy dastur


MAIN:
; Qizil rangni yoqish
ldi R16, (1<
out PORT, R16 ; Portga qiymat yozish
call DELAY ; kuchli kuchga o'tish uchun kutilayotgan vaqtni kutish

; Qizil va yashil ranglarni bir vaqtning o'zida yoqish


ldi R16, (RED<
out PORT, R16 ; Portga qiymat yozish
call DELAY ; kuchli kuchga o'tish uchun kutilayotgan vaqtni kutish

; Sariq rangni yoqish


ldi R16, (1<
out PORT, R16 ; Portga qiymat yozish
call DELAY ; kuchli kuchga o'tish uchun kutilayotgan vaqtni kutish

; Sariq va yashil ranglarni bir vaqtning o'zida yoqish


ldi R16, (YELLOW<
out PORT, R16 ; Portga qiymat yozish
call DELAY ; kuchli kuchga o'tish uchun kutilayotgan vaqtni kutish

; Yo'qotish


ldi R16, 0x00 ; Port Bning barcha pinlarini o'chirish
out PORT, R16 ; Portga qiymat yozish
call DELAY ; kuchli kuchga o'tish uchun kutilayotgan vaqtni kutish
rjmp MAIN ; Boshqa ranglarga o'tish uchun asosiy dasturga qaytish

; Kuchli kuchga o'tishni kutish funktsiyasi


DELAY:
ldi R17, 0xFF ; Inner loop uchun o'zgaruvchi
OUTER_LOOP:
ldi R18, 0xFF ; Inner loopning boshlang'ich qiymati
INNER_LOOP:
dec R18 ; Inner loopning qiymatini kamaytirish
brne INNER_LOOP ; Inner loopni takrorlash
dec R17 ; Outer loopning qiymatini kamaytirish
brne OUTER_LOOP ; Outer loopni takrorlash
ret
Ushbu kod mikrokontrollerdagi PORTB registri orqali svetofornning turli ranglari uchun yoqish va yo'qotish jarayonini amalga oshiradi. Kodning boshida yordamchi holat sozlanishi va asosiy dastur (MAIN) mavjud. DELAY nomli yordamchi funktsiya vaqtni kutilayotgan kuchli kuchga o'tish uchun ishlatiladi.
Svetofornning har bir rangi uchun kerakli pinlar yonaltiriladi va yoqiladi. Uch to'rtinchi qadam DELAYfunktsiyasi orqali kuchli kuchga o'tish uchun vaqtni kutishingiz mumkin. Ushbu kod mikrokontrollerga svetofornni yoqish, ranglarni almashtirish va yo'qotishning o'zaro mosligini amalga oshiradi. B
// Svetoforni Arduino platformasida C++ tilida dasturlovchi koddan o'tkazamiz

// Pinlar


const int redPin = 9;
const int yellowPin = 10;
const int greenPin = 11;

// Svetofor holatlari


enum TrafficLightState {
RED,
YELLOW,
GREEN
};

// Svetofor obyekti


class TrafficLight {
private:
TrafficLightState currentState;
unsigned long previousMillis;
int interval;

public:
TrafficLight(int _interval) {


currentState = RED;
previousMillis = 0;
interval = _interval;
}

void update() {


unsigned long currentMillis = millis();

switch (currentState) {


case RED:
if (currentMillis - previousMillis >= interval) {
currentState = GREEN;
previousMillis = currentMillis;
}
break;

case YELLOW:


if (currentMillis - previousMillis >= interval) {
currentState = RED;
previousMillis = currentMillis;
}
break;

case GREEN:


if (currentMillis - previousMillis >= interval) {
currentState = YELLOW;
previousMillis = currentMillis;
}
break;
}

// Svetoforni holatga mos ravishda sozlash


switch (currentState) {
case RED:
digitalWrite(redPin, HIGH);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, LOW);
break;

case YELLOW:


digitalWrite(redPin, LOW);
digitalWrite(yellowPin, HIGH);
digitalWrite(greenPin, LOW);
break;

case GREEN:


digitalWrite(redPin, LOW);
digitalWrite(yellowPin, LOW);
digitalWrite(greenPin, HIGH);
break;
}
}
};

// Svetofor obyektini yaratish


TrafficLight trafficLight(5000); // Har bir holat uchun 5 sekundliq davr

// Arduino setup funksiyasi


void setup() {
pinMode(redPin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(greenPin, OUTPUT);
}

// Arduino loop funksiyasi


void loop() {
trafficLight.update();
}

Mikrokontrollerlar uchun C++ tilida svetoforni dasturlovchi koddan o'tkazish haqida qollanma quyidagi ko'rinishda bo'lishi mumkin:




  1. Yüklə 25,02 Kb.

    Dostları ilə paylaş:
  1   2   3   4




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin