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

📄 lcd-16sn._c

📁 大量ATMEGA16实例, 都可以运行,包括驱动LCD1602, 上位机显示等多功能
💻 _C
字号:
// Title:	LCD-avr-sm.c
// Ourpose: Test LCD display
// Connection:
// 			  PORTD to J26 colon pin header of dot0matrix
//			  PORTB ro J22 LCD pin header PB0-> D0 .... PB7 -> D7
//			  PA0 - RS, PA1 - R/W PA2 - E
//			  PB3 - out of J32 of Light sensor
//			  PB1 and PB2 to J34 of 3-color LED
// 			  Test result: LCD display welcome message 
//			  display "Too dark" when light to Light sensor is blocked
//			  simultanously 3-color LED is on

#include <iom16v.h>
#include <macros.h>
#include <stdlib.h>

unsigned char lcd_enable = 0b00000001, lcd_disable = 0b11111110;
unsigned char in_instr_wr = 0b00000000, in_data_wr = 0b00000100;
unsigned char in_instr_rd = 0b00000010, in_data_rd = 0b00000110;

void wr_instruction(unsigned char instr);
void wr_data(unsigned char data);

int indexhead=0, indextail=0, indexcount;
int cursor_index=0;


const char message1[]= {"  Hello World!"};
const char message2[]= {"www.inovacs.com"}; 
const char message3[]= {" Happy New Year"};
const char message4[]= {" 2008 is coming"};
const char message5[]= {" Greeting From"};
const char message6[]= {"www.inovacs.com"};
const char message7[]= {"Contact Inova"};
const char message8[]= {"Tel:0592 5563570"};

void cursor_home(void);
void cursor_line1(void);
void cursor_line2(void);
void lcd_delay(int);

unsigned char swin, swin2;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Initilization file

void port_init(void)
 {
 DDRA = 0xff;		 //PA0-PA2 for LCD control
 DDRB = 0xff;	 	 //PB0-PB7 for LCD data
 	  				 
 DDRD = 0xff;	  	 //Control LEDs on left group 
 DDRC = 0x00;	  	 //Receive from dip-switch and button 		
 PORTC=0xff;		
 }

void WDT_off(void)
 {
 WDR();
 WDTCR=0b00010111;
 WDTCR=0b00000111;
 }
 
void lcd_delay(int count)
{
 int a,b;
    b=2*count;
 	for(a=0; a<b; a++)
			 ;
}

void ldelay(int ms)
{
 int i;
 for(i=0; i<=ms; i++)
 	 lcd_delay(0x2000);	  	  //2000-->4000
}		  

// ************************************************************
// LCD functions follows

//PORTB to LCD data port
//PORTA to LCD control port 
//PA0--> R/S
//PA1--> R/W
//PA2--> E

void wr_instruction(unsigned char instr)
{
 DDRA=0xff;
 lcd_delay(1);
 PORTA=0b00000000;
 PORTB=instr;	  		 //send out instruction
 lcd_delay(2);
 PORTA=0b00000100;		 //PA2<--1 LCD-E ON (enable)
 lcd_delay(2);
 PORTA=0b00000000; 		 //PA2<--0 LCD-E OFF (disable) 
 lcd_delay(1);
}

void wr_data(unsigned char data)
{
 DDRA=0xff;
 PORTA=0b00000001;	  	  //PA0 <-- 1, Select data register
 PORTB=data;			  //Send out data
 lcd_delay(1);
 PORTA=0b00000101;	  	  //Enable write to Data register
 lcd_delay(2);
 PORTA=0b00000000;		  //disable ????
 lcd_delay(1);
}

// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Following functions for LCD panel control

void wr_instruction2(unsigned char instr)
{
 unsigned char flags;
  lcd_delay(10);
  wr_instruction(instr);
  //PORTD=0x2f;
  lcd_delay(10);
}

void wr_data2(unsigned char data)
{
 unsigned char flags;
 if(cursor_index==0x10)
    cursor_line2();
 else if(cursor_index==0x50)
    cursor_line1();	
 wr_data(data);
 cursor_index++;
 //PORTD=0x4f;
 lcd_delay(50); 
 }
 
void cursor_home(void)		//Move cursor to Home
{
 wr_instruction2(0x01);
 lcd_delay(10);
 wr_instruction2(0x80);
 ldelay(5);
 cursor_index=0;
}
 
void cursor_line1(void)		//Move cursor to the first line 
{
 wr_instruction2(0x80);
 cursor_index=0;
 ldelay(5);
}

void cursor_line2(void)		//Move cursor to the second line
{
 wr_instruction2(0b11000000);
 cursor_index=0x40;
 ldelay(2);
} 

void space(void)   			 //Write space, ' ', on the panel
{
 wr_data2(' ');
 }
 
void out_string(const char array[])	 //Send a string stored in array to LCD panel
{
 int i=0;
 unsigned char onechar;
 i=0;
 while(array[i])
  {
   onechar=array[i++];
   wr_data2(onechar);
  }
}

void lcd_on(void)
{
 unsigned char i, ctrl_data;
 ldelay(20);
 PORTD=0x01;
 ldelay(40);
 wr_instruction(0b00110000);   //Function Set:set 8-bit, 2line, 5x7 fonts
 ldelay(2);
 wr_instruction(0b00110000);   //
 ldelay(2);
 wr_instruction(0b00110000);   //
 lcd_delay(80);
 wr_instruction(0b00111100);   //8-bit, 2 lines, 5x7 font
 lcd_delay(20);
 wr_instruction(0b00001110);   //display on, cursor on, blink off
 lcd_delay(20);
 wr_instruction(0b00001110);   //display on, 
 lcd_delay(20);
 wr_instruction(0b00000001);   //clear display
 ldelay(5);
 wr_instruction(0b00000110);   //Entry mode
 lcd_delay(10);
 wr_instruction(0b00000001);   //clear display
 lcd_delay(20);
 }
//***********************************************************

// **********************************************************
// Main program
/*
void main(void)
{
 unsigned char data, pfin, dark, darkfg=0xff;
 int i=0;
 unsigned char j, jsave, array1[10];
 int a;
 CLI();
 port_init();
 WDT_off();
 ldelay(200);
 lcd_on();
 PORTD=0x50;
 ldelay(50);
 PORTD=0x05;
 ldelay(50);
 out_string(message1);
 cursor_line2();
 out_string(message2);
 PORTD=0x55;
 PORTA=0xF8;
 PORTB=0x00;
 ldelay(100);

 while(1)
 {
  WDR();
  swin=PINC&0x30;
  swin=(swin>>4)&0x03;
  
  
  if(swin2!=swin)
    {
	 PORTD=swin;
	 swin2=swin;
  	 if(swin==0x00)
      {
	  cursor_home();
	  out_string(message1);
 	  cursor_line2();
 	  out_string(message2);
	  }
  	 else if(swin==0x01)
      {
	  cursor_home();
	  out_string(message3);
 	  cursor_line2();
 	  out_string(message4);
	  }
     else if(swin==0x02)
      {
	  cursor_home();
	  out_string(message5);
 	  cursor_line2();
 	  out_string(message6);
	  }
     else
      {
	  cursor_home();
	  out_string(message7);
 	  cursor_line2();
 	  out_string(message8);
	  }		
	}
  ldelay(10);
  }  
}*/

⌨️ 快捷键说明

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