#define F_CPU 4000000
//******************SPI写寄存器函数*******************************//
void WriteToReg_ADC(uint8_t byteword)
{
PORTB &= 0<<4 ; //DRDY,使能SPI器件
//SPE_1;
SPDR=byteword; //发送数据
while(!(SPSR & (1<<SPIF)));//等待发送完毕
PORTB |= 1<<4 ; //DRDY,禁止SPI器件
//delay5us(10);
}
//******************SPI读函数*******************************//
unsigned char Read_Byte_SPI(void)
{
unsigned char i;
unsigned int x=0 ;
PORTB |= 1<<PINB7; //SCK闲时是高电平
for (i = 0 ; i < 8 ; i++)
{
PORTB &= 0<<PINB7; //SCK低电平
x<<=1;
if (PORTB & PINB6) //数据输入端
x = x+1;
PORTB |= 1<<PINB7; //SCK高电平
}
return x;
}
int main(void)
{
SPCR = 0x5C;
SPSR = 0x01;
DDRB = 0xB7;
PORTB |= 1<<PINB6;
DDRC = 0XFF;
PORTC = 0x55;
_delay_ms(2000);
WriteToReg_ADC(0x20); //通道1,下一个写时钟寄存器
if (PINB & 0x08)
PORTC = ~PORTC;
_delay_ms(2000);
WriteToReg_ADC(0x0B); //写时钟寄存器设置更新速率为200hz
PORTC = ~PORTC;
_delay_ms(2000);
WriteToReg_ADC(0x28);
PORTC = ~PORTC;
_delay_ms(2000);
PORTC = Read_Byte_SPI();
while(1) {}
}
刚开始接触AD7705,对寄存器和时序的操作不熟悉,通过C口的灯来观察程序进行到哪一步。
PORTC = Read_Byte_SPI();到这一步时 PORTC = 0;和预想的不一样
还有,AD7705已经CS端置0,RESET端置1了 |