//****************************************************************************************** // Project : STM8S105C6T6 Sample Program (STM8S-Discovery) // Program Name : // Programer : // Create date : Version 1.0 H24.10.01 // History : // Laungage : STM8Cosmic C Launguage //****************************************************************************************** // Description // USART2 Tx2:PD5, Rx2:PD6 //****************************************************************************************** //#include "iostm8s.h" #include "iostm8s105.h" void InitUART(void); void SendUART(unsigned char); void RecUART(unsigned char *); void SendUARTStr(unsigned char *); int main(void) { unsigned char IO_char; // ******************** // 1.Initialized // ******************** // (1)Define CLK_PCKENR1(Peripheral clock gating register 1) // After a device reset, all peripheral clocks are enabled. Reset value: 0xFF // CLK_PCKENR1 CLK_PCKENR1[3-2]: UART1/2/3 0:Fmaster to peripheral disabled, 1:Fmaster to peripheral enabled // (2)Define GPIO (Rx:PD6 ,Tx:PD5) // For Alternate Function input, you should select floating or pull-up input configuration in the DDR and CR1 registers. // Alternate function outputs provide a direct path from a peripheral to an output or to an I/O pad, // taking precedence over the port bit in the data output latch register (Px_ODR) and forcing the Px_DDR corresponding bit to 1. // An alternate function output can be push-pull or pseudo-open drain depending on the peripheral and // Control register 1 (Px_CR1) and slope can be controlled depending on the Control register 2 (Px_CR2) values. PD_DDR = 0b00100000; PD_CR1 = 0b00100000; // PD_CR2 = 0b00100000; // (3)Initialized UART InitUART(); // (4)Put Message SendUARTStr(" STM8S-Discovery Connected OK> "); // ******************** // 2.Main // ******************** while(1){ RecUART(&IO_char); SendUART(IO_char); } } //****************************************************************************************** // Function Name : InitUART // Title : Initialized UART // Input : // Output : // Description : 9600BPS //****************************************************************************************** void InitUART(void) { // ********************************** // UART_BRR(Baud rate register) : UART_DIV = fMASTER / baud // Example: fMASTER(10MHz), baud(9600BPS) // UART_DIV : 10,000,000 / 9600 => 0d1042 => 0x0412 => [0][41][2] => UART_BRR1[41], UART_BRR2[0][2] // ********************************** // UART_BRR1 // UART_BRR1[7:0] UART_DIV[11:4] These 8 bits define the 2nd and 3rd nibbles of the 16-bit UART divider (UART_DIV). // UART_BRR2 // UART_BRR1[7:4] UART_DIV[15:12] MSB of UART_DIV. These 4 bits define the MSB of the UART Divider (UART_DIV) // UART_BRR1[3:0] UART_DIV[3:0]: LSB of UART_DIV. These 4 bits define the LSB of the UART Divider (UART_DIV) // // fMASTER:16MHz/8, baud:9600BPS // 16,000,000 / 8 / 9600 ==> 0d208 ==> 0x00D0 ==> [0][0D][0] ==> UART_BRR1[0D], UART_BRR2[0][0] UART2_BRR2 = 0x00; UART2_BRR1 = 0x0d; // ********************************** // UART_CR1(Control register 1) // ********************************** // UART_CR1[7] R8: Receive Data bit 8. // This bit is used to store the 9th bit of the received word when M=1 // UART_CR1[6] T8: Transmit data bit 8. // This bit is used to store the 9th bit of the transmitted word when M=1 // UART_CR1[5] UARTD: UART Disable (for low power consumption). // When this bit is set the UART prescaler and outputs are stopped at the end of the current byte // transfer in order to reduce power consumption. This bit is set and cleared by software. // 0: UART enabled // 1: UART prescaler and outputs disabled // UART_CR1[4] M: word length. // This bit determines the word length. It is set or cleared by software. // 0: 1 Start bit, 8 Data bits, n Stop bit (n depending on STOP[1:0] bits in the UART_CR3 register) // 1: 1 Start bit, 9 Data bits, 1 Stop bit // UART_CR1[3] WAKE: Wakeup method. // This bit determines the UART wakeup method, it is set or cleared by software. // 0: Idle Line // 1: Address Mark // UART_CR1[2] PCEN: Parity control enable. // UART Mode // 0: Parity control disabled // 1: Parity control enabled // UART_CR1[1] PS: Parity selection. // This bit selects the odd or even parity when the parity generation/detection is enabled (PCEN bit set) // in UART mode. It is set and cleared by software. The parity will be selected after the current byte. // 0: Even parity // 1: Odd parity // UART_CR1[0] PIEN: Parity interrupt enable. // This bit is set and cleared by software. // 0: Parity interrupt disabled // 1: Parity interrupt is generated whenever PE=1 in the UART_SR register // UART2_CR1 = 0b00000000; // ********************************** // UART_CR2(Control register 2) // ********************************** // UART_CR2[7] TIEN: Transmitter interrupt enable // This bit is set and cleared by software. // 0: Interrupt is inhibited // 1: An UART interrupt is generated whenever TXE=1 in the UART_SR register // UART_CR2[6] TCIEN: Transmission complete interrupt enable // This bit is set and cleared by software. // 0: Interrupt is inhibited // 1: An UART interrupt is generated whenever TC=1 in the UART_SR register // UART_CR2[5] RIEN: Receiver interrupt enable // This bit is set and cleared by software. // 0: Interrupt is inhibited // 1: An UART interrupt is generated whenever OR=1 or RXNE=1 in the UART_SR register // UART_CR2[4] ILIEN: IDLE Line interrupt enable // This bit is set and cleared by software. // 0: Interrupt is inhibited // 1: An UART interrupt is generated whenever IDLE=1 in the UART_SR register // UART_CR2[3] TEN: Transmitter enable // This bit enables the transmitter. It is set and cleared by software. // 0: Transmitter is disabled // 1: Transmitter is enabled // UART_CR2[2] REN: Receiver enable // This bit enables the receiver. It is set and cleared by software. // 0: Receiver is disabled // 1: Receiver is enabled and begins searching for a start bit // UART_CR2[1] RWU: Receiver wakeup // UART Mode // This bit determines if the UART is in mute mode or not. It is set and cleared by software and can be // cleared by hardware when a wakeup sequence is recognized // UART_CR2[0] SBK: Send break // This bit set is used to send break characters. It can be set and cleared by software.It should be set // by software, and will be reset by hardware during the stop bit of break. // 0: No break character is transmitted // 1: Break character will be transmitted UART2_CR2 = 0b00001100; // ********************************** // UART_CR3(Control register 3) // ********************************** // UART2_CR3 = 0b00000000; } //****************************************************************************************** // Function Name : SendUARTStr // Title : Send UART Strings // Input : // Output : // Description : //****************************************************************************************** void SendUARTStr(unsigned char in_s[]) { unsigned char lp = 0; while(in_s[lp] != 0){ SendUART(in_s[lp]); lp++; } } //****************************************************************************************** // Function Name : SendUART // Title : Send UART // Input : // Output : // Description : //****************************************************************************************** void SendUART(unsigned char in_c) { while (!(UART2_SR & 0b10000000)); // USART_SR[7]:TXE Transmit data register empty // 0: Data is not transferred to the shift register, 1: Data is transferred to the shift register UART2_DR = in_c; } //****************************************************************************************** // Function Name : RecUART // Title : Recive UART // Input : // Output : // Description : //****************************************************************************************** void RecUART(unsigned char *out_c) { while (!(UART2_SR & 0b00100000)); // USART_SR[5]:RXNE Read data register not empty // 0: Data is not received, 1: Received data is ready to be read. *out_c = UART2_DR; }