void COM_txCMD(UINT8 deviceAddress, /*UINT8 cmd,*/ UINT8 *buffer , UINT8 length) { UINT8 cmdPacket[45] = {0}; UINT8 i,j,cs; i = 0; cmdPacket[i++]= CMD_SOP; cmdPacket[i++] = deviceAddress; cmdPacket[i++] = length; // cmdPacket[i++] = cmd; for( j =0; j < length ; j++) { cmdPacket[i+j] = buffer[j]; } i+= j; cs = checksum(&cmdPacket[1], i - 1 ); while((cs == CMD_SOP ) || (cs == CMD_EOP)) //if check sum matches sop or eop { cmdPacket[2]++; // change length cs = checksum(&cmdPacket[1], i - 1 ); //recalculate check sum } cmdPacket[i++] = cs; cmdPacket[i++] = CMD_EOP; for( j = 0 ; j < i ; j++) { UART_write(cmdPacket[j]); } UART_transmit(); }
void COM_task() { UINT8 uartData = 0; #if(defined __18F8722_H) ||(defined __18F46K22_H) if( UART1_hasData() ) { uartData = UART1_read(); UART1_write(uartData); UART1_transmit(); return; } #else if( UART_hasData() ) { uartData = UART_read(); UART_write(uartData); UART_transmit(); return; } #endif }
int main(void) { int read; float input_voltage, temperature; UART_INIT(); // UART 통신 초기화 ADC_INIT(1); // AD 변환기 초기화 while(1) { // 온도 센서의 출력 전압을 ADC를 거쳐 읽는다. read = read_ADC(); // 0에서 1023 사이의 값을 0V에서 5V 사이 값으로 변환한다. input_voltage = read * 5.0 / 1023.0; // 10mV에 1℃이므로 100을 곱해서 현재 온도를 얻는다. temperature = input_voltage * 100.0; UART_print16bitNumber((int)temperature); // 정수값으로 출력 UART_transmit('\n'); // 줄바꿈 _delay_ms(1000); // 1초에 한 번 출력 } }
void COM_txData() { UINT8 bcc = 0; UINT8 i= 0; bcc = checksum(communication.txPacketBuffer, communication.txPacketLength); #if(defined __18F8722_H) ||(defined __18F46K22_H) UART1_write(communication.tx_sop); for( i = 0; i < communication.txPacketLength; i++ ) { UART1_write(communication.txPacketBuffer[i]); } UART1_write(bcc); UART1_write(communication.tx_eop); #ifdef __RESPONSE_ENABLED__ UART_transmit(); #endif #else //(defined __18F8722_H) ||(defined __18F46K22_H) UART_write(communication.tx_sop); for( i = 0; i < communication.txPacketLength; i++ ) { UART_write(communication.txPacketBuffer[i]); } UART_write(bcc); UART_write(communication.tx_eop); #ifdef __RESPONSE_ENABLED__ UART_transmit(); #endif ClrWdt(); #endif }
int main(void) { UART_INIT(); INIT_PORT(); int state =0; while(1){ if(button_pressed()){ //눌러지지 않은 상태애서 눌러진 상태로 바뀔 때마다 '*'전송 if(state == 0) UART_transmit('*'); state = 1; //버튼이 눌러진 상태로 표시 PORTB = 0x20; //LED 켜기 } else { state = 0; //버튼이 눌러지지 않은 상태로 표시 PORTB = 0x00; // LED끄기 } } }
int main(void) { UART_INIT(); // UART 통신 초기화 INIT_PORT(); // 포트 설정 int state = 0; // 현재 버튼의 상태 while(1){ if(PIND & 0x04){ state = 0; // 버튼이 눌러지지 않은 상태로 표시 PORTB = 0x00; // LED 끄기 } else{ // 눌러지지 않은 상태에서 눌러진 상태로 바뀔 때마다 ‘*’ 문자 전송 if(state == 0) UART_transmit('*'); state = 1; // 버튼이 눌러진 상태로 표시 PORTB = 0x20; // LED 켜기 _delay_ms(100); // 디바운싱 } } }
void COM_txStr(rom UINT8 *str) { #if(defined __18F8722_H) ||(defined __18F46K22_H) while(*str) { UART1_write(*str); str++; } UART1_transmit(); #else while(*str) { UART_write(*str); str++; } UART_transmit(); #endif }
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 }
int uart_putchar(char ch, FILE* file) { UART_transmit(ch); return ch; }
int main(void) { InitOsc(); InitPorts(); InitUART0(); InitLCD(); TCPLowLevelInit(); //after TCPLowLevelInit() UCLK = ACLK = MCLK/4 = 2 000 000 Hz UART_transmit (CR); UART_transmit (LF); for (i=0; i!=26; i++) UART_transmit (UART_Message[i]); UART_transmit (CR); UART_transmit (LF); for (i=0; i!=32; i++) { SEND_CHAR(LCD_Message[i]); if (i==15) SEND_CMD (DD_RAM_ADDR2); } SEND_CMD(DD_RAM_ADDR); RX_flag=0; cntr = 0; HTTPStatus = 0; // clear HTTP-server's flag register TCPLocalPort = TCP_PORT_HTTP; // set port we want to listen to while (1) // repeat forever { //--------------buttons scan--------------------------------------------------------- if ((B1) == 0) //B1 is pressed { STATUS_LED_ON; //switch on status_led SEND_CMD(CLR_DISP); SEND_CMD(DD_RAM_ADDR); cntr=0; } else STATUS_LED_OFF; //B1 is released if ((B2) == 0) { // time_out = BUTTON_TIME; // while (time_out != 0) // if ((B2) == 0) time_out--; // else time_out = BUTTON_TIME; Delayx100us(50); RELAY1_ON; } else { // time_out = BUTTON_TIME; // while (time_out != 0) // if ((B2) != 0) time_out--; // else time_out = BUTTON_TIME; Delayx100us(50); RELAY1_OFF; } if ((B3) == 0) { Delayx100us(50); RELAY2_ON; //B3 is pressed } else { Delayx100us(50); RELAY2_OFF; //B3 is released } while ((B4) == 0) //B4 is pressed { BUZ1_OFF; BUZ2_ON; Delay(_100us); Delay(_100us); //buzzer with 5 000 Hz BUZ2_OFF; BUZ1_ON; Delay(_100us); } BUZ1_OFF; //B4 is released BUZ2_OFF; //--------UART0 receiv scan------------------------------------------------------------------ if (RX_flag == 1) //new receiv byte { STATUS_LED_ON; if (cntr == 0) { SEND_CMD(CLR_DISP); SEND_CMD(DD_RAM_ADDR); //set address for first row } SEND_CHAR(RXData); if(cntr == 15) SEND_CMD(DD_RAM_ADDR2); //set address for second row if(cntr++ == 31) cntr = 0; RX_flag = 0; STATUS_LED_OFF; } //---------Digital Inputs scan-------------------------------------------------------------- if ((DI1) == 0) for (i=0 ; i != 5; i++)UART_transmit(DI1_Message[i]); if ((DI2) == 0) for (i=0 ; i != 5; i++)UART_transmit(DI2_Message[i]); if ((DI3) == 0) for (i=0 ; i != 5; i++)UART_transmit(DI3_Message[i]); if ((DI4) == 0) for (i=0 ; i != 5; i++)UART_transmit(DI4_Message[i]); //---------DALLAS scan --------------------------------------------------------------------- if ((DALLAS) == 0) { cntr=0; SEND_CMD(CLR_DISP); SEND_CMD(DD_RAM_ADDR); for (i=0 ; i!= 14; i++) SEND_CHAR(DALLAS_Message[i]); } //---------FREQ scan ---------------------------------------------------------------------- if ((FREQ) != 0) { cntr=0; SEND_CMD(CLR_DISP); SEND_CMD(DD_RAM_ADDR); for (i=0 ; i!= 16; i++) SEND_CHAR(FREQ_Message[i]); } //*********************************************************************************** //this is the end of my programm //*********************************************************************************** if (!(SocketStatus & SOCK_ACTIVE)) { TCPPassiveOpen(); // listen for incoming TCP-connection } DoNetworkStuff(); // handle network and easyWEB-stack // events HTTPServer(); } return 0; }
///* interrupt(UART0RX_VECTOR)usart0_rx (void) { RXData = RXBUF0; UART_transmit (RXData+1); //transmit Echo + 1 RX_flag = 1; //set RX_flag }