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

📄 lcd.i

📁 基于MEGA16单片机的1602液晶显示屏的程序
💻 I
字号:

#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4;      
sfrb ADCSRA=6;
sfrb ADMUX=7;
sfrb ACSR=8;
sfrb UBRRL=9;
sfrb UCSRB=0xa;
sfrb UCSRA=0xb;
sfrb UDR=0xc;
sfrb SPCR=0xd;
sfrb SPSR=0xe;
sfrb SPDR=0xf;
sfrb PIND=0x10;
sfrb DDRD=0x11;
sfrb PORTD=0x12;
sfrb PINC=0x13;
sfrb DDRC=0x14;
sfrb PORTC=0x15;
sfrb PINB=0x16;
sfrb DDRB=0x17;
sfrb PORTB=0x18;
sfrb PINA=0x19;
sfrb DDRA=0x1a;
sfrb PORTA=0x1b;
sfrb EECR=0x1c;
sfrb EEDR=0x1d;
sfrb EEARL=0x1e;
sfrb EEARH=0x1f;
sfrw EEAR=0x1e;   
sfrb UBRRH=0x20;
sfrb UCSRC=0X20;
sfrb WDTCR=0x21;
sfrb ASSR=0x22;
sfrb OCR2=0x23;
sfrb TCNT2=0x24;
sfrb TCCR2=0x25;
sfrb ICR1L=0x26;
sfrb ICR1H=0x27;
sfrb OCR1BL=0x28;
sfrb OCR1BH=0x29;
sfrw OCR1B=0x28;  
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a;  
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c;  
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=0x31;
sfrb OCDR=0x31;
sfrb TCNT0=0x32;
sfrb TCCR0=0x33;
sfrb MCUCSR=0x34;
sfrb MCUCR=0x35;
sfrb TWCR=0x36;
sfrb SPMCR=0x37;
sfrb TIFR=0x38;
sfrb TIMSK=0x39;
sfrb GIFR=0x3a;
sfrb GICR=0x3b;
sfrb OCR0=0X3c;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-

#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x40
	.EQU __sm_mask=0xB0
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0xA0
	.EQU __sm_ext_standby=0xB0
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#endasm

void LCD_init(void); 
void LCD_en_write(void); 
void LCD_write_command(unsigned  char command) ; 
void LCD_write_data(unsigned char data); 
void LCD_set_xy (unsigned char x, unsigned char y); 
void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s); 
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data); 
void delay_nus(unsigned int n); 
void delay_nms(unsigned int n); 

void LCD_init(void)         
{ 
DDRA    |=((1<<4)|(1<<5)|(1<<6)|(1<<7)) ;   
DDRA |=(1<<3) ;       
DDRA |=(1<<2) ;       
LCD_write_command(0x28);  
LCD_en_write(); 
delay_nus(40); 
LCD_write_command(0x28);  
LCD_write_command(0x0c);  
LCD_write_command(0x01);  
delay_nms(2); 
} 

void LCD_en_write(void)  
{ 
PORTA   |=(1<<3) ; 
delay_nus(1); 
PORTA   &=~(1<<3) ; 
} 

void LCD_write_command(unsigned char command) 
{ 
delay_nus(16); 
PORTA   &=~(1<<2) ;        
PORTA   &=0X0f;         
PORTA   |=command&0xf0; 
LCD_en_write(); 
command=command<<4;          
PORTA   &=0x0f;         
PORTA   |=command&0xf0; 
LCD_en_write(); 

} 

void LCD_write_data(unsigned char data) 
{ 
delay_nus(16); 
PORTA   |=(1<<2) ;       
PORTA   &=0X0f;       
PORTA   |=data&0xf0;  
LCD_en_write(); 
data=data<<4;               
PORTA   &=0X0f;        
PORTA   |=data&0xf0;   
LCD_en_write(); 
} 

void LCD_set_xy( unsigned char x, unsigned char y )  
{ 
unsigned char address; 
if (y == 0)                           
address = 0x80 + x;   
else   
address = 0xc0 + x; 
LCD_write_command( address); 
} 

void LCD_write_string(unsigned char x,unsigned char y,unsigned char *s) 
{ 
LCD_set_xy( x, y); 
while (*s)  
{ 
LCD_write_data( *s ); 
s ++; 
} 

} 

void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) 
{ 
LCD_set_xy( X, Y ); 
LCD_write_data( data); 

} 

void delay_1us(void)                 
{ 
#asm("nop"); 
} 

void delay_nus(unsigned int n)       
{ 
unsigned int i=0; 
for (i=0;i<n;i++) 
delay_1us(); 
} 

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

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

unsigned char *sh="Hello,world";
unsigned char time[9]="17:28:50";
unsigned char count = 0;

void timer(void)
{
time[7]++;
if(time[7]>'9')    
{ 
time[7]='0';
if(time[6]=='5')
{ 
time[6]='0';  
time[4]++; 
if(time[4]>'9')
{ 
time[4]='0';
time[3]++;
if(time[3]>'5')
{            
time[3]='0';
time[1]++;
if(time[0]=='2')
{
if(time[1]>3)
{
time[7]='0';
time[6]='0';
time[4]='0';
time[3]='0';
time[1]='0';
time[0]='0';  
}
} 
else 
if(time[1]>'9')
{ 
time[1]='0';
time[0]++;                 
}
}            
} 
}  
else
time[6]++;
} 
}

interrupt [10] void timer0_ovf_isr(void)     
{

count++;                                        
if(count ==99)
{                                        
timer();
count = 0;                         

}                                           
TCNT0 = 256 - 0.01/(1024.0/4000000UL  );          
}  

void main(void)
{

PORTA=0x00;
DDRA=0xFF;

TCCR0=0x05;
TCNT0=256 - 0.01/(1024.0/4000000UL  );;
OCR0=0x00;

TIMSK=0x01;

ACSR=0x80;
SFIOR=0x00;

#asm("sei")
LCD_init( );
while (1)
{

LCD_write_string(1,0,sh); 
for(;;) 
{ 

LCD_write_string(0,1,time);  
LCD_write_char(12,1,'4');
LCD_write_char(13,0,time[0]);    
}  
};
}

⌨️ 快捷键说明

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