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

📄 lcd.c

📁 加速度采集器。基于ATMega16L.AVRSTUIDIO4
💻 C
字号:
//LCD.c
#include "lcd.h"
#include <iom16v.h>
#include <macros.h>

//保证平时LCD不在输出
void delay1us()	   //4MHz Crystal
{
 NOP();
 NOP();
 NOP();
 NOP();
}

void lcdWriteInstruction(unsigned char instruction, unsigned char testBusyFlag)
{
 	 if(testBusyFlag)
	 	lcdWaitUntilReady();
 	 //1. Write Control Line
	 DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E;	//set control output
	 PORT_LCDCTL &= ~(LCD_RS|LCD_RW|LCD_E); 
	 			 //RS=0(instruction); RW=0(write); E=0;
	 //2. Write Data Line			 
	 DDR_LCDDATA = 0xff;
	 PORT_LCDDATA = instruction;
	 
	 delay1us();//wait for address set (RW) (140ns)
	 
	 PORT_LCDCTL |= LCD_E; //E=1;
	 delay1us();//wait for pwen (450ns)
	 PORT_LCDCTL &= ~LCD_E; //E=0; 
	 
	 delay1us();//wait for E cycle 
}

void lcdWriteData(unsigned char data, unsigned char testBusyFlag)
{
 	 if(testBusyFlag)
	 	lcdWaitUntilReady();
 	 //1. Write Control Line
	 DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E;	//set control output
	 PORT_LCDCTL &= ~(LCD_RW|LCD_E); 
	 			 //RW=0(write); E=0;
	 PORT_LCDCTL |= LCD_RS;
	 			 //RS=1(data)
	 //2. Write Data Line			 
	 DDR_LCDDATA = 0xff;
	 PORT_LCDDATA = data;
	 
	 delay1us();//wait for address set (RW) (140ns)
	 
	 PORT_LCDCTL |= LCD_E; //E=1;
	 delay1us();//wait for pwen (450ns)
	 PORT_LCDCTL &= ~LCD_E; //E=0; 
	 //wait for E cycle
	 delay1us();//wait for E cycle 
}

void lcdWaitUntilReady()
{
 	 //1. HiZ Data Line			 
	 DDR_LCDDATA = 0x00;
	 PORT_LCDDATA = 0x00;
	 
 	 //2. Write Control Line
	 DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E;	//set control output
	 PORT_LCDCTL &= ~(LCD_RS|LCD_E); 
	 			 // RS=0(instruction); E=0;
	 PORT_LCDCTL |= LCD_RW;
	 			 //RW=1(read);
	 
	 delay1us();//wait for address set (RW) (140ns)
	 
	 //3. Enable E and read
	 PORT_LCDCTL |= LCD_E; //E=1;
	 delay1us();//wait for pwen (450ns)
	 //4. test busy flag
	 while(PIN_LCDDATA&0x80);//loop until bf=0;
	 
	 //5. Disable E
	 PORT_LCDCTL &= ~LCD_E; //E=0;
	 PORT_LCDCTL &= ~(LCD_RS|LCD_RW|LCD_E);//set RW=0(write)

	 delay1us();//wait for E period
}
	

//================================================================
//==================Basic Operation================================
//===================================================================

void lcdClearScreen()
{
	 //wait for idle
 	 lcdWriteInstruction(0x01,1);
	 //Screen clear
	 //AC=0, auto increase
}

void lcdWriteChar(unsigned char row, unsigned char col, unsigned char ch)
{
	 unsigned char ADD;
 	 //row=0:top; row=1:bottom; col=0:right; col=7 
	 ADD = (row*0x40)|col;
 	 lcdWriteInstruction(0x80|ADD,1);
	 lcdWriteData(ch,1);
}

void lcdWriteString(unsigned char row, unsigned char col, unsigned char num, const unsigned char* pCh)
{
	 unsigned char ADD;
	 unsigned char cnt;
 	 //row=0:top; row=1:bottom; col=0:right; col=7 
	 ADD = (row*0x40)|col;
 	 lcdWriteInstruction(0x80|ADD,1);
	 for(cnt=0;cnt<num;cnt++)
	   lcdWriteData(pCh[cnt],1);
}

void lcdWriteNumber(unsigned char row, unsigned char col, unsigned char n, unsigned short x)
{// n<=5!!!				 //if right aglined, col+n==max digits.
	 unsigned char ADD;
	 unsigned char i;
	 unsigned short b; //b=pow(10,j);
	 unsigned char digit;
	 unsigned char leadingZero = 1;
	 
  	 //row=0:top; row=1:bottom; col=0:right; col=7
	 //Move cursor 
	 ADD = (row*0x40)|col;
 	 lcdWriteInstruction(0x80|ADD,1);
	 //Calculate pow(10,n-1)
	 b=1;
	 for(i=1;i<n;i++)
	 	b *= 10;
	 //Get digit n..1
	 for(i=n;i>=1;i--)
	 {
	    digit = x/b; // x\pow(10,n-1..0);
	  	if (leadingZero && (digit==0) && (i>1))
	   	   lcdWriteData(' ',1);
		else
		{
		   leadingZero = 0;
	   	   lcdWriteData(digit+48,1);
		}
	   x -= digit*b;
	   b /= 10;
	 }
}
	   
   


void lcdSwichOn()
{
	 //wait for idle
 	 lcdWriteInstruction(0xC0,1);
	 //Screen clear
	 //AC=0, auto increase
}

void _delay_ms(unsigned char t)
{
 	 int cnt = t*1000;
	 for(;cnt>0;cnt--);
}

void lcdInit()
{
      DDR_LCDCTL |= LCD_RS|LCD_RW|LCD_E;         //LCD控制线在PD口,故将其设为输出
      DDR_LCDDATA = 0xff;                       //E\RS\RW等接在PA口

       _delay_ms(15);
       lcdWriteInstruction(0x38,0);     //设为8位接口模式,显示2行字符,busy=0不检测忙信号
       _delay_ms(5);
       lcdWriteInstruction(0x38,0);
       _delay_ms(5);
       lcdWriteInstruction(0x38,0);

       lcdWriteInstruction(0x38,1);
       lcdWriteInstruction(0x08,1);     //显示关闭
       lcdWriteInstruction(0x01,1);     //显示清屏
       lcdWriteInstruction(0x06,1);     //写入新数据后光标右移
       lcdWriteInstruction(0x0c,1);      //显示功能开,显示光标
}

⌨️ 快捷键说明

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