Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.93 KB | None | 0 0
  1. /** Private function ################################################################ */
  2.  
  3. /* Envoi une commande ou un octet de donnée à l'écran */
  4. static void LCD_SendData(const uint8_t cmd, const uint8_t data)
  5. {
  6.     uint8_t bit  = 0;
  7.     uint8_t byte = 0;
  8.  
  9.     /* Place DC suivant le mode voulu (COMMAND ou DATA) */
  10.     if(cmd == LCD_COMMAND) PORTD &= ~(1 << PIN_DC);
  11.     if(cmd == LCD_DATA)    PORTD |=  (1 << PIN_DC);
  12.  
  13.     /* Synchronisation des données */
  14.     PORTD &= ~(1 << PIN_CE);
  15.  
  16.     /* Envoi la commande / donnée */
  17.     for(byte = 0; byte < 8; byte++)
  18.     {
  19.         bit = data & (1 << (7 - byte));
  20.  
  21.         if(bit)
  22.             PORTD |= (1 << PIN_DIN);
  23.         else
  24.             PORTD &= ~(1 << PIN_DIN);
  25.  
  26.         PORTD |=  (1 << PIN_CLK);
  27.         PORTD &= ~(1 << PIN_CLK);
  28.     }
  29.  
  30.     PORTD |= (1 << PIN_CE);
  31. }
  32.  
  33. /** Extern function ################################################################ */
  34.  
  35. /* Initialise l'écran en envoyant la séquence d'initialisation */
  36. void LCD_InitLCD(void)
  37. {
  38.     /* Place les broches de contrôle en sorties */
  39.     DDRD |= 0xFC;
  40.  
  41.     /* Reset l'écran pour être sur de son état initial */
  42.     PORTD &= ~(1 << PIN_RESET);
  43.     PORTD |=  (1 << PIN_RESET);
  44.  
  45.     /* Séquence d'initialisation */
  46.     LCD_SendData(LCD_COMMAND, PCD8544_EXTENDEDINSTRUCTION); // Extended Commands
  47.     LCD_SendData(LCD_COMMAND, PCD8544_SETVOP); // LCD VOP (contrast) - 0xB1 @ 3v3 ou 0xBF @ 5v
  48.     LCD_SendData(LCD_COMMAND, PCD8544_SETTEMP); // Temp coefficent
  49.     LCD_SendData(LCD_COMMAND, PCD8544_SETBIAS); // LCD bias mode = 1:48
  50.     LCD_SendData(LCD_COMMAND, PCD8544_FUNCTIONSET); // Function set
  51.     LCD_SendData(LCD_COMMAND, PCD8544_DISPLAYCONTROL); // Display control = normal mode(0x0C) (0x0D pour mode négatif)
  52. }
  53.  
  54. /* Actualise l'écran */
  55. void LCD_Udapte(void)
  56. {
  57.     uint16_t i = 0;
  58.  
  59.     /* Initialise x et y aux coordonnées 0,0 */
  60.     LCD_SendData(LCD_COMMAND, PCD8544_SETXADDR);
  61.     LCD_SendData(LCD_COMMAND, PCD8544_SETYADDR);
  62.  
  63.     for(i = 0; i < LCD_BUFFER; i++)
  64.         LCD_SendData(LCD_DATA, lcdBuf[i]);
  65. }
  66.  
  67. /* Réinitialise tout les pixels de l'écran */
  68. void LCD_Clear(void)
  69. {
  70.     uint16_t i = 0;
  71.  
  72.     for(i = 0; i < LCD_BUFFER; i++)
  73.         lcdBuf[i] = 0x00;
  74. }
  75.  
  76. /* Allume ou éteint un pixel à une position donnée */
  77. uint8_t LCD_SetPixel(Coordonnees *coord, const uint8_t pixel)
  78. {
  79.     uint16_t pos   = 0;
  80.     uint8_t bShift = 0;
  81.  
  82.     if((coord->x >= 0 && coord->x < 84)
  83.     || (coord->y >= 0 && coord->y < 48))
  84.     {
  85.         pos  = ((coord->y / 8) * 84) + coord->x;
  86.         bShift = coord->y % 8;
  87.  
  88.         if(pixel == HIGH)
  89.             lcdBuf[pos] |= (1 << bShift);
  90.         else
  91.             lcdBuf[pos] &= ~(1 << bShift);
  92.  
  93.         return EXIT_SUCCESS;
  94.     }
  95.  
  96.     return EXIT_FAILURE;
  97. }
  98.  
  99. /* Récupère l'état d'un pixel (allumé ou éteint) à une position donnée */
  100. uint8_t LCD_GetPixel(Coordonnees *coord)
  101. {
  102.     uint16_t pos = 0;
  103.     uint8_t bShift = 0;
  104.  
  105.     if((coord->x >= 0 && coord->x < 84)
  106.     || (coord->y >= 0 && coord->y < 48))
  107.     {
  108.         pos = ( (coord->y / 8) * 84) + coord->x;
  109.         bShift = coord->y % 8;
  110.  
  111.         if(lcdBuf[pos] & (1 << bShift))
  112.             return HIGH;
  113.         else
  114.             return LOW;
  115.     }
  116.  
  117.     return EXIT_FAILURE;
  118. }
  119.  
  120. /* Déssine un caractère a l'écran */
  121. void LCD_DrawChar(const char *chr, Coordonnees *coord)
  122. {
  123.     uint8_t w, h  = 0;
  124.     uint8_t bit   = 0;
  125.     uint8_t cbyte = 0;
  126.     const uint16_t val = (*chr - 0x20) * 5;
  127.     Coordonnees row = {0, 0};
  128.  
  129.     /* Si on dépasse l'écran on sort */
  130.     if(coord->x > 83 || coord->y > 47)
  131.         return;
  132.  
  133.     /* Déssine le caractère */
  134.     for(w = 0; w < WIDTH_FONT; w++)
  135.     {
  136.         cbyte = pgm_read_byte_near(asciiTable + val + w);
  137.  
  138.         for(h = 0; h < HEIGH_FONT; h++)
  139.         {
  140.             bit = cbyte & (1 << h);
  141.  
  142.             row.x = coord->x + w;
  143.             row.y = coord->y + h;
  144.  
  145.             if(bit != LOW)
  146.                 LCD_SetPixel(&row, HIGH);
  147.             else
  148.                 LCD_SetPixel(&row, LOW);
  149.         }
  150.     }
  151. }
  152.  
  153. /* Déssine une chaine de caractères a l'écran */
  154. void LCD_Print(const char *str, Coordonnees *coord)
  155. {
  156.     while(*str != '\0')
  157.     {
  158.         /* Si on dépasse l'écran on arrête */
  159.         if(coord->x > LCD_WIDTH - WIDTH_FONT)
  160.             return;
  161.  
  162.         LCD_DrawChar(str++, coord);
  163.         coord->x += WIDTH_FONT;
  164.     }
  165. }
  166.  
  167. void LCD_DrawBitmap(const uint8_t *bitmap, Coordonnees *coord,
  168.                                             Dimensions *dim)
  169. {
  170.     uint8_t bit      = 0;
  171.     uint8_t cbyte    = 0;
  172.     uint8_t w, h, b  = 0;
  173.     uint16_t iBitmap = 0;
  174.     Coordonnees row  = {0, 0};
  175.  
  176.     for(h = 0; h <= (dim->h / 8); h++)
  177.     {
  178.         for(w = 0; w < dim->w; w++)
  179.         {
  180.             cbyte = pgm_read_byte_near(bitmap + iBitmap);
  181.             iBitmap++;
  182.  
  183.             for(b = 0; b <= 8; b++)
  184.             {
  185.                 bit = cbyte & (1 << b);
  186.  
  187.                 row.x = coord->x + w;
  188.                 row.y = coord->y + b;
  189.  
  190.                 if(bit != LOW)
  191.                     LCD_SetPixel(&row, HIGH);
  192.             }
  193.         }
  194.         coord->y += 8;
  195.     }
  196. }
  197.  
  198. void LCD_DrawVLine(Coordonnees *coord, Dimensions *dim)
  199. {
  200.     uint8_t l = 0;
  201.     Coordonnees row  = {0, 0};
  202.  
  203.     for(l = 0; l <= dim->w; l++)
  204.     {
  205.         row.x = coord->x + l;
  206.         row.y = coord->y;
  207.  
  208.         if(row.x >= LCD_WIDTH)
  209.             return;
  210.  
  211.         LCD_SetPixel(&row, HIGH);
  212.     }
  213. }
  214.  
  215. void LCD_DrawHLine(Coordonnees *coord, Dimensions *dim)
  216. {
  217.     uint8_t h = 0;
  218.     Coordonnees row  = {0, 0};
  219.  
  220.     for(h = 0; h <= dim->h; h++)
  221.     {
  222.         row.x = coord->x;
  223.         row.y = coord->y + h;
  224.  
  225.         if(row.y >= LCD_WIDTH)
  226.             return;
  227.  
  228.         LCD_SetPixel(&row, HIGH);
  229.     }
  230. }
  231.  
  232. void setup(void)
  233. {
  234.     LCD_InitLCD();
  235.  
  236.     Coordonnees coord;
  237.     coord.x = 0;
  238.     coord.y = 0;
  239.    
  240.     LCD_Print("Hello world !", &coord);
  241.     LCD_Udapte();
  242. }
  243.  
  244. void loop(void)
  245. {
  246.    
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement