×
单片机 > 单片机程序设计 > 详情

STM32 AD7792驱动调试总结

发布时间:2020-05-18 发布时间:
|

调了好久,终于通了。。为什么用了一周时间这么久?主要原因是我不知道隔离模块有问题,导致一直是盲目的改代码,今天没办法,直接把隔离模块短路,一下子就读出了ID号。

7792挂在SPI2上,PB12,PB13,PB14,PB15,可我用SPI调的时候一直读出来是0XFF,以为是SPI2有问题,于是我直接抛弃SPI,用时序直接读。很好用!!!

下面是我的代码:

 

#define SCLOCK1   GPIO_SetBits(GPIOB,GPIO_Pin_13);
 #define SCLOCK0   GPIO_ResetBits(GPIOB,GPIO_Pin_13);

 #define CS1       GPIO_SetBits(GPIOB,GPIO_Pin_12);
 #define CS0       GPIO_ResetBits(GPIOB,GPIO_Pin_12);

 #define DIN1      GPIO_SetBits(GPIOB,GPIO_Pin_15);
 #define DIN0      GPIO_ResetBits(GPIOB,GPIO_Pin_15);

 #define DOUT1     GPIO_SetBits(GPIOB,GPIO_Pin_14);
 #define DOUT0     GPIO_ResetBits(GPIOB,GPIO_Pin_14);
void Delay(unsigned int Time)
{

    while(Time)
    {
        Time--;
    }
}
unsigned char DataRead[3];
void WriteToReg(unsigned char ByteData) // write ByteData to the register
{
    unsigned char temp;
    int i;    
    CS0;
    temp=0x80;
    for(i=0;i<8;i++)
    {
         if((temp & ByteData)==0)
        {        
              DIN0;
        }    
         else
        {
             DIN1;
         }
        SCLOCK0;
        Delay(10);
           SCLOCK1;
        Delay(10);
         temp=temp>>1;
    }
    CS1;
}


void ReadFromReg(unsigned char nByte) // nByte is the number of bytes which need to be read
{
    int i,j;
       unsigned char temp;
       DIN1;
     CS0;
    temp=0;
//    DOUT1;

    for(i=0; i    {
        for(j=0; j<8; j++)
        {
             SCLOCK0;
             if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)
             {
                temp=temp<<1;
             }else
             {
                temp=temp<<1;
                 temp=temp+0x01;
            }
            Delay(10);
            SCLOCK1;
            Delay(10);
          }
          DataRead[i]=temp;
          temp=0;
    }
    CS1;
}
extern void SSD1305_ShowASC16(u8 x,u8 y,u8 len,unsigned char *str);
void task_7792(void* parameter)
{
    char str[20];
    short iData[3];
    int ResetTime;    
    /* PRECONFIGURE...*/
    ResetTime=32;

    SCLOCK1; 
     CS0;          //to keep DIN=1 for 32 sclock to reset the part
     DIN1;
     while(ResetTime--)
    {
        Delay(10);
        SCLOCK0;
        Delay(10);
         SCLOCK1;
    }
     CS1;    

    while(1)
    {
          WriteToReg(0x10); //write to Communication register.The next step is writing to Configuration register.
        WriteToReg(0x00); //set the Configuration bipolar mode.Gain=1.
        WriteToReg(0x80); //Configuration internal reference selected.     

        WriteToReg(0x08);//write to Communication register.The next step is writing to Mode register.
        WriteToReg(0x20);//set the mode register as single conversion mode.
        WriteToReg(0x00);//inter 64 kHZ clock.internal clock is not available at the clk pin.
        
        WriteToReg(0x40);//write to Communication register.The next step is to read from Status register.
        ReadFromReg(1);    
        while((DataRead[0]&0x80)==0x80)//wait for the end of convertion by polling the status register RDY bit
        {            
            WriteToReg(0x40); 
            ReadFromReg(1);    
        }
        WriteToReg(0x58);//write to Communication register.The next step is to read from Data register.
    //    WriteToReg(0x60) ;
        ReadFromReg(2);
        memcpy(&iData[0],&DataRead[0],2);    
        sprintf(str,"%d",iData[0]);
        SSD1305_ShowASC16(10,20,6,str) ;          
    //    printf("Data:%02BX %02BXrn",DataRead[0],DataRead[1]);     }
}

嗯。出来了。。。。下面 就是自己配置寄存器,看读出来是哪个通道的值!




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ARM 汇编的必知必会