司生产的一种低功耗的电压输出型10位串行D/A转换器,兼容SPI接口,MAX515固定增益为2,用+5V单电源工作。本例运行时,通过调节RV1向单片机输入模拟电压,单片机将A/D转换后的数字量输出给MAX515,经D/A转换后所输出的 模拟电压控制LED亮度变化
Proteus仿真截图:
Ateml Studio6.2 编译通过截图:
程序清单:
/*
* GccApplication34.c
*
* Created: 2014-12-15 21:07:48
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <stdint.h>
#define SPI_EN() (PORTB &=~_BV(PB4))
#define SPI_DI() (PORTB |= _BV(PB4))
void SPI_MasterInit()
{
DDRB = 0B10110000;PORTB = 0xFF;
SPCR |= _BV(SPE)|_BV(MSTR)|_BV(SPR0);
}
uint8_t SPI_Transmit(uint8_t d)
{
SPDR = d;
while(!(SPSR & _BV(SPIF)));
SPSR |= _BV(SPIF);
return SPDR;
}
uint16_t ADC_Convert(uint8_t CH)
{
ADMUX = CH;
return ADC;
}
int main(void)
{
uint16_t dat;
DDRA = 0x00;
SPI_MasterInit();
ADCSRA = 0xE6;
_delay_ms(3000);
while(1)
{
//TODO:: Please write your application code
dat = ADC_Convert(4);
dat <<=2;
SPI_EN();
SPI_Transmit(dat>>8);
SPI_Transmit(dat);
SPI_DI();
_delay_ms(100);
}
}
|