ssd1309.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include "gpio.h"
  7. #include "lcd.h"
  8. #include "log.h"
  9. void ssd1309_on(lcd_handle_t *h, int flag)
  10. {
  11. if(flag) {
  12. lcd_write_reg(h,0x8D);
  13. lcd_write_reg(h,0x14);
  14. lcd_write_reg(h,0xAF);
  15. }
  16. else {
  17. lcd_write_reg(h,0x8D);
  18. lcd_write_reg(h,0x10);
  19. lcd_write_reg(h,0xAE);
  20. }
  21. return 0;
  22. }
  23. int ssd1309_init(lcd_handle_t *h)
  24. {
  25. ssd1309_reset();
  26. lcd_write_reg(h,0xFD);
  27. lcd_write_reg(h,0x12);
  28. lcd_write_reg(h,0xAE);//--turn off oled panel
  29. lcd_write_reg(h,0xd5);//--set display clock divide ratio/oscillator frequency
  30. lcd_write_reg(h,0xA0);
  31. lcd_write_reg(h,0xA8);//--set multiplex ratio(1 to 64)
  32. lcd_write_reg(h,0x3f);//--1/64 duty
  33. lcd_write_reg(h,0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
  34. lcd_write_reg(h,0x00);//-not offset
  35. lcd_write_reg(h,0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
  36. lcd_write_reg(h,0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
  37. lcd_write_reg(h,0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
  38. lcd_write_reg(h,0xDA);//--set com pins hardware configuration
  39. lcd_write_reg(h,0x12);
  40. lcd_write_reg(h,0x81);//--set contrast control register
  41. lcd_write_reg(h,0x7F);// Set SEG Output Current Brightness
  42. lcd_write_reg(h,0xD9);//--set pre-charge period
  43. lcd_write_reg(h,0x82);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  44. lcd_write_reg(h,0xDB);//--set vcomh
  45. lcd_write_reg(h,0x34);//Set VCOM Deselect Level
  46. lcd_write_reg(h,0xA4);// Disable Entire Display On (0xa4/0xa5)
  47. lcd_write_reg(h,0xA6);// Disable Inverse Display On (0xa6/a7)
  48. ssd1309_refresh(h);
  49. ssd1309_on(h,1);
  50. return 0;
  51. }
  52. int ssd1309_refresh(lcd_handle_t *h)
  53. {
  54. int i,n,offset;
  55. for(i=0;i<8;i++) {
  56. lcd_write_reg(h,0xb0+i); //设置行起始地址
  57. lcd_write_reg(h,0x00); //设置低列起始地址
  58. lcd_write_reg(h,0x10); //设置高列起始地址
  59. gpio_set(DC_GPIO, 1);
  60. lcd_hw_write(h, &h->buf[i*h->width], h->width);
  61. }
  62. }