[Atmel] 用AtmelStudio6.2跑mega16例程(70)DAC0832

[复制链接]
 楼主| ddllxxrr 发表于 2014-12-12 21:53 | 显示全部楼层 |阅读模式
DAC0832是8位的D/A转换器件,转换结果惟电流形式输——为通过DAC0832生成所需要的波形,电路中采用运放uA741将电流信号转换为电压信号。
当输出字节值由0x00-0xFF循环递增时,输出电压值由5V向0v循环递减,从而输出锯齿波。
当输出由0x00 - 0xFF循环递增,再由0xFF- 0x00循环递减时,即形成三角波效果。
使用正弦函数sin生成输出值时,即可得到正弦波。


Ptoteus仿真截图:


Atmel Studio6.2编译通过截图:


程序清单:
  1. /*
  2. * GccApplication33.c
  3. *
  4. * Created: 2014-12-12 21:15:48
  5. *  Author: Administrator
  6. */

  7. #define  F_CPU 1000000UL
  8. #include <avr/io.h>
  9. #include <util/delay.h>
  10. #include <math.h>
  11. #include <stdint.h>
  12. #define  PI 3.1415926


  13. #define  DAC0832 (uint8_t *)0xFFFE
  14. #define  S1_ON() (PINB & _BV(0)) == 0x00
  15. #define  S2_ON() (PINB & _BV(PB1)) == 0x00
  16. #define  S3_ON() (PINB & _BV(PB2)) == 0x00
  17. #define  S4_ON() (PINB & _BV(PB3)) == 0x00

  18. void SawTooth_Wave()
  19. {
  20.         uint8_t i;
  21.         for(i = 0;i<255;i++)
  22.         {
  23.                 *DAC0832 = i;
  24.                 _delay_ms(3);
  25.                 if(!S1_ON()) return;
  26.         }
  27. }

  28. void Triangle_Wave()
  29. {
  30.         uint8_t i;
  31.         for(i=0;i<255;i++)
  32.         {
  33.                 *DAC0832 = i;
  34.                 _delay_ms(3);
  35.                 if(!S2_ON())return;
  36.         }
  37.         for(i = 255;i>0;i--)
  38.         {
  39.                 *DAC0832 = i;
  40.                 _delay_ms(3);
  41.                 if(!S2_ON())return;
  42.         }
  43. }

  44. void Sin_Wave()
  45. {
  46.         float i;
  47.         for(i = 0;i<= 2*PI;i+=0.02)
  48.         {
  49.                 *DAC0832 = 128 + sin(i)*127;
  50.                 _delay_ms(100);
  51.                 if(!S3_ON())return;
  52.         }
  53. }

  54. int main(void)
  55. {
  56.     DDRA = 0xFF;
  57.         DDRB = 0x00;PORTB = 0xFF;
  58.         MCUCR |= 0x80;
  59.         while(1)
  60.     {
  61.         //TODO:: Please write your application code
  62.                 if(S1_ON()) SawTooth_Wave();
  63.                 else if (S2_ON()) Triangle_Wave();
  64.                 else if (S3_ON()) Sin_Wave();
  65.                 else if (S4_ON()) *DAC0832 = 0xFF;
  66.                 else _delay_ms(100);
  67.     }
  68. }

本帖子中包含更多资源

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

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

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

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