[方案相关] 华大HC32F003.UART例程分享

[复制链接]
1505|18
 楼主| 自动化陈稳 发表于 2020-10-27 23:40 | 显示全部楼层 |阅读模式
HC32F003 系列是一款 Low Pin Count、宽电压工作范围的 MCU。集成 12 位 1M sps 高精度SARADC 以及集成了比较器,多路 UART,SPI,I2C 等丰富的通讯外设,具有高整合度、高抗干扰、高可靠性的特点。HC32F003 内核采用 Cortex-M0+ 内核,配合成熟的 Keil μVision 调试开发软件,支持 C 语言及汇编语言,汇编指令。
 楼主| 自动化陈稳 发表于 2020-10-27 23:40 | 显示全部楼层
适用领域:小家电,充电器,重合闸,遥控器,电子烟,燃气报警器,数显表,温控器,记录仪等行业
 楼主| 自动化陈稳 发表于 2020-10-27 23:41 | 显示全部楼层
华大MCU单片机资料分享连接
HC32F003 HC32F005 HC32L110 HC32L136 HC32F030 HC32M140 HC32F146 HC32L150 HC32L156
作者:虹芯侠客1
来源:CSDN
原文:https://blog.csdn.net/jetson024/article/details/84619088
版权声明:本文为博主原创**,转载请附上博文链接!
 楼主| 自动化陈稳 发表于 2020-10-27 23:42 | 显示全部楼层
样例使用说明
  1. /****************************************************************************/
  2. / \file main.c
  3. **
  4. ** A detailed description is available at
  5. ** [url=home.php?mod=space&uid=48136]@link[/url] Sample Group Some description @endlink
  6. **
  7. ** - 2017-05-17 1.0 cj First version for Device Driver Library of Module.
  8. **
  9. ******************************************************************************/

  10. /******************************************************************************

  11. Include files
  12. ******************************************************************************/
  13. #include “ddl.h”
  14. #include “uart.h”
  15. #include “bt.h”
  16. #include “lpm.h”
  17. #include “gpio.h”
  18. /******************************************************************************

  19. Local pre-processor symbols/macros (’#define’)
  20. ******************************************************************************/
  21. /******************************************************************************

  22. Global variable definitions (declared in header file with ‘extern’)
  23. ******************************************************************************/
  24. /******************************************************************************

  25. Local type definitions (‘typedef’)
  26. ******************************************************************************/
  27. /******************************************************************************

  28. Local function prototypes (‘static’)
  29. ******************************************************************************/
  30. /******************************************************************************

  31. Local variable definitions (‘static’) *
  32. ******************************************************************************/
  33. /******************************************************************************

  34. Local pre-processor symbols/macros (’#define’)
  35. */
  36. #define T1_PORT (3)
  37. #define T1_PIN (3)
  38. /
  39. Function implementation - global (‘extern’) and local (‘static’)
  40. ****************************************************************************/
  41. /
 楼主| 自动化陈稳 发表于 2020-10-27 23:43 | 显示全部楼层
  1. ** \brief Main function of project
  2. **
  3. ** \return uint32_t return value, if needed
  4. **
  5. ** This sample
  6. **
  7. ******************************************************************************/
  8. //uint8_t u8TxData[10] = {0x99,0x88,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x00};//“HUADA MCU!”;
  9. uint8_t u8TxData[10] = {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};
  10. uint32_t u32RxData[10];
  11. uint8_t u8TxCnt=0,u8RxCnt=0;

  12. void TxIntCallback(void)
  13. {
 楼主| 自动化陈稳 发表于 2020-10-27 23:44 | 显示全部楼层
  1. if(u8TxCnt<10)
  2. {
  3.     M0P_UART1->SBUF = u32RxData[u8TxCnt];
  4.     u8TxCnt++;
  5. }
  6. else //if(u8TxCnt>10)
  7. {
  8.     u8TxCnt = 0;
  9.     Uart_ClrStatus(UARTCH1,UartTxEmpty);   
  10.     Uart_DisableIrq(UARTCH1,UartTxIrq);
  11.     Uart_EnableIrq(UARTCH1,UartRxIrq);
  12. }
  13. 1
 楼主| 自动化陈稳 发表于 2020-10-27 23:44 | 显示全部楼层
  1. }
  2. void RxIntCallback(void)
  3. {
  4. u32RxData[u8RxCnt]=Uart_ReceiveData(UARTCH1);
  5. u8RxCnt++;
  6. if(u8RxCnt>=10)
  7. {
  8. Uart_DisableIrq(UARTCH1,UartRxIrq);
  9. Uart_EnableIrq(UARTCH1,UartTxIrq);
  10. u8RxCnt = 0;
  11. M0P_UART1->SBUF = u32RxData[0];
  12. u8TxCnt = 1;
  13. }

  14. }
  15. void ErrIntCallback(void)
  16. {
  17. }
  18. int32_t main(void)
  19. {
  20. uint16_t timer;
  21. uint32_t pclk;
  22. stc_clk_config_t stcCfg;
  23. stc_uart_config_t stcConfig;
  24. stc_uart_irq_cb_t stcUartIrqCb;
  25. stc_uart_multimode_t stcMulti;
  26. stc_uart_baud_config_t stcBaud;
  27. stc_bt_config_t stcBtConfig;
  28. stc_lpm_config_t stcLpmCfg;
 楼主| 自动化陈稳 发表于 2020-10-27 23:45 | 显示全部楼层
  1. en_uart_mmdorck_t enTb8;

  2. DDL_ZERO_STRUCT(stcConfig);
  3. DDL_ZERO_STRUCT(stcUartIrqCb);
  4. DDL_ZERO_STRUCT(stcMulti);
  5. DDL_ZERO_STRUCT(stcBaud);
  6. DDL_ZERO_STRUCT(stcBtConfig);
  7. DDL_ZERO_STRUCT(stcLpmCfg);

  8. stcLpmCfg.enSLEEPDEEP = SlpDpDisable;

  9. //时钟部分配置
  10. stcCfg.enClkSrc = ClkXTH;
  11. stcCfg.enHClkDiv = ClkDiv1;
  12. stcCfg.enPClkDiv = ClkDiv1;
  13. Clk_Init(&stcCfg);

  14. Gpio_InitIO(T1_PORT,T1_PIN,GpioDirIn);
  15. Gpio_InitIO(0,3,GpioDirOut);
  16. Gpio_SetIO(0,3,1);

  17. Gpio_InitIOExt(3,5,GpioDirOut,TRUE,FALSE,FALSE,FALSE);   
  18. Gpio_InitIOExt(3,6,GpioDirOut,TRUE,FALSE,FALSE,FALSE);

  19. //通道端口配置
  20. Gpio_SetFunc_UART1TX_P35();
  21. Gpio_SetFunc_UART1RX_P36();

  22. //外设时钟使能
  23. Clk_SetPeripheralGate(ClkPeripheralBt,TRUE);//模式0/2可以不使能
  24. Clk_SetPeripheralGate(ClkPeripheralUart1,TRUE);



  25. stcUartIrqCb.pfnRxIrqCb = RxIntCallback;
  26. stcUartIrqCb.pfnTxIrqCb = TxIntCallback;
  27. stcUartIrqCb.pfnRxErrIrqCb = ErrIntCallback;
  28. stcConfig.pstcIrqCb = &stcUartIrqCb;
  29. stcConfig.bTouchNvic = TRUE;


  30. stcConfig.enRunMode = UartMode1;//测试项,更改此处来转换4种模式测试


  31. stcMulti.enMulti_mode = UartNormal;//测试项,更改此处来转换多主机模式,mode2/3才有多主机模式
  32. enTb8 = DataOrEven;
  33. Uart_SetMMDOrCk(UARTCH1,enTb8);
  34. stcConfig.pstcMultiMode = &stcMulti;
BettyBerry 发表于 2020-10-27 23:46 | 显示全部楼层
感谢分享 能问一下F003 的具体参数吗
 楼主| 自动化陈稳 发表于 2020-10-27 23:55 | 显示全部楼层
  1. #if 1
  2. stcBaud.bDbaud = 0u;//双倍波特率功能
  3. stcBaud.u32Baud = 2400u;//更新波特率位置
  4. stcBaud.u8Mode = UartMode1; //计算波特率需要模式参数
  5. pclk = Clk_GetPClkFreq();
  6. timer=Uart_SetBaudRate(UARTCH1,pclk,&stcBaud);
 楼主| 自动化陈稳 发表于 2020-10-27 23:56 | 显示全部楼层
  1. stcBtConfig.enMD = BtMode2;
  2. stcBtConfig.enCT = BtTimer;
  3. Bt_Init(TIM1, &stcBtConfig);//调用basetimer1设置函数产生波特率
  4. Bt_ARRSet(TIM1,timer);
  5. Bt_Cnt16Set(TIM1,timer);
  6. Bt_Run(TIM1);
 楼主| 自动化陈稳 发表于 2020-10-27 23:57 | 显示全部楼层
#endif
Uart_Init(UARTCH1, &stcConfig);
Uart_EnableIrq(UARTCH1,UartRxIrq);
Uart_ClrStatus(UARTCH1,UartRxFull);
Uart_EnableFunc(UARTCH1,UartRx);
#if 0 //休眠模式测试
while(1 == Gpio_GetIO(T1_PORT,T1_PIN));//注意:如果用户修改进入深度休眠的话,此处不能屏蔽
Lpm_Config(&stcLpmCfg);
Lpm_GotoLpmMode();
Gpio_SetIO(0,3,0);
#endif
while(1);
}

/******************************************************************************

EOF (not truncated)
******************************************************************************/
yanruiqi 发表于 2020-10-28 21:04 | 显示全部楼层
提醒:HC32F0 这片子的UART  不支持 7位数据位!
凯复Kane 发表于 2020-10-28 23:04 | 显示全部楼层
谢谢楼上提醒 我还没认真看呢 不过F003 是个不错的板子
BettyBerry 发表于 2020-10-28 23:29 | 显示全部楼层
感谢分享 FC32F003 UART不错的板子
慢动作 发表于 2020-10-31 22:48 | 显示全部楼层
yanruiqi 发表于 2020-10-28 21:04
提醒:HC32F0 这片子的UART  不支持 7位数据位!

为什么?
慢动作 发表于 2020-10-31 22:48 | 显示全部楼层
好吧。手册说不能,就是不能,没有理由的。
慢动作 发表于 2020-10-31 22:49 | 显示全部楼层
F003 确实是个不错的板子
rickluo 发表于 2020-11-1 10:19 | 显示全部楼层
感谢楼主分享!

IMG_8496.jpg

您需要登录后才可以回帖 登录 | 注册

本版积分规则

106

主题

1380

帖子

1

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