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

📄 lcd1602._c

📁 数控电压源程序数控电压源程序数控电压源程序数控电压源程序
💻 _C
字号:
#include <iom16v.h>
#include <macros.h>
#include "delay.h"
/*
 数据口接PB口:PB0--DB0,PB1--DB1,.....PB7--DB7;
 控制总线RS--PD3 RW--PD5 EN--PD4

*/
#define LCDPORT PORTC
#define LCDDDR DDRC
#define LCDPIN PINC

#define RS_CLR	PORTA&= ~(1 << PA0)
#define RS_SET	PORTA |= (1 << PA0)

#define RW_CLR	PORTA &= ~(1 << PA1)
#define RW_SET	PORTA |= (1 << PA1)

#define EN_CLR	PORTA &= ~(1 << PA2)
#define EN_SET	PORTA |= (1 << PA2)
#define Clear_Screen  Write_Command(0x01)


void delay_ms(unsigned int n)
{
 unsigned int i;
 for(i=0;i<n;i++)delay_1ms();
}

void En_Toggle()//产生一个使能脉冲
{
 EN_CLR;        //拉低使能位,产生一个下降沿
 delay_ms(5);//保持低电平一定时间
 EN_SET;        //拉高使能位
 delay_ms(3);//保持高电平一定时间
 EN_CLR;        //拉低使能位,产生一个下降沿
}

/* 不断检测LCD的忙标志 (BF),直到其为0,表示可以执行下一条指令*/ 
 void Wait_Until_Ready(void)
 {
  RW_SET;     //设为读状态
  RS_CLR;  //所读为状态位
  LCDDDR&=~(1<<DDD7);//单片机设为输入,用以读取LCD的忙标志
  EN_SET;
  delay_ms(5);
  while((LCDPIN&0x80)==0x80) ;    //不断循环,直至BF=0
  EN_CLR;
  LCDDDR|=(1<<DDD7);
 }

 void Write_Command(unsigned char Command)   //向LCD写入命令字
 {
  RS_CLR;        //写入的是命令 
  RW_CLR;        //置为写状态
  LCDDDR=0xFF;
  LCDPORT=Command;  //写命令字 
  delay_ms(2);
  En_Toggle();
  Wait_Until_Ready(); //等待指令执行完毕
 } 

void Write_Data(unsigned int Data)
{
  RS_SET;
  RW_CLR; //写入的是数据
  LCDDDR=0xFF;
  delay_ms(2);
  LCDPORT=Data;
  En_Toggle();
  Wait_Until_Ready();
} 

void Write_Position(unsigned int row,unsigned int colum)//设字符位置
{
 unsigned char p;
  if(row==1)
  {
   p=0x80+colum-1;
   Write_Command(p);
   }  
   else 
     {
      p=0xC0+colum-1;
      Write_Command(p);
     }
}
  
void Write_String(char *s)//写入字符串
{
 for(;*s!='\0';s++)
  Write_Data(*s);
}
void Initialize_LCD(void)
{ 
 CLI(); //禁止所有中断
 PORTA=0x07;
 DDRA=0X07;
 LCDPORT=0xFF;
 LCDDDR=0xFF;
 Write_Command(0X38);
 Write_Command(0x06);
 Write_Command(0x0c);
 Clear_Screen;
 SEI();//开全局中断
}

⌨️ 快捷键说明

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