Operácie

Ovládanie RGB LED pásika

Zo stránky SensorWiki

Záverečný projekt predmetu MIPS / LS2023 - Dominik Illés


Zadanie

Ovládanie RGB led pásika pomocou jednoduchého menu v serial linke.

Arduino Nano.

Literatúra:


Analýza a opis riešenia

Zapojenie LED pasíka je jednoduché len tri vodiče a v data vstupom som dal resistor, aby náhodne veľké prúdy nepoškodili pása. Najťažšia časť bola nastavid pás aby poslané farby vykresloval, na to som použil asembler kód čo som našiel na internete. Na ovládanie som napísal malú menu s tromi zvláštnymi módmi, používaním numpadu môžme navigovať cez módy prednastavené, manuálna nastavenia celého pása a manuálna nastavenia hocijakeho LEDa. Aby nevykreslilo vždy pod sebou text a vyzeralo lepšie som použil printf("\033[2J"); čo vymaže čo je na terminále a printf("\033[H"); aby kurzor bolo na začiatku.


Schéma zapojenia


Algoritmus a program

.

int main(void)
{
	char ReceivedChar;
	uart_init();
	stdout = &mystdout;
	uint16_t time = 0;
	int m = 0;	
		
while(1)
	{
		if(m==0){
		printf("Menu\n 1.Nastavene efeky\n 2.Nastavit cely pas\n 3.Nastavit LEDky");
		ReceivedChar = uart_getc();
		m=1;
		printf("\033[2J");
		printf("\033[H");
		}
//------------------------------------Predpisane effekty----------------------------------------------------------------
		if(ReceivedChar == '1')
		{
		//--------------------------------Effekty-----------------------------
		printf("Effekty\n 0.Vypnut\n 1.Red\n 2.Green\n 3.Blue\n 4.Rainbow-1\n 5.Rinbow-2\n 6.Breathing\n 7.Spat");
		while(m==1)
		{
		ReceivedChar = uart_getc();	
		//------------------------------RED---------------------------------
		if ( ReceivedChar == '1' )   
       {
			RedStrip();						         
	   }
	   //------------------------------Green---------------------------------	                     
		if (ReceivedChar == '2')                          
       {
			GreenStrip();
	   }     
	    //------------------------------Blue--------------------------------                            
		if (ReceivedChar == '3')                          
       {
			BlueStrip();
	   }   
	   //------------------------------Vypnut--------------------------------
	   	if (ReceivedChar == '0')                          
       {
			OffStrip();
	   }
//------------------------------------RainbowV1---------------------------------
	   if (ReceivedChar == '4')                          
       {
		Rainbow1();
//-------------------------------------Rainbow-----------------------------------	
	   }		
	if (ReceivedChar == '5') {
		Rainbow2();
	}
		//----------------------------Breathing------------------------------------
	   if (ReceivedChar == '6')                          
       {
		Breathing();
		}
//---------------------------------------Spat---------------------------------------				
		if (ReceivedChar == '7')                          
       {
		m=0;
		printf("\033[2J");
		printf("\033[H");
			 }		
		}   		
	}
//-------------------------------Nastavenie celej pasy-------------------------------------------------------
		if(ReceivedChar == '2')
		{
			fullLED(&m);		
		}
//-------------------------------Nastavenie zvlast ledky----------------------------------------------------------
		if(ReceivedChar == '3')
		{
			pieceLED(&m);
		}	   
	}

	return(0);
}
void __attribute__((noinline)) led_strip_write(rgb_color * colors, uint16_t count)
{
  // Set the pin to be an output driving low.
  LED_STRIP_PORT &= ~(1<<LED_STRIP_PIN);
  LED_STRIP_DDR |= (1<<LED_STRIP_PIN);

  cli();   // Disable interrupts temporarily because we don't want our pulse timing to be messed up.
  while (count--)
  {
    // Send a color to the LED strip.
    // The assembly below also increments the 'colors' pointer,
    // it will be pointing to the next color at the end of this loop.
    asm volatile (
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0\n"
        "rcall send_led_strip_byte%=\n"  // Send red component.
        "ld __tmp_reg__, -%a0\n"
        "rcall send_led_strip_byte%=\n"  // Send green component.
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0+\n"
        "ld __tmp_reg__, %a0+\n"
        "rcall send_led_strip_byte%=\n"  // Send blue component.
        "rjmp led_strip_asm_end%=\n"     // Jump past the assembly subroutines.

        // send_led_strip_byte subroutine:  Sends a byte to the LED strip.
        "send_led_strip_byte%=:\n"
        "rcall send_led_strip_bit%=\n"  // Send most-significant bit (bit 7).
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"
        "rcall send_led_strip_bit%=\n"  // Send least-significant bit (bit 0).
        "ret\n"

        // send_led_strip_bit subroutine:  Sends single bit to the LED strip by driving the data line
        // high for some time.  The amount of time the line is high depends on whether the bit is 0 or 1,
        // but this function always takes the same time (2 us).
        "send_led_strip_bit%=:\n"
#if F_CPU == 8000000
        "rol __tmp_reg__\n"                      // Rotate left through carry.
#endif
        "sbi %2, %3\n"                           // Drive the line high.

#if F_CPU != 8000000
        "rol __tmp_reg__\n"                      // Rotate left through carry.
#endif

#if F_CPU == 16000000
        "nop\n" "nop\n"
#elif F_CPU == 20000000
        "nop\n" "nop\n" "nop\n" "nop\n"
#elif F_CPU != 8000000
#error "Unsupported F_CPU"
#endif

        "brcs .+2\n" "cbi %2, %3\n"              // If the bit to send is 0, drive the line low now.

#if F_CPU == 8000000
        "nop\n" "nop\n"
#elif F_CPU == 16000000
        "nop\n" "nop\n" "nop\n" "nop\n" "nop\n"
#elif F_CPU == 20000000
        "nop\n" "nop\n" "nop\n" "nop\n" "nop\n"
        "nop\n" "nop\n"
#endif

        "brcc .+2\n" "cbi %2, %3\n"              // If the bit to send is 1, drive the line low now.

        "ret\n"
        "led_strip_asm_end%=: "
        : "=b" (colors)
        : "0" (colors),         // %a0 points to the next color to display
          "I" (_SFR_IO_ADDR(LED_STRIP_PORT)),   // %2 is the port register (e.g. PORTC)
          "I" (LED_STRIP_PIN)     // %3 is the pin number (0-8)
    );

    // Uncomment the line below to temporarily enable interrupts between each color.
    //sei(); asm volatile("nop\n"); cli();
  }
  sei();          // Re-enable interrupts now that we are done.
  _delay_us(80);  // Send the reset signal.
}
#define F_CPU 16000000
#define BAUD 9600
#include <avr/io.h>
#include <util/setbaud.h>

void uart_init( void ) 
{
    UBRR0H = UBRRH_VALUE;
    UBRR0L = UBRRL_VALUE;

#if USE_2X
    UCSR0A |= _BV(U2X0);
#else
    UCSR0A &= ~(_BV(U2X0));
#endif

    UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
    UCSR0B = _BV(RXEN0) | _BV(TXEN0);   /* Enable RX and TX */
}

//------------------------------------Uart Prikazy-------------------------------------------------
void uart_putc(char c) 
{
   if (c == '\n') 
    {
       uart_putc('\r');
    }
   loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
   UDR0 = c;
}

void uart_puts(const char *str) {
    // Transmit a null-terminated string via UART
    while (*str) {
        // Wait for the UART data register to be ready for transmission
        while (!(UCSR0A & (1 << UDRE0)));
        // Transmit the character
        UDR0 = *str++;
    }
}

char uart_getc(void) {
    loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
    return UDR0;
}

void uart_gets(char *buffer, uint8_t max_length) {
    uint8_t count = 0;
    char received_char;

    while (1) {
        received_char = uart_getc();

        // Check for newline or carriage return
        if (received_char == '\n' || received_char == '\r') {
            break;
        }

        // Append received character to the buffer
        if (count < max_length - 1) {
            buffer[count++] = received_char;
        }
    }

    // Null-terminate the buffer
    buffer[count] = '\0';
}
// Function to convert HSV to RGB
rgb_color hsv_to_rgb(uint8_t hue) {
    uint8_t sector = hue / 42; // Divide the hue into 6 sectors (42 degrees each)
    uint8_t remainder = (hue % 42) * 6; // Get the remainder and scale it to fit in a 8-bit range

    uint8_t red = 0, green = 0, blue = 0;

    switch (sector) {
        case 0:
            red = 255;
            green = remainder;
            break;
        case 1:
            red = 255 - remainder;
            green = 255;
            break;
        case 2:
            green = 255;
            blue = remainder;
            break;
        case 3:
            green = 255 - remainder;
            blue = 255;
            break;
        case 4:
            red = remainder;
            blue = 255;
            break;
        case 5:
            red = 255;
            blue = 255 - remainder;
            break;
    }

    rgb_color rgb = { red, green, blue };
    return rgb;
}

Funkcia led_strip_write používa C a AVR assembler kód na nastavenie LED pasíka a na to aby vykreslilo jednotlivé farby a effekty. Assembler kód je na odosielanie jedného bájtu do LED pásu(jednotlivé farebé zložky). Assembly kód sa zabezpečí, že načasovanie prenosu bitov je presné a konzistentné, bez ohľadu na frekvenciu mikroprocesora. Kód používa na dosiahnutie tohto cieľa kombináciu inštrukcií nop (bez operácie) a inštrukcií podmienenej vetvy.

Zdrojový kód: LEDovlad.c


Overenie

Aplikácia.

Video:

Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.