Subversion Repositories HomeAutomation

Rev

Rev 762 | 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. GrLcdStateType GrLcdState;
  12.  
  13.  
  14. void SetControls(uint8_t RS, uint8_t RW){
  15.     if(RS) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RS);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RS);};
  16.     if(RW) {LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_RW);} else { LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_RW);};
  17. }
  18.  
  19. void SetData(uint8_t Data){
  20.     if(Data&0b00000001) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
  21.     if(Data&0b00000010) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
  22.     if(Data&0b00000100) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
  23.     if(Data&0b00001000) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
  24.     if(Data&0b00010000) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
  25.     if(Data&0b00100000) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
  26.     if(Data&0b01000000) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
  27.     if(Data&0b10000000) {LCD_DATA_PORT_DB7 |= (1<<LCD_DATA_PIN_DB7);} else { LCD_DATA_PORT_DB7 &= ~(1<<LCD_DATA_PIN_DB7);};
  28. }
  29.  
  30.  
  31. void Disable(){
  32.     LCD_CONTROL_PORT &= ~(1<<LCD_CONTROL_PIN_E);
  33. }
  34.  
  35. void Delay(){ //OBS!! BEHÖVER VEEERKLIGEN FIXAS!
  36.            // 1-8us      ...2-13us     ...5-31us
  37.                                // 10-60us    ...50-290us
  38. unsigned int i;             // 100-580us  ...500-2,9ms
  39. unsigned char j;            // 1000-5,8ms ...5000-29ms
  40. uint8_t p = 90;                             // 10000-56ms ...30000-170ms
  41.                              // 50000-295ms...60000-345ms
  42. //  for (i = 0; i < p; i++) for (j = 0; j < 10; j++) asm volatile ("nop");
  43.     for (i = 0; i < p; i++) for (j = 0; j < 10; j++);
  44.  
  45. }
  46. void Enable(){
  47.     Delay();
  48.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
  49.     Delay();
  50.     Disable();
  51.     Delay();
  52. }
  53.  
  54.  
  55. void glcdWriteData(uint8_t data){
  56.     SetControls(1,0);
  57.     SetData(data);
  58.     Enable();
  59.     GrLcdState.lcdXAddr++;
  60.  
  61.     if (GrLcdState.lcdXAddr > 63){
  62.         glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  63.     }
  64.     if (GrLcdState.lcdXAddr > 128){
  65.         glcdSetXY(0,GrLcdState.lcdYAddr+1);
  66.     }
  67. }
  68.  
  69. void glcdPowerOn(){
  70.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  71.     Disable();
  72.     Delay();
  73.     SetControls(0,0);
  74.     SetData(0b00111111);
  75.     Enable();
  76.    
  77.     glcdClear();
  78. }
  79.  
  80. void glcdClear(){
  81.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  82.     Enable();
  83.     Delay();
  84.     Disable();
  85.  
  86.  
  87.     uint8_t i;
  88.     uint8_t j;
  89.  
  90.     for(j = 0; j < 8; j++){
  91.         SetControls(0,0);
  92.         SetData(0b10111000 + j);
  93.         Enable();
  94.  
  95.         SetControls(0,0);
  96.         SetData(0b01000000);
  97.         Enable();
  98.         for (i = 0; i < 64; i++){
  99.             SetControls(1,0);
  100.             SetData(0x00);
  101.             Enable();
  102.         }
  103.     }
  104.  
  105. }
  106.  
  107. void glcdSetXY(uint8_t x, uint8_t y){
  108.     GrLcdState.lcdXAddr = x;
  109.     GrLcdState.lcdYAddr = y;
  110.  
  111.     //Vi börjar med X. Steg 1: Välj rätt chip:
  112.     if (x > 63){
  113.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2);
  114.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS1);
  115.     } else {
  116.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS1);
  117.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS2);  
  118.     }
  119.  
  120.  
  121.     //Steg 2: Sätt rätt x-adress på det aktiva chippet
  122.     SetControls(0,0);
  123.     SetData(0b01000000 + x%64);
  124.  
  125.     Enable();
  126.  
  127.     //Steg 3: Sätt rätt y-adress på det aktiva chippet
  128.     SetData(0b10111000 + y);
  129.    
  130.    
  131.     Enable();
  132.    
  133. }
  134.  
  135.  
  136. //New:
  137. /*
  138. void glcdWriteChar(char c)
  139. {
  140.     uint8_t i = 0;
  141.     if (GrLcdState.lcdXAddr > (128-5)){
  142.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  143.     }
  144.  
  145.     for(i=0; i<5; i++)
  146.     {
  147.         glcdWriteData(0xFF);
  148.         //glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  149.     }
  150.     glcdWriteData(0x00);
  151.  
  152. }
  153.  
  154.  
  155. void glcdPutStr(const char *s)
  156. {
  157.     register char c;
  158.    
  159.     while ( (c = *s++) ) {
  160.         glcdWriteChar(c);
  161.    
  162.     }
  163. }
  164.  
  165. */
  166.  
  167. void glcdWriteChar(char c)
  168. {
  169.     uint8_t i = 0;
  170.     if (GrLcdState.lcdXAddr > (128-5))
  171.     {
  172.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  173.     }
  174.  
  175.     for(i=0; i<5; i++)
  176.     {  
  177.         glcdWriteData(~pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  178.     }
  179.     glcdWriteData(~0x00);
  180.  
  181. }
  182.  
  183. void glcdPutStr(char *data){
  184.     while (*data){
  185.         glcdWriteChar(*data);
  186.         data++;
  187.     }
  188. }
  189.