Unicode转为GBK的C语⾔程序#include
#define uint unsigned int
#define uchar unsigned char
uint GBcode=0;
xdata uchar a_dot[192];
uchar UCSadd1,UCSadd2,UCSadd3;
uchar ADH,ADL,ADLL;
uchar i,num,k,temp1;
bit done=0;
/////SPI引脚连接//////
sbit CS=P1^7;
sbit MOSI=P0^2;
sbit MISO=P0^1;
sbit SCK=P0^0;
/////LED引脚连接//////
sbit CLK=P1^0; // 时钟信号
sbit STB=P1^1; // 锁存信号,上升沿
sbit R=P1^2; //数据输⼊端⼝
sbit OE=P1^3; // 使能信号,⾼电平有效
void Oscillator_Init();//f410初始化
void SPI_Init(); //SPI初始化
void int_T2(); //T2初始化
void delay();
void save(uchar x); //存储点阵代码
void display(); //LED显⽰
void WriteByte(uchar dat); //写⼀个字节的数据
void LED(uint GB_code);
void UnicodeToGB2312(uchar jj,uchar *ss);
void main()
{
uchar q;
uchar Unicode[]={0x4f,0x60};
CS=1;
Oscillator_Init(); //硬件初始
SPI_Init(); //SPI初始化
for(q=0;q<192;q++) a_dot[q]=0;
UnicodeToGB2312(0,Unicode);//Unicode转GB2312
LED(GBcode);
while(1);
}
void Oscillator_Init()
{
PCA0MD&=~0x40; ///关看门狗
PCA0MD=0x00;
OSCICN=0x87;///8分频
P0MDOUT=0x1D; ///MOSI CLK设置为推挽输出; MISO设为漏极输出P1MDOUT =0x7f;//p1设置为开漏
P2MDOUT =0xff;
XBR0=0x03;////开 SPI 端⼝连接
XBR1=0x40; /////开交叉开关
// IP |= 0x10; // Make UART high priority
// IE=0xb0; // Enable UART0 interrupts
}
void SPI_Init()
{
SPI0CFG=0x77; //上升沿时钟,⼯作在主⽅式;
SPI0CKR=0xdf; //SPI0时钟频率;
SPI0CN=0x03; //SPI使能
}
////////////T2初始化/////////
void int_T2()
{ CKCON=0x00; //T0频率为f/12
// EA=1;
TMR2RLH=0xf4;
TMR2RLL=0xac;
// ET2=1;
}
/
********************led写数据******************************/ void WriteByte(uchar dat) //写⼀个字节的数据
{
uchar i11;
for(i11=0;i11<8;i11++) //循环8次把编码传给锁存器
{
dat=dat<<1; //移⼀位,取出该字节的最低位
R=CY; //将该字节的最低位传给R
CLK=0; //将数据移⼊595,上升沿
CLK=1;
}
}
/
*********************LED扫描数据**************************/ void display()
{
uchar temp2,a;
temp2=i;
for(a=0;a<4;a++ )
{
WriteByte(a_dot[num+i+k]); //送出四个字节,给四个595 i-=8;
}
i=temp2+1;
for(a=0;a<4;a++ )
{
WriteByte(a_dot[num+i+k]); //送出四个字节,给应四个595 i-=8;
}
}
void delay()
{
unsigned int Z0,Z1;
for(Z0=0;Z0<2;Z0++)
{
for (Z1=0;Z1<3;Z1++);
}
}
void save(uchar x)
{
uchar i0,j0,f0;
for(j0=0;j0<32;j0++)
{
for(i0=0;i0<8;i0++)
{
f0=f0<<1;
SCK = 0;
SCK = 1;
if(MISO) f0++;
}
a_dot[j0+x*64]=f0;
}
}
void UnicodeToGB2312(uchar jj,uchar *ss)
{
uint code1;
unsigned long ptr=0;
uchar result=0,result1=0;
uchar GB2312Code[2];
unsigned long UToGb2312Buff =0x67d70;
uint h;
uchar s0,s1,dat0;
code1=(uint)(ss[jj]*256+ss[jj+1]);
if(code1<0x20) result=1;
else if(code1<0xa0) result1=1;
else if(code1<=0xf7) h=code1-160;
else if(code1<0x2c7) result=1;
else if(code1<=0x2c9) h=code1-160-463;
else if(code1<0x2010) result=1;
else if(code1<=0x2312) h=code1-160-463-7494;
else if(code1<0x2460) result=1;
else if(code1<=0x2642) h=code1-160-463-7494-333;unicode字符转中文
else if(code1<0x3000) result=1;
else if(code1<=0x3017) h=code1-160-463-7494-333-2493;
else if(code1<0x3220) result=1;
else if(code1<=0x3229) h=code1-160-463-7494-333-2493-520;
else if(code1<0x4e00) result=1;
else if(code1<=0x9b54) h=code1-160-463-7494-333-2493-520-7126;
else if(code1<0x9c7c) result=1;
else if(code1<=0x9ce2) h=code1-160-463-7494-333-2493-520-7126-295;
else if(code1<0x9e1f) result=1;
else if(code1<=0x9fa0) h=code1-160-463-7494-333-2493-520-7126-295-316;
else if(code1<0xe76c) result=1;
else if(code1<=0xe774) h=code1-160-463-7494-333-2493-520-7126-295-316-18379;
else if(code1<0xff00) result=1;
else if(code1<=0xff5f) h=code1-160-463-7494-333-2493-520-7126-295-316-18379-6027; else if(code1<0xffe0) result=1;
else if(code1<=0xffe5) h=code1-160-463-7494-333-2493-520-7126-295-316-18379-6027-128; else result=1;
if(result1==0)
{
if(result==0)
{
ptr = UToGb2312Buff + (h<<1);
}
{
ptr=0x0000a1a1;
}
UCSadd1=ptr/65536;
UCSadd2=ptr/256;
UCSadd3=ptr%256;
CS=1;
CS=0;
SPI0DAT=0x03;
while (!SPIF);
SPIF=0;
SPI0DAT=UCSadd1;
while (!SPIF);
SPIF=0;
SPI0DAT=UCSadd2;
while (!SPIF);
SPIF=0;
SPI0DAT=UCSadd3;
while (!SPIF);
SPIF=0;
XBR0=0x01;////关 SPI 端⼝连接
for(s0=0;s0<2;s0++)
{
for(s1=0;s1<8;s1++)
{
dat0=dat0<<1;
SCK = 0;
SCK = 1;
if(MISO) dat0++;
}
GB2312Code[s0]=dat0;
}
CS=1;
XBR0=0x03;////开 SPI 端⼝连接
GBcode=(uint)(GB2312Code[0]*256+GB2312Code[1]);
}
else
GBcode=code1+0xa380;
// return (GBcode);
}
void LED(uint GB_code)
{
unsigned long Address=0;
uchar MSB,LSB;
MSB=GB_code/256;
LSB=GB_code%256;
if(MSB >=0xA1 && MSB <= 0xA3 && LSB >=0xA1)
Address =(unsigned long)( (MSB - 0xA1) * 94 + (LSB - 0xA1))*32; else if(MSB >=0xA4 && MSB <= 0xa8 && LSB >=0xA1)
Address = 0;
else if(MSB ==0xA9 && LSB >=0xA1)
Address =(unsigned long)(282 + (LSB - 0xA1 ))*32;
else if(MSB >=0xB0 && MSB <= 0xF7 && LSB >=0xA1)
Address =(unsigned long)((MSB - 0xB0) * 94 + (LSB - 0xA1)+ 846)*32; ADH=Address/0x10000;
ADL=Address%0x10000/256;
ADLL=Address%256;
delay(); //////延时确保正确写⼊数据
CS=0;
SPI0DAT=0x03;
while (!SPIF);
SPIF=0;
SPI0DAT=ADH;
while (!SPIF);
SPIF=0;
SPI0DAT=ADL;
while (!SPIF);
SPIF=0;
SPI0DAT=ADLL;
while (!SPIF);
SPIF=0;
XBR0=0x01;////关 SPI 端⼝连接save(0);
CS=1;
XBR0=0x03;////关 SPI 端⼝连接}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。