⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glcd_i2c_batron.c

📁 this document is source code for arm9 of atmel
💻 C
字号:
/*-----------------------------------------------------------------------------
            ATMEL Microcontroller Software Support  -  ROUSSET  -
-------------------------------------------------------------------------------
/ The software is delivered "AS IS" without warranty or condition of any
/ kind, either express, implied or statutory. This includes without
/ limitation any warranty or condition with respect to merchantability or
/ fitness for any particular purpose, or against the infringements of
/ intellectual property rights of others.
/-----------------------------------------------------------------------------
/ File Name           : glcd_i2c_batron.c
/ Object              : Graphic LCD routines for Chip-On-Glass LCD Module.
/                     :
/ Version | mm | dd | yy | author :
/   1.0     11   07   06    PFi   : Creation
/   1.1     11   04   07    PFi   : START / STOP bit clean up according to new
/                                   TWI doc.
/
/-----------------------------------------------------------------------------
 1)Description:

 The functions below show how to communicate with the 96 X 40 DOTS GLCD module,
 BT 96040AV-SRE-12-I2C-COG from BATRON is based on the Philips PCF8558 display
 controller.

 The registers of GLCD are write only.

* TO DO: Add NACK management, due to AT91SAM7S TWI errata, for functions
* which are not using the TWI Lib functions.
-----------------------------------------------------------------------------*/
/* Include Standard LIB  files */
#include "Board.h"
#include "..\common\lib_twi.h"
#include "glcd_i2c_batron.h"

extern void AT91F_Wait_MicroSecond (unsigned int MicroSecond);

#include "font\full_5_x_7_charset.h" /* This character set contains */
                                     /* standard and extended codes */


/*-----------------------------------------------------------------------------
 This Function writes one charactere in a lcd page (LcdPage) and at the specific
 column address (X_Address).
 This function uses the TWI in Master write mode with multiple data.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_PutChar(unsigned char c,
                        unsigned char LcdPage,
                        unsigned char X_Address)
{
   int i;
   unsigned int status;

   /* Enable Master Mode of the TWI */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the command + the Page Address */
    AT91C_BASE_TWI->TWI_THR = Normal+LcdPage;

    /* Wait until TXRDY is high to transmit the next data */
    status = AT91C_BASE_TWI->TWI_SR;
       while (!(status & AT91C_TWI_TXRDY))
        status = AT91C_BASE_TWI->TWI_SR;

      /* Send X address (column address) */
      AT91C_BASE_TWI->TWI_THR = X_START+X_Address;

   /* Send bit to display a character by using the charset table */
   for ( i=0; i<5; i++ )
   {
     /* Wait for the Transmit ready is set to send the next data */
     status = AT91C_BASE_TWI->TWI_SR;
      while (!(status & AT91C_TWI_TXRDY))
        status = AT91C_BASE_TWI->TWI_SR;
     /* Send the data to turn on the right pixels to display the character */
     AT91C_BASE_TWI->TWI_THR = charset[(c-32)][i];
   }


   /* Wait for the Transmit complete is set */
   status = AT91C_BASE_TWI->TWI_SR;
   while (!(status & AT91C_TWI_TXCOMP))
     status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
 This Function writes a string in a lcd page at the specific column (X_Address)
 Max 19 characters per line/string.
 Do not use /r/n if sprintf function is used before calling AT91_Glcd_PutString
-----------------------------------------------------------------------------*/
void AT91F_Glcd_PutString(char *s,
                          unsigned char LcdPage,
                          unsigned char X_Address)
{
  while ( *s != 0 )
  {
     AT91F_Glcd_PutChar(*s++, LcdPage,X_Address);
     X_Address+=LCD_CHAR_WIDTH;
  }
}

/*-----------------------------------------------------------------------------
* This Function configure the video Mode the LCD.
* - Normal: Blank background and black characters.
* - Inverse : Blank characters black background.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_VideoMode(char VideoMode)
{

  AT91F_TWI_WriteSingle(AT91C_BASE_TWI, AT91C_GLCD_I2C_ADDRESS, &VideoMode);

}

/*-----------------------------------------------------------------------------
* This Function clears a column (X_ADDRES) in a LCD page.
* This function uses the TWI in Master write mode with one data.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_ClearLcdColumn(unsigned char Column, unsigned char LcdPage)
{
    unsigned int status;

    /*  Enable Master Mode */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the LCD mode and the page to address */
    AT91C_BASE_TWI->TWI_THR = Normal+LcdPage;

    /*  Wait for the Transmit ready is set to send the data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send column address to access */
     AT91C_BASE_TWI->TWI_THR = Column;

    /* Wait for the Transmit ready is set to send the data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send 0 to clear it */
    AT91C_BASE_TWI->TWI_THR = 0x0;

    /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
* This Function clears a char in a page @ location X.
* This function uses the TWI in Master write mode with multiple data.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_ClearChar(unsigned char Column, unsigned char LcdPage)
{
    unsigned int status,i;

    /* Enable Master Mode */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the LCD mode and the page to address */
    AT91C_BASE_TWI->TWI_THR = Normal+LcdPage;

    /* Wait for the Transmit ready is set to send the data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send column address to access */
     AT91C_BASE_TWI->TWI_THR = Column;

    /* Wait for the Transmit ready is set to send the data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send 0 to clear each lcd colum for a 5 x 7 char */
    for ( i=0; i<5; i++ )
    {
      /*  Wait for the Transmit ready is set to send the next data */
      status = AT91C_BASE_TWI->TWI_SR;
        while (!(status & AT91C_TWI_TXRDY))
          status = AT91C_BASE_TWI->TWI_SR;
      /* Send the data to turn on the right pixel to display the character */
      AT91C_BASE_TWI->TWI_THR = 0x0 ;
    }

    /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
* This Function clears a LCD page by sending 0 to all pixels address.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_ClearLcdPage(unsigned char LcdPage)
{
    int Pixel;
    unsigned int status;

    /* Enable Master Mode */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the LCD mode and the page to address */
    AT91C_BASE_TWI->TWI_THR = Normal+LcdPage;

    /* Wait for the Transmit ready is set to send the next data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send X address (column address) */
    AT91C_BASE_TWI->TWI_THR = X_START;

    for ( Pixel=X_START; Pixel<X_END; Pixel++ )
    {
      /* Wait for the Transmit ready is set to send the next data */
      status = AT91C_BASE_TWI->TWI_SR;
      while (!(status & AT91C_TWI_TXRDY))
        status = AT91C_BASE_TWI->TWI_SR;

      /* Send 0 to all pixel to turn them off */
      AT91C_BASE_TWI->TWI_THR = 0x0;
    }

   /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
* This Function clears the whole LCD by sending 0 to all pixels address.
* It does not need to increment the page address. just send 0's.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_ClearLcd(void)
{
  int Page=0,Pixel;
  unsigned int status;

    /* Enable Master Mode */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the LCD mode and the page to address */
    AT91C_BASE_TWI->TWI_THR = Normal+Page;

    /* Wait for the Transmit ready is set to send the next data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send X address (column address) */
    AT91C_BASE_TWI->TWI_THR = X_START;

    for( Pixel=0; Pixel<96*40; Pixel++ )
    {
      /* Wait for the Transmit ready is set to send the next data */
      status = AT91C_BASE_TWI->TWI_SR;
      while (!(status & AT91C_TWI_TXRDY))
        status = AT91C_BASE_TWI->TWI_SR;

      /* Send 0 to all pixel to turn them off */
      AT91C_BASE_TWI->TWI_THR = 0x0;
    }

    /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;
}

/*-----------------------------------------------------------------------------
* This Function displays an image (BMP 1bpp) on the LCD.
-----------------------------------------------------------------------------*/
void AT91F_Glcd_DisplayBitmap(unsigned char *Image,
                              unsigned int X_Location,
                              unsigned int LcdPageStart,
                              unsigned int X_Size,
                              unsigned int Y_Size)
{
  unsigned int LcdPage, PixelIndex, LcdColumIndex;
  unsigned int NumOfPage=0;
  unsigned int status;

  PixelIndex = 0 ;

  if ( (LcdPage = Y_Size % LCD_PAGE_HEIGHT) == 0 )
    NumOfPage = 5;

  else
    NumOfPage= Y_Size % LCD_PAGE_HEIGHT ;

  for ( LcdPage= LcdPageStart; LcdPage < NumOfPage; LcdPage++ )
  {
    /* Enable Master Mode */
    AT91C_BASE_TWI->TWI_CR = AT91C_TWI_MSEN ;

    /* Set the TWI Master Mode Register */
    AT91C_BASE_TWI->TWI_MMR =  AT91C_GLCD_I2C_ADDRESS & ~AT91C_TWI_MREAD;

    /* Send the LCD mode and the page to address */
    AT91C_BASE_TWI->TWI_THR = Normal+LcdPage;

    /* Wait for the Transmit ready is set to send the next data */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXRDY))
      status = AT91C_BASE_TWI->TWI_SR;

    /* Send X address (column address) */
    AT91C_BASE_TWI->TWI_THR = X_START+X_Location;

    for ( LcdColumIndex = X_Location;
          (LcdColumIndex < X_Size)&&(PixelIndex < ((X_Size*Y_Size)/8));
          LcdColumIndex++ )
    {
      /* Wait for the Transmit ready is set to send the next data */
      status = AT91C_BASE_TWI->TWI_SR;
      while (!(status & AT91C_TWI_TXRDY))
        status = AT91C_BASE_TWI->TWI_SR;

      /* Send pixel */
      AT91C_BASE_TWI->TWI_THR = Image[PixelIndex];
      PixelIndex++;
    }

    /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;

  }

    /* Wait for the Transmit complete is set */
    status = AT91C_BASE_TWI->TWI_SR;
    while (!(status & AT91C_TWI_TXCOMP))
      status = AT91C_BASE_TWI->TWI_SR;

}

/*-----------------------------------------------------------------------------
Function: AT91F_Glcd_Reset(void)

Arguments:
- <const AT91PS_PIO pPio> : Pointer to a PIO structure,
- <int PioID> :  PIO peripheral ID
- <int Twck> : PIO/TWI clock pin
- <int Twd> : PIO/TWI data pin

Comments:
This function sends a "bus recovery" sequence" compatible with I2C(tm)
MEMORY RESET: After an interruption in protocol, power loss or system reset,
any two-wire part can be reset by following these steps:
1. Clock up to 9 cycles.
2. Send a stop condition.

Return Value: None
-----------------------------------------------------------------------------*/
void AT91F_Glcd_Reset (const AT91PS_PIO pPio,
                            int PioID,
                            int Twck,
                            int Twd)
{

  unsigned int ClockPulse;

  /* First, enable the clock of the PIO */
  AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_PIOA ) ;

  /* Set the TWI pins controlled by the PIO */
  AT91F_PIO_Enable(pPio,Twck|Twd ) ;

  /* then, we configure the PIO Lines corresponding to TWD & TWCK
     to be outputs. */
  AT91F_PIO_CfgOutput (pPio, Twck|Twd ) ;

  /* Enable open drain on TWCK / TWD PIO lines */
  pPio->PIO_MDER = (Twck|Twd) ;

  /* Disable the pull up on the TWI line */
  pPio->PIO_PPUDR = (Twck|Twd) ;

  /* Set TWCK and TWD to high level */
  AT91F_PIO_SetOutput(pPio, Twck|Twd ) ;
  AT91F_Wait_MicroSecond(2);

  /* Perform the bus recovery sequence */

  /* Clock up to nine cycles */
  for (ClockPulse=0 ; ClockPulse < 9 ; ClockPulse++)
  {
     /* Toggle the clock */
     AT91F_PIO_SetOutput(pPio, Twck );
     AT91F_Wait_MicroSecond(2);
     AT91F_PIO_ClearOutput(pPio, Twck );
     AT91F_Wait_MicroSecond(2);
  }

  /* Send a stop condition */
  AT91F_PIO_ClearOutput(pPio, Twck );
  AT91F_Wait_MicroSecond(2);
  AT91F_PIO_ClearOutput(pPio, Twd );
  AT91F_Wait_MicroSecond(2);

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -