bf87ec3f8167afdea85660614048b258563d63a9.svn-base 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /* */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved. */
  4. /* */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. //***********************************************************************************************************
  7. // Website: http://www.nuvoton.com
  8. // E-Mail : [email protected]
  9. // Date : Jan/21/2017
  10. //***********************************************************************************************************
  11. #include "Common.h"
  12. #include "Delay.h"
  13. //----------------------------------------------------------------------------------
  14. // UART0 baud rate initial setting
  15. //----------------------------------------------------------------------------------
  16. void InitialUART0_Timer1 ( uint32 u32Baudrate ) //T1M = 1, SMOD = 1
  17. {
  18. P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  19. P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  20. SCON = 0x50; //UART0 Mode1,REN=1,TI=1
  21. TMOD |= 0x20; //Timer1 Mode1
  22. set_SMOD; //UART0 Double Rate Enable
  23. set_T1M;
  24. clr_BRCK; //Serial port 0 baud rate clock source = Timer1
  25. #ifdef FOSC_160000
  26. TH1 = 256 - ( 1000000 / u32Baudrate + 1 ); /*16 MHz */
  27. #endif
  28. #ifdef FOSC_166000
  29. TH1 = 256 - ( 1037500 / u32Baudrate ); /*16.6 MHz */
  30. #endif
  31. set_TR1;
  32. set_TI; //For printf function must setting TI = 1
  33. }
  34. //---------------------------------------------------------------
  35. void InitialUART0_Timer3 ( uint32 u32Baudrate ) //use timer3 as Baudrate generator
  36. {
  37. P06_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  38. P07_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  39. SCON = 0x50; //UART0 Mode1,REN=1,TI=1
  40. set_SMOD; //UART0 Double Rate Enable
  41. T3CON &= 0xF8; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
  42. set_BRCK; //UART0 baud rate clock source = Timer3
  43. #ifdef FOSC_160000
  44. RH3 = HIBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
  45. RL3 = LOBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
  46. #endif
  47. #ifdef FOSC_166000
  48. RH3 = HIBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
  49. RL3 = LOBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
  50. #endif
  51. set_TR3; //Trigger Timer3
  52. set_TI; //For printf function must setting TI = 1
  53. }
  54. uint8 Receive_Data_From_UART0 ( void )
  55. {
  56. uint8 c;
  57. while ( !RI );
  58. c = SBUF;
  59. RI = 0;
  60. return ( c );
  61. }
  62. void Send_Data_To_UART0 ( uint8 c )
  63. {
  64. TI = 0;
  65. SBUF = c;
  66. while ( TI == 0 );
  67. }
  68. #ifdef N76E003_IAR
  69. void printf_UART ( uint8 *str, ... );
  70. void printInteger ( uint32 u32Temp )
  71. {
  72. uint8 print_buf[16];
  73. uint32 i = 15, j;
  74. * ( print_buf + i ) = '\0';
  75. j = u32Temp >> 31;
  76. if ( j )
  77. u32Temp = ~u32Temp + 1;
  78. do
  79. {
  80. i--;
  81. * ( print_buf + i ) = '0' + u32Temp % 10;
  82. u32Temp = u32Temp / 10;
  83. }
  84. while ( u32Temp != 0 );
  85. if ( j )
  86. {
  87. i--;
  88. * ( print_buf + i ) = '-';
  89. }
  90. printf_UART ( print_buf + i );
  91. }
  92. void printHex ( uint32 u32Temp )
  93. {
  94. uint8 print_buf[16];
  95. uint32 i = 15;
  96. uint32 temp;
  97. * ( print_buf + i ) = '\0';
  98. do
  99. {
  100. i--;
  101. temp = u32Temp % 16;
  102. if ( temp < 10 )
  103. * ( print_buf + i ) = '0' + temp;
  104. else
  105. * ( print_buf + i ) = 'a' + ( temp - 10 ) ;
  106. u32Temp = u32Temp / 16;
  107. }
  108. while ( u32Temp != 0 );
  109. printf_UART ( print_buf + i );
  110. }
  111. void printf_UART ( uint8 *str, ... )
  112. {
  113. va_list args;
  114. va_start ( args, str );
  115. while ( *str != '\0' )
  116. {
  117. if ( *str == '%' )
  118. {
  119. str++;
  120. if ( *str == '\0' ) return;
  121. if ( *str == 'd' )
  122. {
  123. str++;
  124. printInteger ( va_arg ( args, int ) );
  125. }
  126. else if ( *str == 'x' )
  127. {
  128. str++;
  129. printHex ( va_arg ( args, int ) );
  130. }
  131. }
  132. Send_Data_To_UART0 ( *str++ );
  133. }
  134. }
  135. #endif
  136. //----------------------------------------------------------------------------------
  137. // UART1 baud rate initial setting
  138. //----------------------------------------------------------------------------------
  139. void InitialUART1_Timer3 ( uint32 u32Baudrate ) //use timer3 as Baudrate generator
  140. {
  141. P02_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  142. P16_Quasi_Mode; //Setting UART pin as Quasi mode for transmit
  143. SCON_1 = 0x50; //UART1 Mode1,REN_1=1,TI_1=1
  144. T3CON = 0x08; //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1), UART1 in MODE 1
  145. clr_BRCK;
  146. #ifdef FOSC_160000
  147. RH3 = HIBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
  148. RL3 = LOBYTE ( 65536 - ( 1000000 / u32Baudrate ) - 1 ); /*16 MHz */
  149. #endif
  150. #ifdef FOSC_166000
  151. RH3 = HIBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
  152. RL3 = LOBYTE ( 65536 - ( 1037500 / u32Baudrate ) ); /*16.6 MHz */
  153. #endif
  154. set_TR3; //Trigger Timer3
  155. }
  156. uint8 Receive_Data_From_UART1 ( void )
  157. {
  158. uint8 c;
  159. while ( !RI_1 );
  160. c = SBUF_1;
  161. RI_1 = 0;
  162. return ( c );
  163. }
  164. void Send_Data_To_UART1 ( uint8 c )
  165. {
  166. TI_1 = 0;
  167. SBUF_1 = c;
  168. while ( TI_1 == 0 );
  169. }
  170. /* Redefine the putchar function */
  171. char putchar( UINT8 c)
  172. {
  173. Send_Data_To_UART1( c );
  174. return c;
  175. }
  176. void Send_String_To_UART0( UINT8 *string )
  177. {
  178. while( *string )
  179. {
  180. Send_Data_To_UART0( *string++ );
  181. }
  182. }
  183. /*==========================================================================*/
  184. #ifdef SW_Reset
  185. void SW_Reset ( void )
  186. {
  187. TA = 0xAA;
  188. TA = 0x55;
  189. set_SWRST;
  190. }
  191. #endif
  192. /*==========================================================================*/