Subversion Repositories HomeAutomation

Rev

Rev 930 | Rev 1017 | 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.     delay_us(1);
  37. }// 1-8us      ...2-13us     ...5-31us
  38.  /*                              // 10-60us    ...50-290us
  39. unsigned int i;             // 100-580us  ...500-2,9ms
  40. unsigned char j;            // 1000-5,8ms ...5000-29ms
  41. uint8_t p = 250;                             // 10000-56ms ...30000-170ms
  42.                              // 50000-295ms...60000-345ms
  43. //  for (i = 0; i < p; i++) for (j = 0; j < 10; j++) asm volatile ("nop");
  44.     for (i = 0; i < p; i++) for (j = 0; j < 50; j++);
  45.  
  46. }
  47. */
  48. void Enable(){
  49.     Delay();
  50.     LCD_CONTROL_PORT |= (1<<LCD_CONTROL_PIN_E);
  51.     Delay();
  52.     Disable();
  53.     Delay();
  54. }
  55.  
  56.  
  57. void glcdWriteData(uint8_t data){
  58.     SetControls(1,0);
  59.     SetData(data);
  60.     Enable();
  61.     GrLcdState.lcdXAddr++;
  62.  
  63.     if (GrLcdState.lcdXAddr > 63){
  64.         glcdSetXY(GrLcdState.lcdXAddr,GrLcdState.lcdYAddr);
  65.     }
  66.     if (GrLcdState.lcdXAddr > 128){
  67.         glcdSetXY(0,GrLcdState.lcdYAddr+1);
  68.     }
  69. }
  70.  
  71. void glcdPowerOn(){
  72.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_RS);
  73.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_RW);
  74.     LCD_CONTROL_DDR |= (1<<LCD_CONTROL_PIN_E);
  75.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS1);
  76.     LCD_CONTROL_DDR_CS |= (1<<LCD_CONTROL_PIN_CS2);
  77.  
  78.     LCD_DATA_DDR_DB0 |= (1<<LCD_DATA_PIN_DB0);
  79.     LCD_DATA_DDR_DB1 |= (1<<LCD_DATA_PIN_DB1);
  80.     LCD_DATA_DDR_DB2 |= (1<<LCD_DATA_PIN_DB2);
  81.     LCD_DATA_DDR_DB3 |= (1<<LCD_DATA_PIN_DB3);
  82.     LCD_DATA_DDR_DB4 |= (1<<LCD_DATA_PIN_DB4);
  83.     LCD_DATA_DDR_DB5 |= (1<<LCD_DATA_PIN_DB5);
  84.     LCD_DATA_DDR_DB6 |= (1<<LCD_DATA_PIN_DB6);
  85.     LCD_DATA_DDR_DB7 |= (1<<LCD_DATA_PIN_DB7);
  86.  
  87.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  88.     Disable();
  89.     Delay();
  90.     SetControls(0,0);
  91.     SetData(0x3F);
  92.     Enable();
  93.  
  94.     glcdClear();
  95. }
  96.  
  97. void glcdClear(){
  98.     LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2)|(1<<LCD_CONTROL_PIN_CS1);
  99.     Enable();
  100.     Delay();
  101.     Disable();
  102.  
  103.  
  104.     uint8_t i;
  105.     uint8_t j;
  106.  
  107.     for(j = 0; j < 8; j++){
  108.         SetControls(0,0);
  109.         SetData(0xB8 + j);
  110.         Enable();
  111.  
  112.         SetControls(0,0);
  113.         SetData(0x40);
  114.         Enable();
  115.         for (i = 0; i < 64; i++){
  116.             SetControls(1,0);
  117.             SetData(0x00);
  118.             Enable();
  119.         }
  120.     }
  121.  
  122. }
  123.  
  124. void glcdSetXY(uint8_t x, uint8_t y){
  125.     GrLcdState.lcdXAddr = x;
  126.     GrLcdState.lcdYAddr = y;
  127.  
  128.     //Vi b�rjar med X. Steg 1: V�lj r�tt chip:
  129.     if (x > 63){
  130.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS2);
  131.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS1);
  132.     } else {
  133.         LCD_CONTROL_PORT_CS |= (1<<LCD_CONTROL_PIN_CS1);
  134.         LCD_CONTROL_PORT_CS &= ~(1<<LCD_CONTROL_PIN_CS2);
  135.     }
  136.  
  137.  
  138.     //Steg 2: S�tt r�tt x-adress p� det aktiva chippet
  139.     SetControls(0,0);
  140.     SetData(0x40 + x%64);
  141.  
  142.     Enable();
  143.  
  144.     //Steg 3: S�tt r�tt y-adress p� det aktiva chippet
  145.     SetData(0xB8 + y);
  146.  
  147.  
  148.     Enable();
  149.  
  150. }
  151.  
  152.  
  153. //New:
  154. /*
  155. void glcdWriteChar(char c)
  156. {
  157.     uint8_t i = 0;
  158.     if (GrLcdState.lcdXAddr > (128-5)){
  159.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  160.     }
  161.  
  162.     for(i=0; i<5; i++)
  163.     {
  164.         glcdWriteData(0xFF);
  165.         //glcdWriteData(pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  166.     }
  167.     glcdWriteData(0x00);
  168.  
  169. }
  170.  
  171.  
  172. void glcdPutStr(const char *s)
  173. {
  174.     register char c;
  175.  
  176.     while ( (c = *s++) ) {
  177.         glcdWriteChar(c);
  178.  
  179.     }
  180. }
  181.  
  182. */
  183.  
  184. void glcdWriteChar(char c)
  185. {
  186.     uint8_t i = 0;
  187.     if (GrLcdState.lcdXAddr > (128-5))
  188.     {
  189.         glcdSetXY(0,GrLcdState.lcdYAddr + 1);
  190.     }
  191.  
  192.     for(i=0; i<5; i++)
  193.     {
  194.         glcdWriteData(~pgm_read_byte(&Font5x7[((c - 0x20) * 5) + i])); //Borde snyggas till med pekare
  195.     }
  196.     glcdWriteData(~0x00);
  197.  
  198. }
  199.  
  200. void glcdPutStr(char *data){
  201.     while (*data){
  202.         glcdWriteChar(*data);
  203.         data++;
  204.     }
  205. }
  206.