DAC0832是8位的D/A转换器件,转换结果惟电流形式输——为通过DAC0832生成所需要的波形,电路中采用运放uA741将电流信号转换为电压信号。
当输出字节值由0x00-0xFF循环递增时,输出电压值由5V向0v循环递减,从而输出锯齿波。
当输出由0x00 - 0xFF循环递增,再由0xFF- 0x00循环递减时,即形成三角波效果。
使用正弦函数sin生成输出值时,即可得到正弦波。
Ptoteus仿真截图:
Atmel Studio6.2编译通过截图:
程序清单:
- /*
- * GccApplication33.c
- *
- * Created: 2014-12-12 21:15:48
- * Author: Administrator
- */
- #define F_CPU 1000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include <math.h>
- #include <stdint.h>
- #define PI 3.1415926
- #define DAC0832 (uint8_t *)0xFFFE
- #define S1_ON() (PINB & _BV(0)) == 0x00
- #define S2_ON() (PINB & _BV(PB1)) == 0x00
- #define S3_ON() (PINB & _BV(PB2)) == 0x00
- #define S4_ON() (PINB & _BV(PB3)) == 0x00
- void SawTooth_Wave()
- {
- uint8_t i;
- for(i = 0;i<255;i++)
- {
- *DAC0832 = i;
- _delay_ms(3);
- if(!S1_ON()) return;
- }
- }
- void Triangle_Wave()
- {
- uint8_t i;
- for(i=0;i<255;i++)
- {
- *DAC0832 = i;
- _delay_ms(3);
- if(!S2_ON())return;
- }
- for(i = 255;i>0;i--)
- {
- *DAC0832 = i;
- _delay_ms(3);
- if(!S2_ON())return;
- }
- }
- void Sin_Wave()
- {
- float i;
- for(i = 0;i<= 2*PI;i+=0.02)
- {
- *DAC0832 = 128 + sin(i)*127;
- _delay_ms(100);
- if(!S3_ON())return;
- }
- }
- int main(void)
- {
- DDRA = 0xFF;
- DDRB = 0x00;PORTB = 0xFF;
- MCUCR |= 0x80;
- while(1)
- {
- //TODO:: Please write your application code
- if(S1_ON()) SawTooth_Wave();
- else if (S2_ON()) Triangle_Wave();
- else if (S3_ON()) Sin_Wave();
- else if (S4_ON()) *DAC0832 = 0xFF;
- else _delay_ms(100);
- }
- }
|