CURRENT ISSUE
Contests
Feature Article
|
|
Issue #213 April 2008
Programmable Power
Build a Simple USB DAC
by Yoshiyasu Takefuji
Start | USB Communications | Firmware | MAX517 (DAC) Programming | Firmware, Software, & LIBUSB | Host Device Driver Installation | Testing | Sources & PDF
Listing 3—This is a full source listing of a USB device firmware program for an ATtiny45 and a MAX517.
/* Programmable USB power supply using TINY45 and MAX517 */ #include#include #include #include #include "usbdrv.h" void delay(unsigned int p) { unsigned char i, j; //one loop is 3.8us with 12MHz for(i=0;i<p;i++) for(j=0;j<10;j++); } void pulse(unsigned char data) { if((data & 0x01)==0){PORTB = 0x00; } //SDA=0 else {PORTB = 0x02; } //SDA=1 delay(1); PORTB = PORTB | 0x20; //SCL=1 delay(1); PORTB = PORTB & 0x02; //SCL=0 delay(1); } void start_strm() { PORTB = 0x20; //SDA=0 delay(1); PORTB= 0x00; delay(1); } void stop_strm() { PORTB = 0x20; //SCL=1 delay(1); PORTB = 0x22; //SDA=1 delay(1); } void ack_strm() { PORTB = 0x00; //SDA=0 delay(1); PORTB = 0x20; //SCL=1 delay(1); PORTB = 0x00; //SCL=0 delay(1); } /*-------------generate pulse stream---------------*/ void pulse_strm(unsigned char sda) { unsigned char i,ret; for(i=0;i<8;i++){ret=(sda>>(7-i)); pulse(ret); } } uchar usbFunctionSetup(uchar data[3]) { static uchar replybuf[1]; usbMsgPtr = replybuf; if(data[1] == 0){ replybuf[0]=55; start_strm(); pulse_strm(0x5E); //01011110 AD1=AD0=1 ack_strm(); pulse_strm(0x00); //00000000 RST=PD=A0=0 ack_strm(); pulse_strm(data[2]); // data[2] is for voltage ack_strm(); stop_strm(); } else if(data[1] == 1){ replybuf[0]=11;} else if(data[1] == 2) { replybuf[0]=22;} return 1; } int main(void) { DDRB = 0x22; // 0010 0010 PB1=SDA and PB5=SCL // are output PORTB = 0x22; //SDA=SCL=1 usbInit(); sei(); for(;;){ usbPoll(); } return 0; } /************************end of program**************************/
|