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

📄 main.i

📁 TG12864液晶显示程序
💻 I
字号:

// CodeVisionAVR C Compiler
// (C) 1998-2004 Pavel Haiduc, HP InfoTech S.R.L.

// I/O registers definitions for the ATmega32


#pragma used+
#pragma used+
sfrb TWBR=0;
sfrb TWSR=1;
sfrb TWAR=2;
sfrb TWDR=3;
sfrb ADCL=4;
sfrb ADCH=5;
sfrw ADCW=4;      // 16 bit access
sfrb ADCSRA=6;
sfrb ADCSR=6;     // for compatibility with older code
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;   // 16 bit access
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;  // 16 bit access
sfrb OCR1AL=0x2a;
sfrb OCR1AH=0x2b;
sfrw OCR1A=0x2a;  // 16 bit access
sfrb TCNT1L=0x2c;
sfrb TCNT1H=0x2d;
sfrw TCNT1=0x2c;  // 16 bit access
sfrb TCCR1B=0x2e;
sfrb TCCR1A=0x2f;
sfrb SFIOR=0x30;
sfrb OSCCAL=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-

// Interrupt vectors definitions


// Needed by the power management functions (sleep.h)
#asm
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x80
	.EQU __sm_mask=0x70
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0x60
	.EQU __sm_ext_standby=0x70
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#endasm


    // CodeVisionAVR C Compiler
// (C) 1998-2000 Pavel Haiduc, HP InfoTech S.R.L.


#pragma used+
#pragma used+
#pragma used+

void delay_us(unsigned int n);
void delay_ms(unsigned int n);

#pragma used-


//*****************************************


//******************************************
unsigned char LCD_Read1byte(); 
void LCD_Write1byte(unsigned char data) ;  
void LCD_Check_busy();
void LCD_Write_cmd(unsigned char cmd);
void LCD_Write_data(unsigned char data); 
void Init_LCD(void);
void LCD_Read_data(unsigned char row,unsigned char col); 
void LCD_Write_string(unsigned char datacode[]); 
void LCD_Location_xy(unsigned char row,unsigned char col);
void LCD_Write_String_xy(unsigned char row,unsigned char col,unsigned char string[]);
void num(unsigned char row,unsigned char col,unsigned int n);
void num16(unsigned char row,unsigned char col,unsigned int n);
void clear(unsigned char wb);



//**************************************************************

void Init_LCD(void)
{
    PORTA.5=1;DDRA.5=1;
    PORTA.6=1;DDRA.6=1;
    PORTA.7 =1;DDRA.7  =1;  
    delay_ms(1);
    LCD_Write_cmd(0x30);//扩充指令集
    delay_ms(1);
    LCD_Write_cmd(0x0C);//开显示,关光标,不闪烁.[00001DCB],D=1:整体显示开,C=1:游标开,B=1:游标位置反白允许
	delay_ms(1);
	LCD_Write_cmd(0x02);//设定DDRAM的地址计数器(AC)到"00H",并且将游标移到开头原点位置;这个指令不改变DDRAM 的内容
	delay_ms(1);
	LCD_Write_cmd(0x06);//指定在资料写入或读取时,光标的移动方向,DDRAM的地址计数器(AC)加1。
	delay_ms(1);
	LCD_Write_cmd(0x01);//清除显示,将DDRAM填满"20H",并且设定DDRAM的地址计数器(AC)到"00H"
	delay_ms(1);
	LCD_Write_cmd(0x80);//光标复位
	delay_ms(1);  
	//******************
	PORTA.0=1;
	PORTA.1=0;
	DDRA.0=1;
	DDRA.1=1;//
    
}
/***********************************************************************
功能:清屏
************************************************************************/
void clear(unsigned char wb)
{
	unsigned char row,col;
	LCD_Write_cmd(0x34);
	LCD_Write_cmd(0x36);
	for(row=0;row<32;row++)
	{
		LCD_Write_cmd(0x80|row);	//设置行地址
		LCD_Write_cmd(0x80);  		//设置列地址
		if(wb==0)
		for(col=0;col<32;col++)
		{
			LCD_Write_data(0x00);    //????
		}
		else
		for(col=0;col<32;col++)
		{
			LCD_Write_data(0xff);
		}
	}
}
unsigned char LCD_Read1byte()       //
{
    unsigned char i,tempH,tempL,t;
    PORTA.7 =0;
    DDRA.6=1;
    for(i=8;i>0;i--)
    {
        tempH<<=1;
        tempH=PINA.6;
        PORTA.7 =1;
        PORTA.7 =0;
    }          
    for(i=8;i>0;i--)
    {
        tempL<<=1;
        tempL=PINA.6;
        PORTA.7 =1;
        PORTA.7 =0;
    }          
    DDRA.6=1;
    tempL>>=4;
    t=tempH+tempL;
    return t;
}  
void LCD_Write1byte(unsigned char data)    //
{
    unsigned char i,temp;
    temp=data;
    PORTA.7 =0;
    for(i=8;i>0;i--)
    {
        PORTA.6=temp&0x80;
        temp<<=1;
        PORTA.7 =1;
        delay_us(1);
        PORTA.7 =0;
        delay_us(1);
    }
}
void    LCD_Check_busy()            //
{
    PORTA.5=0;
    PORTA.5=1;
    do{LCD_Write1byte(0xFC);}
    while(0x80&LCD_Read1byte());
    
} 
/****************************************************************
功能:写命令到LCM
参数:cmdcode是要写入的命令
****************************************************************/
void LCD_Write_cmd(unsigned char cmd)
{
    PORTA.5=1;
    LCD_Check_busy();
    LCD_Write1byte(0xf8);//00 从MCU至LCD,命令
    LCD_Write1byte(0xf0&cmd);
    LCD_Write1byte(0xf0&(cmd<<4));
    PORTA.5=0;
} 

/****************************************************************
功能:写数据到LCM
参数:Data是要写入的数据
****************************************************************/
void LCD_Write_data(unsigned char data)
{
    PORTA.5=1;
    LCD_Check_busy();
    LCD_Write1byte(0xfa);//00 从MCU至LCD, 
    LCD_Write1byte(0xf0&data);
    LCD_Write1byte(0xf0&(data<<4));
    PORTA.5=0;
}
void LCD_Read_data(unsigned char row,unsigned char col)
{
    unsigned char data_H,data_L;
    unsigned int data;
    LCD_Write_cmd(0x80|row);
    LCD_Write_cmd(0x80|col);
    LCD_Check_busy();    
    LCD_Write1byte(0xfe); 
    LCD_Read1byte();
    LCD_Write1byte(0xfe); 
    data_H=LCD_Read1byte();
    LCD_Write1byte(0xfe); 
    data_L=LCD_Read1byte();
    data=data_H;
    data<<=8;
    data=data+data_L;
}
    
/****************************************************************
功能:写一串字符到LCM
参数:datacode是要写入的数据
****************************************************************/
void LCD_Write_string(unsigned char datacode[])
{
	unsigned char i=0;
	while(datacode[i])
	{
		LCD_Write_data(datacode[i]);
		i++;
		if(i==0xff)break;
	}
}
/*==============设置坐标======================*/
void LCD_Location_xy(unsigned char row,unsigned char col)
{
	switch(row)
	{
	case 0:
		row=0x80;break;
	case 1:
		row=0x90;break;
	case 2:
		row=0x88;break;
	case 3:
		row=0x98;break;
	default:
		row=0x80;
	}
	col=col&0x07;            //防止跑出界
	LCD_Write_cmd(row+col);
}  
/***********************************************************************
功能:在(x,y)处显示字符串string
************************************************************************/
void LCD_Write_String_xy(unsigned char row,unsigned char col,unsigned char string[])
{
	LCD_Write_cmd(0x30);
	LCD_Location_xy(row,col);
	LCD_Write_string(string);
}
/***********************************************************************
功能:在第row行,col列处显示整型数据n
************************************************************************/
void num(unsigned char row,unsigned char col,unsigned int n)
{
    unsigned char i=0,length=0,a[7]={0,0,0,0,0,0,0};
	unsigned int nx=n;
	for(i=0;i<6;i++)
	{
	    if(nx>0)length++;
	    nx/=10;
	}
	nx=n;
    for(i=length;i>0;i--)
    {
        a[i-1]=nx%10+48;
        nx=nx/10;
    }
    if(n==0)a[0]='0';
	LCD_Write_cmd(0x30);
	LCD_Write_String_xy(row,col,a);
}
/***********************************************************************
功能:在第row行,col列处显示16进制数据n
************************************************************************/
void num16(unsigned char row,unsigned char col,unsigned int n)
{
	unsigned char i=0,a[7]={0,0,0,0,0,0,0};
	unsigned int nx=n;
	if(nx<=0xFF)
	{
		a[3]=nx%16;
		nx/=16;
		a[2]=nx%16;
		for(i=2;i<4;i++)
		{
			if(a[i]<10)a[i]+=48;
			else a[i]+=55;
		}
		a[4]=0x00;
	}
	else
	{
		a[5]=nx%16;
		nx/=16;
		a[4]=nx%16;
		nx/=16;
		a[3]=nx%16;
		nx/=16;
		a[2]=nx%16;
		for(i=2;i<6;i++)
		{
			if(a[i]<10)a[i]+=48;
			else a[i]+=55;
		}
	}
	a[1]='x';
	a[0]='0';
	LCD_Write_cmd(0x30);
	LCD_Write_String_xy(row,col,a);
}

 

    
    
    
    
    


void main()
{ 

  unsigned char i=0;
    PORTB=0X0;
  DDRB=0XFF; 
 while(1)  
 {                         
  unsigned int test[]="阿啊";
  Init_LCD();
  LCD_Write_String_xy(0,i,test);
  num(1,5,i);  
  PORTB=~PORTB; 
  delay_ms(1000);
  clear(0);
  num16(2,0,test[0]);
  num16(2,5,test[1]);
  
  delay_ms(1000);
  i++;
  }
  
    
}

⌨️ 快捷键说明

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