[mw_shl_code=c,true]int main(void)
{
unsigned char i=0;
long ulResult;
long double ldVolutage;
u8 t;
u8 len;
u16 times=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//ÉèÖÃϵͳÖжÏÓÅÏȼ¶·Ö×é2
delay_init(168); //ÑÓʱ³õʼ»¯
uart_init(115200); //´®¿Ú³õʼ»¯²¨ÌØÂÊΪ115200
LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú
Init_ADS1256_GPIO(); //³õʼ»¯ADS1256 GPIO¹Ü½Å
GPIO_SetBits(GPIOB, GPIO_Pin_12 );
ADS1256_Init();
printf("ads³õʼ»¯
");
while(1)
{
for(i = 0;i < 8;i++)
{
ulResult = ADS_sum( (i << 4) | ADS1256_MUXN_AINCOM);
//ulResult = ADS_sum( ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM);
if( ulResult & 0x800000 )
{
ulResult = ~(unsigned long)ulResult;
ulResult &= 0x7fffff;
ulResult += 1;
ulResult = -ulResult;
}
ldVolutage = (long double)ulResult*0.59604644775390625;
printf("µÚ%dͨµÀ:",(i & 0x07)?(i & 0x07) - 1:7);
printf("%lf",ldVolutage); //double
printf("uV
");
//printf("%x",(unsigned long)ulResult);//16
delay_us(100);
}
}
}
void SPI2_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/****Initial SPI2******************/
/* Enable SPI2 and GPIOB clocks */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
/* Configure SPI2 pins: NSS, SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀ­
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); //PB3¸´ÓÃΪ SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2); //PB4¸´ÓÃΪ SPI1
GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PB5¸´ÓÃΪ SPI1
//SPI2 NSS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* SPI2 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //SPI1ÉèÖÃΪÁ½ÏßÈ«Ë«¹¤
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI2ΪÖ÷ģʽ
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //SPI·¢ËͽÓÊÕ8λ֡½á¹¹
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //´®ÐÐʱÖÓÔÚ²»²Ù×÷ʱ£¬Ê±ÖÓΪµÍµçƽ
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //µÚÒ»¸öʱÖÓÑØ¿ªÊ¼²ÉÑùÊý¾Ý
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ8
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼
SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ
SPI_Init(SPI2, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
printf("SPI³õʼ»¯
");
}
//³õʼ»¯ADS1256 GPIO
void Init_ADS1256_GPIO(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_ADS1256Reset | RCC_ADS1256DRDY, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_RCC_ADS1256Reset;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_RCC_ADS1256Reset_PORT, &GPIO_InitStructure);
GPIO_ResetBits(GPIO_RCC_ADS1256Reset_PORT, GPIO_RCC_ADS1256Reset );
GPIO_InitStructure.GPIO_Pin = GPIO_ADS1256DRDY;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//¸´Óù¦ÄÜ
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_ADS1256DRDY_PORT, &GPIO_InitStructure);
SPI2_Init();
printf("GPIO³õʼ»¯
");
}
//-----------------------------------------------------------------//
// ¹¦ ÄÜ£º Ä£ÄâSPIͨÐÅ
// Èë¿Ú²ÎÊý: / ·¢Ë͵ÄSPIÊý¾Ý
// ³ö¿Ú²ÎÊý: / ½ÓÊÕµÄSPIÊý¾Ý
// È«¾Ö±äÁ¿: /
// ±¸ ×¢: ·¢ËͽÓÊÕº¯Êý
//-----------------------------------------------------------------//
unsigned char SPI_WriteByte(unsigned char TxData)
{
unsigned char RxData=0;
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET); //
SPI_I2S_SendData(SPI2,TxData);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
RxData=SPI_I2S_ReceiveData(SPI2);
return RxData;
printf("Ä£ÄâSPIͨÐÅ
");
}
//-----------------------------------------------------------------//
// ¹¦ ÄÜ£ºADS1256 дÊý¾Ý
// Èë¿Ú²ÎÊý: /
// ³ö¿Ú²ÎÊý: /
// È«¾Ö±äÁ¿: /
// ±¸ ×¢: ÏòADS1256ÖеØַΪregaddrµÄ¼Ä´æÆ÷дÈëÒ»¸ö×Ö½Údatabyte
//-----------------------------------------------------------------//
void ADS1256WREG(unsigned char regaddr,unsigned char databyte)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY));//µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
//Ïò¼Ä´æÆ÷дÈëÊý¾ÝµØÖ·
SPI_WriteByte(ADS1256_CMD_WREG | (regaddr & 0x0F));
//дÈëÊý¾ÝµÄ¸öÊýn-1
SPI_WriteByte(0x00);
//ÏòregaddrµØÖ·Ö¸ÏòµÄ¼Ä´æÆ÷дÈëÊý¾Ýdatabyte
SPI_WriteByte(databyte);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
printf("ADS1256 дÊý¾Ý
");
}
//³õʼ»¯ADS1256
void ADS1256_Init(void)
{
ADS1256WREG(ADS1256_STATUS,0x06); // ¸ßλÔÚÇ°¡¢Ð£×¼¡¢Ê¹Óûº³å
// ADS1256WREG(ADS1256_MUX,0x08); // ³õʼ»¯¶Ë¿ÚA0Ϊ¡®+¡¯£¬AINCOMλ¡®-¡¯
ADS1256WREG(ADS1256_ADCON,0x00); // ·Å´ó±¶Êý1
ADS1256WREG(ADS1256_DRATE,ADS1256_DRATE_10SPS); // Êý¾Ý5sps
ADS1256WREG(ADS1256_IO,0x00);
printf("³õʼ»¯ADS1256
");
}
//¶ÁÈ¡ADÖµ
unsigned int ADS1256ReadData()
{
unsigned char i=0;
unsigned int sum=0;
unsigned int r=0;
GPIO_ResetBits(GPIOB, GPIO_Pin_12);;
while(GPIO_ReadInputDataBit(GPIO_ADS1256DRDY_PORT,GPIO_ADS1256DRDY)); //µ±ADS1256_DRDYΪµÍʱ²ÅÄÜд¼Ä´æÆ÷
// ADS1256WREG(ADS1256_MUX,channel); //ÉèÖÃͨµÀ
SPI_WriteByte(ADS1256_CMD_SYNC);
SPI_WriteByte(ADS1256_CMD_WAKEUP);
SPI_WriteByte(ADS1256_CMD_RDATA);
sum |= (SPI_WriteByte(0xff) << 16);
sum |= (SPI_WriteByte(0xff) << 8);
sum |= SPI_WriteByte(0xff);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
return sum;
printf("¶ÁÈ¡ADÖµ
");
}
//-----------------------------------------------------------------//
// ¹¦ ÄÜ£º¶ÁÈ¡ADS1256µ¥Â·Êý¾Ý
// Èë¿Ú²ÎÊý: /
// ³ö¿Ú²ÎÊý: /
// È«¾Ö±äÁ¿: /
// ±¸ ×¢: /
//-----------------------------------------------------------------//
unsigned int ADS_sum(unsigned char channel)
{
ADS1256WREG(ADS1256_MUX,channel); //ÉèÖÃͨµÀ
return ADS1256ReadData();//¶ÁÈ¡ADÖµ£¬·µ»Ø24λÊý¾Ý¡£
printf("¶ÁÈ¡ADS1256µ¥Â·Êý¾Ý
");
}[/mw_shl_code]
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>