在stm32f107基础上使用LTC1867芯片采样,设置单极性后只能采样要0~2V的电压,基准为4V。请大神帮忙!!!!
- #define ADC_CS_H GPIO_SetBits(GPIOA,GPIO_Pin_4);
- #define ADC_CS_L GPIO_ResetBits(GPIOA,GPIO_Pin_4);
-
- #define ADC_SCK_H GPIO_SetBits(GPIOA,GPIO_Pin_5);
- #define ADC_SCK_L GPIO_ResetBits(GPIOA,GPIO_Pin_5);
-
- //#define ADC_MISO GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)
- #define ADC_MISO GPIOA->IDR&(1<<6)
-
-
- #define ADC_MOSI_H GPIO_SetBits(GPIOA,GPIO_Pin_7);
- #define ADC_MOSI_L GPIO_ResetBits(GPIOA,GPIO_Pin_7);
-
- #define Instructions_LTC1867_CH0 0x80
- #define Instructions_LTC1867_CH1 0xC0
- #define Instructions_LTC1867_CH2 0x90
- #define Instructions_LTC1867_CH3 0xD0
- #define Instructions_LTC1867_CH4 0xA0
- #define Instructions_LTC1867_CH5 0xE0
- #define Instructions_LTC1867_CH6 0xB0
- #define Instructions_LTC1867_CH7 0xF0
-
- #define LTC1867_SLEEP_MODE 0x02
- #define LTC1867_EXIT_SLEEP_MODE 0x00
- #define LTC1867_UNIPOLAR_MODE 0x04
- #define LTC1867_BIPOLAR_MODE 0x00
-
-
- /******************************************
-
- *功能:SPI初始化
-
- *******************************************/
- void SPI_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
-
- //----------------------GPIO----------------------
- // Configure SPI pins: CS
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
- // Configure SPI pins: SCK
- GPIO_InitStructure.GPIO_Pin = 0;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- // Configure SPI pins: MISO
- GPIO_InitStructure.GPIO_Pin = 0;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- // Configure _SPI pins: MOSI
- GPIO_InitStructure.GPIO_Pin = 0;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- //设置为空闲状态
- ADC_CS_H;
- ADC_SCK_H;
-
- }
-
-
-
- /************************************************
-
- *功能: LTC1867ACGN选择1~8通道
-
- *************************************************/
- u8 LTC1867ACGN_SetChnl(u8 chnl)
- {
- u8 cmd;
- switch(chnl)
- {
- case 1:
- cmd = Instructions_LTC1867_CH0|LTC1867_UNIPOLAR_MODE;break;
-
- case 2:
- cmd = Instructions_LTC1867_CH1|LTC1867_UNIPOLAR_MODE;break;
-
- case 3:
- cmd = Instructions_LTC1867_CH2|LTC1867_UNIPOLAR_MODE;break;
-
- case 4:
- cmd = Instructions_LTC1867_CH3|LTC1867_UNIPOLAR_MODE;break;
-
- case 5:
- cmd = Instructions_LTC1867_CH4|LTC1867_UNIPOLAR_MODE;break;
-
- case 6:
- cmd = Instructions_LTC1867_CH5|LTC1867_UNIPOLAR_MODE;break;
-
- case 7:
- cmd = Instructions_LTC1867_CH6|LTC1867_UNIPOLAR_MODE;break;
-
- case 8:
- cmd = Instructions_LTC1867_CH7|LTC1867_UNIPOLAR_MODE;break;
-
- default:
- cmd = Instructions_LTC1867_CH0|LTC1867_UNIPOLAR_MODE;break;
- }
- return cmd;
- }
-
- /**********************************
-
- *功能: SPI读写数据,给定通道选择参数
-
- ***********************************/
- u16 SPI_WriteRead(u8 chnl)
- {
- u8 i = 0;
- u16 dat = 0;
- u8 cmd = 0;
- cmd = LTC1867ACGN_SetChnl(chnl);
- ADC_CS_L;
- delay_5us(10); //50
- for(i=0;i<7;i++)
- {
- if(cmd&(0x80))
- {
- ADC_MOSI_H;
- }else{
- ADC_MOSI_L;
- }
- cmd <<= 1;
- ADC_SCK_L;
- delay_5us(1); //5
- ADC_SCK_H;
- delay_5us(1); //5
- }
- ADC_CS_H;
- delay_5us(2); //10
- ADC_CS_L;
- for(i=0;i<16;i++)
- {
- ADC_SCK_L;
- delay_5us(1); //2
- if(ADC_MISO) dat |= 1;
- dat <<= 1;
- ADC_SCK_H;
- delay_5us(1); //2
- }
- ADC_CS_H;
-
- return dat;
- }
本帖最后由 清风烈酒 于 2018-1-30 12:58 编辑