void spi_init(void)
{
uint8 data;
DDRB |= (1<<MOSI)|(1<<SCK)|(1<<SS)&(~(1<<MISO)); //设置MOSI,SCK,SS为输出,MISO为输入
PORTB |= (1<<MISO)|(1<<SS); //使能MISO上拉电阻有效
SPCR = (1<<SPE)|(1<<MSTR); //SPI使能,MSB,主机,SPI Mode 0SCK=Fosc/4
SPSR = 0x00;
data = SPSR;
data = SPDR;
}
void spi_send_byte(char data)
{
SPDR = data;
while(!(SPSR & (1<<SPIF))); //等待数据传输结束
}
char spi_receive_byte(void)
{
char data;
SPDR = 0xff;
while(!(SPSR & (1<<SPIF))); //等待数据接收结束
data = SPDR;
return data;
}
void HighSpeed_Read_GT21L(uint32 address,char *buf,uint32 length)
{
SS_L();
spi_send_byte(0x0B); //0x0b快速读取方式
spi_send_byte(((address&0xFFFFFF)>>16)); //3个字节的地址
spi_send_byte(((address&0xFFFF)>>8));
spi_send_byte(address&0xFF);
spi_send_byte(0xFF); //dummy字节
while(length--)
{
*(buf++)=spi_receive_byte();
}
SS_H();
}
void Read_Font_GT21L16W(uint8 font_type, uint8 hzzf_h, uint8 hzzf_l, char *dot )
{
uint8 temp[2];
// uint8 hzzf_h,hzzf_l;
uint16 index_addr;
uint32 addr;
// hzzf_h = (uint8)((srcCode)>>8);
// hzzf_l = (uint8)((srcCode)&0xff);
uint16 srcCode;
srcCode = (uint16)hzzf_h<<8+hzzf_l;
switch(font_type)
{
case GB12345:
if(((hzzf_h>=0xa1)&&(hzzf_h<=0xa9))&&(hzzf_l>=0xa1))
addr = ((((uint32)hzzf_h)-0xa1)*94 + (((uint32)hzzf_l)-0xa1))*32 + ((uint32)BaseAdd);
else if(((hzzf_h>=0xb0)&&(hzzf_h<=0xf9))&&(hzzf_l>=0xa1))
addr = ((((uint32)hzzf_h)-0xb0)*94 + (((uint32)hzzf_l)-0xa1)+1038)*32 + ((uint32)BaseAdd);
else
return;
HighSpeed_Read_GT21L(addr ,dot, 32);
break;
case BIG5:
if((hzzf_h>=0xa1)&&((hzzf_l>=0x40)&&(hzzf_l<=0x7e)))
{
index_addr = (hzzf_h-0xa1)*157+(hzzf_l-0x40);
}
else if((hzzf_h>=0xa1)&&((hzzf_l>=0xa1)&&(hzzf_l<=0xfe)))
{
index_addr = (hzzf_h-0xa1)*157+(hzzf_l-0xa1)+63;
}
else
return;
addr = Big5Table+(index_addr*2);
HighSpeed_Read_GT21L(addr,temp,2);
addr = BaseAdd+(((uint32)temp[0]<<8)+temp[1])*32;
HighSpeed_Read_GT21L(addr,dot,32);
break;
case JIS0208:
if(((hzzf_h>=1)&&(hzzf_h<=89))&&((hzzf_l>=1)&&(hzzf_l<=94)))
{
index_addr = (hzzf_h-1)*94+(hzzf_l-1);
// index_addr = ((((hzzf_h&0xf0)>>4)*10+(hzzf_h&0x0f))-1)*94+(((hzzf_l&0xf0)>>4)*10+(hzzf_l&0x0f))-1;
}
else
return;
addr = JISTable+(index_addr*2);
HighSpeed_Read_GT21L(addr,temp,2);
addr = BaseAdd+(((uint32)temp[0]<<8)+temp[1])*32;
HighSpeed_Read_GT21L(addr,dot,32);
break;
case ASCLL:
if((hzzf_h==0x00)&&((hzzf_l>=0x20)&&(hzzf_l<=0x7e)))
addr = (hzzf_l-0x20)*16+ASCLL16;
else
return;
HighSpeed_Read_GT21L(addr,dot,16);
break;
case LDUNICODE:
if((srcCode>=0x00a0)&&(srcCode<=0x0217))
addr = (srcCode-0x00a0)*16+LDTable;
else
return;
HighSpeed_Read_GT21L(addr,dot,16);
break;
case XLUNICODE:
if((srcCode>=0x0370)&&(srcCode<=0x03cf))
addr = (srcCode-0x00a0)*16+XLTable;
else
return;
HighSpeed_Read_GT21L(addr,dot,16);
break;
case JLEUNICODE:
if((srcCode>=0x0400)&&(srcCode<=0x04f9))
addr = (srcCode-0x0400)*16+JLETable;
else
return;
HighSpeed_Read_GT21L(addr,dot,16);
break;
default:
break;
}
}