Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
Repozitorijs ir arhivēts. Tam var aplūkot failus un to var klonēt, bet nevar iesūtīt jaunas izmaiņas, kā arī atvērt jaunas problēmas/izmaiņu pieprasījumus.

backlight.c 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright 2013,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 <avr/io.h>
  15. #include <avr/interrupt.h>
  16. #include <avr/pgmspace.h>
  17. #include "backlight.h"
  18. #include "breathing_led.h"
  19. #ifdef BACKLIGHT_ENABLE
  20. void backlight_enable(void);
  21. void backlight_disable(void);
  22. inline void backlight_set_raw(uint8_t raw);
  23. #ifdef GH60_REV_CHN
  24. #else
  25. #define SOFTPWM_TIMER_TOP F_CPU/(256*64)
  26. uint8_t softpwm_ocr = 0;
  27. uint8_t softpwm_ocr_buff = 0;
  28. #endif
  29. static const uint8_t backlight_table[] PROGMEM = {
  30. 0, 16, 128, 255
  31. };
  32. /* Backlight pin configuration
  33. * PWM: PB6 (Rev.CHN)
  34. * GPIO: PF7 PF6 PF5 PF4 (Rev.B)
  35. */
  36. void backlight_enable(void)
  37. {
  38. #if defined(GH60_REV_CHN)
  39. // Turn on PWM
  40. DDRB |= (1<<PB6);
  41. cli();
  42. TCCR1A |= ((1<<WGM10) | (1<<COM1B1));
  43. TCCR1B |= ((1<<CS11) | (1<<CS10));
  44. sei();
  45. #else
  46. DDRF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  47. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  48. cli();
  49. TCCR1B |= (1<<WGM12);
  50. TCCR1B |= (1<<CS10);
  51. OCR1AH = (SOFTPWM_TIMER_TOP>>8)&0xff;
  52. OCR1AL = SOFTPWM_TIMER_TOP&0xff;
  53. TIMSK1 |= (1<<OCIE1A);
  54. sei();
  55. #endif
  56. }
  57. void backlight_disable(void)
  58. {
  59. #if defined(GH60_REV_CHN)
  60. // Turn off PWM
  61. cli();
  62. DDRB &= ~(1<<PB6);
  63. TCCR1A &= ~( (1<<WGM10) | (1<<COM1B1) );
  64. TCCR1B &= ~( (1<<CS11) | (1<<CS10) );
  65. sei();
  66. OCR1B = 0;
  67. #else
  68. DDRF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  69. cli();
  70. TCCR1B &= ~(1<<WGM12);
  71. TCCR1B &= ~(1<<CS10);
  72. TIMSK1 &= ~(1<<OCIE1A);
  73. sei();
  74. OCR1A = 0;
  75. #endif
  76. }
  77. void backlight_set(uint8_t level)
  78. {
  79. #ifdef BREATHING_LED_ENABLE
  80. switch (level) {
  81. case 1:
  82. case 2:
  83. case 3:
  84. backlight_enable();
  85. breathing_led_disable();
  86. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  87. break;
  88. case 4:
  89. case 5:
  90. case 6:
  91. backlight_enable();
  92. breathing_led_enable();
  93. breathing_led_set_duration(6 - level);
  94. break;
  95. case 0:
  96. default:
  97. breathing_led_disable();
  98. backlight_disable();
  99. break;
  100. }
  101. #else
  102. if (level > 0) {
  103. backlight_enable();
  104. backlight_set_raw(pgm_read_byte(&backlight_table[level]));
  105. }
  106. else {
  107. backlight_disable();
  108. }
  109. #endif
  110. }
  111. #ifdef BREATHING_LED_ENABLE
  112. void breathing_led_set_raw(uint8_t raw)
  113. {
  114. backlight_set_raw(raw);
  115. }
  116. #endif
  117. inline void backlight_set_raw(uint8_t raw)
  118. {
  119. #if defined(GH60_REV_CHN)
  120. OCR1B = raw;
  121. #else
  122. softpwm_ocr_buff = raw;
  123. #endif
  124. }
  125. #if defined(GH60_REV_CHN)
  126. #else
  127. ISR(TIMER1_COMPA_vect)
  128. {
  129. static uint8_t pwm = 0;
  130. pwm++;
  131. // LED on
  132. if (pwm == 0) {
  133. //PORTB |= (1<<PB6);
  134. PORTF &= ~(1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  135. softpwm_ocr = softpwm_ocr_buff;
  136. }
  137. // LED off
  138. if (pwm == softpwm_ocr) {
  139. //PORTB &= ~(1<<PB6);
  140. PORTF |= (1<<PF7 | 1<<PF6 | 1<<PF5 | 1<<PF4);
  141. }
  142. }
  143. #endif
  144. #endif