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

📄 avr12864__.c

📁 AVR的12864驱动程序
💻 C
字号:
/************************************************************ 
   LCD采用HD61202,128x64
   控制单片机ATMage16L 8MHz
************************************************************/

#include <mega16.h>
//#include <delay.h>
//#include <stdio.h>
#include "hzk.h"

/*LCD引脚定义 PB0-7对应LCD DB0-DB7*/
#define CS1 PORTA.6
#define CS2 PORTA.5
#define RS PORTA.2
#define RW PORTA.3
#define E PORTA.4
#define RST PORTA.7
#define SPEAK PORTA.0
#define LCDEL PORTA.1

#define MOTO1A PORTD.6
#define MOTO1B PORTD.7     
#define MOTO2A PORTC.0
#define MOTO2B PORTC.1     

#define HD7279CS 		0
#define HD7279CLK 		1
#define HD7279DATA		4
#define HD7279KEY		5

#define PD2		2
#define	PD3		3

#define  URSEL    7
//#define  UMSEL    6
//#define  UPM1     5
//#define  UPM0     4
//#define  USBS     3
#define  UCSZ1    2
#define  UCSZ0    1
//#define  UCPOL    0
  
#define  RXCIE    7
#define  TXCIE    6
#define  UDRIE    5
#define  RXEN     4
#define  TXEN     3
#define  UCSZ2    2
#define  RXB8     1
#define  TXB8     0
         
#define  RXC      7
#define  TXC      6
#define  UDRE     5
#define  FE       4
#define  DOR      3
#define  OVR      3    /*This definition differs from the databook*/
#define  PE       2
#define  U2X      1
#define  MPCM     0


typedef const struct  
{
   unsigned char X;
   unsigned char Y;
}LCDPos;

LCDPos const LCDInfo[]={{0x28,0x00},{0x30,0x00},{0x38,0x00},{0x40,0x00},{0x48,0x00},
                      {0x50,0x00},{0x58,0x00},
                      {0x30,0x02},{0x38,0x02},{0x40,0x02},
                      {0x48,0x02},{0x50,0x02},
                      {0x30,0x04},{0x40,0x04},
                      {0x30,0x06},{0x40,0x06},{0x48,0x06}};
unsigned char LCDInfoBuf[17];

int Count = 1234;

void delay_us(int time)
 {     
  do
	{
	time--;
	}	
  while (time>1);
 }

void Delay(int x)
{
	int i,j;
	for(j=0;j<x;j++)
	{
  		for(i=0;i<250;i++);
	}
}

/*         字节接收函数          */
unsigned char receive_byte(void)
{
   unsigned char i,temp=0;
   DDRD &= ~(1<<HD7279DATA);
   delay_us(90);
   for(i=0;i<8;i++)
   {
        temp=temp<<1;  
        PORTD |= (1<<HD7279CLK);
        delay_us(10);
        if((PIND & (1<<HD7279DATA))!=0) 
           temp|=0x01;   
        PORTD &= ~(1<<HD7279CLK);
        delay_us(10);
   }
   DDRD |= (1<<HD7279DATA);  
   return temp;
}
/*         字节发送函数            */
void send_byte(unsigned char para)
{
    unsigned char i;
    PORTD &= ~(1<<HD7279CS);
    delay_us(90);
    for(i=0;i<8;i++)
    {
    	if((para&0x80)==0x80)
     		PORTD |= (1<<HD7279DATA);   
       	else
        	PORTD &= ~(1<<HD7279DATA);   
        PORTD |= (1<<HD7279CLK);      
        delay_us(10);
        PORTD &= ~(1<<HD7279CLK);      
        delay_us(10);
        para=para<<1;                
	}	  	 
}
/*               写HD7279双字节命令                */      
void write7279(unsigned char cmd1, unsigned char cmd2)
{
	send_byte (cmd1);
	send_byte (cmd2);                              
	PORTD |= (1<<HD7279CS);
}
     
void Rst7279(void)
{
    send_byte(0xa4);      // 复位HD7279
	PORTD |= (1<<HD7279CS);
}         

unsigned char Read7279(void)
{                     
	unsigned char keyval;
	if((PIND & (1<<HD7279KEY))==0)
	{
		send_byte(0x15);
		keyval=receive_byte();
		PORTD |= (1<<HD7279CS);
		return keyval;
	}
	return 0xff;
}
void LCD_NOP(void)
{
	unsigned char i;
	for(i=0;i<50;i++); 
//	#asm("NOP");
}


//LCD左半屏写命令字
void LCD_Write_ComL(unsigned char x)
{ 
  	CS1=0;
  	CS2=1;
  	LCD_NOP();
  	RS=0;
  	E=1;
  	LCD_NOP();
  	RW=0;
  	PORTB=x;
  	E=1;
  	E=0;
  	CS1=1;
  	CS2=1;
}

//LCD左半屏写数据
void LCD_Write_DatL(unsigned char x)
{ 
  	CS1=0;	
  	CS2=1;
  	LCD_NOP();
  	RS=0;
  	E=1;
  	LCD_NOP();
  	RS=1;
  	RW=0;
  	PORTB=x;
  	E=1;
  	E=0;
  	CS1=1;
  	CS2=1;
}

//LCD右半屏写命令字
void LCD_Write_ComR(unsigned char x)
{ 
  	CS1=1;
  	CS2=0;
  	LCD_NOP();
  	RS=0;
  	E=1;
  	LCD_NOP();
  	RW=0;
  	PORTB=x;
  	E=1;
  	E=0;
  	CS1=1;
  	CS2=1;
  
}      

//LCD右半屏写数据
void LCD_Write_DatR(unsigned char x)
{ 
  	CS1=1;
  	CS2=0;
  	LCD_NOP();
  	RS=0;
  	E=1;
  	LCD_NOP();
  	RS=1;
  	RW=0;
  	PORTB=x;
  	E=1;
  	E=0;
  	CS1=1;
  	CS2=1;
  
}

//LCD初始化
void LCD_Init(void)
{  
	RST = 1;
	RST = 0;
	Delay(200);
    RST = 1;
  	LCD_Write_ComL(0x3e);
  	LCD_Write_ComR(0x3e);
  	LCD_Write_ComL(0x3f);
  	LCD_Write_ComR(0x3f);
  	LCD_Write_ComL(0xc0);
  	LCD_Write_ComR(0xc0);
}


//LCD清屏
void LCD_Clr(void)
{
	unsigned char i,j;

	for(j=0;j<8;j++)
	{
  		LCD_Write_ComL(0xb8|j);  
  		LCD_Write_ComL(0x40);
  		LCD_Write_ComR(0xb8|j);  
  		LCD_Write_ComR(0x40);
  		for(i=0;i<64;i++)
  		{
    		LCD_Write_DatL(0x00);  
    		LCD_Write_DatR(0x00); 
  		}
	}
}

//------------------------------------------------------
//            中英文写入函数             
//  x方向 x+1屏幕上移动1点;y方向 y+1屏幕上移动8点
//------------------------------------------------------
void CCW_PR(unsigned int neima,unsigned char X,unsigned char Y,unsigned char fan)
{
    unsigned char k,com,dat1,hz;
    unsigned int i;
    Y=Y|0xb8;				//置页地址
    LCD_Write_ComL(Y);
    LCD_Write_ComR(Y);
    if(neima<CB1B1)          //判断字库是不是汉字
        hz=8;
    else
        hz=0;
    if(X<0x40)				//判断是否写命令字在第一块HD61202
    {
       com=X+0x40;			//置列地址
       LCD_Write_ComL(com);
       k='L';
    }
    else
    {
       com=X;
       LCD_Write_ComR(X);				//Com=X-0x40+0x40
    }
    com=com&0x3f;
    for(i=neima;i<(16+neima-hz);i++)
    {
        dat1=hzdot[i];
        if(fan)
           dat1=~dat1;
        if(k=='L')
        {
            LCD_Write_DatL(dat1);
        }
        else
        {
            LCD_Write_DatR(dat1);
        }
        com++;
        if(com==0x40)			//判断是否为第二块HD61202
        {
           LCD_Write_ComR(com);
           k='R';
        }
    }
    Y++;
    LCD_Write_ComL(Y);
    LCD_Write_ComR(Y);
    if(X<0x40)				//判断是否写命令字在第一块HD61202
    {
       com=X|0x40;
       LCD_Write_ComL(com);
       k='L';
    }
    else
    {
       com=X;
       LCD_Write_ComR(X);				//Com=X-0x40+0x40
    }
    com=com&0x3f;
    for(i=(16+neima-hz);i<(32+neima-hz-hz);i++)    //调整字库点阵的位置
    {
        dat1=hzdot[i];
        if(fan)
           dat1=~dat1;
        if(k=='L')
        {
            LCD_Write_DatL(dat1);
        }
        else
        {
            LCD_Write_DatR(dat1);
        }
        com++;
        if(com==0x40)			//判断是否为第二块HD61202
        {
           LCD_Write_ComR(com);
           k='R';
        }
    }
}

void logo(void)
{

    CCW_PR(CB5E7,0x4-2,0x06,0);       //电
    CCW_PR(CBBB0,0x14-2,0x06,0);      //话

    CCW_PR(CHAR_30,0x28-2,0x06,0);
    CCW_PR(CHAR_31,0x30-2,0x06,0);
    CCW_PR(CHAR_32,0x38-2,0x06,0);
    CCW_PR(CHAR_33,0x40-2,0x06,0);
    CCW_PR(CHAR_34,0x48-2,0x06,0);
    CCW_PR(CHAR_35,0x50-2,0x06,0);
    CCW_PR(CHAR_36,0x58-2,0x06,0);
    CCW_PR(CHAR_37,0x60-2,0x06,0);
    CCW_PR(CHAR_38,0x68-2,0x06,0);
    CCW_PR(CHAR_39,0x70-2,0x06,0);
//    CCW_PR(ASC0,0x78-2,0x06,0);
}

// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
	if(PIND & (1<<PD3))
		Count++;
	else
		Count--;
}

//主程序
void main(void)
{
	unsigned int i;
	unsigned char Key;
	DDRB=0xff;
	PORTB=0x00;
	DDRA=0xff;

	DDRC = 0xff;
	PORTC = 0x00;

// Port D initialization
// Func7=Out Func6=Out Func5=In Func4=Out Func3=In Func2=In Func1=Out Func0=Out
// State7=0 State6=0 State5=P State4=0 State3=P State2=P State1=0 State0=0
	PORTD=0x2C;
	DDRD=0xD3;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: Off
// INT2: Off
	GICR|=0x40;
	MCUCR=0x03;
	MCUCSR=0x00;
	GIFR=0x40;

	Rst7279();
    #asm("wdr");
  
	#asm("SEI");    //开总中断
	
	LCD_Init(); //LCD初始化
	LCD_Clr();     //LCD清屏
    MOTO1A = 0;
    MOTO1B = 1;
    MOTO2A = 0;
    MOTO2B = 1;
    SPEAK = 1;
    LCDEL = 1;
	logo();
	Delay(200);
	Delay(200);
	while(1)
	{                    
		Key = Read7279();
		if(Key != 0xff)
			Count = Key;
    	LCDInfoBuf[0]=(Count/10000)%10;
    	LCDInfoBuf[1]=(Count/1000)%10;
    	LCDInfoBuf[2]=(Count/100)%10;
    	LCDInfoBuf[3]=(Count/10)%10;
    	LCDInfoBuf[4]=(Count)%10;
		for (i=0;i<7;i++)
		{
			CCW_PR(LCDInfoBuf[i]*16,LCDInfo[i].X,LCDInfo[i].Y,0);	
		}
	
	}   
}

⌨️ 快捷键说明

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