#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include <string.h>
#define OC1A PB5
#define OC3A PE3
#define OC4A PH3
volatile uint16_t redValue = 0;
volatile uint16_t greenValue = 0;
volatile uint16_t blueValue = 0;
void port_ini(void)
{
PORTB = 0x00; // Clear PORTB
DDRB = (1<<OC1A); // Set PB5 as output
DDRE = (1<<OC3A); // Set PE3 as output
DDRH = (1<<OC4A); // Set PH3 as output
}
void pwm_timer_init(void)
{
// Timer 1 configuration for red channel
TCCR1A = (1 << COM1A1) | (0 << COM1A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM1B1) | (0 << COM1B0) /* Normal port operation, OCB disconnected */
| (1 << WGM11) | (1 << WGM10); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR1B = (0 << WGM13) | (1 << WGM12) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC1 /* Input Capture Noise Canceler: disabled */
| 0 << ICES1 /* Input Capture Edge Select: disabled */
| (0 << CS12) | (1 << CS11) | (1 << CS10); /* IO clock divided by 64 */
OCR1A = 1; /* Output compare A: 0x64 */
TIMSK1 = 0 << OCIE1B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE1A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE1 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE1; /* Overflow Interrupt Enable: enabled */
// Timer 3 configuration for green channel
TCCR3A = (1 << COM3A1) | (0 << COM3A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM3B1) | (0 << COM3B0) /* Normal port operation, OCB disconnected */
| (1 << WGM31) | (1 << WGM30); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR3B = (0 << WGM33) | (1 << WGM32) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC3 /* Input Capture Noise Canceler: disabled */
| 0 << ICES3 /* Input Capture Edge Select: disabled */
| (0 << CS32) | (1 << CS31) | (1 << CS30); /* IO clock divided by 64 */
OCR3A = 1; /* Output compare A: 0x64 */
TIMSK3 = 0 << OCIE3B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE3A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE3 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE3; /* Overflow Interrupt Enable: enabled */
// Timer 4 configuration for blue channel
TCCR4A = (1 << COM4A1) | (0 << COM4A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM4B1) | (0 << COM4B0) /* Normal port operation, OCB disconnected */
| (1 << WGM41) | (1 << WGM40); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR4B = (0 << WGM43) | (1 << WGM42) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC4 /* Input Capture Noise Canceler: disabled */
| 0 << ICES4 /* Input Capture Edge Select: disabled */
| (0 << CS42) | (1 << CS41) | (1 << CS40); /* IO clock divided by 64 */
OCR4A = 1; /* Output compare A: 0x64 */
TIMSK4 = 0 << OCIE4B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE4A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE4 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE4; /* Overflow Interrupt Enable: enabled */
}
void setColor(const char *color)
{
long number = strtol(color, NULL, 16);
redValue = ((number >> 16) & 0xFF) * 1023 / 255;
greenValue = ((number >> 8) & 0xFF) * 1023 / 255;
blueValue = (number & 0xFF) * 1023 / 255;
OCR1A = redValue;
OCR3A = greenValue;
OCR4A = blueValue;
if (OCR1A == 0)
OCR1A = 1; // Set OCR1A to 1 if it is 0
if (OCR3A == 0)
OCR3A = 1; // Set OCR3A to 1 if it is 0
if (OCR4A == 0)
OCR4A = 1; // Set OCR4A to 1 if it is 0
}
void setup()
{
sei();
port_ini(); // Initialize ports
pwm_timer_init(); // Initialize PWM timer
}
void loop()
{
char buffer[7]; // Buffer to hold the incoming color code
int index = 0;
while (1)
{
char c = getchar(); // Read a character from the serial port
if (c == '\n' || c == '\r')
{
buffer[index] = '\0'; // Null-terminate the string
setColor(buffer); // Set the color based on the received code
index = 0; // Reset the index for the next input
}
else
{
buffer[index++] = c; // Add the character to the buffer
}
}
}
ISR(TIMER1_OVF_vect)
{
// Handle Timer 1 overflow interrupt
}
ISR(TIMER1_COMPA_vect)
{
// Handle Timer 1 compare match A interrupt
}
ISR(TIMER3_OVF_vect)
{
// Handle Timer 3 overflow interrupt
}
ISR(TIMER3_COMPA_vect)
{
// Handle Timer 3 compare match A interrupt
}
ISR(TIMER4_OVF_vect)
{
// Handle Timer 4 overflow interrupt
}
ISR(TIMER4_COMPA_vect)
{
// Handle Timer 4 compare match A interrupt
}
int main(void)
{
setup();
loop();
return 0;
}
#define OC1A PB5
// #define OC3A PB6
// #define OC4A PB7
#define OC3A PE3//
#define OC4A PH3//RED
volatile uint16_t redValue = 0;
volatile uint16_t greenValue = 0;
volatile uint16_t blueValue = 0;
void port_ini(void)
{
PORTB = 0x00; // Clear PORTB
DDRB = (1<<OC1A) | (1<<OC3A) | (1<<OC4A); // Set PB5, PB6, PB7 as output (assuming these are the PWM output pins)
DDRE=(1<<OC3A);
DDRH=(1<<OC4A);
Serial.begin(115200);
}
void pwm_timer_init(void)
{
// Timer 1 configuration for red channel
TCCR1A = (1 << COM1A1) | (0 << COM1A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM1B1) | (0 << COM1B0) /* Normal port operation, OCB disconnected */
| (1 << WGM11) | (1 << WGM10); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR1B = (0 << WGM13) | (1 << WGM12) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC1 /* Input Capture Noise Canceler: disabled */
| 0 << ICES1 /* Input Capture Edge Select: disabled */
| (0 << CS12) | (1 << CS11) | (1 << CS10); /* IO clock divided by 64 */
OCR1A = 1; /* Output compare A: 0x64 */
TIMSK1 = 0 << OCIE1B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE1A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE1 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE1; /* Overflow Interrupt Enable: enabled */
// Timer 3 configuration for green channel
TCCR3A = (1 << COM3A1) | (0 << COM3A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM3B1) | (0 << COM3B0) /* Normal port operation, OCB disconnected */
| (1 << WGM31) | (1 << WGM30); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR3B = (0 << WGM33) | (1 << WGM32) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC3 /* Input Capture Noise Canceler: disabled */
| 0 << ICES3 /* Input Capture Edge Select: disabled */
| (0 << CS32) | (1 << CS31) | (1 << CS30); /* IO clock divided by 64 */
OCR3A = 1; /* Output compare A: 0x64 */
TIMSK3 = 0 << OCIE3B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE3A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE3 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE3; /* Overflow Interrupt Enable: enabled */
// Timer 4 configuration for blue channel
TCCR4A = (1 << COM4A1) | (0 << COM4A0) /* Clear OCA on Compare Match, set OCA on BOTTOM (non-inverting mode) */
| (0 << COM4B1) | (0 << COM4B0) /* Normal port operation, OCB disconnected */
| (1 << WGM41) | (1 << WGM40); /* TC16 Mode 7 Fast 10-bit PWM */
TCCR4B = (0 << WGM43) | (1 << WGM42) /* TC16 Mode 7 Fast 10-bit PWM */
| 0 << ICNC4 /* Input Capture Noise Canceler: disabled */
| 0 << ICES4 /* Input Capture Edge Select: disabled */
| (0 << CS42) | (1 << CS41) | (1 << CS40); /* IO clock divided by 64 */
OCR4A = 1; /* Output compare A: 0x64 */
TIMSK4 = 0 << OCIE4B /* Output Compare B Match Interrupt Enable: disabled */
| 1 << OCIE4A /* Output Compare A Match Interrupt Enable: enabled */
| 0 << ICIE4 /* Input Capture Interrupt Enable: disabled */
| 1 << TOIE4; /* Overflow Interrupt Enable: enabled */
}
void setColor(const char *color)
{
long number = strtol(color, NULL, 16);
redValue = (number >> 16) & 0xFF;
greenValue = (number >> 8) & 0xFF;
blueValue = number & 0xFF0;
// Mise à l'échelle des valeurs pour PWM (0-255)
// redValue = (redValue * 1023) / 255;
// greenValue = (greenValue * 1023) / 255;
// blueValue = (blueValue * 1023) / 255;
OCR1A = redValue+1;
OCR3A = greenValue+1;
OCR4A = blueValue+1;
}
int main(void)
{
sei();
port_ini(); // Initialize ports
pwm_timer_init(); // Initialize PWM timer
// Example command
// setColor("FF0000"); // Set red color
// setColor("00FF00"); //Set green color
// setColor("0000FF"); // Set blue color
while(1)
{
setColor("ffffff"); // Set red color
_delay_ms(1000);
setColor("FFFF00");
_delay_ms(1000);
// Serial.println(OCR1A);
// Serial.println(OCR3A);
// Serial.println(OCR4A);
// setColor("00FF00"); //Set green color
// _delay_ms(1000);
// Serial.println(OCR1A);
// Serial.println(OCR3A);
// Serial.println(OCR4A);
// setColor("0000FF"); // Set blue color
// _delay_ms(1000);
// Serial.println(OCR1A);
// Serial.println(OCR3A);
// Serial.println(OCR4A);
// Boucle principale vide
}
}
ISR(TIMER1_OVF_vect)
{
// Handle Timer 1 overflow interrupt
}
ISR(TIMER1_COMPA_vect)
{
// Handle Timer 1 compare match A interrupt
}
ISR(TIMER3_OVF_vect)
{
// Handle Timer 3 overflow interrupt
}
ISR(TIMER3_COMPA_vect)
{
// Handle Timer 3 compare match A interrupt
}
ISR(TIMER4_OVF_vect)
{
// Handle Timer 4 overflow interrupt
}
ISR(TIMER4_COMPA_vect)
{
// Handle Timer 4 compare match A interrupt
}
// 1) ЗАЖЕЧЬ rgb СВЕТОДИОД С ПОМОЩЬЮ ТРЕХ ТАЙМЕРОВБ И МЕНЯТ Ь ЕГО ОТТЕНОК В ЗАВИСИМОСТИ ОТ ПРИНЯТОЙ КОМАНДЫ В ФОРМАТЕ "RRGGBB"