6e8e3b8a780887ec65610ff44629f8512fe3bdc3.svn-base 5.6 KB

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