[技术问答] N76E003如何用串口发字符串?

[复制链接]
 楼主| lansefeiyang 发表于 2018-1-24 16:54 | 显示全部楼层 |阅读模式
请问如何用串口发字符串?
huangcunxiake 发表于 2018-1-24 18:40 | 显示全部楼层
发送数字是:
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 UART-0 Mode1 demo code
  14. //***********************************************************************************************************
  15. #include "N76E003.h"
  16. #include "Common.h"
  17. #include "Delay.h"
  18. #include "SFR_Macro.h"
  19. #include "Function_define.h"

  20. /******************************************************************************
  21. * FUNCTION_PURPOSE: Serial interrupt, echo received data.
  22. * FUNCTION_INPUTS : P0.7(RXD) serial input
  23. * FUNCTION_OUTPUTS: P0.6(TXD) serial output
  24. * Following setting in Common.c
  25. ******************************************************************************/
  26. #if 0
  27. //void InitialUART0_Timer1(UINT32 u32Baudrate)    //T1M = 1, SMOD = 1
  28. //{
  29. //                P06_Quasi_Mode;               
  30. //                P07_Quasi_Mode;
  31. //       
  32. //    SCON = 0x52;     //UART0 Mode1,REN=1,TI=1
  33. //    TMOD |= 0x20;    //Timer1 Mode1
  34. //   
  35. //    set_SMOD;        //UART0 Double Rate Enable
  36. //    set_T1M;
  37. //    clr_BRCK;        //Serial port 0 baud rate clock source = Timer1

  38. //
  39. //#ifdef FOSC_160000
  40. //    TH1 = 256 - (1000000/u32Baudrate+1);               /*16 MHz */
  41. //#endif           
  42. //    set_TR1;
  43. //}
  44. ////---------------------------------------------------------------
  45. //void InitialUART0_Timer3(UINT32 u32Baudrate) //use timer3 as Baudrate generator
  46. //{
  47. //                P06_Quasi_Mode;
  48. //                P07_Quasi_Mode;       
  49. //       
  50. //    SCON = 0x52;     //UART0 Mode1,REN=1,TI=1
  51. //    set_SMOD;        //UART0 Double Rate Enable
  52. //    T3CON &= 0xF8;   //T3PS2=0,T3PS1=0,T3PS0=0(Prescale=1)
  53. //    set_BRCK;        //UART0 baud rate clock source = Timer3

  54. //#ifdef FOSC_160000
  55. //        RH3    = HIBYTE(65536 - (1000000/u32Baudrate)-1);                  /*16 MHz */
  56. //  RL3    = LOBYTE(65536 - (1000000/u32Baudrate)-1);                        /*16 MHz */
  57. //#endif
  58. //    set_TR3;         //Trigger Timer3
  59. //}
  60. #endif

  61. /*******************************************************************************
  62. * FUNCTION_PURPOSE: Main function
  63. ******************************************************************************/
  64. void main (void)
  65. {

  66. #if 0       
  67.     InitialUART0_Timer1(9600);           //UART0 Baudrate initial,T1M=0,SMOD=0
  68.     while(1)
  69.     Send_Data_To_UART0(0x55);
  70. #else
  71.                 InitialUART0_Timer3(115200);
  72.     while(1)
  73.     Send_Data_To_UART0(0x55);
  74. #endif
  75.                        
  76.        
  77.                
  78. }
  79.   
huangcunxiake 发表于 2018-1-24 18:42 | 显示全部楼层
字符串用printf函数

  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2017 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /*                                                                                                         */
  5. /*---------------------------------------------------------------------------------------------------------*/

  6. //***********************************************************************************************************
  7. //  Nuvoton Technoledge Corp.
  8. //  Website: http://www.nuvoton.com
  9. //  E-Mail : MicroC-8bit@nuvoton.com
  10. //  Date   : Apr/21/2017
  11. //***********************************************************************************************************

  12. //***********************************************************************************************************
  13. //  File Function: N76E003 ADC demo code
  14. //***********************************************************************************************************

  15. #include "N76E003.h"
  16. #include "SFR_Macro.h"
  17. #include "Function_define.h"
  18. #include "Common.h"
  19. #include "Delay.h"


  20. //*****************  The Following is in define in Fucntion_define.h  ***************************
  21. //****** Always include Function_define.h call the define you want, detail see main(void) *******
  22. //***********************************************************************************************
  23. #if 0
  24. //#define Enable_ADC_AIN0                        ADCCON0&=0xF0;P17_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT0;ADCCON1|=SET_BIT0                                                                        //P17
  25. //#define Enable_ADC_AIN1                        ADCCON0&=0xF0;ADCCON0|=0x01;P30_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT1;ADCCON1|=SET_BIT0                //P30
  26. //#define Enable_ADC_AIN2                        ADCCON0&=0xF0;ADCCON0|=0x02;P07_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT2;ADCCON1|=SET_BIT0                //P07
  27. //#define Enable_ADC_AIN3                        ADCCON0&=0xF0;ADCCON0|=0x03;P06_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT3;ADCCON1|=SET_BIT0                //P06
  28. //#define Enable_ADC_AIN4                        ADCCON0&=0xF0;ADCCON0|=0x04;P05_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT4;ADCCON1|=SET_BIT0                //P05
  29. //#define Enable_ADC_AIN5                        ADCCON0&=0xF0;ADCCON0|=0x05;P04_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT5;ADCCON1|=SET_BIT0                //P04
  30. //#define Enable_ADC_AIN6                        ADCCON0&=0xF0;ADCCON0|=0x06;P03_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT6;ADCCON1|=SET_BIT0                //P03
  31. //#define Enable_ADC_AIN7                        ADCCON0&=0xF0;ADCCON0|=0x07;P11_Input_Mode;AINDIDS=0x00;AINDIDS|=SET_BIT7;ADCCON1|=SET_BIT0                //P11
  32. //#define Enable_ADC_BandGap        ADCCON0|=SET_BIT3;ADCCON0&=0xF8;                                                                                                                                                                                                                                                        //Band-gap 1.22V

  33. //#define PWM0_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  34. //#define PWM2_FALLINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  35. //#define PWM4_FALLINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1&=~SET_BIT2;ADCCON1|=SET_BIT1
  36. //#define PWM0_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  37. //#define PWM2_RISINGEDGE_TRIG_ADC                ADCCON0&=~SET_BIT5;ADCCON0|=SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1
  38. //#define PWM4_RISINGEDGE_TRIG_ADC                ADCCON0|=SET_BIT5;ADCCON0&=~SET_BIT4;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1

  39. //#define P04_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  40. //#define P13_FALLINGEDGE_TRIG_ADC                ADCCON0|=0x30;ADCCON1&=0xF3;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  41. //#define P04_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1&=~SET_BIT6
  42. //#define P13_RISINGEDGE_TRIG_ADC                        ADCCON0|=0x30;ADCCON1&=~SET_BIT3;ADCCON1|=SET_BIT2;ADCCON1|=SET_BIT1;ADCCON1|=SET_BIT6
  43. #endif

  44. /******************************************************************************
  45. The main C function.  Program execution starts
  46. here after stack initialization.
  47. ******************************************************************************/
  48. void main (void)
  49. {
  50.                 InitialUART0_Timer1(115200);

  51.                 Enable_ADC_AIN3;                                                // Enable AIN0 P1.7 as ADC input, Find in "Function_define.h" - "ADC INIT"
  52.                 while(1)
  53.     {
  54.                         clr_ADCF;
  55.                         set_ADCS;                                                                        // ADC start trig signal
  56.       while(ADCF == 0);
  57.                         printf ("\n Value = 0x%bx",ADCRH);
  58.                         printf ("\n Value = 0x%bx",ADCRL);
  59.                         Timer0_Delay1ms(100);
  60.     }
  61. }


huangcunxiake 发表于 2018-1-24 18:42 | 显示全部楼层
下载这个文件
N76E003_BSP_Keil_C51_V1.0.4
去官网下载。
 楼主| lansefeiyang 发表于 2018-1-25 14:03 | 显示全部楼层

我想知道发送字符串的
 楼主| lansefeiyang 发表于 2018-1-25 14:19 | 显示全部楼层
huangcunxiake 发表于 2018-1-24 18:42
下载这个文件
N76E003_BSP_Keil_C51_V1.0.4
去官网下载。

我有,这里是字符的,和字符串应该不一样
heisexingqisi 发表于 2018-1-26 10:12 | 显示全部楼层
发送一个数字都行,你直接添加个string发送的函数调用这个发送数字的不就行了吗
heisexingqisi 发表于 2018-1-26 10:15 | 显示全部楼层
Send_String_to_UART0(string a)
{

while(*a!=''\0'')
Send_Data_To_UART0(a++);

}


差不多就是这么写的。
 楼主| lansefeiyang 发表于 2018-2-2 15:14 | 显示全部楼层
heisexingqisi 发表于 2018-1-26 10:15
Send_String_to_UART0(string a)
{

这个不是003的代码吧,003没看到有字符串类型。
我用电脑串口软件发送字符串可以控制模组,但是用003发送就失败,基本可以确定是字符串的问题。
gugogol 发表于 2018-3-5 15:07 | 显示全部楼层
/*******************************************************************************
**void Uart_SendString(u8 *data , u8 strlen)
**发送多个字符
**入口:u8 *data   ,  u8 strlen
**返回:无
*******************************************************************************/
void Uart_SendString(uint8_t *datas , uint8_t strlen)
{
  unsigned char datalen;
        InitialUART0_Timer1(115200);
  for(datalen = 0 ; datalen < strlen ; datalen++)
  {
    Send_Data_To_UART0(datas[datalen]);
  }
}


void Send_Data_To_UART0 (UINT8 c)
{
    TI = 0;
    SBUF = c;
    while(TI==0);
}
gugogol 发表于 2018-3-5 15:11 | 显示全部楼层
如果要发送:datasheet,则Uart_SendString("datasheet" ,9);
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

16

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部