?? 186.c
字號:
#include<iom16v.h>
#include<macros.h>
#include<stdio.h>
#define uint unsigned int
#define uchar unsigned char
//#define SPI_CS PB4
#define SetCS PORTB|=(1<<PB4)
#define ClrCS PORTB&=~(1<<PB4)
//1602
#define SetRS PORTD|=(1<<PD7)
#define ClrRS PORTD&=~(1<<PD7)
#define SetRW PORTD|=(1<<PD6)
#define ClrRW PORTD&=~(1<<PD6)
#define SetE PORTD|=(1<<PD5)
#define ClrE PORTD&=~(1<<PD5)
uchar s[4],b[4]; //把數字轉換成字符串
unsigned int d[4];
uchar ReadStatusLCM(void);//讀狀態
void WriteDataLCM(uchar WDLCM);//寫數據
void WriteCommandLCM(uchar WCLCM,uchar c);//寫指令
void LCMInit(void);//初始化
void DisplayOneChar(uchar X,uchar Y,uchar DData);//在指定位置顯示單個字符
void DisplayListChar(uchar X,uchar Y,uchar *DData);//在指定位置顯示字符串
//*******************/延時函數
void delay_1ms(unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
{
for(j=1;j<(1*143-2);j++) //晶振改變時修改
{;}
}
}
//*****************讀狀態,忙則循環,不忙則返回Data
uchar ReadStatusLCM(void)
{
uchar Data;
DDRA=0xff; //設置指令
PORTA=0xff;
DDRD=0xff;//1,輸出,置數 0,輸入,讀數
ClrRS;
SetRW;
SetE; //執行指令
SetE;
DDRA=0x00; //讀忙標志,忙則循環
Data=PINA;
while(Data&0x80)
Data=PINA;
return(Data);
}
//**************************寫指令
void WriteCommandLCM(uchar WCLCM,uchar c)
{
if(c==1) //標志為0不忙檢測,標志為1檢測
ReadStatusLCM();
DDRA=0xff; //設置指令
PORTA=WCLCM;
DDRD=0xff;//1,輸出,置數 0,輸入,讀數
ClrRS;
ClrRW;
SetE;
SetE;
ClrE;
}
//**************************初始化
void LCMInit(void)
{
DDRA=0xff;
PORTA=0x00;
WriteCommandLCM(0x38,0);//三次顯示設置 ,不需忙檢
delay_1ms(5); //四位總線,雙行顯示,5*7點陣
WriteCommandLCM(0x38,0);
delay_1ms(5);
WriteCommandLCM(0x38,0);
delay_1ms(5);
WriteCommandLCM(0x38,1);//顯示模式設置,以后都須忙檢
WriteCommandLCM(0x08,1);//關閉顯示
WriteCommandLCM(0x01,1);//清屏
WriteCommandLCM(0x06,1);//光標右移,字符不移
WriteCommandLCM(0x0c,1);//顯示開,有光標,有閃爍
}
//***************************寫數據
void WriteDataLCM(uchar WDLCM)
{
ReadStatusLCM(); //忙檢測
DDRA=0xff; //設置指令
PORTA=WDLCM;
DDRD=0xff;//1,輸出,置數 0,輸入,讀數
SetRS;
ClrRW;
SetE;
SetE;
ClrE;
}
//****************************按指定位置顯示一個字符
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=0x01;//Y不能>1
X&=0x0f;//X不能>15
if(Y)
X+=0x40;//顯示第二行時地址碼+0x40
X+=0x80;//計算指令碼
WriteCommandLCM(X,0);//發送地址碼不需忙檢
WriteDataLCM(DData);
}
//*****************************在指定位置顯示字符串
void DisplayListChar(uchar X,uchar Y,uchar *DData)
{
uchar pose=0;
Y&=0x01;//Y不能>1
X&=0x0f;//X不能>15
while(DData[pose]>0x20)
{
DisplayOneChar(X,Y,DData[pose]);//顯示單個字符
pose++;
X++;
if(X>=0xF)
{X=0x00;
Y=0x01;
}
}
}
//***************主機初始化*******//
//設置端口方向,SPI控制寄存器
void SPI_MasterInit(void)
{
DDRB=(1<<PB5)|(1<<PB7)|(1<<PB4);//PB6:MISO自動為輸入 PB7:SCK
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -