//SPI example using ADC0832 from National Semi //connect MOSI B.5 to DI on ADC0832 //connect MISO B.6 to DO on ADC0832 //connect SCLK B.7 to CLK on ADC0832 //connect B.4 to chip select on the ADC0832 #include #include // SPI driver routine #include #define begin { #define end } unsigned char Ain, junk; void main(void) begin //init the UART UCSRB = 0x18; UBRRL = 103; printf("starting...\n\r"); //set up SPI //bit 7 SPIE=0 no ISR //bit 6 SPE=1 enable spi //bit 5 DORD=0 msb first //bit 4 MSTR=1 Mega32 is spi master //bit 3 CPLO=1 clock polarity //bit 2 CPHA=1 clock phase //bit 1,0 rate sel=10 along with SPRC=1 sets clk to f/32 = 500 kHz SPCR = 0b01011110 ; SPSR = 1; //set up i/o data direction DDRB.4 = 1; //output chip select for ADC DDRB.5 = 1; //output MOSI DDRB.6 = 0; //input MISO DDRB.7 = 1; //output SCLK while (1) begin PORTB.4 = 0; //chip select low begins conversion junk = spi(0b00001100) ; //set single-ended, channel 0 Ain = spi(0x00); //get msb first PORTB.4 = 1; //chip select high ends conversion printf("%d\n\r",Ain); end end