/* defines */ #define F_CPU 1200000 /* for ATtiny13, running at 1.2MHz */ #define DATAPORT PORTB #define DATAPINS PINB #define CSPIN 2 /* PB2 (Pin#7) */ #define MCPIN 1 /* PB1 (Pin#6) */ #define MDIPIN 0 /* PB0 (Pin#5) */ /* macros */ #define CS_low() DATAPORT &= ~_BV(CSPIN) #define CS_high() DATAPORT |= _BV(CSPIN) #define MDI_low() DATAPORT &= ~_BV(MDIPIN) #define MDI_high() DATAPORT |= _BV(MDIPIN) #define MC_tick() { DATAPORT |= _BV(MCPIN); _delay_us(1); DATAPORT &= ~_BV(MCPIN); } #include #include void write_register(uint8_t reg, uint8_t value) { uint16_t word= (reg << 8) | value; CS_low(); for (uint8_t i=0; i<16; i++) { if (word & 0x8000) MDI_high(); else MDI_low(); MC_tick(); word <<= 1; } CS_high(); } int main(void) { /* hardware init*/ /* all pins input, except CS, MC, MDI */ DATAPINS=(_BV(CSPIN)|_BV(MCPIN)|_BV(MDIPIN)); /* all pins H (pullups enabled), except MC */ DATAPORT=~_BV(MCPIN); /* write DAC register(s) */ write_register(18, 0x50); /* endless loop */ while (1) ; }