📄 lcd1602显示._c
字号:
//ICC-AVR application builder : 2009-3-12 16:02:34
// Target : M8
// Crystal: 8.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/*************************************************************************
用 途:LCD1602显示
Taget :mega8
crystal :8M
介 绍:
入口参数:
出口参数:
*************************************/
const unsigned char tab1[]=" I LIKE AVR!!! ";
const unsigned char tab2[]=" www.cumt.edu.cn ";
//延时程序
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i<ms;i++)
{for(j=0;j<200;j++);}
}
//送指令子程序
void com_lcd(unsigned char com)
{
PORTC&=~(1<<PC2);
PORTC&=~(1<<PC1);
PORTB=com;
PORTC|=(1<<PC0);
delay(5);
PORTC&=~(1<<PC0);
}
//送数据子程序
void data_lcd(unsigned char data)
{
PORTC|=(1<<PC2);
PORTC&=~(1<<PC1);
PORTB=data;
PORTC|=(1<<PC0);
delay(5);
PORTC&=~(1<<PC0);
}
//初始化
void LCD1602_init()
{
DDRB=0XFF;
DDRC|=0X07;
PORTC&=~(1<<PC0);
com_lcd(0x38);//5*7,2行显示
delay(5);
com_lcd(0x01); //清屏
delay(5);
com_lcd(0x0C);//文字不动,光标自动右移
delay(5);
com_lcd(0x06);//开显示
delay(5);
}
//清屏
void LCD1602_clear()
{
com_lcd(0x01); //清屏
delay(5);
}
//定位x,y(x-列,y-行)
void LCD1602_goxy(unsigned char line,unsigned char row)
{
if (row==0)
line+=0x81;
else
line+=0xc0;
com_lcd(line);//第二行首地址
delay(5);
}
//显示字符串
LCD1602_print(char *str)
{
while(*str)
{
data_lcd(*str);
delay(10);
str++;
}
}
//显示一个二位十位数
LCD1602_printD(unsigned char n)
{
data_lcd(n/10+0x30);
delay(10);
data_lcd(n%10+0x30);
delay(10);
}
//**************************************************************************
void main()
{
unsigned char i=23;
port_init();
init_devices();
LCD1602_init();
LCD1602_goxy(2,0);
LCD1602_print("xiaozhiyong");
LCD1602_goxy(2,1);
LCD1602_printD(i);
while(1)
{
;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -