ADS1118使用实例

      ADS1118主要应用于对热电偶或者热敏电阻的测量,属于△-∑类型,采样速度为8SPS 至 860SPS,16位,数据通过串行外设接口spi,我应用的ADS1118作为电流电压的采集,mcu用的是stm32f030c8t6。

ADS1118使用实例_第1张图片

代码如下:

1、spi的初始化

void SpiInit(void)
{
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
	GPIO_InitStruct.GPIO_Mode 	= GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_Speed	= GPIO_Speed_Level_3;
	GPIO_InitStruct.GPIO_PuPd	= GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_OType	= GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin	=GPIO_Pin_3| GPIO_Pin_4 | GPIO_Pin_5;
	GPIO_Init(ADC_SCK2_BASE, &GPIO_InitStruct);

	GPIO_InitStruct.GPIO_Mode 	= GPIO_Mode_OUT;
	GPIO_InitStruct.GPIO_Pin	= ADC_CS2_PIN;
	GPIO_Init(ADC_CS2_BASE, &GPIO_InitStruct);
	ADC_CS2_BASE->BSRR = ADC_CS2_PIN;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
	SPI_Cmd(SPI2, DISABLE);
	SPI_StructInit(&SPI_InitStruct);
	SPI_InitStruct.SPI_BaudRatePrescaler	= SPI_BaudRatePrescaler_32;
	SPI_InitStruct.SPI_CPHA					= SPI_CPHA_2Edge;
	SPI_InitStruct.SPI_CPOL					= SPI_CPOL_Low;
	SPI_InitStruct.SPI_CRCPolynomial		= SPI_CRCLength_16b;
	SPI_InitStruct.SPI_DataSize				= SPI_DataSize_16b;
	SPI_InitStruct.SPI_Direction			= SPI_Direction_2Lines_FullDuplex;
	SPI_InitStruct.SPI_FirstBit				= SPI_FirstBit_MSB;
	SPI_InitStruct.SPI_Mode					= SPI_Mode_Master;
	SPI_InitStruct.SPI_NSS					= SPI_NSS_Soft;
	SPI_Init(SPI2, &SPI_InitStruct);
	SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_HF);
	SPI_Cmd(SPI2, ENABLE);
}

2、定时器的初始化


void Timer1Init(void)
{
	NVIC_InitTypeDef 			NVIC_InitStructure;	
	TIM_TimeBaseInitTypeDef  	TIM_TimeBaseStructure;

	// TIM6 clock enable
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM1, ENABLE);
	
	// Time base configuration
	TIM_TimeBaseStructure.TIM_Period 		= (unsigned int)(TIMER_CLOCK * 0.003);
	TIM_TimeBaseStructure.TIM_Prescaler 	= (CORE_CLOCK / TIMER_CLOCK) - 1;
	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseStructure.TIM_CounterMode 	= TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
	
	TIM6->SR  = (uint16_t)~TIM_FLAG_Update;
	TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
	TIM_Cmd(TIM6, ENABLE);

	NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
	
	return;
}

 3、定时器的中断处理函数

在定时器1中断处理函数中根据你的实际应用,通道数量再具体的处理。

你可能感兴趣的:(嵌入式)