AnsweredAssumed Answered

Interfacing ARM and AD8403

Question asked by p.sowmya on Feb 20, 2015
Latest reply on Mar 7, 2015 by Clive One
Dear sir/mam,
        As i am working interfacing ARM and AD8403 IC(digital potentiometer),i am measure resistance value,what i am doing is from ARM i am sending data through SPI every 20sec data i am tranmitting,and AD8403 IC what i am sending it will give the exact resistance value.the problem is resistance value is fluctuation,it should not fluctuate,i need your help regarding this i will update my developed code please have a look at it. 

emergency please help me.....

/*****************MY CODE****************/

#include <stdio.h>
#include <stm32f10x.h>


void RCC_Configuration(void);
void GPIOA_Configuration(void);
void GPIOB_Configuration(void);
void SysTick_configuration(void);
void SPI1_configuration(void);
void SPI_AD8402_CHECK_FUNCTION(void);


#define     SPI1_BUSY_FLAG     SPI1->SR &= 0x0080


volatile unsigned short int fl_i = 0;
volatile unsigned short int fl_j = 0;                              
volatile unsigned int data=0;
volatile unsigned int data_flag=0;
volatile unsigned short int _20_Sec_Delay = 40000; 
unsigned short int Count = 0;
unsigned short int Delay_Flag1 = 0;


main()
{                                                       
     RCC_Configuration(); 
     RCC->APB2ENR |= 0x0001;                                                          // AFIO Enable               
     GPIOA_Configuration();
     GPIOB_Configuration();
     SysTick_configuration();
     SPI1_configuration();
     SPI_AD8402_CHECK_FUNCTION();


     while(1)
     {
       if(data_flag==1)
       {
            data_flag=0;
            SPI_AD8402_CHECK_FUNCTION();
       }
     }
}


void RCC_Configuration(void)
{
    RCC->CSR|=0x01000000;
     RCC->CR  =0x00010080;                                                     //external high speed clock enabled     
      if ((RCC->CR & RCC_CR_HSEON))                                                                                                                                                                                                                                                                                                                                                                                             
      {                                                                    // if HSE enabled
          while ((RCC->CR & RCC_CR_HSERDY)==0 );                           // Wait for HSERDY = 1 (HSE is ready)


             /******Flash access control reg*******/


          FLASH->ACR=0X12;                                              //prefetch buffer enabled and flash latacy(2 wait state)


          RCC->CFGR= 0x001D8402 ;
            
            RCC->CR|=0x01000000;                                      //PLL enable
            if (RCC->CR & RCC_CR_PLLON) 
            {                                                       // if PLL enabled
              while ((RCC->CR & RCC_CR_PLLRDY) == 0);            // Wait for PLLRDY = 1 (PLL is ready)
            }      
            while ((RCC->CFGR & 0x0000008) ==0);      
      }      
}


void GPIOA_Configuration(void)
{
   RCC->APB2ENR |= 0x0004;                                                      //GPIOA clk enable 
   GPIOA->CRL   =(GPIOA->CRL & 0x0000FFFF) | 0xBBB80000;                    //PA4 i/p  5,6,7 alternate O/P PUSH PULL,O/P MODE 50MHZ                                                                                          //sck,miso,mosi-o/p nss-i/p
   GPIOA->ODR   |=0x10;
}


void GPIOB_Configuration(void)
{
   RCC->APB2ENR |= 0x0008;                                                  //GPIOB clk enable 
   GPIOB->CRL=(GPIOB->CRL & 0xFFFFFFF0) | 0x00000003;                    //PB0 CHIP SELECT  O/P                                                                                         //sck,miso,mosi-o/p nss-i/p
}


void SysTick_configuration(void)   
{
   SysTick->LOAD = 36000;                                       //36000 = 0.5ms FOR 72MHZ SYSTEM FREQ
   SysTick->VAL = 0x00;                                                   //Current counter value
   SysTick->CTRL = 0x7;                                                  //1: Counter enabled,TICKINT:SysTick exception request enable,CLKSOURCE: Clock source selection
}


void SysTick_Handler(void)
{  
static unsigned int count1 = 0;


     if(Count++ > _20_Sec_Delay)
       {
            Count = 0;
          data++;
          data_flag=1;     
            
          if(data>255)
          data = 0;     
       }
     if(Delay_Flag1 == 1)
     {
          if(count1++ >32)                      // 16msec = 32
          {
               count1 = 0;
               Delay_Flag1 = 0;
          }                    
     }
}
                      
void SPI1_configuration(void)
{
  RCC->APB2ENR |= 0x1000;                                //SPI_1 clk enable     
  SPI1->CR1 |= 0x0038;                                      //Baud rate min val fpclk/256 SPI1-72mhz,SPI2-36mhz  
  SPI1->CR1 |= 0x0004;                                     //Master sel-1 (master confi)
  SPI1->CR1 |= 0x0800;                                    //DFF     16BIT
  SPI1->CR1     |= 0x0002;                             //SPI1 CPOL-1 High
  SPI1->CR1     |= 0x0001;                            //SPI1 CPHA-1 in 2nd Clock Cycle
  SPI2->CR1 |= 0x0200;                                //SSM-1 EN
  SPI1->CR2 |= 0x0004;                               //SSOE-1 EN
  SPI1->CR1 |= 0x0040;                            //SPE-1 SPI EN 
}


void SPI_AD8402_CHECK_FUNCTION(void)

  GPIOB->BSRR =(GPIOB->BSRR & 0xFFFFFFFE)|0x0001;                    //SET     PB0 CHIP SELECT-high ,ODR CHECK  
  Delay_Flag1 = 1;
  while(Delay_Flag1);
             
  GPIOB->BSRR =(GPIOB->BSRR & 0xFFFEFFFF)|0x10000;                  //RESET     CHIP SELECT - low   
  Delay_Flag1 = 1;
  while(Delay_Flag1);


  while(SPI1_BUSY_FLAG);     
  Delay_Flag1 = 1;
  while(Delay_Flag1);                                             
 
  SPI1->DR =0x00;                                                          //address      
  Delay_Flag1 = 1;
  while(Delay_Flag1);
         
  while(SPI1_BUSY_FLAG);        
  Delay_Flag1 = 1;
  while(Delay_Flag1);  
      
  SPI1->DR =data; 
  Delay_Flag1 = 1;
  while(Delay_Flag1);


  while(SPI1_BUSY_FLAG);            
  GPIOB->BSRR =(GPIOB->BSRR & 0xFFFFFFFE)|0x0001;
//  Delay_Flag1 = 1;
//  while(Delay_Flag1);
}

Outcomes