|
发表于 2018-12-12 16:26:11
|
显示全部楼层
#include <reg52.h>
typedef unsigned char u8 ;
typedef unsigned int u16 ;
#define LCD1602_DB P0
sbit LCD1602_RS = P2^6 ;
sbit LCD1602_RW = P2^5 ;
sbit LCD1602_E = P2^7 ;
void delay_n1ms ( u8 time ) ;
void LCD_WriteCmd () ;
void LCD_init () ;
void LCD_WriteDat ( u8 dat ) ;
void LCD_Cursor ( u8 x , u8 y ) ;
void LCD_ShowStr ( u8 x , u8 y , u8 * str , u8 len ) ;
void intertupt_10ms () ;
u8 string[ 35 ] = { 0 } ;
void main ()
{
u8 i , index , cnt ;
LCD_init () ;
intertupt_10ms () ;
for ( i = 0 ; i < 16 ; i++ )
{
string[ i ] = 32 ;
}
string[ 16 ] = 84 ;
string[ 17 ] = 79 ;
string[ 18 ] = 77 ;
for ( i = 19 ; i < 35 ; i++ )
{
string[ i ] = 32 ;
}
while ( 1 )
{
if ( TF0 == 1 )
{
TF0 = 0 ;
TH0 = ( 65536 - 10000 ) / 256 ;
TL0 = ( 65536 - 10000 ) % 256 ;
cnt++ ;
if ( cnt == 100 )
{
cnt = 0 ;
LCD_ShowStr ( 0 , 1 , string + index , 16 ) ;
index ++ ;
if ( index == 19 ) index = 0 ;
}
}
}
}
void intertupt_10ms ()
{
TMOD |= 0x01 ;
TH0 = ( 65536 - 10000 ) / 256 ;
TL0 = ( 65536 - 10000 ) % 256 ;
TR0 = 1 ; ET0 = 1 ;
}
void delay_n1ms ( u8 time )
{
u8 a , b , c , n ;
for ( n = 0 ; n < time ; n++ )
{
for ( c= 1 ; c > 0 ; c-- )
for ( b = 142 ; b > 0 ; b-- )
for ( a = 2 ; a > 0 ; a-- ) ;
}
}
void LCD_WriteCmd ( u8 cmd )
{
LCD1602_DB = cmd ;
LCD1602_RS = 0 ;
LCD1602_RW = 0 ;
LCD1602_E = 1 ;
delay_n1ms ( 1 ) ;
LCD1602_E = 0 ;
}
void LCD_init ()
{
LCD_WriteCmd ( 0x38 ) ;
LCD_WriteCmd ( 0x0c ) ;
LCD_WriteCmd ( 0x06 ) ;
LCD_WriteCmd ( 0x01 ) ;
LCD_WriteCmd ( 0x80 ) ;
}
void LCD_WriteDat ( u8 dat )
{
LCD1602_DB = dat ;
LCD1602_RS = 1 ;
LCD1602_RW = 0 ;
LCD1602_E = 1 ;
delay_n1ms ( 1 ) ;
LCD1602_E = 0 ;
}
void LCD_Cursor ( u8 x , u8 y )
{
u8 addr ;
if ( y == 1 ) addr = 0x40 + x ;
else addr = 0x00 + x ;
LCD_WriteCmd ( addr | 0x80 ) ;
}
void LCD_ShowStr ( u8 x , u8 y , u8 *str , u8 len )
{
LCD_Cursor ( x , y ) ;
while ( len-- )
{
LCD_WriteDat ( *str++ ) ;
}
} |
|