用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记

ADS1220

ADS1220是一款具有四通道的,24bit的模数转换器,其中两两可以组成差分输入,内部有可以编程的放大器,最大的放大倍数是128。

ADS1220的数据交换是通过SPI总线进行数据交换的,SPI工作在模式1。CPOL为0空闲时为低电平,CPHA为1,第二个边沿触发。

ADS的指令:
用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第1张图片
寄存器0:关于通道以及增益的配置用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第2张图片
寄存器1:关于转换速度的配置:
用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第3张图片
不同工作模式下的转换速度:
用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第4张图片
寄存器2:内部参考电压用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第5张图片
寄存器3:
用STM32F103获取ADS1220模数转换的值,以及C#做上位机的笔记_第6张图片

STM32的配置

SPI的配置

static void ads_init(void)
{
	/*APB BUS INIT*/
	LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
	LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
	LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
	LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SPI1);
	/**SPI GPIO Configuration  
	PA5   ------> SPI1_SCK
	PA6   ------> SPI1_MISO
	PA7   ------> SPI1_MOSI
	PA4   ------> SPI1_NSS
	
	*/
	LL_GPIO_SetPinMode(GPIOA,LL_GPIO_PIN_4,LL_GPIO_MODE_OUTPUT);
	LL_GPIO_SetPinOutputType(GPIOA,LL_GPIO_PIN_4,LL_GPIO_OUTPUT_PUSHPULL);
	LL_GPIO_SetPinSpeed(GPIOA,LL_GPIO_PIN_4,LL_GPIO_SPEED_FREQ_HIGH);
	SPI1_DISENABLE;
	
	LL_GPIO_SetPinMode(GPIOA,LL_GPIO_PIN_5,LL_GPIO_MODE_ALTERNATE);
	LL_GPIO_SetPinOutputType(GPIOA,LL_GPIO_PIN_5,LL_GPIO_OUTPUT_PUSHPULL);
	LL_GPIO_SetPinSpeed(GPIOA,LL_GPIO_PIN_5,LL_GPIO_SPEED_FREQ_HIGH);
	
	LL_GPIO_SetPinMode(GPIOA,LL_GPIO_PIN_7,LL_GPIO_MODE_ALTERNATE);
	LL_GPIO_SetPinOutputType(GPIOA,LL_GPIO_PIN_7,LL_GPIO_OUTPUT_PUSHPULL);
	LL_GPIO_SetPinSpeed(GPIOA,LL_GPIO_PIN_7,LL_GPIO_SPEED_FREQ_HIGH);
	
	LL_GPIO_SetPinMode(GPIOA,LL_GPIO_PIN_6,LL_GPIO_MODE_FLOATING); //SPI1 GPIO INIT
}

SPI的数据收发

void ads_Reset()
{
	SPI1_ENABLE;
	uint32_t time_out = 0;	
	LL_SPI_TransmitData8( SPI1, ADS1220_RESET_CMD);
	while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	time_out = 0;
	while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	(void)LL_SPI_ReceiveData8(SPI1);
	SPI1_DISENABLE;
}

void ads_Start()
{
	uint32_t time_out = 0;
	SPI1_ENABLE;
	LL_SPI_TransmitData8( SPI1, ADS1220_START_CMD);
	while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	time_out = 0;
	while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	(void)LL_SPI_ReceiveData8(SPI1);
	SPI1_DISENABLE;;
}


void ads_SetREG(uint8_t *w_data,uint8_t data_size,uint8_t begin_addr)
{
	/*
	Note the value of the array,Prevent pointer overflow 
	bit3|bit2 is begin addr
	bit1|bit0 is number of data after the addr
	*/	
	uint32_t time_out = 0;
	if((begin_addr + data_size)>4)
	{
		return ;
	}
	else
		begin_addr = ADS1220_WREG_CMD|( begin_addr << 2 ) + data_size -1;
	

	SPI1_ENABLE;
	
	LL_SPI_TransmitData8(SPI1, begin_addr);
	while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	time_out = 0;
	while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
	{
		time_out++;
	}
	(void)LL_SPI_ReceiveData8(SPI1);
	time_out = 0;
	
	for(uint8_t i = 0;i<data_size;i++)
	{
		LL_SPI_TransmitData8(SPI1, *w_data);
		while ((!LL_SPI_IsActiveFlag_TXE(SPIx))&&time_out<0xfffff)
		{
			time_out++;
		}
		time_out = 0;
		while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
		{
			time_out++;
		}
		(void)LL_SPI_ReceiveData8(SPI1);
		w_data ++;
	}
	SPI1_DISENABLE;
}
void ads_GetREG(uint8_t *r_data,uint8_t size, uint8_t begin_addr)
{
	/*
	Note the value of the array,Prevent pointer overflow 
	bit3|bit2 is begin addr
	bit1|bit0 is number of data after the addr
	*/
	uint32_t time_out = 0;
	if((begin_addr + size)>4)
	{
		return ;
	}
	else
		begin_addr = ADS1220_RREG_CMD|( begin_addr << 2 ) + size -1;
	
	SPI1_ENABLE;

	
	LL_SPI_TransmitData8(SPI1, begin_addr);
	while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xfffff)
	{
		time_out ++;
	}
	time_out = 0;
	while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
	{
	}
	(void)LL_SPI_ReceiveData8(SPI1);
	time_out = 0;
	for(uint8_t i = 0;i<size;i++)
	{
		LL_SPI_TransmitData8(SPI1,0x00);
		while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xfffff)
		{
			time_out ++;
		}
		time_out = 0;
		while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xfffff)
		{
			time_out ++;
		}
		*r_data=LL_SPI_ReceiveData8(SPI1);
		r_data ++;
	}
	SPI1_DISENABLE;;
}

void ads_GetData(uint8_t *r_data)
{
	/*
	Note the value of the array,Prevent pointer overflow 
	bit3|bit2 is begin addr
	bit1|bit0 is number of data after the addr
	*/
	uint32_t time_out = 0;
	SPI1_ENABLE;
	
	LL_SPI_TransmitData8(SPI1, ADS1220_RDATA_CMD);
	while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xffff)
	{
		time_out++;
	}
	time_out = 0;
	while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xffff)
	{
		time_out++;
	}
	(void)LL_SPI_ReceiveData8(SPI1);
	time_out = 0;
	for(uint8_t i = 0;i<3;i++)
	{
		LL_SPI_TransmitData8(SPI1,0xFF);
		while ((!LL_SPI_IsActiveFlag_TXE(SPI1))&&time_out<0xffff)
		{
			time_out++;
		}
		time_out = 0;
		while ((!LL_SPI_IsActiveFlag_RXNE(SPI1))&&time_out<0xffff)
		{
			time_out++;
		}
		*r_data=LL_SPI_ReceiveData8(SPI1);
		r_data ++;
	}
	SPI1_DISENABLE;
}

C#

1.将一组字符串转换为,二进制数组

        string s = "123456";
        var bt = Regex.Matches(s, @"[a-fA-F0-9]{2}").Cast<Match>().Select(t => (byte)Convert.ToInt32(t.Value, 16)).ToArray();//不是很懂不知道有人交流一下么

开一个线程

 	cbbBaudrates.SelectedIndex = 3;
    ThreadCapture = new Thread(MyStaticThreadCapture);
    ThreadCapture.Start();  // 只要使用Start方法,线程才会运行  

你可能感兴趣的:(单片机,笔记,c#)