⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printer.c

📁 51单片机驱动针式打印头的程序
💻 C
字号:
//#include <x25045.h>

sbit Pfeedback = 0xa0;				/* 打印机脉冲信号	*/
sbit Preset    = 0xa1;				/* 打印机复位信号	*/
sbit Ppaper    = 0xa2;				/* 打印纸测试		*/

sbit Print4 = 0x87;				/* 打印机一针		*/
sbit Print3 = 0x86;				/* 打印机二针		*/
sbit Print2 = 0x85;				/* 打印机三针		*/
sbit Print1 = 0x84;				/* 打印机四针		*/
sbit Motor  = 0x83;				/* 打印机马达控制	*/

char code CharLib[]={0x3e,0x45,0x49,0x51,0x3e,0,/* 0	*/
                     0x00,0x21,0x7f,0x01,0x00,0,/* 1	*/
                     0x21,0x43,0x45,0x49,0x31,0,/* 2	*/
                     0x42,0x41,0x51,0x69,0x46,0,/* 3	*/
                     0x0c,0x14,0x24,0x7f,0x04,0,/* 4	*/
                     0x72,0x51,0x51,0x51,0x4e,0,/* 5	*/
                     0x1e,0x29,0x49,0x49,0x06,0,/* 6	*/
                     0x40,0x47,0x48,0x50,0x60,0,/* 7	*/
                     0x36,0x49,0x49,0x49,0x36,0,/* 8	*/
                     0x30,0x49,0x49,0x4a,0x3c,0,/* 9	*/
                     0x00,0x00,0x00,0x00,0x00,0,/* 空格	*/
                     0x3f,0x44,0x44,0x44,0x3f,0,/* A	*/
                     0x7f,0x49,0x49,0x49,0x36,0,/* B	*/
                     0x3e,0x41,0x41,0x41,0x22,0,/* C	*/
                     0x7f,0x41,0x41,0x22,0x1c,0,/* D	*/
                     0x7f,0x49,0x49,0x49,0x41,0,/* E	*/
                     0x7f,0x48,0x48,0x48,0x40,0,/* F	*/
                     0x3e,0x41,0x49,0x49,0x2f,0,/* G	*/
                     0x7f,0x08,0x08,0x08,0x7f,0,/* H	*/
                     0x00,0x41,0x7f,0x41,0x00,0,/* I	*/
                     0x02,0x01,0x41,0x7e,0x40,0,/* J	*/
                     0x7f,0x08,0x14,0x22,0x41,0,/* K	*/
                     0x7f,0x01,0x01,0x01,0x01,0,/* L	*/
                     0x7f,0x20,0x18,0x20,0x7f,0,/* M	*/
                     0x7f,0x10,0x08,0x04,0x7f,0,/* N	*/
                     0x3e,0x41,0x41,0x41,0x3e,0,/* O	*/
                     0x7f,0x48,0x48,0x48,0x30,0,/* P	*/
                     0x3e,0x41,0x45,0x42,0x3d,0,/* Q	*/
                     0x7f,0x48,0x4c,0x4a,0x31,0,/* R	*/
                     0x31,0x49,0x49,0x49,0x46,0,/* S	*/
                     0x40,0x40,0x7f,0x40,0x40,0,/* T	*/
                     0x7e,0x01,0x01,0x01,0x7e,0,/* U	*/
                     0x7c,0x02,0x01,0x02,0x7c,0,/* V	*/
                     0x7e,0x01,0x0e,0x01,0x7e,0,/* W	*/
                     0x63,0x14,0x08,0x14,0x63,0,/* X	*/
                     0x70,0x08,0x07,0x08,0x70,0,/* Y	*/
                     0x43,0x45,0x49,0x51,0x61,0,/* Z	*/
                     0x12,0x2e,0x6a,0x3f,0x2a,0,/* 年	*/
                     0x01,0x7e,0x54,0x55,0x7f,0,/* 月	*/
                     0x00,0x7f,0x49,0x49,0x7f,0,/* 日	*/
                     0x11,0x5e,0x50,0x5f,0x11,0,/* 元	*/
                     0x13,0x65,0x49,0x65,0x13,0,/* 公	*/
                     0x7d,0x57,0x7f,0x57,0x7d,0,/* 里	*/
                     0x00,0x36,0x36,0x00,0x00,0,/* :	*/
                     0x00,0x03,0x03,0x00,0x00,0,/* .	*/
                     0x08,0x08,0x08,0x08,0x08,0 /* -	*/
};

void InitialPrinter(void)			/* 打印机初始化		*/
{
  Print4=Print3=Print2=Print1=Motor=1;		/* 马达和打印针断电	*/
}

void PrintFree(char Number)			/* 打印空行		*/
{
  register i;
  while(Number > 0){				/* 空行未结束继续	*/
    Motor = 0;					/* 启动马达		*/
    Preset = 1;					/* 测试复位信号		*/
    while(Preset){				/* 一行一个复位信号	*/
      #if WatchdogEn==1
      Watchdog();
      #else
      ;
      #endif
    }
    while(!Preset){
      #if WatchdogEn==1
      Watchdog();
      #else
      ;
      #endif
    }
    Motor = 1;					/* 关闭马达		*/
    Number--;
    for(i=120;i>0;i--);
  }  
}

void PaperIn(void)				/* 开始进纸		*/
{
  Motor = 0;
  #if WatchdogEn==1
  Watchdog();
  #endif
}

bit PrintTest(void)				/* 测试打印机		*/
{
  uint i = 2000;
  Motor = 0;
  while(Pfeedback){
    #if WatchdogEn==1
    Watchdog();
    #endif
    if(--i == 0) goto Perror;
  }
  while(!Pfeedback){
    #if WatchdogEn==1
    Watchdog();
    #endif
    if(--i == 0) goto Perror;
  }
  Motor = 1;
  return(TRUE);
  Perror:
  Motor = 1;
  return(FALSE);
}

void Printer(BYTE idata *Index)			/* M150打印机控制	*/
{
  char i,j,k,l;
  bit pin=1;
bit sample = 1;
  Motor = 0;					/* 启动马达		*/
  for(i=0;i<8;i++){				/* 一个字符八行		*/
    Watchdog();
    Preset = 1;					/* 测试复位信号		*/
    while(Preset){				/* 信号上跳变为开始	*/
      if (WatchdogEn==1)
      {Watchdog();}
      
    }
    while(!Preset){
      #if WatchdogEn==1
      Watchdog();
      #else
      ;
      #endif
    }
    for(j=0;j<4;j++){				/* 一根针控制四字符	*/
      for(k=0;k<6;k++){				/* 一个字符为六列	*/
        for(l=0;l<4;l++){			/* 四根针轮流控制	*/
          Pfeedback = 1;			/* 测试针控信号		*/
          if(sample){				/* 上下跳变轮流		*/
            while(Pfeedback){
              #if WatchdogEn==1
              Watchdog();
              #else
              ;
              #endif
            }
          }		
          else{
            while(!Pfeedback){
              #if WatchdogEn==1
              Watchdog();
              #else
              ;
              #endif
            }
          }		
          sample = !sample;			/* 测试位取反		*/
          pin = (bit)((CharLib[(WORD)((WORD)(*(Index + l * 4 + j)) * 6 + k)] << i) & 0x80);
						/* 取打印点数据		*/
          Print4 = Print3 = Print2 = Print1 = 1;/* 打印针断电		*/
          switch(l){
            case 0: Print4 = !pin; break;	/* 打印针通电		*/
            case 1: Print3 = !pin; break;
            case 2: Print2 = !pin; break;
            case 3: Print1 = !pin; break;
          }
        }
      }
    }
  }
  InitialPrinter();				/* 打印机初始化		*/
  PrintFree(10);
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -