ST7920 Testes

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Marcelocostanzo
Date:
Tue Dec 15 12:27:29 2020 +0000
Parent:
1:08481151e25e
Commit message:
Versao funcionando com linhas horizontais e verticais e strings

Changed in this revision

ST7920.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/ST7920.cpp	Mon Dec 14 19:10:22 2020 +0000
+++ b/ST7920.cpp	Tue Dec 15 12:27:29 2020 +0000
@@ -10,79 +10,145 @@
 
 void ST7920_Init()
 {
-    wait_ms(40); // wait 40ms
+    //function set  8-bit mode
+    wait_ms(100);
+    RS = 0;
+    RW = 0;
+    wait_us(100);
+    EN = 1;
+    DB = FUNCTION_SET | DATA_LENGTH_DL;
+    wait_us(100);
     EN = 0;
+
+    //function set  8-bit mode again
+    wait_us(1000);
     RS = 0;
     RW = 0;
     wait_us(100);
     EN = 1;
     DB = FUNCTION_SET | DATA_LENGTH_DL;
+    wait_us(100);
     EN = 0;
-    
-    wait_us(100); // wait 100us
-    RS = 0;
-    RW = 0;
-    wait_us(100);
-    EN = 1;
-    DB = FUNCTION_SET | DATA_LENGTH_DL;
-    EN = 0;
-    
-    
-    wait_us(37); // wait 37us
+
+    // display on
+    wait_us(1000);
     RS = 0;
     RW = 0;
     wait_us(100);
     EN = 1;
     DB = DISPLAY_CONTROL | DISPLAY_ON_D;
+    wait_us(100);
     EN = 0;
-    
-    
-    wait_us(100); // wait 100us
+
+    // cursor - Display shift right by 1, cursor also follows to shift.
+    wait_us(1000);
+    RS = 0;
+    RW = 0;
+    wait_us(100);
+    EN = 1;
+    DB = 0x1C;
+    wait_us(100);
+    EN = 0;
+
+    // clear screen
+    wait_us(1000);
     RS = 0;
     RW = 0;
     wait_us(100);
     EN = 1;
     DB = DISPLAY_CLEAR;
+    wait_us(100);
     EN = 0;
-    
-    wait_ms(10); // wait 10ms
-    RS = 0;
-    RW = 0;
-    wait_us(100);
-    EN = 1;
-    DB = ENTRY_MODE_SET | INCREASE_DECREASE_ID;
-    EN = 0;
-    
-    wait_ms(10); // wait 10ms
+
+    // Returns to home. Cursor moves to starting point
+    wait_ms(1);
     RS = 0;
     RW = 0;
     wait_us(100);
     EN = 1;
     DB = RETURN_HOME;
+    wait_us(100);
+    EN = 0;
+
+    // Setting location to write characters. In this case 0,0 - Top Left Corner
+    wait_ms(1);
+    RS = 0;
+    RW = 0;
+    wait_us(100);
+    EN = 1;
+    DB = 0x80;
+    wait_us(100);
     EN = 0;
 }
 
 void ST7920_Clear()
 {
-    /*wait_us(100);
-    RS = 0;
-    RW = 0;
-    EN = 1;
-    wait_us(100);
-    DB = FUNCTION_SET | DATA_LENGTH_DL;
-    EN = 0;
-    */
-    wait_us(100);
-    RS = 0;
-    RW = 0;
-    EN = 1;
-    wait_us(100);
-    DB = DISPLAY_CLEAR;
-    EN = 0;
+    //=========== This function set all the pixels to off in the graphic controller =================
+    for(int y = 0; y < 64; y++) 
+    {
+        if(y < 32) 
+        {
+            wait_us(100);//wait_us(1000);
+            RS = 0;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x80 | y;
+            wait_us(100);
+            EN = 0;
+            wait_us(100);
+
+            wait_us(100);//wait_us(1000);
+            RS = 0;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x80;
+            wait_us(100);
+            EN = 0;
+            wait_us(100);
+        }
+
+        else 
+        {
+            wait_us(100);//wait_us(1000);
+            RS = 0;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x80 | (y - 32);
+            wait_us(100);
+            EN = 0;
+            wait_us(100);
+
+            wait_us(100);//wait_us(1000);
+            RS = 0;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x88;
+            wait_us(100);
+            EN = 0;
+            wait_us(100);
+        }
+
+        for(int x = 0; x < 16; x++) 
+        {
+            wait_us(100);//wait_us(1000);
+            RS = 1;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x00;
+            wait_us(100);
+            EN = 0;
+            wait_us(20);
+        }
+    }
 }
 
 void ST7920_SetGraphicsMode()
-{    
+{
     wait_us(1000);
     RS = 0;
     RW = 0;
@@ -91,7 +157,7 @@
     DB = EXTENDED_FUNCTION_SET | DATA_LENGTH_DL;
     wait_us(100);
     EN = 0;
-    
+
     wait_us(1000);
     RS = 0;
     RW = 0;
@@ -99,8 +165,8 @@
     wait_us(100);
     DB = EXTENDED_FUNCTION_SET | DATA_LENGTH_DL | EXTENDED_INSTRUCTION_RE;
     EN = 0;
-    
-    wait_us(1000);
+
+    wait_us(100);
     RS = 0;
     RW = 0;
     EN = 1;
@@ -109,7 +175,7 @@
     EN = 0;
 }
 
-void ST7920_SetTextMode() 
+void ST7920_SetTextMode()
 {
     wait_us(100);
     RS = 0;
@@ -122,70 +188,7 @@
 void ST7920_DrawHorizontalLine(uint8_t X, uint8_t Y,  uint8_t LEN)
 {
     //desenhando uma linha de tamanho T em Y, com X offset
-    
-    wait_us(1000);
-    RS = 0;
-    RW = 0;
-    wait_us(100);
-    EN = ON;
-    DB = 0x80 | Y;
-    wait_us(100);
-    EN = OFF;
-    wait_us(100);
-    
-    wait_us(1000);
-    RS = 0;
-    RW = 0;
-    wait_us(100);
-    EN = ON;
-    DB = 0x80 | X;
-    wait_us(100);
-    EN = OFF;
-    wait_us(100);
-    
-    for(int i = 0; i < (LEN / 8); i++)
-    {
-        wait_us(1000);
-        RS = 1;
-        RW = 0;
-        wait_us(100);
-        EN = ON;
-        DB = (255);
-        wait_us(100);
-        EN = OFF;
-        wait_us(20);
-    }
 
-    if(LEN%8 != 0)
-    {
-        int LEN_rest = LEN - ((LEN / 8) * 8);
-        int bus_rest;
-        
-        switch (LEN_rest) 
-        {
-            case 1: bus_rest = 0x80; break;
-            case 2: bus_rest = 0xC0; break;
-            case 3: bus_rest = 0xE0; break;
-            case 4: bus_rest = 0xF0; break;
-            case 5: bus_rest = 0xF8; break;
-            case 6: bus_rest = 0xFC; break;
-            case 7: bus_rest = 0xFE; break;
-        }
-        //pc.printf("%x\n\r",bus_rest);
-        wait_us(1000);
-        RS = 1;
-        RW = 0;
-        wait_us(100);
-        EN = ON;
-        DB = bus_rest; // tem que ser 254 no exemplo de T 255
-        wait_us(100);
-        EN = OFF;
-        wait_us(20);
-    }
-}
-
-void ST7920_DrawVerticalLine(uint8_t X, uint8_t Y,  uint8_t LEN)
-{    
     wait_us(1000);
     RS = 0;
     RW = 0;
@@ -195,7 +198,7 @@
     wait_us(100);
     EN = 0;
     wait_us(100);
-    
+
     wait_us(1000);
     RS = 0;
     RW = 0;
@@ -205,66 +208,142 @@
     wait_us(100);
     EN = 0;
     wait_us(100);
-    
-    for(int y = 0; y < LEN; y++)
-   {
-       if(y < 32)
-       {
-            wait_us(1000);
+
+    for(int i = 0; i < (LEN / 8); i++) 
+    {
+        wait_us(1000);
+        RS = 1;
+        RW = 0;
+        wait_us(100);
+        EN = 1;
+        DB = 255;
+        wait_us(100);
+        EN = 0;
+        wait_us(20);
+    }
+
+    if(LEN%8 != 0) 
+    {
+        int LEN_rest = LEN - ((LEN / 8) * 8);
+        int bus_rest;
+
+        switch (LEN_rest) 
+        {
+            case 1:
+                bus_rest = 0x80;
+                break;
+            case 2:
+                bus_rest = 0xC0;
+                break;
+            case 3:
+                bus_rest = 0xE0;
+                break;
+            case 4:
+                bus_rest = 0xF0;
+                break;
+            case 5:
+                bus_rest = 0xF8;
+                break;
+            case 6:
+                bus_rest = 0xFC;
+                break;
+            case 7:
+                bus_rest = 0xFE;
+                break;
+        }
+        //pc.printf("%x\n\r",bus_rest);
+        wait_us(1000);
+        RS = 1;
+        RW = 0;
+        wait_us(100);
+        EN = 1;
+        DB = bus_rest; // tem que ser 254 no exemplo de T 255
+        wait_us(100);
+        EN = 0;
+        wait_us(20);
+    }
+}
+
+void ST7920_DrawVerticalLine(uint8_t X, uint8_t Y,  uint8_t LEN)
+{
+    wait_us(100);
+    RS = 0;
+    RW = 0;
+    wait_us(100);
+    EN = 1;
+    DB = 0x80 | Y;
+    wait_us(100);
+    EN = 0;
+    wait_us(100);
+
+    wait_us(100);
+    RS = 0;
+    RW = 0;
+    wait_us(100);
+    EN = 1;
+    DB = 0x80 | X;
+    wait_us(100);
+    EN = 0;
+    wait_us(100);
+
+    for(int y = 0; y < LEN; y++) 
+    {
+        if(y < 32) {
+            wait_us(100);
             RS = 0;
             RW = 0;
             wait_us(100);
-            EN = ON;
-            DB = (0x80 | y);
+            EN = 1;
+            DB = 0x80 | y;
             wait_us(100);
-            EN = OFF;
+            EN = 0;
             wait_us(100);
-            
+
             wait_us(1000);
             RS = 0;
             RW = 0;
             wait_us(100);
-            EN = ON;
+            EN = 1;
             DB = 0x80;
             wait_us(100);
-            EN = OFF;
+            EN = 0;
+            wait_us(100);
+        }
+
+        else 
+        {
             wait_us(100);
-       }
-       
-       else
-       {
+            RS = 0;
+            RW = 0;
+            wait_us(100);
+            EN = 1;
+            DB = 0x80 | (y - 32);
+            wait_us(100);
+            EN = 0;
+            wait_us(100);
+
             wait_us(1000);
             RS = 0;
             RW = 0;
             wait_us(100);
-            EN = ON;
-            DB = (0x80 | (y - 32));
-            wait_us(100);
-            EN = OFF;
-            wait_us(100);
-            
-            wait_us(1000);
-            RS = 0;
-            RW = 0;
-            wait_us(100);
-            EN = ON;
+            EN = 1;
             DB = 0x88;
             wait_us(100);
-            EN = OFF;
+            EN = 0;
             wait_us(100);
-       }     
-    
-       wait_us(1000);
-       RS = 1;
-       RW = 0;
-       wait_us(100);
-       EN = ON;
-       DB = 0x80;
-       wait_us(100);
-       EN = OFF;
-       wait_us(20);
-    }   
-} 
+        }
+
+        wait_us(100);
+        RS = 1;
+        RW = 0;
+        wait_us(100);
+        EN = 1;
+        DB = 0x80;
+        wait_us(100);
+        EN = 0;
+        wait_us(20);
+    }
+}
 
 void ST7920_DisplayString(int Row,int Column,unsigned char *ptr,int length)
 {
@@ -289,11 +368,12 @@
             break;
     }
 
-    if (Column%2!=0) {
+    if (Column%2!=0) 
+    {
         Column-=1;
         i=1;
     }
-    
+
     wait_us(100);
     RS = 0;
     RW = 0;
@@ -320,4 +400,4 @@
         DB = ((unsigned int)ptr[i]);
         EN = 0;
     }
-}
\ No newline at end of file
+}
--- a/main.cpp	Mon Dec 14 19:10:22 2020 +0000
+++ b/main.cpp	Tue Dec 15 12:27:29 2020 +0000
@@ -1,53 +1,30 @@
-/*
-*   ATENÇÃO ST7920_CLEAR DESATIVA O  MODO GRÁFICO
-*
-*
-*/
-
 #include "mbed.h"
 #include "ST7920.h"
 
 Serial pc(USBTX, USBRX);
 
-int main() 
-{ 
-    unsigned char texto[]={'O','l','a','!', ' ', 'S', 'T', '7', '9', '2', '0'};
+int main()
+{
+    unsigned char texto[11]= {'O','l','a','!', ' ', 'S', 'T', '7', '9', '2', '0'};
     pc.printf("OI \n\r");
-    
+
     ST7920_Init();
-    wait_ms(100); 
-    
-    //ST7920_Clear();
-    //wait_ms(100); 
-    
-    //ST7920_SetGraphicsMode();
-    //wait_ms(100);  
-    
-    //ST7920_Clear();
-    //wait_ms(100); 
-    
-    //ST7920_SetGraphicsMode();
-    
-    //ST7920_Clear();  
-    
-    //ST7920_DrawHorizontalLine(0, 0, 100);  
-    //wait_ms(100);
-    
-    //ST7920_DrawVerticalLine(0, 0, 50);  
-    //wait_ms(100);
-    
-    //ST7920_SetTextMode();
-    //wait_ms(100);
-    
-    ST7920_Clear();  
-    //wait_ms(100);
-    
-    ST7920_DisplayString(1, 1, texto, sizeof(texto));
-    //wait_ms(100);
-    
+
+    ST7920_SetGraphicsMode();
+
+    ST7920_Clear();
+
+    ST7920_DrawHorizontalLine(0, 0, 30);
+
+    ST7920_DrawVerticalLine(0, 0, 50);
+
+    ST7920_SetTextMode();
+
+    ST7920_DisplayString(0, 0, texto, 11);
+
     while(1) 
     {
-       
+
     }
 }