找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10493|回复: 20
打印 上一主题 下一主题
收起左侧

基于stm32的热敏打印机程序(demo)版,目前打印逻辑有些问题

  [复制链接]
跳转到指定楼层
楼主
这个程序我是用stm32cubemx生成,所有初始化都是按照ST官方样式编写

单片机源程序如下:
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "global.h"
  3. /* USER CODE BEGIN Includes */

  4. /* USER CODE END Includes */

  5. /* Private variables ---------------------------------------------------------*/
  6. union{
  7.          uint8_t ACC;
  8.          struct{
  9.           uint8_t h0 :1;
  10.           uint8_t h1 :1;
  11.           uint8_t h2 :1;
  12.           uint8_t h3 :1;
  13.     uint8_t h4 :1;
  14.     uint8_t h5 :1;
  15.     uint8_t h6 :1;
  16.     uint8_t h7 :1;        
  17.         }bits;        
  18. }Bytes;
  19. uint8_t val=0;
  20. void read(unsigned char hz)
  21. {
  22.         Bytes.ACC=hz;
  23.         CLK_PutVal(GPIO_PIN_RESET);
  24.         CLK_PutVal(GPIO_PIN_SET);
  25.         
  26.         DI_PutVal((GPIO_PinState)Bytes.bits.h7);
  27.         CLK_PutVal(GPIO_PIN_RESET);
  28.         CLK_PutVal(GPIO_PIN_SET);
  29.         
  30.         DI_PutVal((GPIO_PinState)Bytes.bits.h6);
  31.         CLK_PutVal(GPIO_PIN_RESET);
  32.         CLK_PutVal(GPIO_PIN_SET);
  33.         
  34.         DI_PutVal((GPIO_PinState)Bytes.bits.h5);
  35.         CLK_PutVal(GPIO_PIN_RESET);
  36.         CLK_PutVal(GPIO_PIN_SET);
  37.         
  38.         DI_PutVal((GPIO_PinState)Bytes.bits.h4);
  39.         CLK_PutVal(GPIO_PIN_RESET);
  40.         CLK_PutVal(GPIO_PIN_SET);
  41.         
  42.           DI_PutVal((GPIO_PinState)Bytes.bits.h3);
  43.         CLK_PutVal(GPIO_PIN_RESET);
  44.         CLK_PutVal(GPIO_PIN_SET);
  45.         
  46.         DI_PutVal((GPIO_PinState)Bytes.bits.h2);
  47.         CLK_PutVal(GPIO_PIN_RESET);
  48.         CLK_PutVal(GPIO_PIN_SET);
  49.         
  50.         DI_PutVal((GPIO_PinState)Bytes.bits.h1);
  51.         CLK_PutVal(GPIO_PIN_RESET);
  52.         CLK_PutVal(GPIO_PIN_SET);
  53.         DI_PutVal((GPIO_PinState)Bytes.bits.h0);
  54. }




  55. void printf24(u8 *p)
  56. {        
  57.   uint8_t i1=0;
  58.   uint8_t *pp;
  59.   uint8_t k,k2;
  60.   unsigned int k1=0;
  61.   char printhzflag;
  62.   unsigned char what,what1;
  63.   
  64.   char hh;
  65.   
  66.   pp=p;
  67.   while(1)
  68.   {
  69.     p++;
  70.     if(*p==0x0a) p++;               
  71.     pp=p;
  72.         
  73.     //缺纸/* ++@zengziwen modiffy@++ */
  74.     while((PHK_GetVal()==0))
  75.    {
  76.            return;
  77.    }
  78.     for(k=0;k<24;k++)
  79.     {
  80.                 //OneStep();
  81.         __MotorEnable();
  82.         OneStep();
  83.         __MotorEnable();
  84.         //runmotor(4);
  85.       p=pp;
  86.       //while(*p!=0x0d)
  87.       /* ++@zengziwen modiffy@++ */
  88.       while(*p=='\r')
  89.               {
  90.                       p+=1;
  91.               }
  92.       while(*p!='\r')
  93.       {
  94.         what1=what=*p;
  95.         if(what==0x20)
  96.         {
  97.           what=0;
  98.           printhzflag=0;
  99.         }
  100.         else
  101.         {
  102.           if(what==0x03)
  103.           {
  104.             if(*(p-1)==0x0a)
  105.             return;
  106.           }
  107.           //if((what>=0x61)&(what<0x80))
  108.           if((what>=0x61)&&(what<=0x7a)) /* a---->z */
  109.           {
  110.             printhzflag=0;
  111.             //what=what-74+9;
  112.             what=what-65;
  113.             goto print;
  114.           }
  115.           if(what1>=0x80)/* zh */
  116.           {
  117.             what=what-41;
  118.             printhzflag=1;
  119.             goto print;
  120.           }
  121.           if(what1==0x40)
  122.           {
  123.             what1=what1+45;
  124.             printhzflag=1;
  125.             goto print;
  126.           }
  127.           //if((what1>0x40)&(what1<=0x5a))
  128.            if((what1>=0x41)&&(what1<=0x5a)) /* A---->Z */
  129.           {
  130.             what=what-0x41;
  131.             printhzflag=2;
  132.             goto print;
  133.           }
  134.           //if((what1>=0x21)&(what1<=0x3f))
  135.           if((what1>=0x21)&&(what1<=0x3f))
  136.           {
  137.             printhzflag=0;
  138.             what=what-0x20;
  139.             goto print;
  140.           }
  141.         }
  142.                
  143.         print:
  144.                         
  145.         switch(printhzflag)
  146.         {
  147.           case 0:
  148.             for(i1=0;i1<2;i1++)
  149.             {
  150.               read(hzk16[what*24+k][i1]);
  151.               spe++;
  152.             }
  153.           break;
  154.           case 1:
  155.             what=what1-109;
  156.             for(i1=0;i1<3;i1++)
  157.             {
  158.               read(hzk24[what*24+k][i1]);
  159.               spe++;
  160.             }
  161.           break;
  162.           case 2:
  163.             for(i1=0;i1<2;i1++)
  164.             {
  165.             //hh = what;
  166.             //hh = A_Z[what*24+k][i1];
  167.               read(A_Z[what*24+k][i1]);
  168.               spe++;
  169.             }
  170.           break;
  171.         }
  172.         p++;
  173.       }
  174.          
  175.       for(i1=0;i1<48-spe;i1++)
  176.       {
  177.                       read(0);
  178.       }
  179.       
  180.       spe=0;
  181.       nLAT_PutVal(GPIO_PIN_RESET);
  182.         /* ++@zengziwen modiffy@++ */
  183.         delay_us(10);
  184.       nLAT_PutVal(GPIO_PIN_SET);
  185.       if(HEAT_GetVal(&hadc1)!=0)//太热
  186.       {
  187.         do{
  188.         STB1_PutVal(GPIO_PIN_RESET);
  189.         STB2_PutVal(GPIO_PIN_RESET);
  190.                
  191.          STB3_PutVal(GPIO_PIN_RESET);
  192.         STB4_PutVal(GPIO_PIN_RESET);
  193.          //STB5_PutVal(GPIO_PIN_RESET);
  194.         //STB6_PutVal(GPIO_PIN_RESET);
  195.         }while((HEAT_GetVal(&hadc1)==0));//
  196.       }
  197.       STB1_PutVal(GPIO_PIN_SET);
  198.       STB2_PutVal(GPIO_PIN_SET);

  199.           STB3_PutVal(GPIO_PIN_SET);
  200.         STB4_PutVal(GPIO_PIN_SET);
  201.          //STB5_PutVal(GPIO_PIN_SET);
  202.         //STB6_PutVal(GPIO_PIN_SET);
  203.       k1++;
  204.            
  205.         //OneStep();
  206.         //runmotor(4);
  207.         __MotorEnable();
  208.         OneStep();
  209.         __MotorEnable();
  210.       //delay_us(400);
  211.       //delay_us(800);
  212.       delay_us(1090);
  213.       k1++;
  214.       
  215.       nLAT_PutVal(GPIO_PIN_RESET);
  216.       STB1_PutVal(GPIO_PIN_RESET);
  217.       STB2_PutVal(GPIO_PIN_RESET);  

  218.           STB3_PutVal(GPIO_PIN_RESET);
  219.         STB4_PutVal(GPIO_PIN_RESET);
  220.          //STB5_PutVal(GPIO_PIN_RESET);
  221.         //STB6_PutVal(GPIO_PIN_RESET);
  222.     }
  223.     for(k2=0;k2<16;k2++)
  224.     {
  225.       
  226.         //OneStep();
  227.         //runmotor(4);
  228.         __MotorEnable();
  229.         OneStep();
  230.         __MotorEnable();
  231.       delay_us(400);
  232.       delay_us(800);
  233.       k1++;
  234.     }
  235.   }
  236. }

  237. /* USER CODE BEGIN PV */
  238. /* Private variables ---------------------------------------------------------*/

  239. /* USER CODE END PV */

  240. /* Private function prototypes -----------------------------------------------*/
  241. void SystemClock_Config(void);

  242. /* USER CODE BEGIN PFP */
  243. /* Private function prototypes -----------------------------------------------*/

  244. /* USER CODE END PFP */

  245. /* USER CODE BEGIN 0 */

  246. /* USER CODE END 0 */

  247. int main(void)
  248. {

  249.   /* USER CODE BEGIN 1 */
  250. int i;
  251.   /*  USER CODE END 1 */

  252.   /* MCU Configuration----------------------------------------------------------*/

  253.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  254.   HAL_Init();

  255.   /* USER CODE BEGIN Init */

  256.   /* USER CODE END Init */

  257.   /* Configure the system clock */
  258.   SystemClock_Config();

  259.   /* USER CODE BEGIN SysInit */

  260.   /* USER CODE END SysInit */

  261.   /* Initialize all configured peripherals */
  262.   MX_GPIO_Init();
  263.   USART1_Init(&huart1);
  264.   ADC1_Init(&hadc1);
  265.   __VPENOFF();
  266. ThermalHeadInit();

  267.   nLAT_PutVal(0);
  268.   STB1_PutVal(0);//
  269.   STB2_PutVal(0);

  270.   /* USER CODE BEGIN 2 */
  271.     for(i=0;i<48;i++)
  272.   read(0);
  273.   print_flag = 0;
  274.   if(PHK_GetVal()==1)
  275.   {
  276.     paper_flag = 1;
  277.     runmotor(200);
  278.   }
  279.   else
  280.   {
  281.     paper_flag = 0;
  282.   }
  283.   /* USER CODE END 2 */
  284.   
  285.   while(1)
  286.           {
  287.                   //val = PHK_GetVal();
  288.                   printf24(p);
  289.           }
  290.   /* USER CODE BEGIN WHILE */
  291.         while(1)
  292.         {
  293.       switch(print_flag)
  294.       {
  295.         case 0:
  296.           nLAT_PutVal(0);
  297.           STB1_PutVal(0);//
  298.           STB2_PutVal(0);
  299.           if(PHK_GetVal()==0)
  300.           {
  301.               if(paper_flag == 0)
  302.               {
  303.                   runmotor(1000);
  304.               }
  305.               if(PHK_GetVal()==1)
  306.               {
  307.                   paper_flag = 1;
  308.               }
  309.               else
  310.               {
  311.                   paper_flag = 0;
  312.               }
  313.           }
  314.           else
  315.           {
  316.             paper_flag = 0;
  317.           }
  318.         break;
  319.         case 1://出纸间隙
  320.           print_flag = 0;
  321.         break;
  322.         case 2://打印机状态
  323.           print_flag = 0;
  324.           if(PHK_GetVal()==1)
  325.           {
  326.             //flag = UART_SendChar(0xa1);//有纸
  327.           }
  328.           else
  329.           {
  330.             //flag = UART_SendChar(0xa0);
  331.           }

  332.         break;
  333.         case 3://版本号
  334.         #if 0
  335.           print_flag = 0;
  336.           flag = UART_SendChar(2);delay(1000);
  337.           flag = UART_SendChar('V');delay(1000);
  338.           flag = UART_SendChar('E');delay(1000);
  339.           flag = UART_SendChar('R');delay(1000);
  340.           flag = UART_SendChar('2');delay(1000);
  341.           flag = UART_SendChar('.');delay(1000);
  342.           flag = UART_SendChar('0');delay(1000);
  343.           flag = UART_SendChar('1');delay(1000);
  344.           flag = UART_SendChar(0X0D);delay(1000);
  345.           flag = UART_SendChar(0X0A);delay(1000);
  346.           flag = UART_SendChar(3);delay(1000);
  347.                 #endif
  348.         break;
  349.         case 4://正常打印
  350.           print_flag = 0;
  351.           printf24(huart1.RxBuff);
  352.           if(PHK_GetVal()==1)//有纸
  353.           {
  354.             for(i=0;i<line_out*17;i++)  
  355.             runmotor(2);
  356.           }
  357.           memset(huart1.RxBuff, 0, 2048);
  358.           print_flag = 0;
  359.         break;
  360.       }
  361.         }
  362.   /* USER CODE END 3 */

  363. }

  364. /** System Clock Configuration
  365. */
  366. void SystemClock_Config(void)
  367. {

  368.   RCC_OscInitTypeDef RCC_OscInitStruct;
  369.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  370.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  371.     /**Initializes the CPU, AHB and APB busses clocks
  372.     */
  373.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  374.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  375.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  376.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  377.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  378.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  379.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  380.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  381.   {
  382.     _Error_Handler(__FILE__, __LINE__);
  383.   }

  384.     /**Initializes the CPU, AHB and APB busses clocks
  385.     */
  386.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  387.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  388.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  389.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  390.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  391.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  392.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  393.   {
  394.     _Error_Handler(__FILE__, __LINE__);
  395.   }

  396.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  397.   PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
  398.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  399.   {
  400.     _Error_Handler(__FILE__, __LINE__);
  401.   }

  402.     /**Configure the Systick interrupt time
  403.     */
  404.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  405.     /**Configure the Systick
  406. ……………………

  407. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
Printer-Demo_v1.3.rar (11.76 MB, 下载次数: 201)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏3 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:386408 发表于 2018-8-15 14:37 | 只看该作者
请问楼主,如何控制热敏打印头加热
回复

使用道具 举报

板凳
ID:161033 发表于 2018-10-14 22:17 | 只看该作者
不下也要点赞。
回复

使用道具 举报

地板
ID:345283 发表于 2018-10-18 22:16 | 只看该作者
能打印内容出来吗?
回复

使用道具 举报

5#
ID:345283 发表于 2018-10-18 22:20 | 只看该作者
打印头是多少点的,最快打印速度能达到多少mm/s,有外部字库吗?
回复

使用道具 举报

6#
ID:478116 发表于 2019-2-19 15:08 | 只看该作者
要具体看看.打印头驱动,字符转图片.好多内容.
回复

使用道具 举报

7#
ID:170774 发表于 2019-5-13 11:46 | 只看该作者
兄弟用什么机芯的?
回复

使用道具 举报

8#
ID:351680 发表于 2019-6-9 21:44 | 只看该作者
langge 发表于 2019-5-13 11:46
兄弟用什么机芯的?

用的是精芯科技的,字库方式打印
回复

使用道具 举报

9#
ID:351680 发表于 2019-6-9 21:45 | 只看该作者
shark313 发表于 2018-10-18 22:20
打印头是多少点的,最快打印速度能达到多少mm/s,有外部字库吗?

24点阵,速度我忘了,字库得自己做,不过程序里有
回复

使用道具 举报

10#
ID:461897 发表于 2019-9-19 09:10 | 只看该作者
下载了,为什么打不开?解压报告没有发现档案文件?
回复

使用道具 举报

11#
ID:461897 发表于 2019-9-23 15:14 | 只看该作者
铁杵鸵鸟 发表于 2019-9-19 09:10
下载了,为什么打不开?解压报告没有发现档案文件?

用好压就解决了。
回复

使用道具 举报

12#
ID:461897 发表于 2019-9-24 15:36 | 只看该作者

原来用winrar就报错!
回复

使用道具 举报

13#
ID:635290 发表于 2019-11-7 16:28 | 只看该作者
图片怎么打印?
回复

使用道具 举报

14#
ID:448382 发表于 2020-1-28 01:36 | 只看该作者
你好,麻烦问一下有没有机芯的手册呢?
回复

使用道具 举报

15#
ID:230666 发表于 2020-2-4 11:13 | 只看该作者
不下也要点赞。
回复

使用道具 举报

16#
ID:753075 发表于 2020-5-16 09:21 | 只看该作者
楼主打印机调通了吗?
回复

使用道具 举报

17#
ID:726918 发表于 2020-7-16 15:27 | 只看该作者
感谢分享,下载下来学习一下
回复

使用道具 举报

18#
ID:754694 发表于 2020-8-31 18:00 来自手机 | 只看该作者
刚好手头有报废的poss机,谢谢楼主分享学习。
回复

使用道具 举报

19#
ID:406609 发表于 2021-2-19 10:01 | 只看该作者
最近在调热敏打印 参考一下
回复

使用道具 举报

20#
ID:8464 发表于 2021-3-25 21:36 | 只看该作者
标记,以后研究。
回复

使用道具 举报

21#
ID:82944 发表于 2022-9-17 14:01 | 只看该作者
没接触过打印机,但也要学习,感觉热心人士
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表