char UART_RcvEscapeSeq() { while (!UARTDataAvailable()); if (UART_receive() != 0x5B) return 0; while (!UARTDataAvailable()); return UART_receive(); }
int glove_init (void) { UART_send (0x41); if (UART_receive()!=0x55) return 0; else return 1; }
void get_glove_data (glove_data *data) { int sy,cs; UART_send (0x43); sy=UART_receive (); data->f1=UART_receive (); data->f2=UART_receive (); data->f3=UART_receive (); data->f4=UART_receive (); data->f5=UART_receive (); data->pitch=UART_receive (); data->raw=UART_receive (); cs=UART_receive (); if (sy!=0x80) { /* error */ } }
int main(void) { int counter = 100; // 카운터 int index = 0; // 수신 버퍼에 저장할 위치 int process_data = 0; // 문자열 처리 char buffer[20] = ""; // 수신 데이터 버퍼 char data; // 수신 데이터 UART_INIT(); // UART 통신 초기화 UART_printString("Current Counter Value : "); UART_print16bitNumber(counter); UART_printString("\n"); while(1) { data = UART_receive(); // 데이터 수신 if(data == TERMINATOR){ // 종료 문자를 수신한 경우 buffer[index] = '\0'; process_data = 1; // 수신 문자열 처리 지시 } else{ buffer[index] = data; // 수신 버퍼에 저장 index++; } if(process_data == 1){ // 문자열 처리 if(strcmp(buffer, "DOWN") == 0){ // 카운터 감소 counter--; UART_printString("Current Counter Value : "); UART_print16bitNumber(counter); UART_printString("\n"); } else if(strcmp(buffer, "UP") == 0){ // 카운터 증가 counter++; UART_printString("Current Counter Value : "); UART_print16bitNumber(counter); UART_printString("\n"); } else{ // 잘못된 명령어 UART_printString("** Unknown Command **"); UART_printString("\n"); } index = 0; process_data = 0; } } }
int main(void) { UART_init(); spi_init(); delay_ms(100); LCD_init (); delay_ms(100); LCD_clear (); // send the init frame //UART_transmit(0xFB); UART_transmit(0xFB); UART_transmit(0x0A); UART_transmit(0x0A); UART_transmit(0x0A); UART_transmit(0x0D); // upload the pixel array done = 0; while (done == 0) { // receive data frame df_data = UART_receive(); df_seq_msb = UART_receive(); df_seq_lsb = UART_receive(); df_end = UART_receive(); df_cr = UART_receive(); // store into byte pixel array if (df_seq_msb == 0x01) { byte_pixel[256 + df_seq_lsb] = df_data; } else { byte_pixel[df_seq_lsb] = df_data; } // send the ack UART_transmit(0xFA); UART_transmit(df_seq_msb); UART_transmit(df_seq_lsb); UART_transmit(0x0A); UART_transmit(0x0D); // break when the last byte (503) is received if ((df_seq_msb == 0x01) && (df_seq_lsb == 247)) { done = 1; } } // set the cursor LCD_gotoXY(0,0); // write byte_pixel by byte_pixel for (index = 0; index < 504; index++) { LCD_writeData(byte_pixel[index]); } /* //LCD_drawBorder ( ); LCD_gotoXY (4,2); //LCD_writeChar (0x41); //LCD_writeString_F("Thermometer"); //LCD_writeString_F("0123"); LCD_writeChar (0x20); LCD_writeChar (0x20); LCD_writeChar (0x68); LCD_writeChar (0x6f); LCD_writeChar (0x77); LCD_writeChar (0x64); LCD_writeChar (0x79); LCD_gotoXY (4,3); //LCD_writeChar (0x43); //LCD_writeString_F("by DHARMANI"); //LCD_writeString_F("6789"); LCD_writeChar (0x44); LCD_writeChar (0x61); LCD_writeChar (0x6D); LCD_writeChar (0x6D); LCD_writeChar (0x69); LCD_writeChar (0x74); LCD_writeChar (0x21); */ }
void main(void) { unsigned char bufPos = 0; unsigned char cursorPos = 0; unsigned char localEcho = 1; unsigned char c; int i; unsigned char gpibIndex = 0; unsigned char command = 0; int result = 0; unsigned char msgLen = 0; unsigned char msgEOI = 1; GPIO_init(); /* timer & interrupt initialize */ TIMSK = _BV(TOIE0); // wlacz obsluge przerwan T/C0 TCNT0 = T0_INIT; // wartosc poczatkowa T/C0 TCCR0 = _BV(CS00)|_BV(CS02); // preskaler 1024 sei(); ReconfigureGPIO_GPIBNormalMode(); UART_init(); fdevopen(uart_putchar, NULL); #if 1 if (0 == (PINB & _BV(PB5))) // printer mode { ledBlinking = SLOW; ReconfigureGPIO_GPIBReceiveMode(); _delay_ms(1); while (1) { result = GPIB_Receive(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex); if (gpibIndex != 0) { for (i=0; i<gpibIndex; i++) UART_transmit(gpibBuf[i]); } else { _delay_ms(10); } } } localEcho = (PINB & _BV(PB7))?1:0; #endif SetLed(1); while (1) //main loop { selectedCommand = savedCommands; if (localEcho && !bufPos) printf("<GPIB> "); do { while (!UARTDataAvailable()); c = UART_receive(); if (0x08 == c) //backspace { if ((bufPos > 0) && (cursorPos == bufPos)) { --bufPos; --cursorPos; if (localEcho) { UART_transmit(0x08); UART_transmit(' '); UART_transmit(0x08); } } else if ((bufPos > 0) && (cursorPos > 0)) { --bufPos; --cursorPos; memmove(&buf[cursorPos], &buf[cursorPos+1], bufPos-cursorPos); if (localEcho) { UART_transmit(0x08); buf[bufPos] = 0; printf("%s ", &buf[cursorPos]); for (i=cursorPos; i<(bufPos+1); i++) UART_transmit(0x08); } } } else if (10 == c) //ignore LF { } /* else if (9 == c) //tab key { printf("<bufPos=%d cursorPos=%d>", bufPos, cursorPos); } */ else if (0x1b == c) //escape character { switch (UART_RcvEscapeSeq()) { case ESC_KEY_UP: selectedCommand = selectedCommand?selectedCommand-1:0; memmove(&buf[0], &commandsHistory[selectedCommand*BUF_SIZE], BUF_SIZE); if (localEcho) { while (cursorPos < bufPos) { UART_transmit(' '); cursorPos++; } while (bufPos--) { UART_transmit(0x08); UART_transmit(' '); UART_transmit(0x08); } printf("%s", &buf[0]); } bufPos = strlen((char*)&buf[0]); cursorPos = bufPos; break; case ESC_KEY_DOWN: if ((selectedCommand+1) == savedCommands) //current command is last command in buffer { selectedCommand = savedCommands; if (localEcho) { while (cursorPos < bufPos) { UART_transmit(' '); cursorPos++; } while (bufPos--) { UART_transmit(0x08); UART_transmit(' '); UART_transmit(0x08); } } bufPos = 0; cursorPos = 0; } else if ((selectedCommand+1) < savedCommands) // <MAX_COMMANDS { selectedCommand++; memmove(&buf[0], &commandsHistory[selectedCommand*BUF_SIZE], BUF_SIZE); if (localEcho) { while (cursorPos < bufPos) { UART_transmit(' '); cursorPos++; } while (bufPos--) { UART_transmit(0x08); UART_transmit(' '); UART_transmit(0x08); } printf("%s", &buf[0]); } bufPos = strlen((char*)&buf[0]); cursorPos = bufPos; } break; case ESC_KEY_LEFT: if (cursorPos) { --cursorPos; if (localEcho) { UART_transmit(0x1B); UART_transmit(0x5B); UART_transmit('D'); } } break; case ESC_KEY_RIGHT: if (cursorPos < bufPos) { cursorPos++; if (localEcho) { UART_transmit(0x1B); UART_transmit(0x5B); UART_transmit('C'); } } break; default: break; } } else if (13 == c) { if (localEcho) { UART_transmit(13); //CR UART_transmit(10); //LF } if (bufPos) command = toupper(buf[0]); else command = EMPTY_LINE; } else { if (bufPos < BUF_SIZE-1) { if (cursorPos == bufPos) { buf[bufPos++] = c; cursorPos++; if (localEcho) UART_transmit(c); //local echo } else { memmove(&buf[cursorPos+1], &buf[cursorPos], bufPos-cursorPos); buf[cursorPos++] = c; bufPos++; buf[bufPos] = 0; if (localEcho) { UART_transmit(c); //local echo printf("%s", &buf[cursorPos]); for (i=cursorPos; i<bufPos; i++) UART_transmit(0x08); } } } } } while (!command); if ('D' == command) //send data { if (!listenMode) { if (1 == msgEndSeq) buf[bufPos++] = 13; //CR else if (2==msgEndSeq) buf[bufPos++] = 10; //LF else if (3==msgEndSeq) { buf[bufPos++] = 13; //CR buf[bufPos++] = 10; //LF } result = GPIB_Transmit(buf+1, bufPos-1, 1); if (result == 255) // transmit ok printf("OK\r\n"); else //timeout printf("TIMEOUT\r\n"); if ((1==msgEndSeq) || (2==msgEndSeq)) --bufPos; else if (3==msgEndSeq) bufPos -= 2; } else printf("ERROR\r\n"); } else if ('M' == command) //send data without EOI { if (!listenMode) { if (1 == msgEndSeq) buf[bufPos++] = 13; //CR else if (2==msgEndSeq) buf[bufPos++] = 10; //LF else if (3==msgEndSeq) { buf[bufPos++] = 13; //CR buf[bufPos++] = 10; //LF } result = GPIB_Transmit(buf+1, bufPos-1, 0); if (result == 255) // transmit ok printf("OK\r\n"); else //timeout printf("TIMEOUT\r\n"); if ((1==msgEndSeq) || (2==msgEndSeq)) --bufPos; else if (3==msgEndSeq) bufPos -= 2; } else printf("ERROR\r\n"); } else if ('C' == command) //send command { for (i=1; i<bufPos; i++) { if ((buf[i] == '?') || (buf[i] == (64+listenAddress)))//unlisten { listenMode = 0; ledBlinking = OFF; SetLed(1); } else if (buf[i] == (32+listenAddress)) { listenMode = 1; ledBlinking = FAST; } } if (1 == msgEndSeq) buf[bufPos++] = 13; //CR else if (2==msgEndSeq) buf[bufPos++] = 10; //LF else if (3==msgEndSeq) { buf[bufPos++] = 13; //CR buf[bufPos++] = 10; //LF } ReconfigureGPIO_GPIBNormalMode(); SetATN(0); _delay_us(100); result = GPIB_Transmit(buf+1, bufPos-1, 1); if (result == 255) // transmit ok printf("OK\r\n"); else //timeout printf("TIMEOUT\r\n"); SetATN(1); if ((1==msgEndSeq) || (2==msgEndSeq)) --bufPos; else if (3==msgEndSeq) bufPos -= 2; if (listenMode) ReconfigureGPIO_GPIBReceiveMode(); else ReconfigureGPIO_GPIBNormalMode(); listenMode_prev = listenMode; } else if ('R' == command) { SetREN(0); remoteState = 1; printf("OK\r\n"); } else if ('L' == command) { SetREN(1); remoteState = 0; printf("OK\r\n"); } else if ('I' == command) { SetIFC(0); _delay_ms(1); SetIFC(1); if (listenMode) { listenMode = 0; ledBlinking = OFF; SetLed(1); ReconfigureGPIO_GPIBNormalMode(); } printf("OK\r\n"); } else if ('S' == command) { UART_transmit(remoteState?'1':'0'); UART_transmit((0 == (PINC & SRQ))?'1':'0'); UART_transmit(listenMode?'1':'0'); UART_transmit(13); UART_transmit(10); } else if ('P' == command) { listenMode_prev = 0; //cancel listen mode listenMode = 0; //cancel listen mode ledBlinking = SLOW; ReconfigureGPIO_GPIBReceiveMode(); // if (localEcho) // printf("PRINTER MODE, send <ESC> to return to normal mode\r\n"); _delay_ms(1); while (c != 27) { if (UARTDataAvailable()) c = UART_receive(); result = GPIB_Receive(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex); if (gpibIndex != 0) { for (i=0; i<gpibIndex; i++) UART_transmit(gpibBuf[i]); } else { _delay_ms(10); } } c = 0; ReconfigureGPIO_GPIBNormalMode(); ledBlinking = OFF; SetLed(1); } else if ('X' == command) //ascii receive { if (!listenMode) { ReconfigureGPIO_GPIBReceiveMode(); _delay_ms(1); } result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex); if (gpibIndex != 0) { gpibBuf[gpibIndex] = 0; printf("%s",gpibBuf); } else printf("TIMEOUT\r\n"); if (!listenMode) ReconfigureGPIO_GPIBNormalMode(); } else if ('Y' == command) //binary receive { if (!listenMode) { ReconfigureGPIO_GPIBReceiveMode(); _delay_ms(1); } result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex); UART_transmit(gpibIndex); for (i=0; i<gpibIndex; i++) { UART_transmit(gpibBuf[i]); } if (!listenMode) ReconfigureGPIO_GPIBNormalMode(); } else if ('Z' == command) //hex receive { if (!listenMode) { ReconfigureGPIO_GPIBReceiveMode(); _delay_ms(1); } result = GPIB_Receive_till_eoi(gpibBuf, GPIB_BUF_SIZE-2, &gpibIndex); printf("%02x", gpibIndex); for (i=0; i<gpibIndex; i++) printf("%02x",gpibBuf[i]); printf("\r\n"); if (!listenMode) ReconfigureGPIO_GPIBNormalMode(); } else if ('?' == command) { ShowHelp(); } else if ('E' == command) { if (bufPos == 1) printf("%d\r\n", localEcho); else if ((bufPos==2) && ('0' == buf[1])) { localEcho = 0; printf("OK\r\n"); } else if ((bufPos==2) && ('1' == buf[1])) { localEcho = 1; printf("OK\r\n"); } else printf("ERROR\r\n"); } else if ('H' == command) //show history { for (i=0; i<savedCommands; i++) printf("%d: %s\r\n", i, &commandsHistory[i*BUF_SIZE]); command = 0; //to avoid saving this command in history } else if ('A' == command) //listen address { if (bufPos == 1) printf("%02d\r\n", listenAddress); else if ((bufPos==3) && isdigit(buf[1]) && isdigit(buf[2])) { i = atoi((char*)buf+1); if ((i>=0) && (i<=30)) { listenAddress = i; printf("OK\r\n"); } else printf("ERROR\r\n"); } else printf("ERROR\r\n"); } else if ('Q' == command) { if (bufPos == 1) printf("%d\r\n", msgEndSeq); else if ((2==bufPos) && (('0'==buf[1]) || ('1'==buf[1]) || ('2'==buf[1]) || ('3'==buf[1]))) { if ('0'==buf[1]) msgEndSeq = 0; else if ('1'==buf[1]) msgEndSeq = 1; else if ('2'==buf[1]) msgEndSeq = 2; else if ('3'==buf[1]) msgEndSeq = 3; printf("OK\r\n"); } else printf("ERROR\r\n"); } else if ('T' == command) { if (CheckHexMsg(&buf[1], bufPos-1, msgBuf, &msgLen, &msgEOI)) { if ('D' == toupper(buf[2])) //send data { result = GPIB_Transmit(msgBuf, msgLen, msgEOI); if (result == 255) // transmit ok printf("OK\r\n"); else //timeout printf("TIMEOUT\r\n"); } else //send command { for (i=0; i<msgLen; i++) { if ((msgBuf[i] == '?') || (msgBuf[i] == (64+listenAddress)))//unlisten { listenMode = 0; ledBlinking = OFF; SetLed(1); } else if (buf[i] == (32+listenAddress)) { listenMode = 1; ledBlinking = FAST; } } ReconfigureGPIO_GPIBNormalMode(); SetATN(0); _delay_us(100); result = GPIB_Transmit(msgBuf, msgLen, 1); if (result == 255) // transmit ok printf("OK\r\n"); else //timeout printf("TIMEOUT\r\n"); SetATN(1); if (listenMode) ReconfigureGPIO_GPIBReceiveMode(); else ReconfigureGPIO_GPIBNormalMode(); listenMode_prev = listenMode; } } else printf("ERROR\r\n"); } else { if (bufPos) printf("WRONG COMMAND\r\n"); command = 0; } if (command && bufPos) { buf[bufPos] = 0; //add string termination //avoids saving same command twice if ((savedCommands > 0) && (0 == strcmp(&commandsHistory[(savedCommands-1)*BUF_SIZE], (char*)&buf[0]))) { command = 0; } else //save command { if (savedCommands < MAX_COMMANDS) { memmove(&commandsHistory[savedCommands*BUF_SIZE], &buf[0], BUF_SIZE); ++savedCommands; } else { memmove(&commandsHistory[0], &commandsHistory[BUF_SIZE], BUF_SIZE*(savedCommands-1)); memmove(&commandsHistory[(savedCommands-1)*BUF_SIZE], &buf[0], BUF_SIZE); } } } command = 0; bufPos = 0; cursorPos = 0; buf[0] = 0; } //end of endless loop block }