【CuriosityNano测评报告】+ DAC生成三角波

[复制链接]
1609|1
 楼主| masterqqq 发表于 2020-8-12 15:18 | 显示全部楼层 |阅读模式
10位DAC生成 100Hz左右的三角波
  1. /* 4 MHz (needed for delay function) */
  2. #define F_CPU                   (4000000UL)

  3. #include <avr/io.h>
  4. #include <util/delay.h>
  5. #include <math.h>

  6. /* 参考电压延迟 */
  7. #define VREF_STARTUP_TIME       (50)
  8. /* 低2位 */
  9. #define LSB_MASK                (0x03)
  10. /* 单周期点数 */
  11. #define Single_Wave_Dots       (400)
  12. /* 偏置电压 */
  13. #define DC_OFFSET          (510)
  14. /* 频率 */
  15. #define FRE               (100)
  16. /* 每个点延迟时间 */
  17. #define Bit_Period         ((1000000 / FRE) / Single_Wave_Dots)

  18. static void tWaveInit(void);
  19. static void VREF_init(void);
  20. static void DAC0_init(void);
  21. static void DAC0_setVal(uint16_t value);

  22. /* Buffer to store the sine wave samples */
  23. signed int tWave[Single_Wave_Dots];
  24. signed int tx ;
  25. static void tWaveInit(void)
  26. {
  27.     uint16_t i;
  28.     for(i = 0; i < Single_Wave_Dots; i++)
  29.     {
  30.         if(  i>99 && i <300){
  31.         tx = 200-i ;
  32.         }
  33.                
  34.         if (i >299){
  35.         tx = i-400 ;
  36.         }
  37.          if (i >0 && i <100 ){
  38.         tx = i ;
  39.         }
  40.         tWave[i] = DC_OFFSET + tx*5;
  41.     }
  42. }

  43. static void VREF_init(void)
  44. {
  45.     VREF.DAC0REF = VREF_REFSEL_2V048_gc /* Select the 2.048V Internal Voltage Reference for DAC */
  46.                  | VREF_ALWAYSON_bm;    /* Set the Voltage Reference in Always On mode */
  47.     /* Wait VREF start-up time */
  48.     _delay_us(VREF_STARTUP_TIME);
  49. }

  50. static void DAC0_init(void)
  51. {
  52.     /* Disable digital input buffer */
  53.     PORTD.PIN6CTRL &= ~PORT_ISC_gm;
  54.     PORTD.PIN6CTRL |= PORT_ISC_INPUT_DISABLE_gc;
  55.     /* Disable pull-up resistor */
  56.     PORTD.PIN6CTRL &= ~PORT_PULLUPEN_bm;
  57.     DAC0.CTRLA = DAC_ENABLE_bm          /* Enable DAC */
  58.                | DAC_OUTEN_bm           /* Enable output buffer */
  59.                | DAC_RUNSTDBY_bm;       /* Enable Run in Standby mode */
  60. }

  61. static void DAC0_setVal(uint16_t value)
  62. {
  63.     /* Store the two LSbs in DAC0.DATAL */
  64.     DAC0.DATAL = (value & LSB_MASK) << 6;
  65.     /* Store the eight MSbs in DAC0.DATAH */
  66.     DAC0.DATAH = value >> 2;
  67. }

  68. int main(void)
  69. {
  70.     short tindex = 0;
  71.    
  72.     VREF_init();
  73.     DAC0_init();
  74.    
  75.     tWaveInit();
  76.    
  77.     while (1)
  78.     {
  79.         DAC0_setVal(tWave[tindex++]);
  80.         if(tindex == Single_Wave_Dots)
  81.             tindex = 0;
  82.         _delay_us(Bit_Period);
  83.     }
  84. }





本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
xuanhuanzi 发表于 2020-8-12 19:58 | 显示全部楼层
看起来很纯正的波形。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:32MCU单片机查询网 http://www.32mcu.com MCU选型,嵌入式设计辅助,选择最适合的MCU

35

主题

173

帖子

146

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