Subversion Repositories HomeAutomation

Rev

Blame | Last modification | View Log | SVN | RSS feed

  1. #include <ks0108.h>
  2. #include <avr/io.h>
  3. #include <font.h>
  4. #include <avr\pgmspace.h>
  5.  
  6. GrLcdStateType GrLcdState;
  7.  
  8.  
  9. void SetControls(uint8_t RS, uint8_t RW){
  10.     if(RS) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RS);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RS);};
  11.     if(RW) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RW);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RW);};
  12. }
  13.  
  14. void SetData(uint8_t Data){
  15.     if(Data&0b00000001) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
  16.     if(Data&0b00000010) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
  17.     if(Data&0b00000100) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
  18.     if(Data&0b00001000) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
  19.     if(Data&0b00010000) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
  20.     if(Data&0b00100000) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
  21.     if(Data&0b01000000) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
  22.     if(Data&0b10000000) {LCD_DATA_PORT_DB7 |= (1<<LCD_DATA_PIN_DB7);} else { LCD_DATA_PORT_DB7 &= ~(1<<LCD_DATA_PIN_DB7);};
  23. }
  24.  
  25.  
  26. void Disable(){
  27.     LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_E);
  28. }
  29.  
  30. void Delay(){ //OBS!! BEHÖVER VEEERKLIGEN FIXAS!
  31.            // 1-8us      ...2-13us     ...5-31us
  32.                                // 10-60us    ...50-290us
  33. unsigned int i;             // 100-580us  ...500-2,9ms
  34. unsigned char j;            // 1000-5,8ms ...5000-29ms
  35. uint8_t p = 10;                             // 10000-56ms ...30000-170ms
  36.                              // 50000-295ms...60000-345ms
  37. //  for (i = 0; i < p; i++) for (j = 0; j < 10; j++) asm volatile ("nop");
  38.     for (i = 0; i < p; i++) for (j = 0; j < 10; j++);
  39.  
  40. }
  41. void Enable(){
  42.     Delay();
  43.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
  44.     Delay();
  45.     Disable();
  46.     Delay();
  47. }
  48.  
  49.  
  50. void glcdWriteData(uint8_t data){
  51.     SetControls(1,0);
  52.     SetData(data);
  53.     Enable();
  54.     GrLcdState.lcdXAddr++;
  55.  
  56.     if (GrLcdState.lcdXAddr > 63){
  57.         glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  58.     }
  59.     if (GrLcdState.lcdXAddr > 128){
  60.         glcdSetXY(0,GrLcdState.lcdYAddr+1);
  61.     }
  62. }
  63.  
  64. void glcdPowerOn(){
  65.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  66.     Disable();
  67.     Delay();
  68.     SetControls(0,0);
  69.     SetData(0b00111111);
  70.     Enable();
  71.    
  72.     uint8_t i;
  73.     uint8_t j;
  74.  
  75.     for(j = 0; j < 8; j++){
  76.         SetControls(0,0);
  77.         SetData(0b10111000 + j);
  78.         Enable();
  79.  
  80.         SetControls(0,0);
  81.         SetData(0b01000000);
  82.         Enable();
  83.         for (i = 0; i < 64; i++){
  84.             SetControls(1,0);
  85.             SetData(0x00);
  86.             Enable();
  87.         }
  88.     }
  89.  
  90. }
  91.  
  92. void glcdSetXY(uint8_t x, uint8_t y){
  93.     GrLcdState.lcdXAddr = x;
  94.     GrLcdState.lcdYAddr = y;
  95.  
  96.     //Vi börjar med X. Steg 1: Välj rätt chip:
  97.     if (x > 63){
  98.         LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2);
  99.         LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1);
  100.     } else {
  101.         LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1);
  102.         LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); 
  103.     }
  104.    
  105.     //Steg 2: Sätt rätt x-adress på det aktiva chippet
  106.     SetControls(0,0);
  107.     SetData(0b01000000 + x%64);
  108.  
  109.     Enable();
  110.  
  111.     //Steg 3: Sätt rätt y-adress på det aktiva chippet
  112.     SetData(0b10111000 + y);
  113.     Enable();
  114. }
  115.  
  116. void glcdWriteChar(unsigned char c)
  117. {
  118. uint8_t i = 0;
  119. if (GrLcdState.lcdXAddr > (128-5)){
  120.     glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  121. }
  122.  
  123. for(i=0; i<5; i++)
  124. {
  125.     glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  126. }
  127.     glcdWriteData(0x00);
  128.  
  129. }
  130.  
  131. void glcdPutStr(unsigned char *data){
  132.     while (*data){
  133.         glcdWriteChar(*data);
  134.         data++;
  135.     }
  136. }
  137.  
  138.