|
m16程序范例
#define INT8U unsigned char
#define INT8S signed char
#define INT16U unsigned int
#define INT16S signed int
#define INT32U unsigned long
#define INT32S signed long
const unsigned char font6x8[][6] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp
{ 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // !
{ 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // "
{ 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // #
{ 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $
{ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // %
{ 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // &
{ 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // '
{ 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // (
{ 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // )
{ 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // *
{ 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // +
{ 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // ,
{ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // -
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // .
{ 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // /
{ 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0
{ 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1
{ 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2
{ 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3
{ 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4
{ 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5
{ 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6
{ 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7
{ 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8
{ 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9
{ 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // :
{ 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ;
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // <
{ 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // =
{ 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // >
{ 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ?
{ 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @
{ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C
{ 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F
{ 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G
{ 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H
{ 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I
{ 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J
{ 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K
{ 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L
{ 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M
{ 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P
{ 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q
{ 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R
{ 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S
{ 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T
{ 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U
{ 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V
{ 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W
{ 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X
{ 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y
{ 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z
{ 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [
{ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55
{ 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ]
{ 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^
{ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _
{ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // '
{ 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a
{ 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c
{ 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d
{ 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e
{ 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f
{ 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g
{ 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h
{ 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i
{ 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j
{ 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k
{ 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l
{ 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o
{ 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p
{ 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r
{ 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s
{ 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t
{ 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u
{ 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v
{ 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w
{ 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x
{ 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y
{ 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 } // horiz lines
};
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
/*PA 数据口 cs1 cs2 rs rw e
pb0 pb1 pb2 pb3 pb4**/
/**********************/
void PORT_INIT(void)
{
DDRA=0XFF;
PORTA=0X00;
DDRB=0X1F;
PORTB=0X00;
}
void Set_DI(INT8U RS)
{
if(RS==0){PORTB&=~0X04;}
else{PORTB|=0X04;}
}
/***************/
void Set_RW(INT8U RW)
{
if(RW==0){PORTB&=~0X08;}
else{PORTB|=0X08;}
}
/*******************/
void Set_EN(INT8U EN)
{
if(EN==0){PORTB&=~0X10;}
else{PORTB|=0X10;}
}
/***************/
void Set_CS1(INT8U CS1)
{
if(CS1==0){PORTB&=~0X01;}
else{PORTB|=0X01;}
}
void Set_CS2(INT8U CS2)
{
if(CS2==0){PORTB&=~0X02;}
else{PORTB|=0X02;}
}
/********************/
void Set_Data(INT8U data)
{
PORTA=data;
}
/*****************/
void LCD_Command_DispOnOff(INT8U onoff)
{
Set_RW(0);
Set_DI(0);
Set_EN(0);
Set_Data(0X3D+1+onoff);
Set_EN(1);
Set_EN(0);
}
/*******************/
void LCD_Command_SetStartLine(INT8U startline)
{
Set_RW(0);
Set_DI(0);
Set_EN(0);
Set_Data(0XC0+startline);
Set_EN(1);
Set_EN(0);
}
/**********设置显示行地址**********/
void LCD_Command_Set_X(INT8U x)
{
Set_RW(0);
Set_DI(0);
Set_EN(0);
Set_Data(0XB8+x);
Set_EN(1);
Set_EN(0);
}
/*********设置显示列地址************/
void LCD_Command_Set_Y(INT8U column)
{
Set_RW(0);
Set_DI(0);
Set_EN(0);
Set_Data(0X40+column);
Set_EN(1);
Set_EN(0);
}
/*********************************/
void LCD_Command_WriteByte(INT8U data)
{
Set_RW(0);
Set_DI(1);
Set_EN(0);
Set_Data(data);
Set_EN(1);
Set_EN(0);
}
void LCD_Clear_Line(INT8U startline,INT8U endline)
{
INT8U i,j;
for(i=startline;i<=endline;i++)
{Set_CS1(1);
Set_CS2(1);
LCD_Command_Set_X(i);
LCD_Command_Set_Y(0);
for(j=0;j<64;j++)
{
LCD_Command_WriteByte(0X00);
}
}
}
void LCD_Write_Data(INT8U x,INT8U y,INT8U data) /**写一个字节数据。。。****/
{
if(y<64)
{
Set_CS1(1);
Set_CS2(0);
LCD_Command_Set_X(x);
LCD_Command_Set_Y(y);
}
else
{
Set_CS1(0);
Set_CS2(1);
LCD_Command_Set_X(x);
LCD_Command_Set_Y(y-64);
}
LCD_Command_WriteByte(data);
}
/任意位置写一个点/
void LCD_Clear_Dot(INT8U x,INT8U y)
{
INT8U x_address,y_address;
INT8U data=LCD_Read_byte(x>>3,y);
if((x<64)&&(y<128))
{x_address=x>>3;
y_address=y;
LCD_Write_Data(x_address,y_address,data&(~(1<<(x%8))));
}
}
/**********************/
void LCD_Init(void)
{PORT_INIT();
LCD_Clear_Line(0,7);
LCD_Command_DispOnOff(1);
LCD_Command_SetStartLine(0);
}
void LCD_Write_Char(INT8U x,INT8U y,INT8U c)
{if(y<128)
{
if(y<64)
{
INT8U line,y_address=y;
c-=32;
LCD_Command_Set_X(x);
LCD_Command_Set_Y(y);
for (line=0; line<6; line++)
{
if(y_address<64){ Set_CS1(1);Set_CS2(0);LCD_Command_Set_Y(y_address);}
if(y_address>=64){ Set_CS1(0);Set_CS2(1);LCD_Command_Set_Y(y_address-64);}
LCD_Command_WriteByte(font6x8[c][line]);
y_address++;
}
}
if(y>=64)
{
INT8U line,y_address=y-64;
c-=32;
Set_CS1(0);Set_CS2(1);
LCD_Command_Set_X(x);
LCD_Command_Set_Y(y-64);
for (line=0; line<6; line++)
{
if(y_address<64){ LCD_Command_Set_Y(y_address);
LCD_Command_WriteByte(font6x8[c][line]);
}
y_address++;
}
}
}
}
void LCD_write_english_string(INT8U x,INT8U y,INT8U n,char *s)
{
INT8U i;
for(i=0;i<n;i++)
{
LCD_Write_Char(x,y+i*6,*s);
s++;
}
}
/******************************/
void LCD_Move_string(INT8U x,INT8U y,INT8U n,INT8U speed,char *s)
{
int i;
do{LCD_Clear_Line(x,x);
for(i=y;i>=0;i--)
{LCD_write_english_string(x,i,n,s);
delay_nms(speed);
LCD_Clear_Line(x,x);
}
}while(1);
}
void LCD_Write_any_char(INT8U x,INT8U y,char c)
{int i,rest,x_address,y_address;
if(x>56)x=56;
if(y<128)
{
INT8U pare1=0XFF,pare2=0X00;
c-=32;
rest=x%8;
for(i=0;i<rest;i++)
{
pare1&=~(1<<i);
pare2|=(1<<i);
}
y_address=y;
for(i=0;i<6;i++)
{
if(y_address<64)
{
Set_CS1(1);
Set_CS2(0);
x_address=x>>3;
LCD_Command_Set_X(x_address);
LCD_Command_Set_Y(y_address);
LCD_Command_WriteByte(pare1&font6x8[c]<<rest);
x_address++;
LCD_Command_Set_X(x_address);
LCD_Command_Set_Y(y_address);
LCD_Command_WriteByte(pare2&font6x8[c]>>(8-rest));
y_address++;
}
else
{
Set_CS1(0);
Set_CS2(1);
y_address-=64;
if(y_address<64)
{
x_address=x>>3;
LCD_Command_Set_X(x_address);
LCD_Command_Set_Y(y_address);
LCD_Command_WriteByte(pare1&font6x8[c]<<rest);
x_address++;
LCD_Command_Set_X(x_address);
LCD_Command_Set_Y(y_address);
LCD_Command_WriteByte(pare2&font6x8[c]>>(8-rest));
}
y_address+=64;
y_address++;
}
}
}
}
void LCD_write_any_english_string(INT8U x,INT8U y,INT8U n,char *s)
{
INT8U i;
if(x>56)x=56;
for(i=0;i<n;i++)
{
LCD_Write_any_char(x,y+i*6,*s);
s++;
}
}
void LCD_Move_any_string(INT8U x,INT8U y,INT8U n,INT8U speed,char *s)
{
int i;
if(x>56)x=56;
do{LCD_Clear_Line(0,7);
for(i=y;i>=0;i--)
{LCD_write_any_english_string(x,i,n,s);
delay_nms(speed);
LCD_Clear_Line(x>>3,(x>>3)+1);
}
}while(1);
}
void LCD_Moveup_any_string(INT8U x,INT8U y,INT8U n,INT8U speed,char *s)
{
int i;
if(x>56)x=56;
do{LCD_Clear_Line(0,7);
for(i=x;i>=0;i--)
{LCD_write_any_english_string(i,y,n,s);
delay_nms(speed);
LCD_Clear_Line(0,7);
}
}while(1);
}
void LCD_Movemx_any_string(INT8U x,INT8U y,INT8U n,INT8U speed,char *s)
{
int min,i;
if(x>56)x=56;
if(x<=y)min=x;
else min=y;
do{LCD_Clear_Line(0,7);
for(i=0;i<=min;i++)
{LCD_write_any_english_string(x-i,y-i,n,s);
delay_nms(speed);
LCD_Clear_Line(0,7);
}
}while(1);
}
void LCD_protect(INT8U x,INT8U y,INT8U n,INT8U speed,char *s)
{
int i=-1,j=-1;
if(x>56)x=56;
do{LCD_Clear_Line(0,7);
LCD_write_any_english_string(x+=i,y+=j,n,s);
delay_nms(speed);
if(x==0)i=1;
if(x==56)i=-1;
if(y==0)j=1;
if((y+6*n)==127)j=-1;
}while(1);
}
void Write_word(INT8U x,INT8U y,const char *p)
{
int i,j,m,x_address=x,y_address=y;
for(i=0;i<2;i++)
{y_address=y;
for(j=0;j<12;j++)
{
for(m=0;m<8;m++)
{
if((*p)&(1<<m))LCD_Write_Dot(x_address+m,y_address);
}
p++;y_address++;
}
x_address+=8;
}
}
void Write_word_string(INT8U x,INT8U y,INT8U n,const char (*p)[24])
{
int i;
for(i=0;i<n;i++)
{
Write_word(x,y+12*i,*(p+i));
}
}
void move_string(INT8U x,INT8U y,INT8U n,INT8U speed,const char (*p)[24])
{
int i;
do{LCD_Clear_Line(x>>3,(x>>3)+1);
for(i=y;i>=0;i--)
{Write_word_string(x,i,n,p);
delay_nms(speed);
LCD_Clear_Line(x>>3,(x>>3)+1);
}
}while(1);
}
void move_word(INT8U x,INT8U y,INT8U speed,const char *p)
{
int i;
do{LCD_Clear_Line(x>>3,(x>>3)+1);
for(i=y;i>=0;i--)
{Write_word(x,i,p);
delay_nms(speed);
LCD_Clear_Line(x>>3,(x>>3)+1);
}
}while(1);
}
void Moveup_word_string(INT8U x,INT8U y,INT8U n,INT8U speed,const char (*p)[24])
{
int i;
if(x>56)x=50;
do{LCD_Clear_Line(0,7);
for(i=x;i>=0;i--)
{Write_word_string(i,y,n,p);
delay_nms(speed);
LCD_Clear_Line(0,7);
}
}while(1);
}
void LCD_protect(INT8U x,INT8U y,INT8U n,INT8U speed,const char (*p)[24])
{
int i=-1,j=-1;
if(x>56)x=50;
do{LCD_Clear_Line(0,7);
Write_word_string(x+=i,y+=j,n,p);
delay_nms(speed);
if(x==0)i=1;
if(x==50)i=-1;
if(y==0)j=1;
if((y+12*n)==127)j=-1;
}while(1);
}
还有画表格图形函数续传。。。
版权声明:本文为博主原创文章,未经博主允许不得转载。 |
|