ICCAVR_mega8_Proteus LCD1602显示
[复制链接]
//ICC-AVR application builder : 2009-3-12 16:02:34
// Http://www.oa-only.com
// Target : M8
// Crystal: 8.0000Mhz
#include
#include
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
介 绍:用PB做端口,
PB0-D0
PB7-D7
------
PC0-6
PC1-5
PC2-4----456为控制引脚
入口参数:
出口参数:
*************************************/
//延时程序
void delay(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
{for(j=0;j<200;j++);}
}
//送指令子程序
void com_lcd(unsigned char com)
{
PORTC&=~(1<
PORTC&=~(1<
PORTB=com;
PORTC|=(1<
delay(5);
PORTC&=~(1<
}
//送数据子程序
void data_lcd(unsigned char data)
{
PORTC|=(1<
PORTC&=~(1<
PORTB=data;
PORTC|=(1<
delay(5);
PORTC&=~(1<
}
//初始化
void LCD1602_init()
{
DDRB=0XFF;
DDRC|=0X07;
PORTC&=~(1<
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("abcd");
LCD1602_goxy(2,1);
LCD1602_printD(i);
while(1)
{
;
}
}
http://www.oa-only.com/upload/20100127143634530.rar