jd45.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "jd45.h"
  2. __attribute__ ((weak))
  3. void matrix_init_user(void) {
  4. };
  5. __attribute__ ((weak))
  6. void matrix_scan_user(void) {
  7. };
  8. #define CHANNEL OCR1C
  9. void backlight_init_ports(void)
  10. {
  11. // Setup PB7 as output and output low.
  12. DDRB |= (1<<7);
  13. PORTB &= ~(1<<7);
  14. // Use full 16-bit resolution.
  15. ICR1 = 0xFFFF;
  16. // I could write a wall of text here to explain... but TL;DW
  17. // Go read the ATmega32u4 datasheet.
  18. // And this: http://blog.saikoled.com/post/43165849837/secret-konami-cheat-code-to-high-resolution-pwm-on
  19. // Pin PB7 = OCR1C (Timer 1, Channel C)
  20. // Compare Output Mode = Clear on compare match, Channel C = COM1C1=1 COM1C0=0
  21. // (i.e. start high, go low when counter matches.)
  22. // WGM Mode 14 (Fast PWM) = WGM13=1 WGM12=1 WGM11=1 WGM10=0
  23. // Clock Select = clk/1 (no prescaling) = CS12=0 CS11=0 CS10=1
  24. TCCR1A = _BV(COM1C1) | _BV(WGM11); // = 0b00001010;
  25. TCCR1B = _BV(WGM13) | _BV(WGM12) | _BV(CS10); // = 0b00011001;
  26. backlight_init();
  27. }
  28. void backlight_set(uint8_t level)
  29. {
  30. if ( level == 0 )
  31. {
  32. // Turn off PWM control on PB7, revert to output low.
  33. TCCR1A &= ~(_BV(COM1C1));
  34. CHANNEL = 0x0;
  35. // Prevent backlight blink on lowest level
  36. PORTB &= ~(_BV(PORTB7));
  37. }
  38. else if ( level == BACKLIGHT_LEVELS )
  39. {
  40. // Prevent backlight blink on lowest level
  41. PORTB &= ~(_BV(PORTB7));
  42. // Turn on PWM control of PB7
  43. TCCR1A |= _BV(COM1C1);
  44. // Set the brightness
  45. CHANNEL = 0xFFFF;
  46. }
  47. else
  48. {
  49. // Prevent backlight blink on lowest level
  50. PORTB &= ~(_BV(PORTB7));
  51. // Turn on PWM control of PB7
  52. TCCR1A |= _BV(COM1C1);
  53. // Set the brightness
  54. CHANNEL = 0xFFFF >> ((BACKLIGHT_LEVELS - level) * ((BACKLIGHT_LEVELS + 1) / 2));
  55. }
  56. }
  57. void matrix_init_kb(void) {
  58. #ifdef BACKLIGHT_ENABLE
  59. backlight_init_ports();
  60. #endif
  61. matrix_init_user();
  62. };
  63. void matrix_scan_kb(void) {
  64. matrix_scan_user();
  65. };