Jednoduchý prijímač diaľkového IR ovládania
Zo stránky SensorWiki
Záverečný projekt predmetu MIPS / LS2024 - Meno Priezvisko
Zadanie
Zostrojte a naprogramujte ovladanie škrtiacej klapky pomocou plynového pedálu.
Literatúra:
Analýza a opis riešenia
Opíšte sem čo a ako ste spravili, ak treba, doplňte obrázkami...
Nezabudnite doplniť schému zapojenia!
Algoritmus a program
Algoritmus programu je....
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
#include "uart.h"
// #define F_CPU 16000000UL
// #define BAUD 9600
// Define button codes (example values, replace with actual values from your remote)
#define RED_BUTTON 0xFFA25D
#define GREEN_BUTTON 0xFF629D
#define BLUE_BUTTON 0xFFE21D
#define OFF_BUTTON 0xFF22DD
#define IRLED PD2
#define toggleBIT(reg, bit) ((reg) ^= (1 << (bit))) //PREPNUTIE STAVU
#define setBIT(reg, bit) ((reg) |= (1 << (bit))) //JEDNA
#define clearBIT(reg, bit) ((reg) &= ~(1 << (bit))) //NULAs
volatile uint32_t ir_code = 0;
volatile uint8_t bit_count = 0;
void initIRReceiver(void);
void initRGBLED(void);
void setColor(uint8_t red, uint8_t green, uint8_t blue);
void handleIRCode(uint32_t code);
FILE mystdout = FDEV_SETUP_STREAM(uart_putc, NULL, _FDEV_SETUP_WRITE);
ISR(INT0_vect) {
// Simple state machine to decode the IR signal
static uint8_t last_edge = 0;
uint8_t edge = (PIND & (1 << PIND2)) >> PIND2;
uint16_t duration = TCNT1;
TCNT1 = 0;
if (edge == last_edge) return;
last_edge = edge;
if (duration > 10000) { // Header
ir_code = 0;
bit_count = 0;
} else if (duration > 0 && duration < 100) { // Logical 0
ir_code = 0xFFA25D ;
bit_count++;
} else if (duration > 100 && duration < 200) { // Logical 1
ir_code = 0xFF629D;
bit_count++;
}
else if (duration > 200 && duration < 300) { // Logical 1
ir_code = 0xFFE21D;
bit_count++;
}
if (bit_count >= 32) {
handleIRCode(ir_code);
printf("IR Code: %08lX\n",ir_code);
ir_code = 0;
bit_count = 0;
}
}
void initIRReceiver(void) {
clearBIT(PORTD,IRLED);
clearBIT(DDRD,IRLED);
EICRA |= (1 << ISC00); // Any logical change on INT0 triggers interrupt
EIMSK |= (1 << INT0); // Enable external interrupt INT0
// Timer1 initialization
TCCR1B |= (1 << CS11); // Prescaler 8
TCNT1 = 0; // Initialize counter
}
void initRGBLED(void) {
// Set PB1, PB2, and PB3 as output (PWM)
DDRB |= (1 << PB1) | (1 << PB2) | (1 << PB3);
// Setup Timer1 for Fast PWM mode on PB1 and PB2
TCCR1A |= (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
TCCR1B |= (1 << WGM12) | (1 << WGM13);
ICR1 = 255; // TOP value for 8-bit PWM
// Setup Timer2 for Fast PWM mode on PB3
TCCR2A |= (1 << COM2A1) | (1 << WGM20) | (1 << WGM21);
TCCR2B |= (1 << CS21); // Prescaler 8
}
void setColor(uint8_t red, uint8_t green, uint8_t blue) {
OCR1A = red; // Set PWM value for PB1 (Red)
OCR1B = green; // Set PWM value for PB2 (Green)
OCR2A = blue; // Set PWM value for PB3 (Blue)
}
void handleIRCode(uint32_t code) {
switch(code) {
case RED_BUTTON:
printf("Red Button Pressed\n");
setColor(255, 0, 0); // Red
break;
case GREEN_BUTTON:
printf("Green Button Pressed\n");
setColor(0, 255, 0); // Green
break;
case BLUE_BUTTON:
printf("Blue Button Pressed\n");
setColor(0, 0, 255); // Blue
break;
case OFF_BUTTON:
printf("Off Button Pressed\n");
setColor(0, 0, 0); // Off
break;
default:
printf("Unknown Button Pressed\n");
break;
}
}
int main(void) {
uart_init();
stdout = &mystdout;
initIRReceiver();
initRGBLED();
printf("System Initialized\n");
sei(); //globalne interupty
while (1)
{
printf("IR Code: %08lX\r",ir_code);
}
}
#include <avr/io.h>
#include <util/delay.h>
#include "uart.h"
#define CLK PD5 // CLK -> pin 5 portD.5
#define DIO PD4 // DIO -> pin 4 portD.4
#define BIT_DELAY 100 // Delay 100ms
// Define for TM1637
#define TM1637_I2C_COMM1 0x40
#define TM1637_I2C_COMM2 0xC0
#define TM1637_I2C_COMM3 0x80
// Nastavenie cisiel na 7 segmentovom displeji
const uint8_t digitToSegment[] = {
// XGFEDCBA
0b00111111, // 0
0b00000110, // 1
0b01011011, // 2
0b01001111, // 3
0b01100110, // 4
0b01101101, // 5
0b01111101, // 6
0b00000111, // 7
0b01111111, // 8
0b01101111, // 9
0b01110111, // A
0b01111100, // b
0b00111001, // C
0b01011110, // d
0b01111001, // E
0b01110001 // F
};
uint8_t brightness = (0x7 & 0x7) | 0x08;
static const uint8_t minusSegments = 0b01000000;
int inicialize_bit(uint8_t byte){
uint8_t data = byte;
// 8 Data Bits
for(uint8_t i = 0; i < 8; i++) {
// CLK low
set_bit(DDRD, CLK);
clear_bit(PORTD, CLK);
_delay_us(BIT_DELAY);
// Set data bit
if (data & 0x01){
clear_bit(DDRD, DIO); // Nastavenie DIO ako input
set_bit(PORTD, DIO); // DIO ako input pull-up on
} else{
set_bit(DDRD, DIO); // Nastavenie DIO ako output
clear_bit(PORTD, DIO); // Nastavenie DIO low
}
_delay_us(BIT_DELAY);
// CLK high
clear_bit(DDRD, CLK); // Nastavenie CLK ako input
set_bit(PORTD, CLK); // CLK ako input pull-up on
_delay_us(BIT_DELAY);
data = data >> 1;
}
// Wait for acknowledge
// CLK to zero
set_bit(DDRD, CLK); // Nastavenie CLK ako output
clear_bit(PORTD, CLK); // Nastavenie CLK low
clear_bit(DDRD, DIO); // Nastavenie DIO ako input
set_bit(PORTD, DIO); // DIO ako input pull-up on
_delay_us(BIT_DELAY);
// CLK to high
clear_bit(DDRD, CLK); // Nastavenie CLK ako input
set_bit(PORTD, CLK); // Nastavenie CLK high
_delay_us(BIT_DELAY);
uint8_t ack = !bit_is_clear(PIND, DIO);
if (ack == 0)
set_bit(DDRD, DIO); // Nastavenie DIO ako output
clear_bit(PORTD, DIO); // Nastavenie DIO low
_delay_us(BIT_DELAY);
set_bit(DDRD, CLK); // Nastavenie CLK ako output
clear_bit(PORTD, CLK); // Nastavenie CLK low
_delay_us(BIT_DELAY);
return ack;
}
void comunication_start(){
set_bit(DDRD, DIO); // Nastavenie DIO ako output
clear_bit(PORTD, DIO); // Nastavenie DIO ako output
_delay_us(BIT_DELAY);
}
void comunication_stop(){
set_bit(DDRD, DIO); // Nastavenie DIO ako output
clear_bit(PORTD, DIO); // Nastavenie DIO ako output
_delay_us(BIT_DELAY);
clear_bit(DDRD, CLK); // Nastavenie CLK ako input
set_bit(PORTD, CLK); // CLK ako input pull-up on
_delay_us(BIT_DELAY);
clear_bit(DDRD, DIO); // Nastavenie DIO ako input
set_bit(PORTD, DIO); // DIO ako input pull-up on
_delay_us(BIT_DELAY);
}
void displayShowDots(uint8_t dots, uint8_t* digits){
for(int i = 0; i < 4; ++i)
{
digits[i] |= (dots & 0x80);
dots <<= 1;
}
}
// Funkcia na zobrazenie cisla
void display_middle_dots(int num, uint8_t dots, const int leading_zero, uint8_t length, uint8_t pos){
display_number(num < 0? -10 : 10, num < 0? -num : num, dots, leading_zero, length, pos);
}
// Funkcia na zobrazenie cisla v danom zaklade
void display_number(int8_t base, uint16_t num, uint8_t dots, const int leading_zero, uint8_t length, uint8_t pos)
{
int negative = 0; // False
if (base < 0) {
base = -base;
negative = 1; // True
}
uint8_t digits[4];
if (num == 0 && !leading_zero) {
for(uint8_t i = 0; i < (length-1); i++)
digits[i] = 0;
digits[length-1] = digitToSegment[0 & 0x0f];;
}
else {
for(int i = length-1; i >= 0; --i)
{
uint8_t digit = num % base;
if (digit == 0 && num == 0 && leading_zero == 0)
digits[i] = 0;
else
digits[i] = digitToSegment[digit & 0x0f];;
if (digit == 0 && num == 0 && negative) {
digits[i] = minusSegments;
negative = 0;
}
num /= base;
}
}
if(dots != 0)
{
displayShowDots(dots, digits);
}
set_segments(digits, length, pos);
}
// Funkcia na zobrazenie cisla s default nastaveniami
void display_number_segment(int num, const int leading_zero, uint8_t length, uint8_t pos){
display_middle_dots(num, 0x40, leading_zero, length, pos);
}
void set_segments(const uint8_t segments[], uint8_t length, uint8_t pos){
// Zapis COMM1
comunication_start();
inicialize_bit(TM1637_I2C_COMM1);
comunication_stop();
// Zapis COMM2 + prvu cislicu adresy
comunication_start();
inicialize_bit(TM1637_I2C_COMM2 + (pos & 0x03));
// Zapis datove bajty
for (uint8_t k=0; k < length; k++)
inicialize_bit(segments[k]);
comunication_stop();
// Zapis COMM3 + brightness
comunication_start();
inicialize_bit(TM1637_I2C_COMM3 + (brightness & 0x0f));
comunication_stop();
}
#define set_bit(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
#define clear_bit(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT))
#ifndef UART_H_
#define UART_H_
void comunication_start();
void comunication_stop();
void displayShowDots(uint8_t dots, uint8_t* digits);
int inicialize_bit(uint8_t byte);
void display_middle_dots(int num, uint8_t dots, const int leading_zero, uint8_t length, uint8_t pos);
void display_number(int8_t base, uint16_t num, uint8_t dots, const int leading_zero, uint8_t length, uint8_t pos);
void display_number_segment(int num, const int leading_zero, uint8_t length, uint8_t pos);
void set_segments(const uint8_t segments[], uint8_t length, uint8_t pos);
#endif /* UART_H_ */
Zdrojový kód: zdrojaky.zip
Overenie
Na používanie našej aplikácie stačia dve tlačítka a postup používania je opísaný v sekcii popis riešenia. Na konci uvádzame fotku záverečnej obrazovky pred resetom. Vypísaný je tu priemerný čas a najlepší čas.
Video:
Kľúčové slová 'Category', ktoré sú na konci stránky nemeňte.