Laboratoriyaning amaliy qismi.
Modelni yig‘ish uchun bizga quyidagilar kerak:
Arduino platasi
uch rangli LED
Arduino IDE dasturi, uni Arduino veb -saytidan yuklab olish mumkin.
Modelning Arduinoga ulanish sxemasi:
Biz RGB LED anodlarini ulaymiz & nbsp; B portining 1,2,3 satrlariga biz katodlarni minus bilan bog‘laymiz. Turli xil rang palitralarini olish uchun biz anodlarga ma'lum ketma -ketlikda PWM signalini qo‘llaymiz. Bu misolda biz maxsus PWM dasturidan foydalanamiz, garchi Atmega8 -da siz 3 ta kanal uchun PWM apparatini osongina olishingiz mumkin. PWM dasturiy ta'minoti taymerlar / hisoblagichlar etishmasligi va boshqa sabablarga ko‘ra ishlatilishi mumkin. Muayyan chastotali PWMni yaratish uchun biz 8 bitli taymer T0 to‘lg‘azish uzilishidan foydalanamiz (TIMER0_OVF_vect). Prescaler ishlatilmagani uchun taymerning to‘lib ketish chastotasi 31250Hz ga teng bo‘ladi. Va agar "pwm_counter" o‘zgaruvchisi 163 gacha bo‘lsa, PWM chastotasi 190 Gts bo‘ladi. Pwm_r, pwm_g, pwm_b o‘zgaruvchilaridagi qiymatlarga asoslanib, uzilishlar ishlovchisida B portining pinlari almashtiriladi. Rang effektlari LED yorug‘lik vaqti o‘rnatilgan funktsiyalar yordamida konfiguratsiya qilinadi. Sinov dasturida birinchi navbatda qizil, yashil, ko‘k, oq ranglar yonadi, so‘ngra rang o‘tishlari bilan tsikl boshlanadi.
Ushbu model uchun quyidagi dastur mos keladi:
// RGB LEDni boshqarish. PWM uchun dasturiy ta'minot
#include
#include
volatile char pwm_counter,pwm_r,pwm_g,pwm_b;
// T0 to‘lib ketganda uzilish
ISR (TIMER0_OVF_vect)
{
if (pwm_counter++ > 163)
{
PORTB = 0x00;
pwm_counter = 0;
}
if (pwm_counter > pwm_r) PORTB |= (1 << PB1);
if (pwm_counter > pwm_g) PORTB |= (1 << PB2);
if (pwm_counter > pwm_b) PORTB |= (1 << PB3);
}
// Protsedurani mikrosaniyalarda kechiktirish
void delay_us(unsigned char time_us)
{ register unsigned char i;
for(i = 0; i < time_us; i++) // 4 tsikl
{ asm (" PUSH R0 "); // 2 tsikl
asm (" POP R0 "); // 2 tsikl
}
}
// Millisekundlarda protsedura kechikishi
void delay_ms(unsigned int time_ms)
{ register unsigned int i;
for(i = 0; i < time_ms; i++)
{ delay_us(250);
delay_us(250);
delay_us(250);
delay_us(250);
}
}
// qizil rang
void red (unsigned int time)
{
for (char a = 0; a < 165; a++)
{
pwm_r = 164 - a; //oshirish
pwm_g = 164;
pwm_b = 164;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_r = a; //kamayish
pwm_g = 164;
pwm_b = 164;
delay_ms(time);
}
}
// Yashil rang
void green (unsigned int time)
{
for (char a = 0; a < 165; a++)
{
pwm_r = 164;
pwm_g = 164 - a;
pwm_b = 164;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_r = 164;
pwm_g = a;
pwm_b = 164;
delay_ms(time);
}
}
// Ko‘k rang
void blue (unsigned int time)
{
for (char a = 0; a < 165; a++)
{
pwm_r = 164;
pwm_g = 164;
pwm_b = 164 - a;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_r = 164;
pwm_g = 164;
pwm_b = a;
delay_ms(time);
}
}
// Oq rang
void white (unsigned int time)
{
for (char a = 0; a < 165; a++)
{
pwm_r = 164 - a;
pwm_g = 164 - a;
pwm_b = 164 - a;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_r = a;
pwm_g = a;
pwm_b = a;
delay_ms(time);
}
}
// Ranglar almashinuvi
void rgb (unsigned int time)
{
for (char a = 0; a < 165; a++)
{
pwm_r = a;
pwm_b = 164 - a;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_b = a;
pwm_g = 164 - a;
delay_ms(time);
}
for (char a = 0; a < 165; a++)
{
pwm_g = a;
pwm_r = 164 - a;
delay_ms(time);
}
}
int main (void)
{
DDRB |= (1 << PB3)|(1 << PB2)|(1 << PB1); // PB3,2,1 - chiqishlar
TCCR0 |= (1 << CS00); // Oldindan hisoblagichsiz
TIMSK |= (1 << TOIE0); // Haddan tashqari uzilish T0 ni yoqadi
sei();// Global uzilishlarni hal qilish
while(1)
{
red(5);
green(5);
blue(5);
white(10);
for(;;)
{rgb(100);}
}
}
Do'stlaringiz bilan baham: |