Subversion Repositories HomeAutomation

Rev

Rev 389 | 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.  
  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.     glcdClear();
  73. }
  74.  
  75. void glcdClear(){
  76.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  77.     Enable();
  78.     Delay();
  79.     Disable();
  80.  
  81.  
  82.     uint8_t i;
  83.     uint8_t j;
  84.  
  85.     for(j = 0; j < 8; j++){
  86.         SetControls(0,0);
  87.         SetData(0b10111000 + j);
  88.         Enable();
  89.  
  90.         SetControls(0,0);
  91.         SetData(0b01000000);
  92.         Enable();
  93.         for (i = 0; i < 64; i++){
  94.             SetControls(1,0);
  95.             SetData(0x00);
  96.             Enable();
  97.         }
  98.     }
  99.  
  100. }
  101.  
  102. void glcdSetXY(uint8_t x, uint8_t y){
  103.     GrLcdState.lcdXAddr = x;
  104.     GrLcdState.lcdYAddr = y;
  105.  
  106.     //Vi börjar med X. Steg 1: Välj rätt chip:
  107.     if (x > 63){
  108.         LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS2);
  109.         LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS1);
  110.     } else {
  111.         LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_CS1);
  112.         LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_CS2); 
  113.     }
  114.    
  115.     //Steg 2: Sätt rätt x-adress på det aktiva chippet
  116.     SetControls(0,0);
  117.     SetData(0b01000000 + x%64);
  118.  
  119.     Enable();
  120.  
  121.     //Steg 3: Sätt rätt y-adress på det aktiva chippet
  122.     SetData(0b10111000 + y);
  123.     Enable();
  124. }
  125.  
  126. void glcdWriteChar(unsigned char c)
  127. {
  128. uint8_t i = 0;
  129. if (GrLcdState.lcdXAddr > (128-5)){
  130.     glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  131. }
  132.  
  133. for(i=0; i<5; i++)
  134. {
  135.     glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  136. }
  137.     glcdWriteData(0x00);
  138.  
  139. }
  140.  
  141. void glcdPutStr(unsigned char *data){
  142.     while (*data){
  143.         glcdWriteChar(*data);
  144.         data++;
  145.     }
  146. }
  147.  
  148.