Source program circuit diagram of optical drive laser engraving machine based on 51 single chip microcomputer + host computer

Publisher:心动代码Latest update time:2019-11-15 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Please do not blame me for the poor hand-drawn schematic diagram of the DIY laser engraving machine by others.
The circuit schematic diagram in the experimental exercise is as follows:

The microcontroller source program is as follows:

/*z address definition

50 1=x+,2=x-,3=y+,4=y-

51 High forward and backward steps

52 Low forward and backward steps

53 

54/55 character width 

56 

57 Low light switch

58/59 Laser Intensity

60 x-axis speed

61 y-axis speed

62 Start printing 0,57

63 Pause

64 Stop Sign

65

66 left and right marks 

100 Grayscale image data at the beginning

*/


#include

#define uint unsigned int 

#define uchar unsigned char 

#define N z[60] //X speed

#define M z[61] //Y speed

sbit a=P1^3; // Stepper motor wiring definition Moving laser head

sbit a_=P1^2;

sbit b=P1^1;

sbit b_=P1^0;

sbit xa=P1^4;

sbit xa_=P1^5;

sbit xb=P1^6;

sbit xb_=P1^7;

/*sbit a=P1^4; //Stepper motor wiring definition mobile base

sbit a_=P1^5;

sbit b=P1^6;

sbit b_=P1^7;

sbit xa=P1^3;

sbit xa_=P1^2;

sbit xb=P1^1;

sbit xb_=P1^0; */

sbit jg=P2^0;

sbit led=P2^1; // indicator light

uchar xdata z[500]={0};//cache

uchar buff[3]; //Serial port buffer

uchar x1,x0,y1,y0,cont2=0;

uchar xfb=4,yfb=4; //walking flag



unsigned char HighRH = 0; //high byte of high level reload value

unsigned char HighRL = 0; //low byte of high level reload value

unsigned char LowRH = 0; //high byte of low level reload value

unsigned char LowRL = 0; //low byte of low level reload value




void delayms(uint xms)                                

{   

        uint i,j;

        for(i=xms;i>0;i--) //i=xms means delay of about xms milliseconds

                for(j=110;j>0;j--);

}


/* Configure and start PWM, fr-frequency, dc-duty cycle*/

void ConfigPWM(unsigned int fr, unsigned char dc)

{

    unsigned int high, low;

    unsigned long tmp;

    

    tmp = (11059200/12) / fr; //Calculate the count value required for one cycle

    high = (tmp*dc) / 100; //Calculate the count value required for the high level

    low = tmp - high; //Calculate the count value required for the low level

    high = 65536 - high + 12; //Calculate the high level reload value and compensate for interrupt delay

    low = 65536 - low + 12; //Calculate the low level reload value and compensate for interrupt delay

    HighRH = (unsigned char)(high>>8); //Split the high-level reload value into high and low bytes

    HighRL = (unsigned char)high;

    LowRH = (unsigned char)(low>>8); // split the low level reload value into high and low bytes

    LowRL = (unsigned char)low;

    TMOD &= 0xF0; // Clear the control bit of T0

    TMOD |= 0x01; //Configure T0 to mode 1

    TH0 = HighRH; //Load T0 reload value

    TL0 = HighRL;

    ET0 = 1; // Enable T0 interrupt

    TR0 = 1; //Start T0

    jg = 1; // Output low level, turn off the laser

}

/* Turn off PWM */

void ClosePWM()

{

    TR0 = 0; //Stop timer 0

    ET0 = 0; //Disable timer 0 interrupt

    jg = 1; // Output low level, turn off the laser

}

/* T0 interrupt service function, generate PWM output */

void InterruptTimer0() interrupt 1

{

    if (jg == 1) //When the current output is low level, load the high level value and output high level

    {

        TH0 = LowRH;

        TL0 = LowRL;

        jg = 0;

    }

    else //When the current output is high level, load the low level value and output low level

    {

        TH0 = HighRH;

        TL0 = HighRL;

        jg = 1;

    }

}




void xfor(uint i) //x-axis forward function, how many steps to advance

{   

  while(1) 

  {

                if(xfb==4)

                  {

         xa=xb=1;

               xb_=xa_=0;

                     xfb=1;

                     i--;                        

               delayms(N);

                     if(i==0){xa=xb=0; break;}

                  }

                   if(xfb==1)

                  {

         xb=xa_=1;

               xa=xb_=0;

                     xfb=2;

                     i--;        

               delayms(N);

                     if(i==0){xa_=xb=0; break;}

                  }

                   if(xfb==2)

                  {

         xa_=xb_=1;

         xb=xa=0;

                     xfb=3; //Walking flag

                     i--;

               delayms(N);

                     if(i==0){xa_=xb_=0; break;}

                   }

                   if(xfb==3)

                  {

         xa_=xb=0;

               xb_=xa=1;

                     xfb=4;

                     i--;        

               delayms(N);

                     if(i==0){xa=xb_=0; break;}

                  }



                }


}



void xbac(uint i) //xxx back function

{   

     while(1) 

         {

                if(xfb==1)

                  {

         xa_=xb=0;

               xb_=xa=1;

                     xfb=4;

                     i--; //Walking flag

               delayms(N);

                     if(i==0){xa=xb_=0; break;}

                   }

                   if(xfb==4)

                  {

         xa_=xb_=1;

         xb=xa=0;

                     xfb=3;

                     i--;        

               delayms(N);

                     if(i==0){xa_=xb_=0; break;}

                   }

                   if(xfb==3)

                  {

         xb=xa_=1;

               xa=xb_=0;

                     xfb=2; //Walking flag

                     i--;        

               delayms(N);

                     if(i==0){xa_=xb=0; break;}

                   }

                   if(xfb==2)

                  {

         xa=xb=1;

               xb_=xa_=0;

                     xfb=1;

                     i--;        

               delayms(N);

                     if(i==0){xa=xb=0; break;}

                  }

                }

}




void yfor(uint i) //y-axis forward function

{   

     while(1) 

     {

               switch(yfb)

                   {

                       case 4:{a=b=1; b_=a_=0; yfb=1; i--; delayms(M); if(i==0){a=b=0;break;}}

                                 case 1:{b=a_=1; a=b_=0; yfb=2; i--; delayms(M); if(i==0){a_=b=0;break;}}

                                 case 2:{a_=b_=1; b=a=0; yfb=3; i--; delayms(M); if(i==0){a_=b_=0;break;}}

                                 case 3:{b_=a=1; a_=b=0; yfb=4; i--; delayms(M); if(i==0){a=b_=0;break;}}

                   }

                   if(i==0) break;

      }

}


void ybac(uint i) //yy back function

{   

     while(1) 

     {

               switch(yfb)

                   {

                       case 1:{a=b_=1; b=a_=0; yfb=4; i--; delayms(M); if(i==0){a=b_=0;break;}}

                                 case 4:{b_=a_=1; a=b=0; yfb=3; i--; delayms(M); if(i==0){a_=b_=0;break;}}

                                 case 3:{a_=b=1; b_=a=0; yfb=2; i--; delayms(M); if(i==0){a_=b=0;break;}}

                                 case 2:{b=a=1; a_=b_=0; yfb=1; i--; delayms(M); if(i==0){a=b=0;break;}}

                   }

                   if(i==0) break;

      }

}


void dazi(uint zik) //print function ******The print function has been changed******

{

    uint x;

                jg=0;


                        for(x=0;x                        {

                          while(z[63]); //pause and wait

                                if(z[64]==1) break;//Stop sign to jump out of the loop

                                SBUF=255; // Every time a dot is printed, send 255 to the host computer, which is used to display the progress of the host computer

                                jg=0; //Turn on the laser

                                delayms((z[99+x]*(z[58]*256+z[59]))/100);

                            jg=1; //Turn off the laser

                        if(z[66]==1)

                        {

                        xbac(1);

                        }        

             else

                         {

                         xfor(1);

                         }                         

                        }          

                if(z[64==1]) z[64]=0;

            else{yfor(1);} //Y axis advances one line

            z[62]=0; //One line of printing completed

                SBUF=1; //Send information, indicating that printing of one line is completed 

                

}


/* Serial port configuration function, baud-communication baud rate*/

void ConfigUART(unsigned int baud)

{

    SCON = 0x50; //Configure the serial port to mode 1

    TMOD &= 0x0F; // Clear the control bit of T1

    TMOD |= 0x20; //Configure T1 to mode 2

    TH1 = 256 - (11059200/12/32)/baud; //Calculate T1 reload value

    TL1 = TH1; //initial value equals reload value

    ET1 = 0; //Disable T1 interrupt

    ES = 1; // Enable serial port interrupt

    TR1 = 1; //Start T1

}



void chuanlo() interrupt 4

{

  if(RI)

  {

     buff[cont2]=SBUF; //3 bytes each time, address high, address low, data,,

    

           cont2++;

     if(cont2==3) //Every time 3 bytes are received, write the data to the address

           {

            z[(buff[0]*256)+buff[1]]=buff[2];

                  cont2=0;

                 SBUF=0; //*****************Add this line of code here to test it********************************

           }

         RI=0;



    

  }

  if(TI)

   {

   TI=0;


   }

}


main()

{  

   EA=1;

   P0=0xff;

   ConfigUART(9600);

        z[60]=15; //Default parameters

        z[61]=15;

        z[56]=1;

        z[62]=0;

        jg=0;

        cont2=0;

        

   while(1)

   {

                                                                                                                      // *****THIS HAS BEEN CHANGED******


    if(z[57]==100){ConfigPWM(100, 98);delayms(10);} //Host computer command processing, turn on low-light positioning, frequency 100Hz, duty cycle 98%                                                                      

    else if(z[57]==1) jg=0; //Turn on the bright light

        else { ClosePWM(); delayms(10);}

         

          if(cont2!=0) led=0; else led=1;//Indicates whether communication is available

      if(z[50]==1){xfor(z[51]*256+z[52]);z[50]=0;}//x+

          if(z[50]==2){xbac(z[51]*256+z[52]);z[50]=0;}//x-

          if(z[50]==3){yfor(z[51]*256+z[52]);z[50]=0;}//y+

          if(z[50]==4){ybac(z[51]*256+z[52]);z[50]=0;}//y-


          

      if(z[62]) //Start printing flag

          {

            dazi(z[54]*256+z[55]);

          }

   

  }


}



Reference address:Source program circuit diagram of optical drive laser engraving machine based on 51 single chip microcomputer + host computer

Previous article:Simulation and source code of a simple automatic tracking car based on single chip microcomputer control
Next article:51 MCU simulation SPI bus communication program and proteus simulation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号