| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- /*---------------------------------------------------------------------------------------------------------*/
- /* */
- /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved. */
- /* */
- /*---------------------------------------------------------------------------------------------------------*/
- //***********************************************************************************************************
- // Website: http://www.nuvoton.com
- // E-Mail : [email protected]
- // Date : Jan/21/2017
- //***********************************************************************************************************
- #include "Common.h"
- #include "Delay.h"
- //----------------------------------------------------------------------------------
- // UART0 baud rate initial setting
- //----------------------------------------------------------------------------------
- void InitialUART0_Timer1 ( uint32 u32Baudrate ) //T1M = 1, SMOD = 1
- {
- P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- SCON = 0x50; //UART0 Mode1,REN=1,TI=1
- TMOD |= 0x20; //Timer1 Mode1
- set_SMOD; //UART0 Double Rate Enable
- set_T1M;
- clr_BRCK; //Serial port 0 baud rate clock source = Timer1
- #ifdef FOSC_160000
- TH1 = 256 - ( 1000000 / u32Baudrate + 1 ); /*16 MHz */
- #endif
- #ifdef FOSC_166000
- TH1 = 256 - ( 1037500 / u32Baudrate ); /*16.6 MHz */
- #endif
- set_TR1;
- set_TI; //For printf function must setting TI = 1
- }
- //---------------------------------------------------------------
- void InitialUART0_Timer3 ( uint32 u32Baudrate ) //use timer3 as Baudrate generator
- {
- P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- SCON = 0x50; //UART0 Mode1,REN=1,TI=1
- set_SMOD; //UART0 Double Rate Enable
- T3CON &= 0xF8; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
- set_BRCK; //UART0 baud rate clock source = Timer3
- #ifdef FOSC_160000
- RH3 = HIBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
- RL3 = LOBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
- #endif
- #ifdef FOSC_166000
- RH3 = HIBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
- RL3 = LOBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
- #endif
- set_TR3; //Trigger Timer3
- set_TI; //For printf function must setting TI = 1
- }
- uint8 Receive_Data_From_UART0 ( void )
- {
- uint8 c;
- while ( !RI );
- c = SBUF;
- RI = 0;
- return ( c );
- }
- void Send_Data_To_UART0 ( uint8 c )
- {
- TI = 0;
- SBUF = c;
- while ( TI == 0 );
- }
- #ifdef N76E003_IAR
- void printf_UART ( uint8 *str, ... );
- void printInteger ( uint32 u32Temp )
- {
- uint8 print_buf[16];
- uint32 i = 15, j;
- * ( print_buf + i ) = '\0';
- j = u32Temp >> 31;
- if ( j )
- u32Temp = ~u32Temp + 1;
- do
- {
- i--;
- * ( print_buf + i ) = '0' + u32Temp % 10;
- u32Temp = u32Temp / 10;
- }
- while ( u32Temp != 0 );
- if ( j )
- {
- i--;
- * ( print_buf + i ) = '-';
- }
- printf_UART ( print_buf + i );
- }
- void printHex ( uint32 u32Temp )
- {
- uint8 print_buf[16];
- uint32 i = 15;
- uint32 temp;
- * ( print_buf + i ) = '\0';
- do
- {
- i--;
- temp = u32Temp % 16;
- if ( temp < 10 )
- * ( print_buf + i ) = '0' + temp;
- else
- * ( print_buf + i ) = 'a' + ( temp - 10 ) ;
- u32Temp = u32Temp / 16;
- }
- while ( u32Temp != 0 );
- printf_UART ( print_buf + i );
- }
- void printf_UART ( uint8 *str, ... )
- {
- va_list args;
- va_start ( args, str );
- while ( *str != '\0' )
- {
- if ( *str == '%' )
- {
- str++;
- if ( *str == '\0' ) return;
- if ( *str == 'd' )
- {
- str++;
- printInteger ( va_arg ( args, int ) );
- }
- else if ( *str == 'x' )
- {
- str++;
- printHex ( va_arg ( args, int ) );
- }
- }
- Send_Data_To_UART0 ( *str++ );
- }
- }
- #endif
- //----------------------------------------------------------------------------------
- // UART1 baud rate initial setting
- //----------------------------------------------------------------------------------
- void InitialUART1_Timer3 ( uint32 u32Baudrate ) //use timer3 as Baudrate generator
- {
- P02_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- P16_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
- SCON_1 = 0x50; //UART1 Mode1,REN_1=1,TI_1=1
- T3CON = 0x08; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
- clr_BRCK;
- #ifdef FOSC_160000
- RH3 = HIBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
- RL3 = LOBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
- #endif
- #ifdef FOSC_166000
- RH3 = HIBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
- RL3 = LOBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
- #endif
- set_TR3; //Trigger Timer3
- }
- uint8 Receive_Data_From_UART1 ( void )
- {
- uint8 c;
- while ( !RI_1 );
- c = SBUF_1;
- RI_1 = 0;
- return ( c );
- }
- void Send_Data_To_UART1 ( uint8 c )
- {
- TI_1 = 0;
- SBUF_1 = c;
- while ( TI_1 == 0 );
- }
- /* Redefine the putchar function */
- char putchar( UINT8 c)
- {
- Send_Data_To_UART1( c );
- return c;
- }
- void Send_String_To_UART0( UINT8 *string )
- {
- while( *string )
- {
- Send_Data_To_UART0( *string++ );
- }
- }
- /*==========================================================================*/
- #ifdef SW_Reset
- void SW_Reset ( void )
- {
- TA = 0xAA;
- TA = 0x55;
- set_SWRST;
- }
- #endif
- /*==========================================================================*/
|