17.c 830 B

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