[DemoCode下载] 【新唐资料分享月】NUC100工程模板

[复制链接]
2624|14
 楼主| wujianwei3980 发表于 2016-12-21 10:10 | 显示全部楼层 |阅读模式
NUC100工程模板, 包含ADC, PWM, Capture,TIMER, 读写Dataflash功能

NUC100_Template.zip

90.06 KB, 下载次数: 99

heisexingqisi 发表于 2016-12-21 18:03 | 显示全部楼层
  1. /*---------------------------------------------------------------------------------------------------------*/
  2. /*                                                                                                         */
  3. /* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved.                                         */
  4. /* Writed by Nuvoton(SH) CF10.   2013/02/25                                                                                                        */
  5. /*---------------------------------------------------------------------------------------------------------*/
  6. #include <stdio.h>
  7. #include "NUC1xx.h"
  8. #include "DrvGPIO.h"
  9. #include "DrvFMC.h"
  10. #include "DrvSYS.h"
  11. #include "DrvPWM.h"
  12. #include "DrvADC.h"
  13. #include "DrvTIMER.h"
  14. /*---------------------------------------------------------------------------------------------------------*/
  15. /* Define functions prototype                                                                              */
  16. /*---------------------------------------------------------------------------------------------------------*/

  17. void TMR0_Callback(uint32_t u32Param)
  18. {       
  19.         GPIOC->DOUT ^= (1 << 3);          //定时器中断取反IO口   
  20. }


  21. void CalPeriodTime(uint8_t u8Capture)
  22. {
  23.     uint16_t u32Count[4];
  24.     uint32_t u32i;
  25.     uint16_t u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid;
  26.    
  27.     /* Clear the Capture Interrupt Flag  */
  28.     DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);
  29.    
  30.     /* Wait for Interrupt Flag  (Falling) */
  31.     while (DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG) != 1);
  32.    
  33.     /* Clear the Capture Interrupt Flag */
  34.     DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);
  35.    
  36.     u32i = 0;
  37.    
  38.     while (u32i < 4)
  39.     {
  40.         /* Wait for Interrupt Flag (Falling) */
  41.         while(DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG) != 1);

  42.         /* Clear the Capture Interrupt Flag */
  43.         DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_FALLING_FLAG);

  44.         /* Clear the Capture Rising Interrupt Flag */
  45.         DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG);
  46.                
  47.         /* Get the Falling Counter Data */
  48.         u32Count[u32i++] = DrvPWM_GetFallingCounter(u8Capture);
  49.         
  50.         /* Wait for Capture Rising Interrupt Flag */
  51.         while(DrvPWM_GetCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG) != 1);
  52.         
  53.         /* Clear the Capture Rising Interrupt Flag */
  54.         DrvPWM_ClearCaptureIntStatus(u8Capture, DRVPWM_CAP_RISING_FLAG);
  55.         
  56.         /* Get the Rising Counter Data */
  57.         u32Count[u32i++] = DrvPWM_GetRisingCounter(u8Capture);
  58.     }   
  59.     //因为使能了下降沿中断,所以下降沿会复位计数器的值,上升沿不会复位计数器
  60.     u16RisingTime = u32Count[1];   //检测到上升沿时计数器的值  
  61.    
  62.     u16FallingTime = u32Count[0];  //检测到下降沿时计数器的值   
  63.    
  64.     u16HighPeroid = u32Count[1] - u32Count[2]; //高电平的时间
  65.    
  66.     u16LowPeroid = 0x10000 - u32Count[1];      //低电平的时间
  67.    
  68.     u16TotalPeroid = 0x10000 - u32Count[2];    //周期时间
  69.         
  70.     printf("Capture Test Result:\nRising Time = %d, Falling Time = %d.\nHigh Period = %d, Low  Period = %d, Total Period = %d.\n\n",
  71.         u16RisingTime, u16FallingTime, u16HighPeroid, u16LowPeroid, u16TotalPeroid);
  72. }


  73. void Test_TIMER(void)
  74. {       
  75.     DrvTIMER_Init();
  76.                 DrvTIMER_Open(E_TMR0, 4, E_PERIODIC_MODE);  //配置定时器为周期模式
  77.                 DrvTIMER_SetTimerEvent(E_TMR0, 2, (TIMER_CALLBACK)TMR0_Callback, 1);//配置每秒中断两次
  78.                 DrvTIMER_EnableInt(E_TMR0);    //使能定时器中断
  79.                 DrvTIMER_Start(E_TMR0);         
  80. }       

  81. void Test_PWM_CAP(void)     //需要PWM0和PWM3连在一起
  82. {       
  83.           uint8_t u8Timer, u8CapTimer;
  84.           S_DRVPWM_TIME_DATA_T sPt;
  85.        
  86.         /* Enable PWM clock */
  87.     DrvPWM_Open();  
  88.    
  89.     /* Set PWM pins */
  90.     DrvGPIO_InitFunction(E_FUNC_PWM01);
  91.                 DrvGPIO_InitFunction(E_FUNC_PWM23);

  92.     /* PWM Timer property */
  93.     sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;  //配置为自动重载初值模式
  94.     sPt.u32Frequency = 1000;     //PWM0输出频率1K
  95.     sPt.u8HighPulseRatio = 30;   //占空比30%
  96.     sPt.i32Inverter = 0;         //输出不反向  
  97.     u8Timer = DRVPWM_TIMER0;     //PWM0
  98.                                 
  99.     /* Select PWM engine clock */
  100.     DrvPWM_SelectClockSource(u8Timer, DRVPWM_HCLK);
  101.    
  102.     /* Set PWM Timer0 Configuration */
  103.     DrvPWM_SetTimerClk(u8Timer, &sPt);

  104.     /* Enable Output for PWM Timer0 */
  105.     DrvPWM_SetTimerIO(u8Timer, 1);
  106.                
  107.                 /* Enable the PWM Timer 0 */
  108.     DrvPWM_Enable(u8Timer, 1);               
  109.                                
  110.                 /*--------------------------------------------------------------------------------------*/
  111.                 /* Set the PWM Capture 3 for capture function                                           */
  112.                 /*--------------------------------------------------------------------------------------*/

  113.                 /* PWM Timer property for Capture */
  114.                 sPt.u8Mode = DRVPWM_AUTO_RELOAD_MODE;
  115.                 sPt.u32Frequency = 100;         /* Set the proper frequency to capture data (Less than the input data)*/
  116.                 sPt.u8HighPulseRatio = 50;      /* High Pulse peroid : Total Pulse peroid = 50 : 100 (Set a non-zero value) */
  117.                 sPt.u32Duty = 0x10000;  //计数器初值/* Set the counter to the maximum value */
  118.                 sPt.i32Inverter = 0;
  119.                 u8CapTimer = DRVPWM_CAP3; //配置PWM3作为捕获通道

  120.                 /* Select PWM engine clock */
  121.                 DrvPWM_SelectClockSource(u8CapTimer, DRVPWM_EXT_12M);//选择外部12M晶振做时钟源
  122.                
  123.                 /* Set PWM Timer 3 for Capture */
  124.                 DrvPWM_SetTimerClk(u8CapTimer, &sPt);
  125.                
  126.                 /* Enable Interrupt Sources of PWM Capture3 */
  127.                 DrvPWM_EnableInt(u8CapTimer, DRVPWM_CAP_FALLING_INT, NULL);//使能下降沿中断
  128.                
  129.                 /* Enable Input function for PWM Capture 3 */
  130.                 DrvPWM_SetTimerIO(u8CapTimer, 1);
  131.                
  132.                 /* Enable the PWM Capture3 */
  133.                 DrvPWM_Enable(u8CapTimer, 1);

  134.                 /* Capture the Input Waveform Data */
  135.                 CalPeriodTime(u8CapTimer);   //计算捕获时间
  136. }

  137. void Test_ADC(void)
  138. {       
  139.     int32_t i32ConversionData;
  140.        
  141.                 DrvADC_Open(ADC_SINGLE_END, ADC_SINGLE_OP, 0, EXTERNAL_12MHZ, 1);       
  142.                 /* Disable the digital input path */
  143.                 DrvGPIO_DisableDigitalInputBit(E_GPA, 0); //关闭ADC0数字输入通道
  144.                 /* Configure the corresponding ADC analog input pin */
  145.                 DrvGPIO_InitFunction(E_FUNC_ADC0);  //使能ADC0管脚功能
  146.                                                                
  147.                 DrvADC_SetADCChannel(1);        //使能ADC0通道
  148.                 while(1)
  149.                 {                                           
  150.                         /* start A/D conversion */
  151.                         DrvADC_StartConvert();      

  152.                         /* Wait conversion done */
  153.                         while(DrvADC_IsConversionDone()==FALSE);
  154.       ADC->ADSR.ADF=1;                           //清转换完成标志
  155.                         i32ConversionData = DrvADC_GetConversionData(0);  //读取转换结果
  156.                         printf("Conversion result: 0x%X (%d)\n\n",  i32ConversionData,        i32ConversionData);
  157.                         DrvSYS_Delay(200000);               
  158.                 }
  159. }       

  160. void Test_DataFlash(void)
  161. {       
  162.                 uint32_t base,Data;
  163.                 uint32_t config0;

  164.                 UNLOCKREG();
  165.                 SYSCLK->PWRCON.OSC22M_EN = 1;
  166.                 DrvSYS_Delay(22);
  167.                 SYSCLK->AHBCLK.ISP_EN = 1;
  168.                 FMC->ISPCON.ISPEN = 1;

  169.                 //这一部分可以在烧录代码时配置好
  170.                 //enable data flash for 128K APROM
  171.                 DrvFMC_Read(0x300000, &config0);      //读Cfg0
  172.                 config0 &= ~0x1;                      //使能数据Flash,(仅对于128K的型号)
  173.                 FMC->ISPCON.CFGUEN = 1;               //使能ISP更新Config功能
  174.                 DrvFMC_Erase(0x300000);
  175.                 DrvFMC_Write(0x300000, config0);
  176.                 DrvFMC_Write(0x300004, 0x1F000);      //配置好config要复位芯片后才生效
  177.                 //用户配置
  178.                 base = DrvFMC_ReadDataFlashBaseAddr();         //读一下数据Flash的基址

  179.                 DrvFMC_Erase(base);                         //erase 0x1F000 - 0x1F200
  180.                 DrvFMC_Write(base, 0x3333CCCC);         //写一个字到基地址
  181.                 DrvFMC_Write(base+4, 0x55555555);       //写第二个字
  182.                 DrvFMC_Read(base, &Data);               //读出第一个数据校验
  183.                 if(Data!=0x3333CCCC)   
  184.                           printf("DataFlash test fail\n");
  185.                 else
  186.                           printf("DataFlash test succeed\n");
  187. }       
  188. /*---------------------------------------------------------------------------------------------------------*/
  189. /* MAIN function                                                                                                                 */
  190. /*---------------------------------------------------------------------------------------------------------*/

  191. int32_t main()
  192. {               
  193.                 /* SYSCLK =>12Mhz*/
  194.                 UNLOCKREG();
  195.                 DrvSYS_SetOscCtrl(E_SYS_XTL12M,ENABLE);
  196.                 DrvSYS_Delay(5000);
  197.                 DrvSYS_Open(50000000);
  198.                 DrvSYS_Delay(5000);
  199.                        
  200.                 DrvGPIO_Open(E_GPC, 3, E_IO_OUTPUT);
  201.        
  202.                 Test_DataFlash();
  203.                        
  204.                 Test_TIMER();
  205.                
  206.                 Test_PWM_CAP();
  207.                
  208.                 Test_ADC();
  209.                
  210.                 while(1);       

  211. }       


dongnanxibei 发表于 2016-12-21 20:09 | 显示全部楼层
void Test_TIMER(void)
{        
    DrvTIMER_Init();
                DrvTIMER_Open(E_TMR0, 4, E_PERIODIC_MODE);  //配置定时器为周期模式
                DrvTIMER_SetTimerEvent(E_TMR0, 2, (TIMER_CALLBACK)TMR0_Callback, 1);//配置每秒中断两次
                DrvTIMER_EnableInt(E_TMR0);    //使能定时器中断
                DrvTIMER_Start(E_TMR0);         
}     
测试定时器?
zhuomuniao110 发表于 2016-12-21 20:50 | 显示全部楼层
捕获的应用很少用,看看怎么做。
643757107 发表于 2016-12-21 23:36 | 显示全部楼层
读写Dataflash功能不会,看看怎么做
syfw 发表于 2017-1-12 10:18 | 显示全部楼层

请问在往FLash里写数据或者读数据需要关闭所有中断吗
syfw 发表于 2017-1-12 10:52 | 显示全部楼层

请问写入Flash或者读Flash是否需要关闭所有中断啊?
wahahaheihei 发表于 2017-1-12 16:02 | 显示全部楼层
syfw 发表于 2017-1-12 10:52
请问写入Flash或者读Flash是否需要关闭所有中断啊?

我觉得这种事情肯定是最好关闭,要不会被打断吧。
wahahaheihei 发表于 2017-1-12 16:04 | 显示全部楼层
做关键事情的时候一定要关掉中断,万一打断了就不好看了。
huangcunxiake 发表于 2017-1-17 23:03 | 显示全部楼层
DrvFMC_Erase(base);                         //erase 0x1F000 - 0x1F200
                DrvFMC_Write(base, 0x3333CCCC);         //写一个字到基地址
                DrvFMC_Write(base+4, 0x55555555);       //写第二个字
                DrvFMC_Read(base, &Data);               //读出第一个数据校验
lee9888 发表于 2017-1-19 16:12 | 显示全部楼层
学习下PWM捕捉功能,谢
643757107 发表于 2017-1-20 19:02 | 显示全部楼层
   /* Enable PWM clock */
    DrvPWM_Open();  
貌似新唐有两套库函数吧,这个应该类似STM32的标准函数,现在应该也有个HAL的。
643757107 发表于 2017-1-26 11:50 | 显示全部楼层
HAL的不知道新唐开始支持没。
zhuomuniao110 发表于 2017-1-26 17:21 | 显示全部楼层
工程模板中的内容也非常丰富。
dongnanxibei 发表于 2017-1-26 19:56 | 显示全部楼层
我看看DataFlash的功能怎么做。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:分享资源,共同进步

99

主题

3237

帖子

10

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