Created
October 17, 2019 11:47
-
-
Save raek/55d29b87c2aff54613bbdf45acc4cc2c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <avr/io.h> | |
| #include <util/delay.h> | |
| #include <stdbool.h> | |
| #include <stdint.h> | |
| #define IR_FREQ 38000uL | |
| #define IR_ADDRESS 0x00u | |
| #define IR_DATA_ON 0x80u | |
| #define IR_DATA_OFF 0x90u | |
| static void delay_time_unit(void) | |
| { | |
| static const int time_unit = 562u; | |
| _delay_us(time_unit); | |
| } | |
| static void enable_wave_output(void) | |
| { | |
| DDRD |= (1u << PD6); | |
| PORTB |= (1u << PB5); | |
| } | |
| static void disable_wave_output(void) | |
| { | |
| DDRD &= ~(1u << PD6); | |
| PORTB &= ~(1u << PB5); | |
| } | |
| static void init_square_wave(void) | |
| { | |
| static const int half_period = (F_CPU/IR_FREQ)/2u; | |
| OCR0A = half_period; | |
| // COM0A = toggle on match | |
| // COM0B = disabled | |
| // WGM = Clear Timer on Compare match (CTC) | |
| // FOCx = disabled | |
| // CS = timer activated, no prescaler | |
| TCCR0A = ((0u << COM0A1) | | |
| (1u << COM0A0) | | |
| (0u << COM0B1) | | |
| (0u << COM0B0) | | |
| (0u << COM0B1) | | |
| (1u << WGM01) | | |
| (0u << WGM00)); | |
| TCCR0B = ((0u << FOC0A) | | |
| (0u << FOC0B) | | |
| (0u << WGM02) | | |
| (0u << CS02) | | |
| (0u << CS01) | | |
| (1u << CS00)); | |
| enable_wave_output(); | |
| } | |
| static void set_wave_output_for_time(bool enabled, uint8_t units) | |
| { | |
| if (enabled) | |
| { | |
| enable_wave_output(); | |
| } | |
| else | |
| { | |
| disable_wave_output(); | |
| } | |
| for (uint8_t i = 0u; i < units; i++) | |
| { | |
| delay_time_unit(); | |
| } | |
| } | |
| static void send_begin_sequence(void) | |
| { | |
| set_wave_output_for_time(true, 16u); | |
| set_wave_output_for_time(false, 8u); | |
| } | |
| static void send_end_sequence(void) | |
| { | |
| set_wave_output_for_time(true, 1u); | |
| set_wave_output_for_time(false, 87u); | |
| } | |
| static void send_bit_value(bool value) | |
| { | |
| set_wave_output_for_time(true, 1u); | |
| set_wave_output_for_time(false, (value ? 3u : 1u)); | |
| } | |
| static void send_byte_value(uint8_t value) | |
| { | |
| uint8_t bits = value; | |
| for (int i = 0; i < 8; i++) | |
| { | |
| send_bit_value(bits & 0x80); | |
| bits <<= 1u; | |
| } | |
| } | |
| static void send_encoded_byte_value(uint8_t value) | |
| { | |
| send_byte_value(value); | |
| send_byte_value(~value); | |
| } | |
| static void send_command_to_address(uint8_t address, uint8_t command) | |
| { | |
| send_begin_sequence(); | |
| send_encoded_byte_value(address); | |
| send_encoded_byte_value(command); | |
| send_end_sequence(); | |
| } | |
| static void send_command_to_address_several_times(uint8_t address, uint8_t command, uint8_t times) | |
| { | |
| for (int i = 0; i < times; i++) | |
| { | |
| send_command_to_address(address, command); | |
| } | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| init_square_wave(); | |
| while(true) { | |
| send_command_to_address_several_times(IR_ADDRESS, IR_DATA_ON, 5u); | |
| _delay_ms(1000u); | |
| send_command_to_address_several_times(IR_ADDRESS, IR_DATA_OFF, 5u); | |
| _delay_ms(1000u); | |
| } | |
| return 42; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment