#define OC1A PB5
// #define OC3A PB6
// #define OC4A PB7
#define OC3A PE3//dsad
#define OC4A PH3//RED
volatile uint16_t redValue = 0;
volatile uint16_t greenValue = 0;
volatile uint16_t blueValue = 0;
volatile float gamma=2.46;
// const uint8_t CRTgammaPGM[256] = {
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
// 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
// 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3,
// 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 8,
// 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 12, 12, 12, 13, 13, 14,
// 14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22,
// 23, 23, 24, 24, 25, 26, 26, 27, 28, 28, 29, 30, 30, 31, 32, 32,
// 33, 34, 35, 35, 36, 37, 38, 39, 39, 40, 41, 42, 43, 44, 45, 45,
// 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
// 62, 63, 64, 65, 66, 68, 69, 70, 71, 72, 73, 74, 76, 77, 78, 79,
// 81, 82, 83, 84, 86, 87, 88, 90, 91, 92, 94, 95, 96, 98, 99, 101,
// 102, 103, 105, 106, 108, 109, 111, 112, 114, 115, 117, 118, 120, 122, 123, 125,
// 126, 128, 130, 131, 133, 135, 136, 138, 140, 142, 143, 145, 147, 149, 150, 152,
// 154, 156, 158, 160, 161, 163, 165, 167, 169, 171, 173, 175, 177, 179, 181, 183,
// 185, 187, 189, 191, 193, 195, 197, 200, 202, 204, 206, 208, 210, 213, 215, 217,
// 219, 221, 224, 226, 228, 231, 233, 235, 238, 240, 242, 245, 247, 250, 252, 255,
// };
uint16_t getBrightCRT(uint16_t val) {
return (((val/255.0f) * pow(val, gamma))*255.0f);
}
// void gamma_correction(uint8_t *input, uint8_t *output, int length, float gamma) {
// for (int i = 0; i < length; i++) {
// // Нормализуем значение в диапазон [0, 1]
// float normalized = input[i] / 255.0f;
// // Применяем гамма-коррекцию
// float corrected = pow(normalized, gamma);
// // Де-нормализуем и сохраняем результат
// output[i] = (uint8_t)(corrected * 255.0f);
// // }
// }
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);
}
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 long color)
{
// Serial.println(number, HEX);
// redValue = (CRTgammaPGM[((color >> 16) & 0xFF)])* 1023 / 255;
// greenValue = (CRTgammaPGM[((color >> 8) & 0xFF)]) * 1023 / 255;
// blueValue = (CRTgammaPGM[(color & 0xFF)]) * 1023 / 255;
redValue = (((color >> 16) & 0xFF))* 1023 / 255;
greenValue = (((color >> 8) & 0xFF)) * 1023 / 255;
blueValue = ((color & 0xFF)) * 1023 / 255;
// Serial.print(" R: ");
// // Serial.print((CRTgammaPGM[(color >> 16) & 0xFF]), DEC);
// Serial.print(CRTgammaPGM[((color >> 16) & 0xFF)], DEC);
// Serial.print(" G: ");
// Serial.print((CRTgammaPGM[((color >> 8) & 0xFF)]), DEC);
// Serial.print(" B: ");
// Serial.println((CRTgammaPGM[(color & 0xFF)]), DEC);
// uint16_t crt3_10(uint16_t val) {
// return ((uint32_t)(val + 1) * (val + 1) * val) >> 20;
// }
OCR1A = getBrightCRT(redValue);
OCR3A =getBrightCRT(greenValue);
OCR4A =getBrightCRT(blueValue);
if (OCR1A == 0)
OCR1A = 1; // Устанавливаем OCR1A в 1, если он равен 0
if (OCR4A == 0)
OCR4A = 1; // Устанавливаем OCR1B в 1, если он равен 0
if (blueValue == 0)
blueValue = 1; // Устанавливаем OCR1B в 1, если он равен 0
if (OCR1A == 1024)
OCR1A = 1023; // Устанавливаем OCR1A в 1, если он равен 0
if (OCR4A == 1024)
OCR4A = 1023; // Устанавливаем OCR1B в 1, если он равен 0
if (blueValue == 1024)
blueValue = 1023; // Устанавливаем OCR1B в 1, если он равен 0
Serial.print(" R: ");
// Serial.print((CRTgammaPGM[(color >> 16) & 0xFF]), DEC);
Serial.print(((OCR1A/4)), DEC);
Serial.print(" G: ");
Serial.print(((OCR3A/4)), DEC);
Serial.print(" B: ");
Serial.println((OCR4A/4), DEC);
Serial.print(" gamma: ");
Serial.println((gamma), DEC);
}
// int main(void)
void setup1()
{
Serial.begin(115200);
sei();
port_ini(); // Initialize ports
pwm_timer_init(); // Initialize PWM timer
ADMUX = (1 << REFS0) | (1 << MUX0); // Настройка референсного напряжения и канала
ADCSRA = (1 << ADEN) | (1 << ADSC) // Включаем АЦП и начинаем преобразование
| (1 << ADPS2) | (1 << ADPS1) // Устанавливаем предделитель на 64
| (1 << ADPS0) // Устанавливаем предделитель на 128
| (1 << ACIE); // Включаем прерывание по сравнению
// setColor("FF0000"); // Set red color
// setColor("00FF00"); //Set green color
// setColor("0000FF"); // Set blue color
}
// while(1)
// void loop1()
// {
// setColor("ff0000"); // Set red color
// _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);
// }
// }
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)
{
setup1();
Serial.println("Введите число в формате RRGGBB");
while(1)
{
if (Serial.available() > 0) {
String input = Serial.readStringUntil('\n'); // Читаем строку до переноса строки
// Проверяем длину строки
if (input.length() == 6) {
long color = strtol(input.c_str(), NULL, 16); // Преобразуем строку в цвет
setColor(color);
} else {
// Выводим сообщение об ошибке
Serial.println("Ошибка ввода!");
_delay_ms(500);
Serial.println("Введите число в формате RRGGBB");
}
// return 0;
}
}
}
ISR(ADC_vect)
{
ADCSRA |= (1 << ADSC);
gamma = 1 + ((float)ADC / 1316);
}
// 1) ЗАЖЕЧЬ rgb СВЕТОДИОД С ПОМОЩЬЮ ТРЕХ ТАЙМЕРОВБ И МЕНЯТ Ь ЕГО ОТТЕНОК В ЗАВИСИМОСТИ ОТ ПРИНЯТОЙ КОМАНДЫ В ФОРМАТЕ "RRGGBB"