backlight.c 954 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <avr/io.h>
  2. #include "backlight.h"
  3. #include "led.h"
  4. #include "print.h"
  5. int pwm_level;
  6. void led_set_kb(uint8_t usb_led)
  7. {
  8. print("led_set\n");
  9. }
  10. void init_backlight_pin(void)
  11. {
  12. // Set C7 to output
  13. DDRC |= (1<<7);
  14. // Initialize the timer
  15. TC4H = 0x03;
  16. OCR4C = 0xFF;
  17. TCCR4A = 0b10000010;
  18. TCCR4B = 0b00000001;
  19. }
  20. void backlight_set(uint8_t level)
  21. {
  22. // Determine the PWM level
  23. switch (level)
  24. {
  25. case 0:
  26. // 33%
  27. pwm_level = 0x54;
  28. break;
  29. case 1:
  30. // 66%
  31. pwm_level = 0xA8;
  32. break;
  33. case 2:
  34. // 100%
  35. pwm_level = 0xFF;
  36. break;
  37. case 3:
  38. // 0%
  39. pwm_level = 0x00;
  40. break;
  41. default:
  42. xprintf("Unknown level: %d\n", level);
  43. }
  44. // Write the PWM level to the timer
  45. TC4H = pwm_level >> 8;
  46. OCR4A = 0xFF & pwm_level;
  47. }