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

📄 lcd-16sn._c

📁 大量ATMEGA16实例, 都可以运行,包括驱动LCD1602, 上位机显示等多功能
💻 _C
字号:
/*
Title:	LCD-16sm.c
Connection:
	  Factory fixed setting:
	  PORTA:
	  		PA0-PA2 LCD control
			PA3-PA7 4x7-segment display control
			Drive LED group2 (the right group of LED)
	  PORTB:
	  		Shared by LCD and 4x7-segment displays
	  		output 8-bit data to LCD or 8-bit data to 4x7-segment displays
	  PORTC:
	        shared by 8-bit dipswitch and 4 x touch switches + 4 buttons
			receive inputs from dipswitch, touch switches and buttons 				
	  PORTD: 
	  		Drive LED group1 (the left group of LED) 
Attention:
	  1. J12 should be capped (connectted)
	  2. J5 is the Jump for LCD back light power 	  

Operation:
	  LCD panel display messages
	  Four messages can be displayed 
	  which message displayed is controlled by SW4 and SW5
	  LED group1 display status of SW4 and SW5
*/

#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[]= {"  RS232 Demo    "};
const char message2[]= {"Baudrate=38400  "}; 
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);
int ifr_decode(void);
unsigned char swin, swin2;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

void WDT_off(void)
 {
 WDR();
 WDTCR=0b00010111;
 WDTCR=0b00000111;
 }
 
void 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++)
 	 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;
 delay(1);
 PORTA=0b00000000;
 PORTB=instr;	  		 //send out instruction
 delay(2);
 PORTA=0b00000100;		 //PA2<--1 LCD-E ON (enable)
 delay(2);
 PORTA=0b00000000; 		 //PA2<--0 LCD-E OFF (disable) 
 delay(1);
}

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

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

void wr_instruction2(unsigned char instr)
{
 unsigned char flags;
  delay(10);
  wr_instruction(instr);
  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++;
 delay(50); 
 }
 
void cursor_home(void)		//Move cursor to Home
{
 wr_instruction2(0x01);
 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 clear_line1(void)
{
 int i;
 cursor_line1();
 for(i=0; i<=15; i++)
   space();
 cursor_line1();  
}

void clear_line2(void)
{
 int i;
 cursor_line2();
 for(i=0; i<=15; i++)
   space();
 cursor_line2();  
} 

 
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);   //
 delay(80);
 wr_instruction(0b00111100);   //8-bit, 2 lines, 5x7 font
 delay(20);
 wr_instruction(0b00001110);   //display on, cursor on, blink off
 delay(20);
 wr_instruction(0b00001110);   //display on, 
 delay(20);
 wr_instruction(0b00000001);   //clear display
 ldelay(5);
 wr_instruction(0b00000110);   //Entry mode
 delay(10);
 wr_instruction(0b00000001);   //clear display
 delay(20);
 }

⌨️ 快捷键说明

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