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초에 한 번 출력 } }
int main(void) { stdout = &mystdout; general_init(); UART_INIT(); PWM_Init(); ADC_Init(); _delay_ms(250); Interrupt_Init(); //beep(); printf("Initialization Complete\n"); for(;;){ if(Serial_Finish){ serialDecodify(); actuateMotors(); //sonarRead(); //if( (sonarReading0 < 1000) | (sonarReading1 < 1000) | (sonarReading2 < 1000)) sonarReading0 = sonarReading1 = sonarReading2 = 1000; //printf("%u%u%u", sonarReading0, sonarReading1, sonarReading2); //might get an error here because needs interrupts to send data through UART ledCounter--; if(!ledCounter){ toggle_leds(); ledCounter = 5; } } } }
/****************************************************************************** * @fn uart_init * * @brief Configures UART and sets up transmit and receive interrupts * * input parameters * * output parameters * * @return */ void uart_init( void ) { volatile unsigned int i; /* make sure the handler functions are cleared in case we are re-initialized */ uart1_tx_handler = NULL; uart0_tx_handler = NULL; uart_rx_handler = NULL; /* initialize the uart interface for operations */ UART_INIT( UART_NUMBER_1, UART_LOCATION_2, UART_FLOW_CONTROL, /* enable/disable flow control */ UART_PARITY_MODE, /* enable/disable parity */ UART_STOP_BITS, /* number of stop bits */ UART1_BAUD_RATE ); /* baud rate to use */ i = UART1_BAUD_RATE >> 5; /* delay approximately 1 bit time */ while( --i != 0 ) /* give the uart some time to initialize */ ; /* null statement */ /* set the interrupt flag so that a transmit interrupt will be pending * that way when a message is sent and the irq is enabled, the interrupt * will happen immediately to start the transmission */ UART_IRQ_FLAG_SET( UART_NUMBER_1, UART1_LOCATION, TX ); /* set the interrupt */ /* enable receive interrupts, they are always welcome. */ UART_IRQ_ENABLE( UART_NUMBER_1, UART1_LOCATION, RX ); /* initialize the uart interface for operations */ UART_INIT( UART_NUMBER_0, UART_LOCATION_2, UART_FLOW_CONTROL, /* enable/disable flow control */ UART_PARITY_MODE, /* enable/disable parity */ UART_STOP_BITS, /* number of stop bits */ UART0_BAUD_RATE ); /* baud rate to use */ UART_IRQ_FLAG_SET( UART_NUMBER_0, UART0_LOCATION, TX ); /* set the interrupt */ UART_IRQ_DISABLE(UART_NUMBER_0,UART_LOCATION_2,RX); return; }
int main(void) { pll_init_128M(); UART_INIT(0,128000000,115200,0); Init_OV7620_DMA(); EnableInterrupts; while(1) { } }
void _jn516_custom_exception_stack_overflow(uint32* pu32Stack, int type) { unsigned char TX_BUFFER[100]; LED_INIT(); UART_INIT(TX_BUFFER); while (TRUE) { SOS(); BLINK(7); vDebug("Stack Overflow\n"); PRINT_ADDRESSES(pu32Stack); LONG_OFF(); } }
void _jn516_custom_exception_illegal_instruction(uint32* pu32Stack, int type) { unsigned char TX_BUFFER[100]; LED_INIT(); UART_INIT(TX_BUFFER); while (TRUE) { SOS(); BLINK(3); vDebug("Illegal Instruction\n"); PRINT_ADDRESSES(pu32Stack); LONG_OFF(); } }
void _jn516_custom_exception_unaligned_access(uint32* pu32Stack, int type) { unsigned char TX_BUFFER[100]; LED_INIT(); UART_INIT(TX_BUFFER); while (TRUE) { SOS(); BLINK(2); vDebug("Unaligned Access\n"); PRINT_ADDRESSES(pu32Stack); LONG_OFF(); } }
void _jn516_custom_exception_bus_error(uint32* pu32Stack, int type) { unsigned char TX_BUFFER[100]; LED_INIT(); UART_INIT(TX_BUFFER); while (TRUE) { SOS(); BLINK(1); vDebug("Bus Error\n"); PRINT_ADDRESSES(pu32Stack); LONG_OFF(); } }
int main(int argc, char *argv[]) { unsigned char data; stdout = &OUTPUT; stdin = &INPUT; UART_INIT(); // UART 통신 초기화 while (1) { scanf("%c", &data); // 데이터 수신 printf("You typed %c", data); // 수신된 문자를 에코 백 } return 0; }
int main(void) { int read, actualVCC; ADC_INIT(0x0E); // 15번 채널 선택 UART_INIT(); // UART 통신 초기화 while(1) { read = read_ADC(); // 15번 채널 값 읽기 actualVCC = 1125300L / read; // mV 단위로 변환 UART_print16bitNumber(actualVCC); // AVCC 값 출력 UART_printString("\n"); // 줄 바꿈 _delay_ms(1000); // 1초에 한 번 읽음 } }
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) { sbi(DDRC, 6); sbi(DDRC, 7); sbi(PORTD, 6); sei(); UART_INIT(UART_BAUD_RATE); TCCR1A = _BV(WGM10) | _BV(COM1A1) | _BV(COM1B1); // enable 8 bit PWM, select inverted PWM TCCR1B = _BV(CS11) | _BV(WGM12); OCR1A = 0; OCR1B = 0; sbi(PORTC,2); sbi(PORTC,3); sbi(DDRD,4); sbi(DDRD,5); sbi(DDRC,2); sbi(DDRC,3); char b = 0, l = 0, r = 0; for (;;) // Loop forever { while(b != 'w') b = UART_GETCHAR(); if(b == 'w') { UART_PUTCHAR(b); while((l < '1') || (l > '9')) l = UART_GETCHAR(); UART_PUTCHAR(l); while((r < '1') || (r > '9')) r = UART_GETCHAR(); UART_PUTCHAR(r); cbi(PORTC, 2); cbi(PORTC, 3); OCR1A = (int) (10-l+'0')*12.8; OCR1B = (int) (10-r+'0')*12.8; b = l = r = 0; } } }
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); // 디바운싱 } } }
int sysu_init(void) { sysu_done(); // Disable all QUEBUF_INIT(RXB); QUEBUF_INIT(TXB); U_(UART_USED, rxerr) = 0; UART_INIT(UART_USED, // Try to initialize UART #if (defined(__MPLAB_SIM) && (UART_USED == 2)) /* =!= It works with UART2 too if set LPBACK here */ U_LPBACK | #endif // SIM supports UART1 only (SIM: UART1 IO must be enabled) U_NOPARITY | UART_EN, // 8-bit, no parity; Enabled U_TXI_READY | U_RXI_ANY | // Defaul event settings U_TXEN, FCY2BRG(FCY2, 9600), // TX Enabled; 9600 baud UART_IPL // All interrupts are enabled ); // UART_INIT() return( sysu_is_init() ); }
// user interface functions void uart_init( void ) { volatile unsigned int i; bspIState_t istate; BSP_ENTER_CRITICAL_SECTION( istate ); // disable the receive and transmit interrupts for the moment UART_IRQ_DISABLE( UART_NUMBER, UART_LOCATION, TX ); UART_IRQ_DISABLE( UART_NUMBER, UART_LOCATION, RX ); BSP_EXIT_CRITICAL_SECTION( istate ); // make sure the handler functions are cleared in case we are re-initialized uart_tx_handler = NULL; uart_rx_handler = NULL; // clear transmit suspend semaphore uart_tx_suspend = false; // initialize the uart interface for operations UART_INIT( UART_NUMBER, UART_LOCATION, UART_FLOW_CONTROL, // enable/disable flow control UART_PARITY_MODE, // enable/disable parity UART_STOP_BITS, // number of stop bits UART_BAUD_RATE ); // baud rate to use i = UART_BAUD_RATE >> 5; // delay approximately 1 bit time while( --i != 0 ) // give the uart some time to initialize ; // null statement // enable receive interrupts, they are always welcome. UART_IRQ_ENABLE( UART_NUMBER, UART_LOCATION, RX ); return; }
//------------------------------------------------------------------------------------ // MAIN Routine //------------------------------------------------------------------------------------ void main(void) { char input; char SFRPAGE_SAVE = SFRPAGE; WDTCN = 0xDE; // Disable the watchdog timer WDTCN = 0xAD; // Note: = "DEAD"! SYSCLK_INIT(); // Initialize the oscillator Timer_Init(); // Initialize timer UART_INIT(); // Initialize UARTs PORT_INIT(); // Initialize the Crossbar and GPIO SPI_Init(); SFRPAGE = UART0_PAGE; // Direct output to UART0 printf("\033[2J"); //clear screen printf("\033[2J"); //clear screen printf("\033[13;0H"); //print divider printf("--------------------------------------------------------------------------------"); //printf("\033[1;12r"); //define scrollable area //printf("\033[14;25r"); while(1) { if (RI0 == 1) { RI0 = 0; input = SBUF0; // If input from UART0, read SBUF0 if (input == 0x7F){ sendChars(); } else{ if (input == 'a') //228 { printf("i am a\n\r"); SPI0CKR += 5; printf("clock: %d\n\r", SPI0CKR); } else if (input == 'z') { SPI0CKR -= 5; printf("clock: %d\n\r", SPI0CKR); } //SFRPAGE_SAVE = SFRPAGE; //SFRPAGE = SPI0_PAGE; NSSMD0 = 0; //slave select SPIF = 0; //clear SPIF SPI0DAT = input; //send input while (!SPIF); //wait until sent NSSMD0 = 1; //release slave writeTop(input); //write to UART0 RI0 = 0; //Clear input flag overflows = 0; //wait while(overflows < 30000); NSSMD0 = 0; //slave select while (!SPIF); //wait until not busy SPIF = 0; //busy SPI0DAT = input; //write dummy character while (!SPIF); //wait until transfer is over NSSMD0 = 1; //release slave input = SPI0DAT; //read SPI0DAT writeBot(input); //SFRPAGE = SFRPAGE_SAVE; } } } }