#include "stm8s.h" void SerialPutString(char *s); void UART_Menu(void); //************************************************* // MAIN //************************************************* void main(void) { CLK_ClockSwitchConfig ( CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSE, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE ); UART2_DeInit(); // PC setting = 115200bps 8bit NO-parity stop=1bit //UART2_Init((u32)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_NO, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); // PC setting = 115200bps 7bit ODD-parity stop=1bit //UART2_Init((u32)115200, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_ODD, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); // PC setting = NG //UART2_Init((u32)115200, UART2_WORDLENGTH_9D, UART2_STOPBITS_1, UART2_PARITY_NO, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); // PC setting = 115200bps 8bit ODD-parity stop=1bit UART2_Init((u32)115200, UART2_WORDLENGTH_9D, UART2_STOPBITS_1, UART2_PARITY_ODD, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE); UART2_ITConfig(UART2_IT_RXNE_OR, ENABLE); UART2_Cmd(ENABLE); enableInterrupts(); SerialPutString("This is a converter small words to capital words.\r\n"); SerialPutString("key in any words.\r\n"); while(1) // Main loop { UART_Menu(); } } //************************************************* // UART //************************************************* void SerialPutChar(char c){ UART2_SendData8(c); while ((UART2->SR & UART2_SR_TXE ) != UART2_SR_TXE ); } void SerialPutString(char *s){ while (*s != '\0'){ SerialPutChar(*s); s ++; } } // UART RX interrupt routine (interrupted each characters) #define LENGTH 100 u16 bytes_read; u8 UART_STR_EXIST; // command string exist flag char command[LENGTH]; void UART2RX_isr(void) interrupt 21 { if(UART2_GetITStatus(UART2_IT_RXNE)==SET) { char c; c = UART2_ReceiveData8(); if(UART_STR_EXIST) return; else if(c == '\r'){ // CR command[bytes_read] = '\0'; bytes_read = 0; UART_STR_EXIST = 1; SerialPutString("\r\n"); return; } else if(c == '\b'){ // BS if (bytes_read > 0){ SerialPutString("\b \b"); bytes_read--; } return; } else if(bytes_read >= LENGTH ){ SerialPutString("Command too long\r\n"); bytes_read = 0; return; } else if(c >= 0x20 && c <= 0x7E){ command[bytes_read++] = c; SerialPutChar(c); } } } //************************************************* // UART MENU routine //************************************************* void UART_Menu(void) { int i; if(!UART_STR_EXIST) return; SerialPutString(command); SerialPutString(" => "); for(i=0;i=0x41 && command[i]<=0x5a) { command[i]=command[i]+0x20; } // capital -> small else if(command[i]>=0x61 && command[i]<=0x7a) { command[i]=command[i]-0x20; } // small -> capital } RET: // Display Menu on HyperTerminal Window SerialPutString("This is a converter small words to capital words.\r\n"); SerialPutString("key in any words.\r\n"); UART_STR_EXIST = 0; return; } #ifdef USE_FULL_ASSERT void assert_failed(u8* file, u32 line) { while (1) { } } #endif