新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于ATMEGA8515的SPI驅(qū)動(dòng)ST7565P液晶屏的程序

基于ATMEGA8515的SPI驅(qū)動(dòng)ST7565P液晶屏的程序

作者: 時(shí)間:2016-11-22 來(lái)源:網(wǎng)絡(luò) 收藏
這是用SPI方式來(lái)驅(qū)動(dòng)12864液晶的源程序,液晶屏的控制芯片為ST7565P經(jīng)測(cè)試SPI的時(shí)鐘可達(dá)到振蕩頻率的二分頻。比用普通IO口模擬串行的方式快多了。。。
下面貼出源代碼,有詳細(xì)注釋。。。。
本程序簡(jiǎn)單實(shí)用,可拿去作簡(jiǎn)單參考。。。。
編譯環(huán)境:GCC+AVR STUDIO
單片機(jī):ATMEGA 8515
晶振: 3.6864M
#include<avr/io.h> //包含頭文件
#include
#define COMMAND 1
#define DATA 0
/////////////////////////////////////////
//數(shù)據(jù)線(xiàn)定義:
// SI==========PB5 //
// A0==========PC0
// CS==========PC2
// RES=========PC1
// SCL=========PB7
//
/////////////////////////////////////////
unsigned char gushi[8][112]PROGMEM=
{
{0x10,0x10,0x92,0x92,0x92,0xF2,0x9E,0x92,0x92,0x92,0x92,0x92,0xFE,0x10,0x10,0x00, //君問(wèn)歸期未有期
0x00,0x00,0xF8,0x01,0x06,0xE0,0x22,0x22,0x22,0x22,0xE2,0x02,0x02,0xFE,0x00,0x00,
0x00,0xF8,0x00,0x00,0xFF,0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,0xFC,0x00,0x00,
0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
0x40,0x40,0x48,0x48,0x48,0x48,0xC8,0xFF,0x48,0x48,0x48,0x48,0x48,0x40,0x40,0x00,
0x00,0x04,0x84,0x44,0xE4,0x34,0x2C,0x27,0x24,0x24,0x24,0xE4,0x04,0x04,0x04,0x00,
0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00},
{0x40,0x20,0x10,0x0C,0xFE,0x45,0x44,0x44,0x44,0x44,0x44,0x44,0xFD,0x00,0x00,0x00,
0x00,0x00,0xFF,0x00,0x00,0x03,0x02,0x02,0x02,0x02,0x03,0x40,0x80,0x7F,0x00,0x00,
0x00,0x87,0x40,0x30,0x0F,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x3F,0x00,0x00,
0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00,
0x20,0x20,0x10,0x10,0x08,0x06,0x01,0xFF,0x01,0x02,0x04,0x08,0x18,0x30,0x10,0x00,
0x02,0x01,0x00,0x00,0xFF,0x09,0x09,0x09,0x29,0x49,0xC9,0x7F,0x00,0x00,0x00,0x00,
0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00},
{0x00,0x00,0xFE,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00, //巴山夜雨漲池
0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
0x04,0x04,0x84,0xC4,0x3C,0x14,0x85,0x66,0xBC,0x24,0x24,0x24,0xE4,0x24,0x04,0x00,
0x02,0x02,0xF2,0x32,0x52,0x92,0x12,0xFE,0x32,0x52,0x92,0x12,0xF2,0x02,0x02,0x00,
0x10,0x61,0x86,0x60,0xF2,0x12,0x12,0x1E,0x80,0xFF,0xA0,0x90,0x88,0x86,0x80,0x00,
0x24,0x24,0xE4,0xFC,0xA2,0x22,0x22,0x80,0x70,0x00,0xFF,0x40,0x20,0x18,0x00,0x00,
0x10,0x60,0x02,0x8C,0x60,0x80,0xF8,0x40,0x40,0xFE,0x20,0x20,0x10,0xF0,0x00,0x00},
{0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x78,0x20,0x00,
0x00,0x20,0x7F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00,
0x02,0x01,0x00,0xFF,0x00,0x82,0x41,0x22,0x14,0x09,0x14,0x23,0x40,0xC0,0x40,0x00,
0x00,0x00,0xFF,0x01,0x02,0x04,0x00,0x7F,0x01,0x02,0x44,0x80,0x7F,0x00,0x00,0x00,
0x04,0xFC,0x03,0x00,0x01,0x41,0x81,0x7F,0x00,0xFF,0x42,0x0C,0x30,0x60,0x20,0x00,
0x02,0x01,0x00,0xFF,0x00,0x83,0x40,0x20,0x18,0x07,0x01,0x06,0x18,0xE0,0x40,0x00,
0x08,0x08,0x7C,0x03,0x00,0x00,0x3F,0x40,0x40,0x5F,0x40,0x42,0x44,0x43,0x70,0x00},
{0x80,0x40,0xF0,0x1C,0x07,0xE2,0x24,0x24,0x24,0xE4,0x04,0x04,0xFC,0x04,0x04,0x00, //何當(dāng)共剪西窗燭
0x00,0x00,0x40,0x42,0x5C,0x48,0x40,0x40,0x7F,0x40,0x50,0x4E,0x44,0xC0,0x00,0x00,
0x00,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x10,0xFE,0x10,0x10,0x10,0x00,0x00,
0x00,0x04,0xF4,0x54,0x55,0x56,0x54,0xF4,0x04,0x04,0xE6,0x05,0x04,0xF4,0x04,0x00,
0x02,0xF2,0x12,0x12,0x12,0xFE,0x12,0x12,0x12,0xFE,0x12,0x12,0x12,0xF2,0x02,0x00,
0x00,0x4C,0x44,0xE4,0x54,0x44,0x64,0xD5,0x46,0x44,0x4C,0x54,0xD4,0x24,0x2C,0x00,
0x80,0x60,0x00,0xFF,0x20,0x18,0xF0,0x10,0x10,0xFF,0x12,0x10,0xF0,0x10,0x00,0x00},
{0x00,0x00,0x7F,0x00,0x00,0x07,0x02,0x02,0x02,0x07,0x20,0x40,0x3F,0x00,0x00,0x00,
0x00,0x00,0x20,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x22,0x7F,0x00,0x00,
0x02,0x02,0x82,0x42,0x32,0x1F,0x0A,0x02,0x02,0x06,0x0B,0x32,0xE2,0x42,0x02,0x00,
0x00,0x08,0x8F,0x89,0x49,0x2B,0x1D,0x0B,0x08,0x08,0x08,0x4A,0x8C,0x7B,0x00,0x00,
0x00,0x7F,0x28,0x24,0x22,0x21,0x20,0x20,0x20,0x21,0x22,0x22,0x22,0x7F,0x00,0x00,
0x00,0x00,0x00,0xFF,0x44,0x62,0x55,0x49,0x55,0x43,0x41,0x40,0xFF,0x00,0x00,0x00,
0x40,0x20,0x18,0x07,0x04,0x28,0x63,0x21,0x21,0x3F,0x11,0x15,0x1B,0x30,0x00,0x00},
{0x80,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00, //卻話(huà)巴山夜雨時(shí)
0x40,0x41,0x4E,0xC4,0x00,0x20,0x24,0x24,0x24,0x24,0xFC,0x22,0x22,0x22,0x20,0x00,
0x00,0x00,0xFE,0x42,0x42,0x42,0x42,0x7E,0x42,0x42,0x42,0x42,0xFE,0x00,0x00,0x00,
0x00,0x00,0xE0,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0xE0,0x00,0x00,
0x04,0x04,0x84,0xC4,0x3C,0x14,0x85,0x66,0xBC,0x24,0x24,0x24,0xE4,0x24,0x04,0x00,
0x02,0x02,0xF2,0x32,0x52,0x92,0x12,0xFE,0x32,0x52,0x92,0x12,0xF2,0x02,0x02,0x00,
0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00},
{0x00,0x10,0x3C,0x13,0x10,0x08,0x0A,0x1C,0x08,0xFF,0x00,0x08,0x10,0x0F,0x00,0x00,
0x00,0x00,0x00,0x7F,0x20,0x10,0x00,0x7E,0x22,0x22,0x23,0x22,0x22,0x7E,0x00,0x00,
0x00,0x00,0x3F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x78,0x20,0x00,
0x00,0x20,0x7F,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,0x20,0x20,0x20,0x7F,0x00,0x00,
0x02,0x01,0x00,0xFF,0x00,0x82,0x41,0x22,0x14,0x09,0x14,0x23,0x40,0xC0,0x40,0x00,
0x00,0x00,0xFF,0x01,0x02,0x04,0x00,0x7F,0x01,0x02,0x44,0x80,0x7F,0x00,0x00,0x00,
0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00},
};
////////////////////////////////
//發(fā)送一個(gè)字節(jié)到ST7565P并等待發(fā)送完畢
//入口:要發(fā)送的字節(jié)
//返回值:無(wú)
////////////////////////////////
void SPIByte(unsigned char byte)
{
SPDR = byte; //寫(xiě)數(shù)據(jù)
while(!(SPSR & (1<//等待發(fā)送完成
}
////////////////////////////////////////////////
//將一個(gè)字節(jié)發(fā)送的ST7565P的函數(shù)
//入口參數(shù): select ,data
//select:根據(jù)此值確定發(fā)送給ST7565P的是數(shù)據(jù)還是命令
//data: 要發(fā)送給ST7565的一個(gè)字節(jié)
//返回值:無(wú)
////////////////////////////////////////////////
void LCD_SPISend(unsigned char select,unsigned char data)
{
PORTC &= ~(1<<2); // CS=0 芯片使能信號(hào)
if(select == COMMAND) //傳輸?shù)氖敲?wbr />
{
PORTC &= ~(1<<0); // 根據(jù)ST7565P的數(shù)據(jù)手冊(cè),傳輸命令時(shí)A0應(yīng)該是為低電平
SPIByte(data);
PORTC |= 1<<0; // A0=1
}
else
{
SPIByte(data);
}
PORTC |= 1<<2; //CS= 恢復(fù)不使能
}
////////////////////////////////////////////
//寫(xiě)坐標(biāo)函數(shù)
//入口參數(shù):x y
//y: 頁(yè)面地址,此液晶將顯示區(qū)域分為9個(gè)頁(yè)面,y的值域0~8
//x: 列地址,值域:0~127
//返回值:無(wú)
////////////////////////////////////////////
void LCD_XYSet(unsigned char x,unsigned char y)
{
LCD_SPISend(COMMAND,0xB0|y); //y的值為0~8
LCD_SPISend(COMMAND,0x10|(x>>4));
LCD_SPISend(COMMAND,0x00|(x&0x0F));
}
/////////////////////////////////////
//延時(shí)函數(shù)
//入口: ms 當(dāng)延時(shí)100毫秒時(shí),ms的值為100即可
//返回值:無(wú)
/////////////////////////////////////
void DelayMs(volatile unsigned int ms)
{
volatile unsigned int i;
volatile unsigned int j;
for(i=0;i
{
j=280;
while(j--);
}
}
//////////////////////////////////////////////////////
//初始化SPI及液晶屏的函數(shù)
//入口:無(wú)
//返回值:無(wú)
/////////////////////////////////////////////////////
void LCD_Init(void)
{
SPCR = 0x5C;
SPSR |= 1<
//SPI的功能設(shè)置解釋?zhuān)菏鼓躍PI;采用主機(jī)模式;SPI的傳輸模式為模式3;時(shí)鐘頻率為系統(tǒng)時(shí)鐘的兩分頻,即 //3.6864/2M
DelayMs(100);
SPIByte(0xFF); //可發(fā)可不發(fā),無(wú)意義
PORTC &= ~(1<<1); //將復(fù)位腳拉低
DelayMs(5); //延時(shí)5ms,等待電源穩(wěn)定
PORTC |= 1<<1; //將復(fù)位腳拉高
DelayMs(5);
LCD_SPISend(COMMAND,0xAE); //將液晶屏的顯示關(guān)掉
LCD_SPISend(COMMAND,0xA3); //設(shè)置偏壓比
LCD_SPISend(COMMAND,0xA0); //設(shè)置SEG 輸出方向
LCD_SPISend(COMMAND,0xC8); //設(shè)置公共端輸出掃描方向
LCD_SPISend(COMMAND,0x25); //調(diào)整 (1 + Rb/Ra) ratio 值,調(diào)整此值影響很大
LCD_SPISend(COMMAND,0x81); //開(kāi)始設(shè)置α的命令
LCD_SPISend(COMMAND,0x28); //設(shè)置α的值,值越大對(duì)比度越強(qiáng),調(diào)整此值影響不太,是微調(diào)的
DelayMs(5);
LCD_SPISend(COMMAND,0x2F); //開(kāi)啟相關(guān)電壓
DelayMs(50);
LCD_SPISend(COMMAND,0xAF); //顯示開(kāi)
LCD_SPISend(COMMAND,0xF8);
LCD_SPISend(COMMAND,0x01); //此兩條命令是設(shè)置升壓比的
DelayMs(500); //不知此延時(shí)可否省去,不過(guò)了安全起見(jiàn),初次調(diào)試保留為好。。
}
////////////////////////////////////////
//清屏函數(shù)
//入口:無(wú)
//返回值:無(wú)
///////////////////////////////////////
void Clear_Display(void)
{
unsigned char i;
unsigned char j;
for(i=0;i<8;i++)
{
LCD_XYSet(0,i);
for(j=0;j<128;j++)
{
LCD_SPISend(DATA,0); //傳輸數(shù)據(jù)
}
}
}
////////////////////////////////
//顯示一首李商隱的詩(shī)歌:
//  君問(wèn)歸期未有期
//  巴山夜雨漲秋池
//  何當(dāng)共剪西窗燭
//  卻話(huà)巴山夜雨時(shí)
//入口:無(wú)
//返回值:無(wú)
////////////////////////////////
void Gushi_Display(void)
{
unsigned char i;
unsigned char j;
for(i=0;i<8;i++)
{
LCD_XYSet(8,i);
for(j=0;j<112;j++)
{
LCD_SPISend(DATA,__LPM(&gushi[i][j]));
}
}
}
int main(void)
{
DDRB = 0xFF; //將IO口配置為輸出
PORTB = 0xFF; //讓IO口都輸出高電平
DDRC = 0xFF; //配置為輸出
PORTC = 0xFF; //輸出高電平
DelayMs(1);
LCD_Init(); //初始化的函數(shù)
Clear_Display(); //清屏函數(shù)
Gushi_Display();
while(1)
{
}
}
編譯結(jié)果如下:
AVR Memory Usage
----------------
Device: atmega8515
Program: 1688 bytes (20.6% Full)
(.text + .data + .bootloader)
Data: 0 bytes (0.0% Full)
(.data + .bss + .noinit)
Build succeeded with 0 Warnings...
顯示效果如下:



評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉