Subversion Repositories HomeAutomation

Rev

Rev 1808 | Blame | Compare with Previous | Last modification | View Log | SVN | RSS feed

  1. #include "ks0108.h"
  2. #include <avr/io.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #include <avr/pgmspace.h>
  7.  
  8. //Some parts of the code is from proycon avrlib written by Pascal Stang:
  9. //*****************************************************************************
  10. //
  11. // File Name    : 'glcd.c'
  12. // Title        : Graphic LCD API functions
  13. // Author       : Pascal Stang - Copyright (C) 2002
  14. // Date         : 5/30/2002
  15. // Revised      : 5/30/2002
  16. // Version      : 0.5
  17. // Target MCU   : Atmel AVR
  18. // Editor Tabs  : 4
  19. //
  20. // NOTE: This code is currently below version 1.0, and therefore is considered
  21. // to be lacking in some functionality or documentation, or may not be fully
  22. // tested.  Nonetheless, you can expect most functions to work.
  23. //
  24. // This code is distributed under the GNU Public License
  25. //      which can be found at http://www.gnu.org/licenses/gpl.txt
  26. //
  27. //*****************************************************************************
  28. #ifndef GRAPHICS_WIDTH
  29. #error GRAPHICS_WIDTH, GRAPHICS_HEIGHT must be defined
  30. #endif
  31.  
  32. #define OUTPUT  0
  33. #define INPUT   1
  34.  
  35. volatile GrLcdStateType GrLcdState;
  36.  
  37. void ks0108SetControls(uint8_t RS, uint8_t RW){
  38.     if(RS) {gpio_set_pin(KS0108_CONTROL_RS);} else {gpio_clr_pin(KS0108_CONTROL_RS);}
  39.     if(RW) {gpio_set_pin(KS0108_CONTROL_RW);} else {gpio_clr_pin(KS0108_CONTROL_RW);}
  40. }
  41.  
  42. void ks0108SetData(uint8_t Data){
  43.     if(Data&0x01) {gpio_set_pin(KS0108_DATA_DB0);} else {gpio_clr_pin(KS0108_DATA_DB0);}
  44.     if(Data&0x02) {gpio_set_pin(KS0108_DATA_DB1);} else {gpio_clr_pin(KS0108_DATA_DB1);}
  45.     if(Data&0x04) {gpio_set_pin(KS0108_DATA_DB2);} else {gpio_clr_pin(KS0108_DATA_DB2);}
  46.     if(Data&0x08) {gpio_set_pin(KS0108_DATA_DB3);} else {gpio_clr_pin(KS0108_DATA_DB3);}
  47.     if(Data&0x10) {gpio_set_pin(KS0108_DATA_DB4);} else {gpio_clr_pin(KS0108_DATA_DB4);}
  48.     if(Data&0x20) {gpio_set_pin(KS0108_DATA_DB5);} else {gpio_clr_pin(KS0108_DATA_DB5);}
  49.     if(Data&0x40) {gpio_set_pin(KS0108_DATA_DB6);} else {gpio_clr_pin(KS0108_DATA_DB6);}
  50.     if(Data&0x80) {gpio_set_pin(KS0108_DATA_DB7);} else {gpio_clr_pin(KS0108_DATA_DB7);}
  51. }
  52. uint8_t ks0108GetData(void){
  53.     uint8_t input = 0;
  54.     if (gpio_get_state(KS0108_DATA_DB0)) {input += 1;}
  55.     if (gpio_get_state(KS0108_DATA_DB1)) {input += 2;}
  56.     if (gpio_get_state(KS0108_DATA_DB2)) {input += 4;}
  57.     if (gpio_get_state(KS0108_DATA_DB3)) {input += 8;}
  58.     if (gpio_get_state(KS0108_DATA_DB4)) {input += 16;}
  59.     if (gpio_get_state(KS0108_DATA_DB5)) {input += 32;}
  60.     if (gpio_get_state(KS0108_DATA_DB6)) {input += 64;}
  61.     if (gpio_get_state(KS0108_DATA_DB7)) {input += 128;}
  62.     return input;
  63. }
  64.  
  65. void ks0108Disable(){
  66.     gpio_clr_pin(KS0108_CONTROL_E);
  67. }
  68.  
  69. void ks0108Delay(){
  70.     _delay_us(2);
  71. }
  72.  
  73. void ks0108Enable(){
  74.     ks0108Delay();
  75.     gpio_set_pin(KS0108_CONTROL_E);
  76.     ks0108Delay();
  77.     ks0108Disable();
  78.     ks0108Delay();
  79. }
  80.  
  81. void ks0108SetDirection(uint8_t dir){
  82.   if (dir == OUTPUT){
  83.     gpio_set_out(KS0108_DATA_DB0);
  84.     gpio_set_out(KS0108_DATA_DB1);
  85.     gpio_set_out(KS0108_DATA_DB2);
  86.     gpio_set_out(KS0108_DATA_DB3);
  87.     gpio_set_out(KS0108_DATA_DB4);
  88.     gpio_set_out(KS0108_DATA_DB5);
  89.     gpio_set_out(KS0108_DATA_DB6);
  90.     gpio_set_out(KS0108_DATA_DB7);
  91.   }else if (dir == INPUT){
  92.     gpio_set_in(KS0108_DATA_DB0);
  93.     gpio_set_in(KS0108_DATA_DB1);
  94.     gpio_set_in(KS0108_DATA_DB2);
  95.     gpio_set_in(KS0108_DATA_DB3);
  96.     gpio_set_in(KS0108_DATA_DB4);
  97.     gpio_set_in(KS0108_DATA_DB5);
  98.     gpio_set_in(KS0108_DATA_DB6);
  99.     gpio_set_in(KS0108_DATA_DB7);
  100.   }
  101. }
  102. void ks0108SetColor(uint8_t color){
  103.   GrLcdState.color=color;
  104. }
  105. uint8_t ks0108GetColor(void){
  106.   return GrLcdState.color;
  107. }
  108. void ks0108WriteData(uint8_t data, uint8_t color){
  109.     ks0108SetControls(1,0);
  110.     if (color == GLCD_COLOR_CLEAR)
  111.       data = ~data;
  112.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  113.       ks0108SetData(data);
  114.     else
  115.       ks0108SetData(~data);
  116.     ks0108Enable();
  117.     GrLcdState.lcdXAddr++;
  118.  
  119.     if (GrLcdState.lcdXAddr == 64 ){
  120.         ks0108SetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  121.     }
  122. #if GRAPHICS_WIDTH > 128
  123. else if (GrLcdState.lcdXAddr == 128 ){
  124.         ks0108SetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  125.     }
  126. #endif
  127.     if (GrLcdState.lcdXAddr > GRAPHICS_WIDTH ){
  128.         ks0108SetXY(0,GrLcdState.lcdYAddr+8);
  129.     }
  130. }
  131. void ks0108WriteDataTransparent(uint8_t inputdata, uint8_t color){
  132.     uint8_t data = 0;
  133.     ks0108SetDirection(INPUT);
  134.     ks0108SetControls(1,1);
  135.     ks0108Enable(); //dummy read
  136.     ks0108Delay();
  137.     gpio_set_pin(KS0108_CONTROL_E);
  138.     ks0108Delay();
  139.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  140.       data = ks0108GetData();
  141.     else
  142.       data = ~ks0108GetData();
  143.     ks0108Disable();
  144.     ks0108Delay();
  145.     ks0108SetControls(1,0);
  146.     ks0108SetDirection(OUTPUT);
  147.     ks0108SetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);
  148.     ks0108SetControls(1,0);
  149.     if (color == GLCD_COLOR_CLEAR)
  150.       data = data&(~inputdata);
  151.     else
  152.       data = data|inputdata;
  153.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  154.       ks0108SetData(data);
  155.     else
  156.       ks0108SetData(~data);
  157.     ks0108Enable();
  158.     GrLcdState.lcdXAddr++;
  159.  
  160.  
  161.     if (GrLcdState.lcdXAddr == 64 ){
  162.         ks0108SetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  163.     }
  164. #if GRAPHICS_WIDTH > 128
  165.     else if (GrLcdState.lcdXAddr == 128 ){
  166.         ks0108SetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  167.     }
  168. #endif
  169.     if (GrLcdState.lcdXAddr > GRAPHICS_WIDTH ){
  170.         ks0108SetXY(0,GrLcdState.lcdYAddr+8);
  171.     }
  172. }
  173.  
  174. uint8_t ks0108ReadData(void){
  175.     uint8_t data = 0;
  176.     ks0108SetDirection(INPUT);
  177.     ks0108SetControls(1,1);
  178.     ks0108Enable(); //dummy read
  179.     ks0108Delay();
  180.     gpio_set_pin(KS0108_CONTROL_E);
  181.     ks0108Delay();
  182.     if (GrLcdState.color == GLCD_COLOR_BLACK)
  183.       data = ks0108GetData();
  184.     else
  185.       data = ~ks0108GetData();
  186.     ks0108Disable();
  187.     ks0108Delay();
  188.     ks0108SetControls(1,0);
  189.     ks0108SetDirection(OUTPUT);
  190.     ks0108SetXY(GrLcdState.lcdXAddr, GrLcdState.lcdYAddr);
  191.     return data;
  192. }
  193.  
  194. void ks0108Init(){
  195.     gpio_set_out(KS0108_CONTROL_RS);
  196.     gpio_set_out(KS0108_CONTROL_RW);
  197.     gpio_set_out(KS0108_CONTROL_E);
  198.     gpio_set_out(KS0108_CONTROL_CS1);
  199.     gpio_set_out(KS0108_CONTROL_CS2);
  200.  
  201.     ks0108SetDirection(OUTPUT);
  202.     GrLcdState.color = GLCD_COLOR_WHITE;
  203. #if KS0108_INVERT_CS == 1
  204.     gpio_clr_pin(KS0108_CONTROL_CS1);
  205.     gpio_clr_pin(KS0108_CONTROL_CS2);
  206. #else
  207.     gpio_set_pin(KS0108_CONTROL_CS1);
  208.     gpio_set_pin(KS0108_CONTROL_CS2);
  209. #endif
  210.     ks0108Disable();
  211.     ks0108Delay();
  212.     //set display on
  213.     ks0108SetControls(0,0);
  214.     ks0108SetData(0x3F);
  215.     ks0108Enable();
  216.  
  217.     //set startaddress of the first row (set to row 0)
  218.     ks0108SetControls(0,0);
  219.     ks0108SetData(0xc0);
  220.     ks0108Enable();
  221.     ks0108Clear();
  222. }
  223.  
  224. void ks0108Clear(){
  225. #if KS0108_INVERT_CS == 1
  226.     gpio_clr_pin(KS0108_CONTROL_CS1);
  227.     gpio_clr_pin(KS0108_CONTROL_CS2);
  228. #else
  229.     gpio_set_pin(KS0108_CONTROL_CS1);
  230.     gpio_set_pin(KS0108_CONTROL_CS2);
  231. #endif
  232.     ks0108Enable();
  233.     ks0108Delay();
  234.     ks0108Disable();
  235.  
  236.     uint8_t i;
  237.     uint8_t j;
  238.  
  239.     for(j = 0; j < 8; j++){
  240.         ks0108SetControls(0,0);
  241.         ks0108SetData(0xB8 + j);
  242.         ks0108Enable();
  243.  
  244.         ks0108SetControls(0,0);
  245.         ks0108SetData(0x40);
  246.         ks0108Enable();
  247.         for (i = 0; i < 64; i++){
  248.             ks0108SetControls(1,0);
  249.             if (GrLcdState.color == GLCD_COLOR_BLACK)
  250.               ks0108SetData(0x00);
  251.             else
  252.               ks0108SetData(0xff);
  253.             ks0108Enable();
  254.         }
  255.     }
  256.  
  257. }
  258.  
  259. void ks0108SetXY(uint8_t x, uint8_t y){
  260.     GrLcdState.lcdXAddr = x;
  261.     GrLcdState.lcdYAddr = y;
  262.     GrLcdState.lcdYpage = y/8;
  263.  
  264.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  265.     if (x > 63 && x <= 127){
  266. #if KS0108_INVERT_CS == 1
  267.     gpio_set_pin(KS0108_CONTROL_CS1);
  268.     gpio_clr_pin(KS0108_CONTROL_CS2);
  269. #else
  270.     gpio_set_pin(KS0108_CONTROL_CS2);
  271.     gpio_clr_pin(KS0108_CONTROL_CS1);
  272. #endif
  273.     } else if (x > 127){
  274. #if KS0108_INVERT_CS == 1
  275.     gpio_set_pin(KS0108_CONTROL_CS1);
  276.     gpio_set_pin(KS0108_CONTROL_CS2);
  277. #else
  278.     gpio_clr_pin(KS0108_CONTROL_CS2);
  279.     gpio_clr_pin(KS0108_CONTROL_CS1);
  280. #endif
  281.     } else {
  282. #if KS0108_INVERT_CS == 1
  283.     gpio_set_pin(KS0108_CONTROL_CS2);
  284.     gpio_clr_pin(KS0108_CONTROL_CS1);
  285. #else
  286.     gpio_set_pin(KS0108_CONTROL_CS1);
  287.     gpio_clr_pin(KS0108_CONTROL_CS2);
  288. #endif
  289.     }
  290.  
  291.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  292.     ks0108SetControls(0,0);
  293.     ks0108SetData(0x40 + x%64);
  294.     ks0108Enable();
  295.  
  296.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  297.     ks0108SetData(0xB8 + GrLcdState.lcdYpage);
  298.     ks0108Enable();
  299. }
  300. uint8_t ks0108GetX(void){
  301.     return GrLcdState.lcdXAddr;
  302. }
  303. uint8_t ks0108GetY(void){
  304.     return GrLcdState.lcdYAddr;
  305. }
  306.  
  307.