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

📄 Копия lcd.c

📁 This is Temperature PID regulator for Tamman ovens. Temperature is measured by thermocouple via lm35
💻 C
字号:
// PC0 -> LCD DB7
// PC1 -> LCD DB6
// PC2 -> LCD DB5
// PC3 -> LCD DB4
// PC4 -> LCD E
// PC5 -> LCD R/W
// PC6 -> LCD RS
// PC7 -> LCD BackLight
//
// PD0 -> 
// PD1 -> 
// PD2 -> 
// PD3 -> 
// PD4 -> 
// PD5 -> 
// PD6 -> 
// PD7 -> LCD CONTRAST
//
//*****************************************************************************
//                        T I M E R   U S A G E
//
// Timer 0 not use
// Timer 1 not use
// Timer 2 not use
// Timer 3 not use
//
//*****************************************************************************

//*****************************************************************************
//                            I N C L U D E
//*****************************************************************************
#include <mega32.h>
#include <delay.h>
#include "shortnametype.h"
#include "lcd.h"

//*****************************************************************************
//                            D E F I N E
//*****************************************************************************

// LCD
#define LCD_D7                  0b10000000
#define LCD_D6                  0b01000000
#define LCD_D5                  0b00100000
#define LCD_D4                  0b00010000
#define LCD_E                   0b00000001
#define LCD_RW                  0x00
#define LCD_RS                  0b00000010
#define LCD_BACKLIGHT           0x00

#define LCD_PIN                 PINB
#define LCD_DDR                 DDRB
#define LCD_PORT                PORTB

#define LCD_DATA                0x01
#define LCD_CTRL                0x00

#define LCD_C_BLINK_ON          0x09
#define LCD_C_BLINK_OFF         0xfe
#define LCD_C_ON                0x0a
#define LCD_C_OFF               0xfd
#define LCD_DISPLAY_ON          0x0c
#define LCD_DISPLAY_OFF         0xfb
#define LCD_BRIGHT_25           0x23
#define LCD_BRIGHT_50           0x22
#define LCD_BRIGHT_75           0x21
#define LCD_BRIGHT_100          0x20
#define LCD_BACKLIGHT_ON        0x24
#define LCD_BACKLIGHT_OFF       0x25

//*****************************************************************************
//                      G L O B A L   V A R I A B L E
//*****************************************************************************
// LCD
unsigned char MaxX, MaxY, LCDStat;

//*****************************************************************************
// LCD Code
//*****************************************************************************
/******************************************************************************

Name:         LCDCreateCHR

Description:  Create a new char in CGRAM

Input:        unsigned char  : Character number (0-7)
              *unsigned char : 7 unsigned char containing the data

Output:       none

Misc:

******************************************************************************/
void LCDCreateCHR(unsigned char chnumber, char *ptr)
{
  unsigned char i,j;

  i = 8 * chnumber;

  for (j=0;j<8;j++)
  {
    LCDWriteData(LCD_CTRL,(0x40 | (i + j)));
    LCDWriteData(LCD_DATA,ptr[j]);
  }
}


/******************************************************************************

Name:         void LCDClrSCR(void)

Description:  Clear the LCD

Input:        none

Output:       none

Misc:

******************************************************************************/
void LCDClrSCR(void)
{ 
  LCDWriteData(LCD_CTRL,0x01);      // Clear display
  delay_ms(2);
}

/******************************************************************************

Name:         LCDGotoXY(unsigned char x, unsigned char y)

Description:  Position cursor on the LCD at X & Y location

Input:        X -> X position on the LCD
              Y -> Y position on the LCD

Output:       none

Misc:

******************************************************************************/
void LCDGotoXY(unsigned char x,unsigned char y)
{
  unsigned char address;

  x--;

  if (MaxY < 3)
  {
    switch(y)
    {
      case '\x01' :
      address = 0 + x;
      break;
      case '\x02' :
      address = 64 + x;
      break;
    }
  }
  else
  {
    switch(y)
    {
      case '\x01' :
      address = 0 + x;
      break;
      case '\x02' :
      address = 64 + x;
      break;
      case '\x03' :
      address = 20 + x;
      break;
      case '\x04' :
      address = 84 + x;
      break;
    }
  }
  LCDWriteData(LCD_CTRL,address | 0x80);
}

/******************************************************************************

Name:         WtireByteLCD(char byte)

Description:  Write a byte on the LCD at cursor position

Input:        byte

Output:       none

Misc:

******************************************************************************/
void LCDWriteChar(char byte)
{
  char tmp;

  tmp = byte & 0xf0;
  tmp = tmp >> 4;
  tmp += 0x30;
  if (tmp > 0x39) tmp += 0x07;
  LCDWriteData(LCD_DATA,tmp);

  tmp = byte & 0x0f;
  tmp += 0x30;
  if (tmp > 0x39) tmp += 0x07;
  LCDWriteData(LCD_DATA,tmp);
}

/******************************************************************************

Name:         void LCDWriteString(char *ptr)

Description:  Write a string from RAM on the LCD

Input:        string pointer

Output:       none

Misc:

******************************************************************************/
void LCDWriteString(char *ptr)
{
  unsigned char i;

  for (i=1;i<41;i++)
  {
    if (*ptr == 0x00) break;
    LCDWriteData(LCD_DATA,*ptr++);
  }
}

/******************************************************************************

Name:         void LCDWriteConstString(const char *ptr)

Description:  Write a constant string on the LCD

Input:        string pointer

Output:       none

Misc:

******************************************************************************/
void LCDWriteConstString(flash char *ptr)
{
  unsigned char i;

  for (i=1;i<41;i++)
  {
    if (*ptr == 0x00) break;
    else if (i == 21) LCDGotoXY(1,2);
    LCDWriteData(LCD_DATA,*ptr++);
  }
}

/******************************************************************************

Name:         void LCDWriteData(unsigned char rs, unsigned char ch)

Description:  Write a byte in rs of the LCD

Input:        rs -> Register select
              ch -> byte to write

Output:       none

Misc:

******************************************************************************/
void LCDWriteData(unsigned char rs,unsigned char ch)
{
  unsigned char Stat;

  Stat = LCD_PIN & LCD_BACKLIGHT;

  LCD_PORT = Stat;

  delay_ms(2);
  
  if ((ch & 0x80) == 0x80) LCD_PORT |= LCD_D7;
  if ((ch & 0x40) == 0x40) LCD_PORT |= LCD_D6;
  if ((ch & 0x20) == 0x20) LCD_PORT |= LCD_D5;
  if ((ch & 0x10) == 0x10) LCD_PORT |= LCD_D4;
  if (rs == 1) LCD_PORT |= LCD_RS;
  LCDDelay50us(1);

  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;

  LCD_PORT = Stat;
  if ((ch & 0x08) == 0x08) LCD_PORT |= LCD_D7;
  if ((ch & 0x04) == 0x04) LCD_PORT |= LCD_D6;
  if ((ch & 0x02) == 0x02) LCD_PORT |= LCD_D5;
  if ((ch & 0x01) == 0x01) LCD_PORT |= LCD_D4;
  if (rs == 1) LCD_PORT |= LCD_RS;
  LCDDelay50us(1);

  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;
}

/******************************************************************************

Name:         void LCDDelay50us(int Delay)

Description:  Delay of 50 us with a 16Mhz resonator

Input:        Delay X x 50us

Output:       none

Misc:

******************************************************************************/
void LCDDelay50us(int Delay)
{
  int i,j;

  for (i=0;i<Delay;i++)
  {
    for (j=1;j<120;j++);
    NOP();
  }
}

/******************************************************************************

Name:         void LCDTextAttr(unsigned char Attribute)

Description:  Set the LCD attribute

Input:        LCD_C_BLINK_ON     : Cursor blink on
              LCD_C_BLINK_OFF    : Cursor blink off
              LCD_C_ON           : Cursor on
              LCD_C_OFF          : Cursor off
              LCD_DISPLAY_ON     : Display ON
              LCD_DISPLAY_OFF    : Display OFF
              LCD_BRIGHT_25      : VFD 25% Bright
              LCD_BRIGHT_50      : VFD 50% Bright
              LCD_BRIGHT_75      : VFD 75% Bright
              LCD_BRIGHT_100     : VFD 100% Bright
              LCD_BACK_LIGHT_ON  : Back Light ON
              LCD_BACK_LIGHT_OFF : Back Light OFF

Output:       none

Misc:

******************************************************************************/
void LCDTextAttr(unsigned char attribute)
{
switch (attribute)
    {
    case LCD_C_BLINK_ON:
         LCDStat |= LCD_C_BLINK_ON;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_C_BLINK_OFF:
         LCDStat &= LCD_C_BLINK_OFF;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_C_ON:
         LCDStat |= LCD_C_ON;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_C_OFF:
         LCDStat &= LCD_C_OFF;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_DISPLAY_ON:
         LCDStat |= LCD_DISPLAY_ON;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_DISPLAY_OFF:
         LCDStat &= LCD_DISPLAY_OFF;
         LCDWriteData(LCD_CTRL,LCDStat);
         break;

    case LCD_BRIGHT_25:
         LCDWriteData(LCD_CTRL,0x20);
         LCDWriteData(LCD_DATA,0x03);
         break;

    case LCD_BRIGHT_50:
         LCDWriteData(LCD_CTRL,0x20);
         LCDWriteData(LCD_DATA,0x02);
         break;

    case LCD_BRIGHT_75:
         LCDWriteData(LCD_CTRL,0x20);
         LCDWriteData(LCD_DATA,0x01);
         break;

    case LCD_BRIGHT_100:
         LCDWriteData(LCD_CTRL,0x20);
         LCDWriteData(LCD_DATA,0x00);
         break;

    case LCD_BACKLIGHT_ON:
         //LCD_PORT |= LCD_BACKLIGHT;
         break;

    case LCD_BACKLIGHT_OFF:
         //LCD_PORT &= ~(LCD_BACKLIGHT);
         break;
    }
}

/******************************************************************************

Name:         void LCDOn(unsigned char X, unsigned char Y)

Description:  Initialize LCD in 4bit mode

Input:        unsigned char X -> X size
              unsigned char Y -> Y size

Output:       none

Misc:

******************************************************************************/
void LCDInit(unsigned char X, unsigned char Y)
{
  MaxX = X;
  MaxY = Y;
  LCDStat = 0x0c;
  LCD_PORT = 0x00;
  LCD_DDR = LCD_D7+LCD_D6+LCD_D5+LCD_D4+LCD_E+LCD_RS+LCD_RW+LCD_BACKLIGHT;
  LCD_PORT = ~(LCD_D7+LCD_D6+LCD_D5+LCD_D4+LCD_E+LCD_RS+LCD_RW);
  //LCD_PORT |= LCD_BACKLIGHT;
  LCDDelay50us(340);

  LCD_PORT = (LCD_D5 + LCD_D4 + LCD_BACKLIGHT);
  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;
  LCDDelay50us(100);
  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;
  LCDDelay50us(100);
  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;
  LCDDelay50us(100);

  LCD_PORT = (LCD_D5 + LCD_BACKLIGHT);
  LCD_PORT |= LCD_E;
  LCD_PORT &= ~LCD_E;
  LCDDelay50us(100);

  if (Y == 1) LCDWriteData(LCD_CTRL,0x20);    // 1 line
  else LCDWriteData(LCD_CTRL,0x28);           // 2 line

  LCDWriteData(LCD_CTRL,0x0c);                // Disp ON-Cur OFF-Blink OFF
  LCDWriteData(LCD_CTRL,0x01);                // Clear display
  LCDDelay50us(50);
  LCDWriteData(LCD_CTRL,0x06);                // Cursor INC Shift OFF

  //DDRD |= 0x80;
  //TCCR2 = 0x79;
  //OCR2 = 0xff;
}


/******************************************************************************

Name:         void LCDOff(void)

Description:  Shut down LCD

Input:        none

Output:       none

Misc:

******************************************************************************/
void LCDOff(void)
{
 LCD_DDR = 0xff;
 LCD_PORT = 0x00;
 DDRD &= ~0x80;
}

⌨️ 快捷键说明

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