我接收进来都是0,帮帮忙,谢谢 #include <iom128.h> #include <ina90.h> #define SS 0 #define SCK 1 #define MOSI 2 #define MISO 3
void SPI_init(void) { PORTB = 0XF8; DDRB = 0X07; //PB0 SS 输出 PB1 SCK 输出 PB2 MOSI输出 PB3 MISO输入 //PB4输出,其余输出 //SPCR|= ((1 << SPE) | (1 << MSTR) | (1 << SPR0)); //SPI控制寄存器初始化,允许SPI,主模式,fosc/16 SPCR|= ((1 << SPE) | (1 << MSTR) ); //SPI控制寄存器初始化,允许SPI,主模式,fosc/4
}
int MAX186_Tran(unsigned char channels) { int adc_data = 0 ; //接收转换结果的变量 unsigned char adc_L = 0 ; //转换结果高字节 unsigned char adc_H = 0 ; //转换结果低字节 unsigned char temp; // unsigned char CtrlByte = 0; //定义控制字 //CtrlByte = 0x1c; PORTB |= 0X01; for (temp = 0; temp < 50; temp++){;} PORTB &= 0XFE; for (temp = 0; temp < 50; temp++){;} SPDR = channels; while(!SPSR&(1 << SPIF)); for (temp = 0; temp < 50; temp++){;} SPDR=0x00; while(!SPSR&(1<<SPIF)); PORTB &= ~(1 << SCK); adc_H = SPDR; SPSR=0x00; while(!SPSR&(1<<SPIF)); adc_L = SPDR; adc_data = (int)(((adc_H<<5) & 0xfe0)); adc_data |= (int)(((adc_L>>3) & 0x1f)) ; PORTB |= 0X01; return adc_data; }
|