Subversion Repositories HomeAutomation

Rev

Rev 838 | Rev 933 | 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. 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&0x01) {LCD_DATA_PORT_DB0 |= (1<<LCD_DATA_PIN_DB0);} else { LCD_DATA_PORT_DB0 &= ~(1<<LCD_DATA_PIN_DB0);};
  21.     if(Data&0x02) {LCD_DATA_PORT_DB1 |= (1<<LCD_DATA_PIN_DB1);} else { LCD_DATA_PORT_DB1 &= ~(1<<LCD_DATA_PIN_DB1);};
  22.     if(Data&0x04) {LCD_DATA_PORT_DB2 |= (1<<LCD_DATA_PIN_DB2);} else { LCD_DATA_PORT_DB2 &= ~(1<<LCD_DATA_PIN_DB2);};
  23.     if(Data&0x08) {LCD_DATA_PORT_DB3 |= (1<<LCD_DATA_PIN_DB3);} else { LCD_DATA_PORT_DB3 &= ~(1<<LCD_DATA_PIN_DB3);};
  24.     if(Data&0x10) {LCD_DATA_PORT_DB4 |= (1<<LCD_DATA_PIN_DB4);} else { LCD_DATA_PORT_DB4 &= ~(1<<LCD_DATA_PIN_DB4);};
  25.     if(Data&0x20) {LCD_DATA_PORT_DB5 |= (1<<LCD_DATA_PIN_DB5);} else { LCD_DATA_PORT_DB5 &= ~(1<<LCD_DATA_PIN_DB5);};
  26.     if(Data&0x40) {LCD_DATA_PORT_DB6 |= (1<<LCD_DATA_PIN_DB6);} else { LCD_DATA_PORT_DB6 &= ~(1<<LCD_DATA_PIN_DB6);};
  27.     if(Data&0x80) {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 = 190;                             // 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_DDR |= (1<<LCD_CONTROL_PIN_RS);
  71.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_RW);
  72.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_E);
  73.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS1);
  74.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS2);
  75.  
  76.     LCD_DATA_DDR_DB0 |= (1<<LCD_DATA_PIN_DB0);
  77.     LCD_DATA_DDR_DB1 |= (1<<LCD_DATA_PIN_DB1);
  78.     LCD_DATA_DDR_DB2 |= (1<<LCD_DATA_PIN_DB2);
  79.     LCD_DATA_DDR_DB3 |= (1<<LCD_DATA_PIN_DB3);
  80.     LCD_DATA_DDR_DB4 |= (1<<LCD_DATA_PIN_DB4);
  81.     LCD_DATA_DDR_DB5 |= (1<<LCD_DATA_PIN_DB5);
  82.     LCD_DATA_DDR_DB6 |= (1<<LCD_DATA_PIN_DB6);
  83.     LCD_DATA_DDR_DB7 |= (1<<LCD_DATA_PIN_DB7);
  84.  
  85.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  86.     Disable();
  87.     Delay();
  88.     SetControls(0,0);
  89.     SetData(0x3F);
  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.  
  102.     uint8_t i;
  103.     uint8_t j;
  104.  
  105.     for(j = 0; j < 8; j++){
  106.         SetControls(0,0);
  107.         SetData(0xB8 + j);
  108.         Enable();
  109.  
  110.         SetControls(0,0);
  111.         SetData(0x40);
  112.         Enable();
  113.         for (i = 0; i < 64; i++){
  114.             SetControls(1,0);
  115.             SetData(0x00);
  116.             Enable();
  117.         }
  118.     }
  119.  
  120. }
  121.  
  122. void glcdSetXY(uint8_t x, uint8_t y){
  123.     GrLcdState.lcdXAddr = x;
  124.     GrLcdState.lcdYAddr = y;
  125.  
  126.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  127.     if (x > 63){
  128.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2);
  129.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS1);
  130.     } else {
  131.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS1);
  132.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS2);
  133.     }
  134.  
  135.  
  136.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  137.     SetControls(0,0);
  138.     SetData(0x40 + x%64);
  139.  
  140.     Enable();
  141.  
  142.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  143.     SetData(0xB8 + y);
  144.  
  145.  
  146.     Enable();
  147.  
  148. }
  149.  
  150.  
  151. //New:
  152. /*
  153. void glcdWriteChar(char c)
  154. {
  155.     uint8_t i = 0;
  156.     if (GrLcdState.lcdXAddr > (128-5)){
  157.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  158.     }
  159.  
  160.     for(i=0; i<5; i++)
  161.     {
  162.         glcdWriteData(0xFF);
  163.         //glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  164.     }
  165.     glcdWriteData(0x00);
  166.  
  167. }
  168.  
  169.  
  170. void glcdPutStr(const char *s)
  171. {
  172.     register char c;
  173.  
  174.     while ( (c = *s++) ) {
  175.         glcdWriteChar(c);
  176.  
  177.     }
  178. }
  179.  
  180. */
  181.  
  182. void glcdWriteChar(char c)
  183. {
  184.     uint8_t i = 0;
  185.     if (GrLcdState.lcdXAddr > (128-5))
  186.     {
  187.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  188.     }
  189.  
  190.     for(i=0; i<5; i++)
  191.     {
  192.         glcdWriteData(~pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  193.     }
  194.     glcdWriteData(~0x00);
  195.  
  196. }
  197.  
  198. void glcdPutStr(char *data){
  199.     while (*data){
  200.         glcdWriteChar(*data);
  201.         data++;
  202.     }
  203. }
  204.