Subversion Repositories HomeAutomation

Rev

Rev 933 | Rev 1019 | Go to most recent revision | Blame | Compare with Previous | 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. #include <avr/io.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8.  
  9. #include <avr/pgmspace.h>
  10.  
  11.  
  12. volatile GrLcdStateType GrLcdState;
  13.  
  14.  
  15. void SetControls(uint8_t RS, uint8_t RW){
  16.     if(RS) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RS);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RS);};
  17.     if(RW) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RW);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RW);};
  18. }
  19.  
  20. void SetData(uint8_t Data){
  21.     if(Data&0x01) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
  22.     if(Data&0x02) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
  23.     if(Data&0x04) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
  24.     if(Data&0x08) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
  25.     if(Data&0x10) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
  26.     if(Data&0x20) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
  27.     if(Data&0x40) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
  28.     if(Data&0x80) {LCD_DATA_PORT_DB7 |= (1<<LCD_DATA_PIN_DB7);} else { LCD_DATA_PORT_DB7 &= ~(1<<LCD_DATA_PIN_DB7);};
  29. }
  30.  
  31.  
  32. void Disable(){
  33.     LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_E);
  34. }
  35.  
  36. void Delay(){
  37.     delay_us(1);
  38. }
  39.  
  40. void Enable(){
  41.     Delay();
  42.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
  43.     Delay();
  44.     Disable();
  45.     Delay();
  46. }
  47.  
  48.  
  49. void glcdWriteData(uint8_t data){
  50.     SetControls(1,0);
  51.     SetData(data);
  52.     Enable();
  53.     GrLcdState.lcdXAddr++;
  54.  
  55.     if (GrLcdState.lcdXAddr > 63){
  56.         glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  57.     }
  58.     if (GrLcdState.lcdXAddr > 128){
  59.         glcdSetXY(0,GrLcdState.lcdYAddr+1);
  60.     }
  61. }
  62.  
  63. void glcdPowerOn(){
  64.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_RS);
  65.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_RW);
  66.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_E);
  67.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS1);
  68.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS2);
  69.  
  70.     LCD_DATA_DDR_DB0 |= (1<<LCD_DATA_PIN_DB0);
  71.     LCD_DATA_DDR_DB1 |= (1<<LCD_DATA_PIN_DB1);
  72.     LCD_DATA_DDR_DB2 |= (1<<LCD_DATA_PIN_DB2);
  73.     LCD_DATA_DDR_DB3 |= (1<<LCD_DATA_PIN_DB3);
  74.     LCD_DATA_DDR_DB4 |= (1<<LCD_DATA_PIN_DB4);
  75.     LCD_DATA_DDR_DB5 |= (1<<LCD_DATA_PIN_DB5);
  76.     LCD_DATA_DDR_DB6 |= (1<<LCD_DATA_PIN_DB6);
  77.     LCD_DATA_DDR_DB7 |= (1<<LCD_DATA_PIN_DB7);
  78.  
  79.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  80.     Disable();
  81.     Delay();
  82.     //set display on
  83.     SetControls(0,0);
  84.     SetData(0x3F);
  85.     Enable();
  86.  
  87.     //set startaddress of the first row (set to row 0)
  88.     SetControls(0,0);
  89.     SetData(0xc0);
  90.     Enable();
  91.  
  92.     glcdClear();
  93. }
  94.  
  95. void glcdClear(){
  96.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  97.     Enable();
  98.     Delay();
  99.     Disable();
  100.  
  101.     uint8_t i;
  102.     uint8_t j;
  103.  
  104.     for(j = 0; j < 8; j++){
  105.         SetControls(0,0);
  106.         SetData(0xB8 + j);
  107.         Enable();
  108.  
  109.         SetControls(0,0);
  110.         SetData(0x40);
  111.         Enable();
  112.         for (i = 0; i < 64; i++){
  113.             SetControls(1,0);
  114.             SetData(0x00);
  115.             Enable();
  116.         }
  117.     }
  118.  
  119. }
  120.  
  121. void glcdSetXY(uint8_t x, uint8_t y){
  122.     GrLcdState.lcdXAddr = x;
  123.     GrLcdState.lcdYAddr = y;
  124.  
  125.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  126.     if (x > 63){
  127.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2);
  128.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS1);
  129.     } else {
  130.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS1);
  131.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS2);
  132.     }
  133.  
  134.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  135.     SetControls(0,0);
  136.     SetData(0x40 + x%64);
  137.     Enable();
  138.  
  139.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  140.     SetData(0xB8 + y);
  141.     Enable();
  142. }
  143.  
  144. void glcdWriteChar(char c)
  145. {
  146.     uint8_t i = 0;
  147.     if (GrLcdState.lcdXAddr > (128-5))
  148.     {
  149.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  150.     }
  151.  
  152.     for(i=0; i<5; i++)
  153.     {
  154.         glcdWriteData(~pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  155.     }
  156.     glcdWriteData(~0x00);
  157. }
  158.  
  159. void glcdPutStr(char *data){
  160.     while (*data){
  161.         glcdWriteChar(*data);
  162.         data++;
  163.     }
  164. }
  165.