Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
Este repositório está arquivado. Você pode visualizar os arquivos e realizar clone, mas não poderá realizar push nem abrir issues e pull requests.

yc059.c 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "yc059.h"
  17. #include "debug.h"
  18. inline
  19. void yc059_ir_high(void)
  20. {
  21. YC059_IR_PORT &= ~(1<<YC059_IR_BIT);
  22. }
  23. inline
  24. void yc059_ir_low(void)
  25. {
  26. YC059_IR_PORT |= (1<<YC059_IR_BIT);
  27. }
  28. void yc059_init(void)
  29. {
  30. YC059_IR_DDR |= (1<<YC059_IR_BIT);
  31. YC059_IR_PORT &= ~(1<<YC059_IR_BIT);
  32. }
  33. void yc059_send(uint8_t data)
  34. {
  35. yc059_send_header();
  36. yc059_send_address();
  37. yc059_send_byte(data);
  38. yc059_send_byte(~data);
  39. yc059_send_stop();
  40. }
  41. inline
  42. void yc059_send_header(void)
  43. {
  44. yc059_ir_high();
  45. _delay_us(YC059_HDR_MARK);
  46. yc059_ir_low();
  47. _delay_us(YC059_HDR_SPACE);
  48. }
  49. inline
  50. void yc059_send_stop(void)
  51. {
  52. yc059_ir_high();
  53. _delay_us(YC059_BIT_MARK);
  54. yc059_ir_low();
  55. }
  56. inline
  57. void yc059_send_address(void)
  58. {
  59. yc059_send_word(YC059_ADDRESS);
  60. }
  61. void yc059_send_byte(uint8_t byte)
  62. {
  63. for (uint8_t i = 0; i < 8; i++) {
  64. yc059_ir_high();
  65. _delay_us(YC059_BIT_MARK);
  66. yc059_ir_low();
  67. if (byte & 1) {
  68. _delay_us(YC059_ONE_SPACE);
  69. }
  70. else {
  71. _delay_us(YC059_ZERO_SPACE);
  72. }
  73. byte >>= 1;
  74. }
  75. }
  76. void yc059_send_word(uint16_t word)
  77. {
  78. yc059_send_byte((word & 0xFF00) >> 8);
  79. yc059_send_byte(word & 0xFF);
  80. }