Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I have initialized PORTB like this
- PORTB=0x00;
- DDRB=0xBF;
- // SPI initialisation
- // SPI clock rate fck/16
- // SPI master
- // SPI MSB first
- // SPI CPOL = 1, CPHA = 1
- SPCR=0x5D;
- PORTB.3 = 1;
- void main (void){
- printf("adc value :%xn",ReadAd());
- }
- #define ADC_CS PORTB.3
- #define WG_CS PORTB.4
- #define MOSI PORTB.5
- #define MISO PINB.6
- #define SCK PORTB.7
- #define ADC_CS_PIN PINB.3
- #define WG_CS_PIN PINB.4
- char spi(char data)
- {
- //Start transmision
- SPDR = data;
- //Wait for transmision complete
- while(!(SPSR & 0x80));
- return SPDR;
- }
- //Sets the waveform generator output to given phase
- void SetWGPhase(unsigned int phase)
- {
- SPCR = 0x5A;
- WG_CS = 0;
- while(WG_CS_PIN);
- spi(0x20);
- spi(0x00);
- spi((char)((phase>>8)|0xC0)); //Load into phase register 0
- spi((char)(phase & 0x00FF));
- WG_CS = 1;
- }
- unsigned int ReadAd(void)
- {
- unsigned int data;
- ChipSelectAd(1);
- //Read data
- CheckStatus(); //Wait for data ready in
- adc register
- spi(0x58); //Place readinstruction
- in communication register
- data = (spi(0xFF)<<8); //Read 8 most significant
- bits from data register
- data |= spi(0xFF); //Read 8 leastsignificant
- bits from data register
- return data;
- ChipSelectAd(0);
- }
- void CheckStatus(void)
- {
- char adcStatus;
- do
- {
- (void) spi(0x40);
- adcStatus = spi(0xFF);
- } while((adcStatus & 0x80) > 0);
- }
- void ChipSelectAd(char s)
- {
- if(s == 1){
- PORTB.3 = 0; //Switch on AD
- while(PINB.3); //Wait for chip select pin
- }
- else
- PORTB.3 = 1; //Switch of AD
- }
Add Comment
Please, Sign In to add comment