//WIFI制御自動彫刻機ソースプログラム #include #include #include #include #pragma config PLLDIV = 5 // (20 MHz crystal on PICDEM FS USB board) #pragma config CPUDIV = OSC1_PLL2 #pragma config USBDIV = 2 // Clock source from 96MHz PLL/2 #pragma config FOSC = HSPLL_HS #pragma config FCMEN = OFF #pragma config IESO = OFF #pragma config PWRT = OFF #pragma config BOR = ON #pragma config BORV = 3 #pragma config VREGEN = ON //USB Voltage Regulator #pragma config WDT = OFF #pragma config WDTPS = 32768 #pragma config MCLRE = ON #pragma config LPT1OSC = OFF #pragma config PBADEN = OFF #pragma config STVREN = ON #pragma config LVP = OFF #pragma config XINST = OFF // Extended Instruction Set #pragma config CP0 = OFF #pragma config CP1 = OFF #pragma config CPB = OFF #pragma config WRT0 = OFF #pragma config WRT1 = OFF #pragma config WRTB = OFF // Boot Block Write Protection #pragma config WRTC = OFF #pragma config EBTR0 = OFF #pragma config EBTR1 = OFF #pragma config EBTRB = OFF #define _XTAL_FREQ 48000000 //__delay_ms #define SW1 PORTEbits.RE0 #define SW2 PORTEbits.RE1 #define LED PORTEbits.RE2 char UART_Init(const long int baudrate); void UART_Write(char data); char UART_TX_Empty(); void UART_Write_Text(char *text); char UART_Data_Ready(); char UART_Read(); void UART_Read_Text(char *Output, unsigned int length); void UARTreadTEXT(); void MyWait(int iT); char UARTreadBuffer[256];//受信バッファ char gN=0;//受信バイト数 char gBuffer[256];//読出しバッファ char gBN=0;//読出しバイト数 char writeBuffer[256];//書込みバッファ void InitInterTimer0 (void); void MyCDCBasicTasks(); unsigned short ADC0(); unsigned char gCT=0;//Timer0割込みカウンタ short gX0=0,gY0=0,gZ0=0;//現在座標 short gX1=0,gY1=0,gZ1=0;//目標座標 unsigned char CHxy[4] = { 0x08,0x04,0x02,0x01};//XY1相励磁パターン unsigned char CHz[4] = { 0x01,0x02,0x04,0x08};//Z1相励磁パターン unsigned char MCx=0,MCy=0,MCz=0;//XVZ移動中フラグ unsigned char fOutM=0;//XVZ移動完了報告フラグ void main(void) { // 入出力設定 ADCON0 = 0b00000001; //A/D converter module is enabled:A/D Idle:Channel 0 (AN0) ADCON1 = 0b00001100; //AN0とAN1とAN2がアナログ設定:VDDとVSSが選択 TRISA = 0b00000111;//AN0とAN1とAN2を入力設定 TRISB = 0b00000000; //出力設定 TRISC = 0b00110000;//D-,D+(USB用)RC4とRC5を入力に設定 TRISD = 0b00000000;//出力設定 TRISE = 0b00000011; //SW1,2=INPUT LED=OUTPUT LATA = 0b00000000;//ラッチ無し LATB = 0b00000000; LATC = 0b00000000; LATD = 0b00000000; LATE = 0b00000000; InitInterTimer0(); PEIE = 0;// 周辺機能割込みの許可 GIE = 1;// 割込み全体の許可 ADCON2=0x3C;//ADC:111 = 20 TAD , 100 = FOSC/4 ADCON2bits.ADFM = 1;//ADC:1 = Right justified UART_Init(115200);//UART初期設定、ボーレイト設定 //割り込み使用時は以下を有効にします。 RCIF = 0; //reset RX pin flag RCIP = 1; //high priority RCIE = 1; //Enable RX interrupt PEIE = 1; //Enable pheripheral interrupt (serial port is a pheripheral) INTCONbits.GIE = 1;//Global Interrupt Enable bit int i; LED=0; while(SW1==1) { UARTreadTEXT(); } UART_Write_Text("AT+CWMODE=1\r\n"); MyWait(100); UARTreadTEXT(); UART_Write_Text("AT+CIPSTART=\"TCP\",\"192.168.11.2\",13000\r\n"); MyWait(200); UARTreadTEXT(); UART_Write_Text("AT+CIPMODE=1\r\n"); MyWait(200); UARTreadTEXT(); UART_Write_Text("AT+CIPSEND\r\n"); MyWait(200); UARTreadTEXT(); UART_Write_Text(">OK\r\n"); gN=0; __delay_ms(15); UARTreadTEXT(); UART_Write_Text(">OK\r\n"); gN=2; while(1) { __delay_ms( 10 ); //20mS PORTEbits.RE2=!PORTEbits.RE2; if(gX0gX1) {//現在値が目標値より大きい gX0=gX0-1; MCx=1;//X移動中 fOutM=1; } else {//現在値=目標値 MCx=0;//X停止中 } } if(gY0gY1) {//現在値が目標値より大きい gY0=gY0-1; MCy=2;//Y移動中 fOutM=1; } else {//現在値=目標値 MCy=0;//Y停止中 } } if(gZ0gZ1) {//現在値が目標値より大きい gZ0=gZ0-1; MCz=4;//Z移動中 fOutM=1; } else {//現在値=目標値 MCz=0;//Z停止中 } } PORTD=16*CHxy[gY0 % 4]+CHxy[gX0 % 4];//XYパルスモータ制御 PORTB=CHz[gZ0 % 4];//Zパルスモータ制御 if(fOutM==1 && MCx+MCy+MCz==0) { fOutM=2;//完了報告 } MyCDCBasicTasks(); } } void interrupt SYS_InterruptHigh(void) { if(PIR1bits.RCIF == 1)//シリアル通信割込み { UARTreadBuffer[gN] = UART_Read();//読出し、フラグクリア gN++; } if(T0IF == 1)// タイマ0割込み { if(gCT < 16) {gCT++;} else {gCT=0;} T0IF = 0;// タイマ0割込みフラグをクリア } } char UART_Init(const long int baudrate) { unsigned int x; BRGH = 1; //Setting High Baud Rate x = (_XTAL_FREQ - baudrate*16)/(baudrate*16); //SPBRG for High Baud Rate if(x>255) //If High Baud Rage Required { BRGH = 0; x = (_XTAL_FREQ - baudrate*64)/(baudrate*64); //SPBRG for Low Baud Rate } if(x<256) { SPBRG = x; //Writing SPBRG Register SYNC = 0; //Setting Asynchronous Mode, ie UART SPEN = 1; //Enables Serial Port TRISC7 = 1; //As Prescribed in Datasheet TRISC6 = 1; //As Prescribed in Datasheet CREN = 1; //Enables Continuous Reception TXEN = 1; //Enables Transmission return 1; //Returns 1 to indicate Successful Completion } return 0; //Returns 0 to indicate UART initialization failed } void UART_Write(char data) { while(!TRMT); TXREG = data; } char UART_TX_Empty() { return TRMT; } void UART_Write_Text(char *text) { int i; for(i=0;text[i]!='\0';i++) UART_Write(text[i]); } char UART_Data_Ready() { return RCIF; } char UART_Read() { while(!RCIF); return RCREG; } void UART_Read_Text(char *Output, unsigned int length) { unsigned int i; for(int i=0;i=1) { iN=gN; gN=0; for(i=1;i=1) { gBN=gN; gN=0; for(i=0;i0) { UARTreadTEXT(); //UART_Write_Text(gBuffer);//TEST用 j=0;k=0; for(i=0;i5){j=4;} k=0; } else { FIN[j][k]=gBuffer[i]; k++;if(k>20){k=19;} } } if(FIN[0][0]=='D' && FIN[0][1]=='C' && FIN[0][2]==0) { I1=atoi((const char *)FIN[1]); if(I1==1) { //fDC=1; PORTCbits.RC0=1;//DCモータON } else { PORTCbits.RC0=0;//DCモータOFF } UART_Write_Text(gBuffer); } if(FIN[0][0]=='A' && FIN[0][1]=='D' && FIN[0][2]==0) { sprintf(writeBuffer,"AD\t%d\r\n",ADC0()); numBytesRead = strlen(writeBuffer); writeBuffer[numBytesRead]=0x00; UART_Write_Text(writeBuffer); } if(FIN[0][0]=='M' && FIN[0][1]=='R' && FIN[0][2]==0) { gX1=gX0+atoi((const char *)FIN[1]); gY1=gY0+atoi((const char *)FIN[2]); gZ1=gZ0+atoi((const char *)FIN[3]); } if(FIN[0][0]=='M' && FIN[0][1]=='A' && FIN[0][2]==0) { gX1=atoi((const char *)FIN[1]); gY1=atoi((const char *)FIN[2]); gZ1=atoi((const char *)FIN[3]); if(gX1==gX0 && gY1==gY0 && gZ1==gZ0){fOutM=2;} } } else { if(fOutM==2) { sprintf(writeBuffer,"MC\t%d\t%d\t%d\r\n",gX0,gY0,gZ0); numBytesRead = strlen(writeBuffer); writeBuffer[numBytesRead]=0x00; UART_Write_Text(writeBuffer); fOutM=0; } } } unsigned short ADC0() { unsigned short result; long X; ADCON0bits.GO = 0; // Stop AD conversion __delay_us( 100 ); ADCON0bits.CHS = 0b0000; //Analog Channel Select bits=Channel 0 (AN0) __delay_us( 100 ); ADCON0bits.GO = 1; // Start AD conversion while(ADCON0bits.NOT_DONE); // Wait for conversion result=ADRESH*256+ADRESL; return result; }