片外ADC驱动程序

2019-07-20 03:58发布

写了一个驱动TLV1544的ADC驱动程序,但在主函数中调用TLV1544.c函数时,没有采集到输入的电压值,TLV1544.c函数如下,有大佬能看下是不是写的有啥不对的吗,非常感谢




#include "TLV1544.h"
#include "usart.h"
#include "delay.h"


void TLV1544_GPIO_Init(void)
{

        GPIO_InitTypeDef  GPIO_InitStructure;
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ê1ÄüGPIOAê±Öó

  //Ïè3õê¼»ˉADC1í¨μà5 IO¿ú
  GPIO_InitStructure.GPIO_Pin = CS;//PA4
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//Ä£Äaêä3ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = SCLK;//PA5
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = DIN;//PA6
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
//        GPIO_InitStructure.GPIO_Pin = DRDY;//PA5
//        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
        GPIO_InitStructure.GPIO_Pin = DOUT;//PA7
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;//Ä£Äaêäèë
        GPIO_Init(GPIOA, &GPIO_InitStructure);
       
}

int        set_TLV1544_mode(u16 channel)//éèÖÃTLV1544×a»»Ä£ê½
{
        u16 i;
//        uint8_t bit;
        channel=channel<<4;
        TLV1544_CS_L();  //0
        delay_ms(5);
        TLV1544_SCLK_L();  //0
        delay_ms(5);
       
        for(i=0;i<10;i++)
        {
                if(channel&0x80)
                {
                        TLV1544_DIN_L();
                }
                else
                        TLV1544_DIN_H();
                __NOP();
               
                TLV1544_SCLK_L();
                TLV1544_SCLK_H();
                channel=channel<<1;
        }
        TLV1544_CS_H();
}

int TLV1544_tra(void) // ADC3&#245;ê&#188;&#187;ˉoˉêy£&#172;êy&#190;Y&#206;a10&#206;&#187;
{
        u16 x=0,i;
        int adc_value=0;
        set_TLV1544_mode(1);//éè&#214;&#195;TLV1544×a&#187;&#187;&#196;£ê&#189;
        delay_ms(60);

        TLV1544_CS_L();
        __NOP();
        __NOP();     //3&#245;ê&#188;&#187;ˉ&#198;&#172;&#209;&#161;
        TLV1544_SCLK_L();//ê±&#214;óà-μí
        __NOP();
        __NOP();
       
       
       
        for(i=0;i<10;i++)
        {
                TLV1544_SCLK_H();
          __NOP();
                x=GPIO_ReadInputDataBit(GPIOA,DOUT);

                adc_value|=x;
                adc_value<<=1;
               
                TLV1544_SCLK_L();
                __NOP();
          __NOP();
               
        }
//        adc_value>>=1;
        TLV1544_CS_H();     //1

        if(adc_value >= 512)
                adc_value=0;
        //printf("---%d--- ",adc_value);
        return  adc_value;
}


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
dxl
1楼-- · 2019-07-20 07:45
 精彩回答 2  元偷偷看……
dxl
2楼-- · 2019-07-20 09:00
 精彩回答 2  元偷偷看……
二九结狐六体
3楼-- · 2019-07-20 14:30
原来是这样啊!
dxl
4楼-- · 2019-07-20 19:58
二九结狐六体 发表于 2019-1-25 10:10
原来是这样啊!

什么意思,实际采电压的时候没有反应。。。

一周热门 更多>