单片机控制打印机例程(北京荣达-串口).doc
/*/ 新荣达电子打印机测试程序/ 51单片机,晶振11.0592/时间:2009-7-10/用与测试串行接口打印机/常用打印机指令以函数行式体现/可根据需要自行调用单一函数*/#include<reg52.h>/51头文件#include<string.h> /字串处理函数头文件 #include<math.h>#define uchar unsigned charsbit BUSY=P34; /并口打印机的BUSY,串口打印机的CTS /*单字节打印函数*/void print(unsigned char ch)while (BUSY=1) /busy为1时,等待,如数据量小,可不判BUSY ; SBUF= ch; /busy=0,将数据送到P1口while(TI=0)/当TI为0时,表示数据未发送完成,一直等待,直到为1;TI=0;/将TI清0/*串口初始化*/void _init(void) TH1=0XFD; /波特率值装入计数器,9600BPSTMOD=0X20; /串口设置,定时器1,设为方式2,8位自动重载,用于产生波特率SCON=0X50; /串口发送方式1,8位数据位,1位停止位TR1=1;/开启定时器1 /*字符打印函数*/void print_chs(uchar ch)uchar i;for(i=0;i<strlen(ch);i+)print(chi);/*汉字打印例程*/void hanzitest(void)uchar i;print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("汉字命令测试:n");for(i=0;i<5;i+) /搋环打印5次print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print(0xd0);print(0xc2);/打印数据"新"print(0x1b);print(0x38);print(0x04);/调用12*12点阵汉字,24点阵为24*24print(0xc8);print(0xd9);print(0xb4);print(0xef);/打印数据“荣达”print(0x0d);/回车/*行间距例程*/void hangjianju(void)uchar i;print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("行间距测试:n");for(i=0;i<11;i=i+2) /循环打印print(0x1b);print(0x31);print(i); /ESC 1,设置行间距print_chs("LINE SPACING");print(0x0a); /打印字符串和换行/*水平造表例程*/void shuipingzhaobiao(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("水平造表测试:n");print_chs("23456");/ '标尺print(0x0d);print(0x1b);print(0x44);print(2);print(9);print(14);print(0);/ESC Dprint(9); /HT命令print_chs("HT1"); /打印字符串print(9); /HT命令print_chs("HT2"); /打印字符串print(9); /HT命令print_chs("HT3"); /打印字符串print(0x0d);/*打印空格或换行例程*/void konggehuanhang(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("换行或空格测试:n");print_chs("TE"); /打印字符串“TE”print(0x1b);print(0x66);print(0);print(0x06);/空六格print_chs("ST"); /打印字符串“ST”print(0x0d); /回车print_chs("TE"); /打印字符串“TE”print(0x1b);print(0x66);print(0x01);print(0x06);/换6行print_chs("ST"); /打印字符串“ST”print(0x0d); /回车/*右限例程*/void youxian(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("右限测试:n");print_chs("9012");/标尺print(0x1b);print(0x51);print(0x06); /ESC Q 命令,右限为6print_chs("9012");/打印字串print(0x0d); /回车/*左限例程*/void zhuoxian(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("左限测试:n");print_chs("9012");/标尺print(0x1b);print(0x6c);print(0x06); /ESC I 命令,左限为6print_chs("9012");/打印字串print(0x0d); /回车/*放大指令例程*/void fangda(void)uchar i;print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("放大测试:n");for(i=1;i<4;i+) /从1到3倍print (0x1b);print(0x55);print(i); /横向放大print_chs("RD"); /打印字符print(0x0d); /回车命令print(0x1b);print(0x40); /初始化打印机for(i=1;i<4;i+) /从1到3倍print (0x1b);print(0x56);print(i); /纵放命令print_chs("RD"); /打印字符print(0x0d); /回车print(0x1b);print(0x40); /初始化打印机for(i=1;i<4;i+) /从1到3倍 print (0x1b);print(0x57);print(i); /横纵向同时放大print_chs("RD"); /打印字符print(0x0d); /回车/*划线指令例程*/void huaxian(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("划线测试:n");print(0x1b);print(0x57);print(2);/放大两倍打印,便于观察划线print_chs("RD"); /打印字串print(0x1b);print(0x2d);print(1);/允许下划线打印print_chs("UPT"); /UPT打印下划线print(0x1b);print(0x2d);print(0);/禁止下划线打印print_chs("RD"); /打印字串print(0x0d); /回车print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x57);print(2);/放大两倍打印,便于观察划线print_chs("RD"); /打印字串print(0x1b);print(0x2b);print(1);/允许上打印线打印print_chs("UPT"); /UPT上打印上划线print(0x1b);print(0x2b);print(0);/禁止上划线 print_chs("RD");/打印字串print(0x0d);/打印回车/*上下标指令例程*/void shangxiabiao(void)print(0x1b);print(0x40); /初始化打印机print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("上下标测试:n");print(0x1b);print(0x57);print(0x02); /放大两倍print('M'); /打印字符'M'print(0x1b);print(0x57);print(0x01); /原倍print(0x1c);print(0x72);print(0); /上标命令print('2'); /打印字符'2'print(0x0d); /回车print(0x1b);print(0x57);print(0x01); /原倍print(0x1b);print(0x57);print(0x02); /放大两倍print('M'); /打印字符'M'print(0x1c);print(0x72);print(1); /上标命令print(0x1b);print(0x57);print(0x01); /原倍print('2'); /打印字符'2'print(0x0d); /回车/*横纵向指令例程*/void hengzhong(void)print(0x1b);print(0x40);print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("横纵向测试:n");print(0x1c);print(0x4a); /设置纵向打印print_chs("ABCDEFGHI"); /打印字符print(0x0d); /回车 print(0x1b);print(0x40);print(0x1C);print(0x4B); /设置横向打印print_chs("ABCDEFGHI"); /打印字符print(0x0d); /回车 /*自定义指令例程*/void zidingyi(void)print(0x1b);print(0x40); /初始化指令print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("自定义字符测试:n");print(0x1b);print(0x36); /如是24点阵机器,在自定义字符前面要加一个1B 36 指令才行print(0x1b);print(0x57);print(2); /横向纵向放大2倍print(0x1b);print(0x26);print(65); /ESC & 命令print(0x02);print(0x7C);print(0x40); /字模数据print(0xC0);print(0x40);print(0x00); /字模数据print(0x1b);print(0x25);print(65);print(65);print(0); /ESC %print(65); /打印定义字符print(0x1b);print(0x3a); /ESC:命令print(65); /复符集中的字符print(0x0d);/*图形指令例程*/void tuxing(void)uchar code tch =0x7c,0x44,0x44,0xff,0x44,0x44,0x7c,0x00,0x41,0x62,0x54,0xc8,0x54,0x62,0x41;uchar i;print(0x1b);print(0x40);/初始化指令print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("图形测试:n");/print(0x1b);print(0x57);print(2);/横向纵向放大2倍print(0x1b);print(0x4b);print(15);print(0); /ESC K命令for(i=0;i<15;i+)/输入15个字节数据print(tchi);print(0x0d);/输入回车/*曲线II指令例程*/仅适用于热敏打印机void quxian_2(void) float X;unsigned int Y,YY;/Y1=50+40*abs(-0.01*X)*sin(X/10)/Y2=50-40*abs(-0.01*X)*sin(X/10)/Y3=50/Y4=50+40*abs(-0.1*X)/Y5=50-40*abs(-0.01*X)/你可以用C语言编写如下的程序:/#include <math.h>/unsigned int Y,YY,X;print(0x1b);print(0x40);/初始化指令print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("曲线1测试:n");for(X=0;X<150;X+)/打印150点行 Y= 40*exp(-0.01*X); YY= Y*sin(X/10); print(0x1b);print(39);print(5);print(0); /ESC命令,M=5,0 print(50+YY);print(0);print(50-YY);print(0);print(50);print(0); print(50+Y);print(0);print(50-Y);print(0);print(13); /*曲线I指令例程*/仅适用于针式打印机void quxian_1(void) float X;unsigned int Y,YY;/Y1=50+40*abs(-0.01*X)*sin(X/10)/Y2=50-40*abs(-0.01*X)*sin(X/10)/Y3=50/Y4=50+40*abs(-0.1*X)/Y5=50-40*abs(-0.01*X)/你可以用C语言编写如下的程序:/#include <math.h>/unsigned int Y,YY,X;print(0x1b);print(0x40);/初始化指令print(0x1b);print(0x38);print(0x00); /调用16*16点阵汉字,24点阵为24*24print_chs("曲线2测试:n");for(X=0;X<150;X+)/打印150点行 Y= 40*exp(-0.01*X); YY= Y*sin(X/10); print(0x1b);print(39);print(5); /ESC命令,M=5 print(50+YY);print(50-YY);print(50); print(50+Y);print(50-Y);print(13); /*旋转指令例程*/void xuanzhuan(void)uchar i;print(0x1b);print(0x40);/初始化指令for(i=0;i<4;i+) /0,90,180,270旋转print(0x1c);print(0x49);print(i); /旋转指令print(0x1b);print(0x38);print(0);/汉字调出指令print_chs("旋转测试");/打印汉字print(0x0d); /回车/*反白指令例程*/void fanbai(void)uchar i;print(0x1b);print(0x40);/初始化指令for(i=0;i<2;i+) /i为1时,反白打印print(0x1b);print(0x69);print(i);print(0x1b);print(0x38);print(0);print_chs("反白测试");print(0x0d);/*反向指令例程*/void fanxiang(void)uchar i; print(0x1b);print(0x40);/初始化指令for(i=0;i<2;i+)/i为1时,禁止反向print(0x1b);print(0x63);print(i);print(0x1b);print(0x38);print(0);print_chs("反向测试");print(0x0d);/*条码指令例程*/只有部分型号的打印机支持此指令void tiaoma(void) print(0x1b);print(0x40);/初始化指令print(0x1d);print(0x72);print(0x01);/打印条码字符print(0x1d);print(0x68);print(0x60);/设置条码高度为96点print(0x1d);print(0x6B);print(0x45);print(8);/打印长度为8的CODE39码print(0x2a);print(0x31);print(0x32);print(0x34);print(0x35);print(0x37);print(0x36);print(0x2a); /CODE39码数据print(0x0d);/回车print(0x1d);print(0x6B);print(3); /打印EAN13码print(0x32);print(0x37);print(0x30);print(0x38);print(0x35);print(0x38);print(0x35);print(0x30); /EAN13码数据print(0x30);print(0x39);print(0x32);print(0x34);print(0x38);print(0x00);print(0x0d);/回车Main()_init();hanzitest();hangjianju();shuipingzhaobiao();konggehuanhang();youxian();fangda();huaxian();shangxiabiao();hengzhong();zhuoxian();zidingyi();tuxing();quxian_2();/如用针打打印机将此函数去掉quxian_1();/如用热敏打印机将此函数去掉xuanzhuan();fanbai();fanxiang();tiaoma();print(0x0d); /回车print(0x0d); /回车print(0x0d); /回车while(1); /死循环9 / 9