You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

yc059.c 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. Copyright 2014 Kai Ryu <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <util/delay.h>
  15. #include <avr/io.h>
  16. #include <avr/interrupt.h>
  17. #include "yc059.h"
  18. inline
  19. void yc059_ir_enable(void)
  20. {
  21. YC059_IR_DDR |= (1<<YC059_IR_BIT);
  22. }
  23. inline
  24. void yc059_ir_disable(void)
  25. {
  26. YC059_IR_DDR &= ~(1<<YC059_IR_BIT);
  27. }
  28. inline
  29. void yc059_ir_high(void)
  30. {
  31. YC059_IR_PORT &= ~(1<<YC059_IR_BIT);
  32. }
  33. inline
  34. void yc059_ir_low(void)
  35. {
  36. YC059_IR_PORT |= (1<<YC059_IR_BIT);
  37. }
  38. void yc059_init(void)
  39. {
  40. YC059_IR_DDR &= ~(1<<YC059_IR_BIT);
  41. YC059_IR_PORT &= ~(1<<YC059_IR_BIT);
  42. }
  43. void yc059_send(uint8_t data)
  44. {
  45. if (data == YC059_ERROR) return;
  46. cli();
  47. yc059_ir_enable();
  48. yc059_send_header();
  49. yc059_send_address();
  50. yc059_send_byte(data);
  51. yc059_send_byte(~data);
  52. yc059_send_stop();
  53. yc059_ir_disable();
  54. sei();
  55. }
  56. inline
  57. void yc059_send_header(void)
  58. {
  59. yc059_ir_high();
  60. _delay_us(YC059_HDR_MARK);
  61. yc059_ir_low();
  62. _delay_us(YC059_HDR_SPACE);
  63. }
  64. inline
  65. void yc059_send_stop(void)
  66. {
  67. yc059_ir_high();
  68. _delay_us(YC059_BIT_MARK);
  69. yc059_ir_low();
  70. }
  71. inline
  72. void yc059_send_address(void)
  73. {
  74. yc059_send_word(YC059_ADDRESS);
  75. }
  76. void yc059_send_byte(uint8_t byte)
  77. {
  78. for (uint8_t i = 0; i < 8; i++) {
  79. yc059_ir_high();
  80. _delay_us(YC059_BIT_MARK);
  81. yc059_ir_low();
  82. if (byte & 1) {
  83. _delay_us(YC059_ONE_SPACE);
  84. }
  85. else {
  86. _delay_us(YC059_ZERO_SPACE);
  87. }
  88. byte >>= 1;
  89. }
  90. }
  91. void yc059_send_word(uint16_t word)
  92. {
  93. yc059_send_byte((word & 0xFF00) >> 8);
  94. yc059_send_byte(word & 0xFF);
  95. }