Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Private function ################################################################ */
- /* Envoi une commande ou un octet de donnée à l'écran */
- static void LCD_SendData(const uint8_t cmd, const uint8_t data)
- {
- uint8_t bit = 0;
- uint8_t byte = 0;
- /* Place DC suivant le mode voulu (COMMAND ou DATA) */
- if(cmd == LCD_COMMAND) PORTD &= ~(1 << PIN_DC);
- if(cmd == LCD_DATA) PORTD |= (1 << PIN_DC);
- /* Synchronisation des données */
- PORTD &= ~(1 << PIN_CE);
- /* Envoi la commande / donnée */
- for(byte = 0; byte < 8; byte++)
- {
- bit = data & (1 << (7 - byte));
- if(bit)
- PORTD |= (1 << PIN_DIN);
- else
- PORTD &= ~(1 << PIN_DIN);
- PORTD |= (1 << PIN_CLK);
- PORTD &= ~(1 << PIN_CLK);
- }
- PORTD |= (1 << PIN_CE);
- }
- /** Extern function ################################################################ */
- /* Initialise l'écran en envoyant la séquence d'initialisation */
- void LCD_InitLCD(void)
- {
- /* Place les broches de contrôle en sorties */
- DDRD |= 0xFC;
- /* Reset l'écran pour être sur de son état initial */
- PORTD &= ~(1 << PIN_RESET);
- PORTD |= (1 << PIN_RESET);
- /* Séquence d'initialisation */
- LCD_SendData(LCD_COMMAND, PCD8544_EXTENDEDINSTRUCTION); // Extended Commands
- LCD_SendData(LCD_COMMAND, PCD8544_SETVOP); // LCD VOP (contrast) - 0xB1 @ 3v3 ou 0xBF @ 5v
- LCD_SendData(LCD_COMMAND, PCD8544_SETTEMP); // Temp coefficent
- LCD_SendData(LCD_COMMAND, PCD8544_SETBIAS); // LCD bias mode = 1:48
- LCD_SendData(LCD_COMMAND, PCD8544_FUNCTIONSET); // Function set
- LCD_SendData(LCD_COMMAND, PCD8544_DISPLAYCONTROL); // Display control = normal mode(0x0C) (0x0D pour mode négatif)
- }
- /* Actualise l'écran */
- void LCD_Udapte(void)
- {
- uint16_t i = 0;
- /* Initialise x et y aux coordonnées 0,0 */
- LCD_SendData(LCD_COMMAND, PCD8544_SETXADDR);
- LCD_SendData(LCD_COMMAND, PCD8544_SETYADDR);
- for(i = 0; i < LCD_BUFFER; i++)
- LCD_SendData(LCD_DATA, lcdBuf[i]);
- }
- /* Réinitialise tout les pixels de l'écran */
- void LCD_Clear(void)
- {
- uint16_t i = 0;
- for(i = 0; i < LCD_BUFFER; i++)
- lcdBuf[i] = 0x00;
- }
- /* Allume ou éteint un pixel à une position donnée */
- uint8_t LCD_SetPixel(Coordonnees *coord, const uint8_t pixel)
- {
- uint16_t pos = 0;
- uint8_t bShift = 0;
- if((coord->x >= 0 && coord->x < 84)
- || (coord->y >= 0 && coord->y < 48))
- {
- pos = ((coord->y / 8) * 84) + coord->x;
- bShift = coord->y % 8;
- if(pixel == HIGH)
- lcdBuf[pos] |= (1 << bShift);
- else
- lcdBuf[pos] &= ~(1 << bShift);
- return EXIT_SUCCESS;
- }
- return EXIT_FAILURE;
- }
- /* Récupère l'état d'un pixel (allumé ou éteint) à une position donnée */
- uint8_t LCD_GetPixel(Coordonnees *coord)
- {
- uint16_t pos = 0;
- uint8_t bShift = 0;
- if((coord->x >= 0 && coord->x < 84)
- || (coord->y >= 0 && coord->y < 48))
- {
- pos = ( (coord->y / 8) * 84) + coord->x;
- bShift = coord->y % 8;
- if(lcdBuf[pos] & (1 << bShift))
- return HIGH;
- else
- return LOW;
- }
- return EXIT_FAILURE;
- }
- /* Déssine un caractère a l'écran */
- void LCD_DrawChar(const char *chr, Coordonnees *coord)
- {
- uint8_t w, h = 0;
- uint8_t bit = 0;
- uint8_t cbyte = 0;
- const uint16_t val = (*chr - 0x20) * 5;
- Coordonnees row = {0, 0};
- /* Si on dépasse l'écran on sort */
- if(coord->x > 83 || coord->y > 47)
- return;
- /* Déssine le caractère */
- for(w = 0; w < WIDTH_FONT; w++)
- {
- cbyte = pgm_read_byte_near(asciiTable + val + w);
- for(h = 0; h < HEIGH_FONT; h++)
- {
- bit = cbyte & (1 << h);
- row.x = coord->x + w;
- row.y = coord->y + h;
- if(bit != LOW)
- LCD_SetPixel(&row, HIGH);
- else
- LCD_SetPixel(&row, LOW);
- }
- }
- }
- /* Déssine une chaine de caractères a l'écran */
- void LCD_Print(const char *str, Coordonnees *coord)
- {
- while(*str != '\0')
- {
- /* Si on dépasse l'écran on arrête */
- if(coord->x > LCD_WIDTH - WIDTH_FONT)
- return;
- LCD_DrawChar(str++, coord);
- coord->x += WIDTH_FONT;
- }
- }
- void LCD_DrawBitmap(const uint8_t *bitmap, Coordonnees *coord,
- Dimensions *dim)
- {
- uint8_t bit = 0;
- uint8_t cbyte = 0;
- uint8_t w, h, b = 0;
- uint16_t iBitmap = 0;
- Coordonnees row = {0, 0};
- for(h = 0; h <= (dim->h / 8); h++)
- {
- for(w = 0; w < dim->w; w++)
- {
- cbyte = pgm_read_byte_near(bitmap + iBitmap);
- iBitmap++;
- for(b = 0; b <= 8; b++)
- {
- bit = cbyte & (1 << b);
- row.x = coord->x + w;
- row.y = coord->y + b;
- if(bit != LOW)
- LCD_SetPixel(&row, HIGH);
- }
- }
- coord->y += 8;
- }
- }
- void LCD_DrawVLine(Coordonnees *coord, Dimensions *dim)
- {
- uint8_t l = 0;
- Coordonnees row = {0, 0};
- for(l = 0; l <= dim->w; l++)
- {
- row.x = coord->x + l;
- row.y = coord->y;
- if(row.x >= LCD_WIDTH)
- return;
- LCD_SetPixel(&row, HIGH);
- }
- }
- void LCD_DrawHLine(Coordonnees *coord, Dimensions *dim)
- {
- uint8_t h = 0;
- Coordonnees row = {0, 0};
- for(h = 0; h <= dim->h; h++)
- {
- row.x = coord->x;
- row.y = coord->y + h;
- if(row.y >= LCD_WIDTH)
- return;
- LCD_SetPixel(&row, HIGH);
- }
- }
- void setup(void)
- {
- LCD_InitLCD();
- Coordonnees coord;
- coord.x = 0;
- coord.y = 0;
- LCD_Print("Hello world !", &coord);
- LCD_Udapte();
- }
- void loop(void)
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement