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

📄 x.c

📁 单片机控制的12864液晶显示器的驱动程序,通过按键控制
💻 C
字号:
//2003.3.12
//write by sunny
//03.4.23 rewrite by sunny
#include "main.h"
#include "X.h"

#define CS25045     P16
#define CLK25045    P17
#define I25045      P35
#define O25045      P34
#define WREN_INST   0x06
#define WRDI_INST   0x04
#define WRSR_INST   0x01
#define RDSR_INST   0x05
#define WRITE_INST  0x02
#define READ_INST   0x03
#define STATUS_REG  0x30  //watch dog = 1ms
#define INIT_STATE  0x09
#define MAX_POLL    0x99
#define SLIC        0x30

#define HIGH    1
#define LOW     0

//user's functions:
//set wotch dog time
void SetWatchDog()
{
        Write25045StatusRigester(STATUS_REG);
}
//feed watch Dog
void FeedDog()
{
        CS25045 = LOW;
        CS25045 = HIGH;
}

////////////////////////////////////////////////////////////
//internal use fuctions:
//send a byte to 25045
void OutByte25045(unsigned char outbyte)
{
        unsigned char temp;
        for(temp = 0; temp <8; temp ++)
        {
                CLK25045 = LOW;
                I25045 = (bit)(outbyte & 0x80);
                CLK25045 = HIGH;
                outbyte = outbyte<<1;
        }
        I25045 = LOW;
}
//received a byte from 25045
unsigned char InByte25045()
{
        unsigned char received = 0x00;
        unsigned char temp;
        for(temp = 0; temp <8; temp ++)
        {
                CLK25045 = HIGH;
                CLK25045 = LOW;
                received = received <<1;
                O25045 = HIGH;
                received += (unsigned char)O25045;
        }
        return received ;

}
//set 25045 write enable
void Set25045WriteEnable()
{
        CLK25045 = LOW;
        CS25045 = LOW;
        OutByte25045(WREN_INST);
        CLK25045 = LOW;
        CS25045 = HIGH;
}
//set 25045 write disable
void Reset25045WriteEnable()
{
        CLK25045 = LOW;
        CS25045 = LOW;
        OutByte25045(WRDI_INST);
        CLK25045 = LOW;
        CS25045 = HIGH;

}
//write 25045 status rigester
void Write25045StatusRigester(unsigned char reg)
{
        Set25045WriteEnable();

        CLK25045 = LOW;
        CS25045  = LOW;
        OutByte25045(WRSR_INST);
        OutByte25045( reg);
        CLK25045 = LOW;
        CS25045 = HIGH;
        WipPoll25045();

        Reset25045WriteEnable();
}
//read 25045 status rigester
unsigned char Read25045StatusRigester()
{
        unsigned char statusrigester;
        CLK25045 = LOW;
        CS25045 = LOW;
        OutByte25045(RDSR_INST);
        statusrigester = InByte25045();
        CLK25045 = LOW;
        CS25045 = HIGH;
        return statusrigester;
}

//write byte to 25045; 0<=address<=1ff
void ByteWrite25045EEROM(unsigned char writebyte,unsigned int address)
{
        unsigned char temp = WRITE_INST;
        Set25045WriteEnable();

        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        OutByte25045(writebyte);
        CLK25045 = LOW;
        CS25045 = HIGH;
        WipPoll25045();

        Reset25045WriteEnable();

}

unsigned char ByteRead25045EEROM(unsigned int address)
{
        unsigned char readbyte;
        unsigned char temp = READ_INST;
        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        readbyte = InByte25045();
        CLK25045 = LOW;
        CS25045 = HIGH;
        return readbyte;
}

//wait for write complete;
void WipPoll25045()
{
        unsigned char temp = MAX_POLL;
        unsigned char statusregister = 0x00;
        while (temp >1)
        {
                temp --;
                statusregister = Read25045StatusRigester();
                if (statusregister&0x01 == 0x00)
                   temp = 0x01;//out loop
        }
}

//write page 2byte to 25045; 0<=address<=1ff,高字节在高位
unsigned char  DataWrite25045EEROM(unsigned int writedata,unsigned int address)
{
        unsigned char  temp = WRITE_INST;
        if(address >0x1FF )
        return FALSE;
        Set25045WriteEnable();
        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        OutByte25045((unsigned char)writedata);
        OutByte25045((unsigned char)(writedata>>8));
        CLK25045 = LOW;
        CS25045 = HIGH;
        WipPoll25045();
        Reset25045WriteEnable();
        return TRUE;
}
unsigned int  DataRead25045EEROM(unsigned int address)
{
        unsigned char temp = READ_INST;
		unsigned int  temp1;
        if(address >0x1FF )
        return FALSE;
        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        temp = InByte25045();
        temp1 = InByte25045();
		temp1 = (temp1<<8)+temp;
        CLK25045 = LOW;
        CS25045 = HIGH;
        return temp1;
}


//write page /4byte to 25045; 0<=address<=1ff
unsigned char  PageWrite25045EEROM(unsigned int address,unsigned char *writeByteArray)
{
        unsigned char  temp = WRITE_INST;
        if(address >0x1FF )
        return FALSE;
        Set25045WriteEnable();
        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        OutByte25045(*writeByteArray);
        OutByte25045(*(writeByteArray+1));
        OutByte25045(*(writeByteArray+2));
        OutByte25045(*(writeByteArray+3));
        CLK25045 = LOW;
        CS25045 = HIGH;
        WipPoll25045();
        Reset25045WriteEnable();
        return TRUE;
}

//read page /4byte to 25045; 0<=address<=1ff
unsigned char  PageRead25045EEROM(unsigned int address,unsigned char *readByteArray)
{
        unsigned char temp = READ_INST;
        if(address >0x1FF )
        return FALSE;
        CLK25045 = LOW;
        CS25045 = LOW;
        if(address >= 0x100)
        temp = temp | 0x03;
        OutByte25045(temp);
        OutByte25045((unsigned char)address);
        *(readByteArray) = InByte25045();
        *(readByteArray+1) = InByte25045();
        *(readByteArray+2) = InByte25045();
        *(readByteArray+3) = InByte25045();
        CLK25045 = LOW;
        CS25045 = HIGH;
        return TRUE;
}


//write data to 25045
void Write25045EEROM(unsigned int address,unsigned char *writeArray,unsigned char num)
{
        unsigned char tempaddress = 0x00;
        while ( num > 0 )
        {
                if ( num  <= 4 )
                   num  = 0;
                else num -=4;
                PageWrite25045EEROM(address+tempaddress*4,(writeArray+tempaddress*4));
                tempaddress++;
        }
}

//read data from 25045
void Read25045EEROM(unsigned int address,unsigned char *readArray,unsigned char num)
{
        unsigned char biasaddress = 0x00;
        while ( num  > 0 )
        {
                if ( num  <= 4 )
                   num  = 0;
                else num -=4;
                PageRead25045EEROM(address+biasaddress*4,(readArray+biasaddress*4));
                biasaddress++;
        }
}



⌨️ 快捷键说明

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