// Global variables int8 rs232_buffer; // Stores the return data from getc() function int8 dataIn[PACKETLENGTH]; // Stores the incoming data packet from Com Port int16 jogDataInputLocation; // Stores the address of the location where interrupts will return int16 modeSelectionLocation; // Stores the address of the location where interrupts will return int1 currentMachineMode; // MachineMode Flag 0=Jog 1=Drill int8 axisDelay; // used in int_ext routine & main()
#int_ext // RB0 External Interrupt Service Routine void isr_ext() { // INTF Flag of INTCON register must be cleared if goto is used in isr int8 switchConditions; disable_interrupts(GLOBAL); ===========================> 8051 KARŞILIĞI NASIL delay_ms(300); // debounce button switchConditions=input_a(); switch (switchConditions) { case 0x3E: // RA0=0 output_bit(DIR_X,1);==============================>8051 KARŞILIĞI NASIL constspeed2(STEPSBACKATLIMIT,axisDelay,1); putc(HOME_SWITCH_X); break; case 0x3D: // RA1=0 output_bit(DIR_X,0); constspeed2(STEPSBACKATLIMIT,axisDelay,1); putc(LIMIT_SWITCH_X); break; case 0x3B: // RA2=0 output_bit(DIR_Y,1); constspeed2(STEPSBACKATLIMIT,axisDelay,2); putc(HOME_SWITCH_Y); break; case 0x37: // RA3=0 output_bit(DIR_Y,0); constspeed2(STEPSBACKATLIMIT,axisDelay,2); putc(LIMIT_SWITCH_Y); break; case 0x2F: // RA4=0 putc(HOME_SWITCH_Z); break; case 0x1F: // RA5=0 putc(LIMIT_SWITCH_Z); break; case 0x3F: // Unpressed switch Interrupt return; break; default: //putc(ERRORLIMITSWITCH); // On Error send ERRORLIMITSWITCH byte return; break; } switch (currentMachineMode) { case 0: // Jog Mode goto_address(jogDataInputLocation); break; case 1: // Drill Mode goto_address(modeSelectionLocation); break; } } // End of RB0 External Interrupt Service Routine
#int_rda // UART Receive Data Interrupt Service Routine void serial_isr() { rs232_buffer=getc(); switch (rs232_buffer) { case (EMERGENCYSTOP): // Disable all drivers,Cut motor coil currents output_bit(ENABLE_X,0); output_bit(ENABLE_Y,0); output_bit(ENABLE_Z,0); putc(MACHINESTOPPED); switch (currentMachineMode) { case 0: // Jog Mode goto_address(jogDataInputLocation); break; case 1: // Drill Mode goto_address(modeSelectionLocation); break; } break; } } // End of UART Receive Data Interrupt Service Routine
output_b(0xF0); // Set all Driver Pins output_d(0xFF); set_tris_b(0x01); // RB0 is input for external interrupts=================>8051 KARŞILIĞI NASIL printf("DRİLL MACHİNE"); jogDataInputLocation = label_address(JOG_DATA_INPUT); modeSelectionLocation = label_address(MODE_SELECTION);
disable_interrupts(GLOBAL); // Disable all Interrputs
while (getc()!=ISMACHINEREADY); // getc() until ISMACHINEREADY has come putc(MACHINEREADY);
MODE_SELECTION: rs232_buffer=getc();
switch (rs232_buffer) { case (ISMACHINEREADY): putc(MACHINEREADY); goto MODE_SELECTION; break; case (OPENJOGMODE): putc(JOGMODEON); goto JOG_DATA_INPUT; break; case (OPENDRILLMODE1): putc(DRILLMODE1ON); goto DRILL_MODE1_INITIALIZE; break; case (OPENDRILLMODE2): // !Not Completed! putc(DRILLMODE2ON); goto JOG_DATA_INPUT; // open jog mode to take 2 hole positions break; }
// Do the axis movement according to given parameters switch (axisSelect) { case 1: // 1=01b -> X-Axis output_bit(MODE_X,axisMode); output_bit(DIR_X,axisDir); constspeed(axisStep,axisDelay,1); // (Stepnumber,delay,pin) break; case 2: // 2=10b -> Y-Axis output_bit(MODE_Y,axisMode); output_bit(DIR_Y,axisDir); constspeed(axisStep,axisDelay,2); // (Stepnumber,delay,pin) break; case 3: // 3=11b -> Z-Axis output_bit(MODE_Z,axisMode); output_bit(DIR_Z,axisDir); constspeed(axisStep,axisDelay,3); // (Stepnumber,delay,pin) break; }
putc(JOGMODEDONE); // Inform PC that Movement has been completed disable_interrupts(GLOBAL); goto JOG_DATA_INPUT; // Wait for new coordinates at JOG_DATA_INPUT
DRILL_MODE1_INITIALIZE: // Drill using offset position of the machine
currentMachineMode = 1; // Machine is in Drill Mode
dataIn[0] = getc(); // Movement Configuration Byte dataIn[1] = getc(); // Step X Low Byte dataIn[2] = getc(); // Step X High Byte dataIn[3] = getc(); // Step Y Low Byte dataIn[4] = getc(); // Step Y High Byte dataIn[5] = getc(); // Step Z Low Byte dataIn[6] = getc(); // Step Z High Byte dataIn[7] = getc(); // Delay X Byte dataIn[8] = getc(); // Delay Y Byte dataIn[9] = getc(); // Delay Z Byte
bit_clear(*0x0B,1); // Clear INTF Flag of INTCON register
if (!(dataIn[0] >> 7)) // If Continue Drilling Flag Reset exit Drill Mode { putc(DRILLMODE1DONE); // inform PC that drilling has been completed disable_interrupts(GLOBAL); goto MODE_SELECTION; // Goto and Wait at MODE_SELECTION }
if ((dataIn[0] & 0x40) >> 6) // If Start Drilling Flag Set return without drilling { putc(ONEHOLEDRILLED); // Machine is ready for taking next hole coordinates disable_interrupts(GLOBAL); // Disable Interrupts before taking data packet goto DRILL_MODE1_INPUT; // Wait for new hole coordinates }
if (DelayX < DelayY) // DelayXY is the speed of slow axis DelayXY = DelayY; else DelayXY = DelayX;
if (StepX > StepY) // StepXY is the step number of the smallest movement { StepXY = StepY; axis = 0; // axis = X axis StepMore = StepX - StepY; } else { StepXY = StepX; axis = 1; // axis = Y axis StepMore = StepY - StepX; }
// Do the dual axis movement for (i=0; i<StepXY; i++) { output_bit(STEP_X,1); // StepX pin HIGH output_bit(STEP_Y,1); // StepY pin HIGH dly_100us(DelayXY); output_bit(STEP_X,0); // StepX pin LOW output_bit(STEP_Y,0); // StepY pin LOW dly_100us(DelayXY); } output_bit(STEP_X,1); // Add the last step on X axis output_bit(STEP_Y,1); // Add the last step on Y axis
// Do the remaining single axis movement switch (axis) { case 0: // X Axis for (i=0; i<StepMore; i++) { output_bit(STEP_X,1); // Step pin HIGH dly_100us(DelayX); output_bit(STEP_X,0); // Step pin LOW dly_100us(DelayX); } output_bit(STEP_X,1); // Add last step break; case 1: // Y Axis for (i=0;i<StepMore;i++) { output_bit(STEP_Y,1); // Step pin HIGH dly_100us(DelayY); output_bit(STEP_Y,0); // Step pin LOW dly_100us(DelayY); } output_bit(STEP_Y,1); // Add last step break; } } // End of constspeed_xy()
void constspeed(int16 conststep, int8 delay, int8 pin) // Build conststep number of step pulses // Frequency = 1/(2*delay*100us) { int16 i;
switch (pin) { case 1: for (i=0;i<conststep;i++) { output_bit(STEP_X,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_X,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_X,1); // Add last step break; case 2: for (i=0;i<conststep;i++) { output_bit(STEP_Y,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_Y,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_Y,1); // Add last step break; case 3: for (i=0;i<conststep;i++) { output_bit(STEP_Z,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_Z,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_Z,1); // Add last step break; } } // End of constspeed()
// Same as constspeed() funtion (used for int_ext routine) void constspeed2(int16 conststep, int8 delay, int8 pin) // Build conststep number of step pulses // Frequency = 1/(2*delay*100us) { int16 i;
switch (pin) { case 1: for (i=0;i<conststep;i++) { output_bit(STEP_X,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_X,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_X,1); // Add last step break; case 2: for (i=0;i<conststep;i++) { output_bit(STEP_Y,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_Y,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_Y,1); // Add last step break; case 3: for (i=0;i<conststep;i++) { output_bit(STEP_Z,1); // Step pin HIGH dly_100us(delay); output_bit(STEP_Z,0); // Step pin LOW dly_100us(delay); } output_bit(STEP_Z,1); // Add last step break; } } // End of constspeed2()
void dly_100us(int8 n) // 3us+(1.2us+d)*n { // Delay for n * 100us for(;n!=0;n--) delay_us(98); // d is exactly 98.8 us delay_cycles(4); // 0.8 us for 20Mhz oscillator }
Bu kodlar CCS C ile yazıldığında 8051 için C kodunu dönüştürme yapılırken sorun olabilir.İşte ANSI C bir derleyici kullanmanın faydası.Port etme işlemleri çok daha kolay oluyor.Yukarıdaki programda çoğu bölüm aynen aktarılabilir.8051 için hangi derleyiciyi kullanacağına bağlı bu?
KEİL kullanıyorum
aslında pic ile yapacaktım ama hoca illa 8051 dedi simdide yeniden yazmam gerekiyo ben pic c lite kullanıyorum