📄 main.c
字号:
//=========================================================================
//
// 葛 胆 疙 : 186TJ1
// 荤 剧 : 1.86",TFT-LCD,128x160,CPU I/F,16Bit
//
// 荤侩 CPU : ATmega128-16M,ATmega128L-8M
// 哪颇老矾 : AVRStudio4.13
// 累 己 磊 : Jini's LCD Cafe
// 累己滚怜 : V0.0
// 累己老磊 : 2008-03-30
// 荤侩器飘 : RESET/:PA0,CS/:PA1,RS:PA2,WR/:PA3,RD/:PA4,IF1:PA5
// DB0~DB7:PC0~PC7,DB8~DB15:PF0~PF7
//
//=========================================================================
//-------------------------------------------------------------------------
//
// 家胶颇老 : main.c
// 扁 瓷 : LCD 扁夯 悼累 犬牢
// Red ℃ Green ℃ Blue ℃ White ℃ Black ℃ 啊肺8Gray
// ℃ 技肺8Gray ℃ 啊肺8Color ℃ 技肺8Color ℃ Image
//
//-------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "image.h"
//-------------------------------------------------------------------------
void Initial_Port(void); //Initial AVR Port
void Initial_Lcd(void); //Initial LCD
void Write_Cmd(int Cmd_High, int Cmd_Low ); //Write Command
void Write_Data(int Data_High, int Data_Low ); //Write Data
void Set_Entry_Hor(void); //Entry Mode Horizontal Set
void Set_Entry_Ver(void); //Entry Mode Vertical Set
void Set_Write_Gram(void); //Data Write to GRAM Set
void Disp_Full_Color(int Full_High,int Full_Low); //Display Full Color
void Disp_8Gray_Hor(void); //Display 8-Gray Horizontal
void Disp_8Gray_Ver(void); //Display 8-Gray Vertical
void Disp_8Color_Hor(void); //Display 8-Color Horizontal
void Disp_8Color_Ver(void); //Display 8-Color Vertical
void Disp_Image(void); //Display Image
//-------------------------------------------------------------------------
#define BV(bit) (1<<(bit)) //Bit Define
#define sbi(reg,bit) reg |= (BV(bit))
#define cbi(reg,bit) reg &= ~(BV(bit))
#define RESET_H sbi(PORTA,0) //RESET_H = PA0
#define RESET_L cbi(PORTA,0) //RESET_L = PA0
#define CS_H sbi(PORTA,1) //CS_H = PA1
#define CS_L cbi(PORTA,1) //CS_L = PA1
#define RS_H sbi(PORTA,2) //RS_H = PA2
#define RS_L cbi(PORTA,2) //RS_L = PA2
#define WR_H sbi(PORTA,3) //WR_H = PA3
#define WR_L cbi(PORTA,3) //WR_L = PA3
#define RD_H sbi(PORTA,4) //RD_H = PA4
#define RD_L cbi(PORTA,4) //RD_L = PA4
#define IF1_H sbi(PORTA,5) //IF1_H = PA5
#define IF1_L cbi(PORTA,5) //IF1_L = PA5
//-------------------------------------------------------------------------
void Delay_us(unsigned char time_us) //Delay Set
{ register unsigned char i;
for(i=0; i<time_us; i++) //4 cycles
{ asm volatile("PUSH R0"); //2 cycles
asm volatile("POP R0"); //2 cycles
asm volatile("PUSH R0"); //2 cycles
asm volatile("POP R0"); //2 cycles
asm volatile("PUSH R0"); //2 cycles
asm volatile("POP R0"); //2 cycles = 16Mhz
}
}
void Delay_ms(unsigned int time_ms) //250us*4 = 1ms
{ register unsigned int i;
for(i=0; i<time_ms; i++)
{ Delay_us(250);
Delay_us(250);
Delay_us(250);
Delay_us(250);
}
}
//-------------------------------------------------------------------------
int main(void)
{
Initial_Port(); //Initial AVR Port
PORTA = RD_H; //If not use Read function, Must be High.
PORTA = IF1_L; //Interface Select Mode (16Bit=Low,9Bit=High)
PORTA = RESET_L; //Reset Operation
Delay_ms(1);
PORTA = RESET_H;
Delay_ms(15);
Initial_Lcd(); //Initial LCD
while(1)
{
Disp_Full_Color(0xf8,0x00); //Display Full Color Red
Delay_ms(500); //Display Speed Control
Disp_Full_Color(0x07,0xe0); //Display Full Color Green
Delay_ms(500); //Display Speed Control
Disp_Full_Color(0x00,0x1f); //Display Full Color Blue
Delay_ms(500); //Display Speed Control
Disp_Full_Color(0xff,0xff); //Display Full Color White
Delay_ms(500); //Display Speed Control
Disp_Full_Color(0x00,0x00); //Display Full Color Black
Delay_ms(500); //Display Speed Control
Disp_8Gray_Hor(); //Display 8-Gray Horizontal
Delay_ms(500); //Display Speed Control
Disp_8Gray_Ver(); //Display 8-Gray Vertical
Delay_ms(500); //Display Speed Control
Disp_8Color_Hor(); //Display 8-Color Horizontal
Delay_ms(500); //Display Speed Control
Disp_8Color_Ver(); //Display 8-Color Vertical
Delay_ms(500); //Display Speed Control
Disp_Image(); //Display Image
Delay_ms(500); //Display Speed Control
}
}
//-------------------------------------------------------------------------
void Initial_Port(void) //Initial AVR Port
{
DDRA = 0xFF; //Set PORTA - All Output
PORTA = 0xFF; //Set PORTA - All High
DDRC = 0xFF; //Set PORTC - All Output
PORTC = 0xFF; //Set PORTC - All High
DDRF = 0xFF; //Set PORTF - All Output
PORTF = 0xFF; //Set PORTF - All High
}
//-------------------------------------------------------------------------
void Initial_Lcd(void) //Initial LCD
{
//Oscillator Start
Write_Cmd(0x00,0x00);Write_Data(0x00,0x01);
Delay_ms(15);
Write_Cmd(0x00,0x07);Write_Data(0x00,0x00);
//Power Supply Setting (Before)
Write_Cmd(0x00,0x11);Write_Data(0x00,0x01);
Write_Cmd(0x00,0x12);Write_Data(0x00,0x0A);
Write_Cmd(0x00,0x13);Write_Data(0x13,0x1F);
Write_Cmd(0x00,0x10);Write_Data(0x00,0x04);
//Power Supply Setting (1)
Write_Cmd(0x00,0x10);Write_Data(0x00,0x64);
Write_Cmd(0x00,0x11);Write_Data(0x00,0x03);
Write_Cmd(0x00,0x12);Write_Data(0x00,0x1A);
Delay_ms(40);
//Power Supply Setting (2)
Write_Cmd(0x00,0x13);Write_Data(0x32,0x19);
Write_Cmd(0x00,0x10);Write_Data(0x05,0x60);
Write_Cmd(0x00,0x15);Write_Data(0x00,0x02);
//Display Control Setting (1)
Write_Cmd(0x00,0x01);Write_Data(0x01,0x13);
Write_Cmd(0x00,0x02);Write_Data(0x07,0x00);
Write_Cmd(0x00,0x03);Write_Data(0x10,0x30);
Write_Cmd(0x00,0x08);Write_Data(0x08,0x08);
Write_Cmd(0x00,0x09);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x0B);Write_Data(0x00,0x0B);
Write_Cmd(0x00,0x0C);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x0D);Write_Data(0x32,0x29);
Write_Cmd(0x00,0x0E);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x21);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x22);
Write_Cmd(0x00,0x23);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x24);Write_Data(0x00,0x00);
//Gamma Setting
Write_Cmd(0x00,0x30);Write_Data(0x00,0x02);
Write_Cmd(0x00,0x31);Write_Data(0x02,0x04);
Write_Cmd(0x00,0x32);Write_Data(0x05,0x04);
Write_Cmd(0x00,0x33);Write_Data(0x01,0x05);
Write_Cmd(0x00,0x34);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x35);Write_Data(0x00,0x03);
Write_Cmd(0x00,0x36);Write_Data(0x00,0x04);
Write_Cmd(0x00,0x37);Write_Data(0x07,0x01);
Write_Cmd(0x00,0x38);Write_Data(0x00,0x09);
Write_Cmd(0x00,0x39);Write_Data(0x00,0x09);
//Display Control Setting (2)
Write_Cmd(0x00,0x40);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x41);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x42);Write_Data(0xA1,0x00);
Write_Cmd(0x00,0x43);Write_Data(0xFF,0x00);
Write_Cmd(0x00,0x44);Write_Data(0x7F,0x00);
Write_Cmd(0x00,0x45);Write_Data(0x9F,0x00);
//Display On Sequence
Write_Cmd(0x00,0x10);Write_Data(0x65,0x60);
Write_Cmd(0x00,0x07);Write_Data(0x00,0x05);
Delay_ms(40);
Write_Cmd(0x00,0x07);Write_Data(0x00,0x25);
Write_Cmd(0x00,0x07);Write_Data(0x00,0x27);
Delay_ms(40);
Write_Cmd(0x00,0x07);Write_Data(0x00,0x37);
}
//-------------------------------------------------------------------------
void Set_Entry_Hor(void) //Entry Mode Horizontal Set
{
Write_Cmd(0x00,0x03);Write_Data(0x10,0x30);
}
//-------------------------------------------------------------------------
void Set_Entry_Ver(void) //Entry Mode Vertical Set
{
Write_Cmd(0x00,0x03);Write_Data(0x10,0x38);
}
//-------------------------------------------------------------------------
void Set_Write_Gram(void) //Data Write to GRAM Set
{
Write_Cmd(0x00,0x21);Write_Data(0x00,0x00);
Write_Cmd(0x00,0x22);
}
//-------------------------------------------------------------------------
void Write_Cmd(int Cmd_High, int Cmd_Low ) //Write Command
{
PORTA = CS_L;
PORTA = RS_L;
PORTF = Cmd_High;
PORTC = Cmd_Low;
PORTA = WR_L;
PORTA = WR_H;
}
//-------------------------------------------------------------------------
void Write_Data(int Data_High, int Data_Low ) //Write Data
{
PORTA = CS_L;
PORTA = RS_H;
PORTF = Data_High;
PORTC = Data_Low;
PORTA = WR_L;
PORTA = WR_H;
}
//-------------------------------------------------------------------------
void Disp_Full_Color(int Full_High,int Full_Low) //Full Color Display Data
{
Set_Entry_Hor();
Set_Write_Gram();
int i,j;
for(i=0; i<128; i++)
for(j=0; j<160; j++)
{
Write_Data(Full_High, Full_Low);
}
}
//-------------------------------------------------------------------------
int Gray[]= //8-Gray Display Data
{
0xffff,
0xd6ba,
0xb5b6,
0x94b2,
0x738e,
0x528a,
0x3186,
0x1082,
};
//-------------------------------------------------------------------------
void Disp_8Gray_Hor(void) //Display 8-Gray Horizontal
{
Set_Entry_Hor();
Set_Write_Gram();
int i,j,k,temp,temp1;
for(i=0; i<8; i++)
{
temp = (Gray[i] & 0xff00)>>8;
temp1= Gray[i] & 0xff;
for(j=0; j<128; j++)
{
for(k=0; k<20; k++)
{
Write_Data(temp, temp1);
}
}
}
}
//-------------------------------------------------------------------------
void Disp_8Gray_Ver(void) //Display 8-Gray Vertical
{
Set_Entry_Ver();
Set_Write_Gram();
int i,j,k,temp,temp1;
for(i=0;i<8;i++)
{
temp = (Gray[i] & 0xff00)>>8;
temp1= Gray[i] & 0xff;
for(j=0; j<160; j++)
{
for(k=0; k<16; k++)
{
Write_Data(temp, temp1);
}
}
}
}
//-------------------------------------------------------------------------
int color[]= //8-Color Display Data
{
0xffff,
0xf800,
0x07e0,
0x001f,
0xad55,
0xffe0,
0x07ff,
0xf81f
};
//-------------------------------------------------------------------------
void Disp_8Color_Hor(void) //Display 8-Color Horizontal
{
Set_Entry_Hor();
Set_Write_Gram();
int i,j,k,temp,temp1;
for(i=0;i<8;i++)
{
temp = (color[i] & 0xff00)>>8;
temp1= color[i] & 0xff;
for(j=0; j<128; j++)
{
for(k=0; k<20; k++)
{
Write_Data(temp, temp1);
}
}
}
}
//-------------------------------------------------------------------------
void Disp_8Color_Ver(void) //Display 8-Gray Horizontal
{
Set_Entry_Ver();
Set_Write_Gram();
int i,j,k,temp,temp1;
for(i=0;i<8;i++)
{
temp = (color[i] & 0xff00)>>8;
temp1= color[i] & 0xff;
for(j=0; j<160; j++)
{
for(k=0; k<16; k++)
{
Write_Data(temp, temp1);
}
}
}
}
//-------------------------------------------------------------------------
void Disp_Image(void) //Image Display
{
{
Set_Entry_Hor();
Set_Write_Gram();
int i,j,temp,temp1,k=0;
for(i=0; i<128; i++)
{
for(j=0; j<80; j++)
{
temp = (pgm_read_word(&ImageUp[k]) & 0xff00)>>8;
temp1 = pgm_read_word(&ImageUp[k]) & 0xff;
Write_Data(temp, temp1);
k++;
}
}
}
{
int i,j,temp,temp1,k=0;
for(i=0; i<128; i++)
{
for(j=0; j<80; j++)
{
temp = (pgm_read_word(&ImageDown[k]) & 0xff00)>>8;
temp1 = pgm_read_word(&ImageDown[k]) & 0xff;
Write_Data(temp, temp1);
k++;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -