Explorar el Código

Настройка ADC.

TelenkovDmitry hace 1 año
padre
commit
a69dcea769

BIN
doc/adc/ad7793_generic.zip


BIN
doc/adc/ad7793_rl78g13.zip


BIN
doc/adc/ad7793_rx62n.zip


+ 62 - 94
fw/modules/adc/adc_transport.c

@@ -5,6 +5,7 @@
 #include "settings_api.h"
 #include "io_utils.h"
 #include "adc_transport.h"
+#include "misc.h"
 #include <stdio.h>
 
 
@@ -105,6 +106,7 @@ unsigned char SPI_Init(unsigned char lsbFirst,
     return(1);
 }
 
+
 /***************************************************************************//**
  * @brief Writes data to SPI.
  *
@@ -115,57 +117,36 @@ unsigned char SPI_Init(unsigned char lsbFirst,
  *
  * @return Number of written bytes.
 *******************************************************************************/
-unsigned char SPI_Write(unsigned char* data,
-                        unsigned char bytesNumber)
+unsigned char SPI_Write(unsigned char* data, unsigned char bytesNumber)
 {
-    unsigned char readData[4]	= {0, 0, 0, 0};
+    unsigned char chipSelect   = data[0];
+    unsigned char writeData[4] = {0, 0, 0, 0};
+    unsigned char readData[4]  = {0, 0, 0, 0};
+    
+    if (chipSelect == 1)
+        MS5192T_CS_LOW;
+    
+    for (unsigned char byte = 0; byte < bytesNumber; byte++)
+    {
+        writeData[byte] = data[byte + 1];
+    }
     
     for (int i = 0; i < bytesNumber; i++)
     {
         while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
-        MS5192T_SPI->dt = data[i];
+        MS5192T_SPI->dt = writeData[i];
   
         while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_RDBF_FLAG) == RESET);
         readData[i] = MS5192T_SPI->dt;
     }  
-#if 0  
-  	unsigned char chipSelect    = data[0];
-	unsigned char writeData[4]  = {0, 0, 0, 0};
-    unsigned char readData[4]	= {0, 0, 0, 0};
-    unsigned char byte          = 0;
     
-    for(byte = 0;byte < bytesNumber;byte ++)
-    {
-        writeData[byte] = data[byte + 1];
-    }
-    if(chipSelect == 1)
-    {
-        ADI_PART_CS_LOW;
-    }
-    if(chipSelect == 2)
-    {
-        ST7579_CS_LOW;
-    }
-    for(byte = 0;byte < bytesNumber;byte ++)
-    {
-        R_CSI10_Send_Receive((uint8_t *)&writeData[byte], 
-                             1,
-                             (uint8_t *)readData);
-	    while(CSIIF10 == 0);
-    }
-    if(chipSelect == 1)
-    {
-        ADI_PART_CS_HIGH;
-    }
-    if(chipSelect == 2)
-    {
-        ST7579_CS_HIGH;
-    }
-
-	return(bytesNumber);
-#endif    
+    if (chipSelect == 1)
+        MS5192T_CS_HIGH;
+    
+    return bytesNumber;
 }
 
+
 /***************************************************************************//**
  * @brief Reads data from SPI.
  *
@@ -178,22 +159,23 @@ unsigned char SPI_Write(unsigned char* data,
  *
  * @return Number of written bytes.
 *******************************************************************************/
-unsigned char SPI_Read(unsigned char* data,
-                       unsigned char bytesNumber)
+unsigned char SPI_Read(unsigned char* data, unsigned char bytesNumber)
 {
-    unsigned char writeData[4]  = {0, 0, 0, 0};
-    unsigned char readData[4]	= {0, 0, 0, 0};
-    unsigned char byte          = 0;
+    unsigned char chipSelect   = data[0];
+    unsigned char writeData[4] = {0, 0, 0, 0};
+    unsigned char readData[4]  = {0, 0, 0, 0};
+    unsigned char byte         = 0;
     
-    for(byte = 0;byte < bytesNumber;byte ++)
+    for (byte = 0; byte < bytesNumber; byte++)
     {
         writeData[byte] = data[byte + 1];
         data[byte + 1] = 0;
     }
     
-    MS5192T_CS_LOW;
+    if (chipSelect == 1)
+        MS5192T_CS_LOW;
     
-    for(byte = 0;byte < bytesNumber;byte ++)
+    for (byte = 0; byte < bytesNumber; byte++)
     {
         while (spi_i2s_flag_get(MS5192T_SPI, SPI_I2S_TDBE_FLAG) == RESET);
         MS5192T_SPI->dt = writeData[byte];
@@ -202,56 +184,15 @@ unsigned char SPI_Read(unsigned char* data,
         readData[byte] = MS5192T_SPI->dt;
     }
     
-    MS5192T_CS_HIGH;
+    if (chipSelect == 1)
+        MS5192T_CS_HIGH;
     
-    for(byte = 0;byte < bytesNumber;byte ++)
+    for (byte = 0; byte < bytesNumber; byte++)
     {
         data[byte] = readData[byte];
     }  
       
-    return(bytesNumber);  
-      
-#if 0  
-  	unsigned char chipSelect    = data[0];
-	unsigned char writeData[4]  = {0, 0, 0, 0};
-    unsigned char readData[4]	= {0, 0, 0, 0};
-    unsigned char byte          = 0;
-    
-    for(byte = 0;byte < bytesNumber;byte ++)
-    {
-        writeData[byte] = data[byte + 1];
-        data[byte + 1] = 0;
-    }
-    if(chipSelect == 1)
-    {
-        ADI_PART_CS_LOW;
-    }
-    if(chipSelect == 2)
-    {
-        ST7579_CS_LOW;
-    }
-    for(byte = 0;byte < bytesNumber;byte ++)
-    {
-        R_CSI10_Send_Receive((uint8_t *)&writeData[byte], 
-                             1,
-                             (uint8_t *)&readData[byte]);
-	    while(CSIIF10 == 0);
-    }
-    if(chipSelect == 1)
-    {
-        ADI_PART_CS_HIGH;
-    }
-    if(chipSelect == 2)
-    {
-        ST7579_CS_HIGH;
-    }
-    for(byte = 0;byte < bytesNumber;byte ++)
-    {
-        data[byte] = readData[byte];
-    }
-    
-	return(bytesNumber);
-#endif    
+    return bytesNumber;  
 }
 
 //
@@ -259,14 +200,41 @@ void adc_task(void *params)
 {
     bool state = false;
     unsigned long value;
+    uint8_t ret;
+    
+    vTaskDelay(1000);
     
     adc_gpio_init();
     
     MS5192T_Reset();
     
+    ret = MS5192T_Init();
+    
+    printf("ADC init status: %s\r\n", ret == 1 ? "OK" : "FAILED");
+        
+    // Запрос регистра конфигурации для (0x710 - значение по умолчанию)
+    value = MS5192T_GetRegisterValue(MS5192T_REG_CONF, 2, 1);
+    printf("ADC configuretion register: 0x%X\r\n", value);
+
+    // Коэф-т усиления: 1
+    printf("ADC. Set gain rate: %u\r\n", (uint8_t)MS5192T_GAIN_1);
+    MS5192T_SetGain(MS5192T_GAIN_1);
+    value = MS5192T_GetRegisterValue(MS5192T_REG_CONF, 2, 1);
+    printf("ADC configuretion register: 0x%X, gain rate = %u (value 0 eq 1 gain rate)\r\n",
+           value, (value & 0x700) >> 8);
+    
+    printf("Set unipolar input mode.\r\n");
+    MS5192T_SetPolar(MS5192T_CONF_UNIPOLAR);
+    value = MS5192T_GetRegisterValue(MS5192T_REG_CONF, 2, 1);
+    printf("ADC configuretion register: 0x%X, polar bit: %u\r\n", value, (value & 0x1000) >> 12);
+    
+    
     for (;;)
     {
-        if (state == false) {
+        
+#if 0      
+        if (state == false) 
+        {
             if (MS5192T_Init() == 1) 
             {
                 vTaskDelay(2000);
@@ -298,7 +266,7 @@ void adc_task(void *params)
         }
         
         printf("Value: %u\r\n", value);
-        
+#endif        
         vTaskDelay(1000);
     }
 }

+ 37 - 9
fw/modules/adc/ms5192t.c

@@ -11,18 +11,19 @@
  *                  Example: 1 - if initialization was successful (ID is 0x0B).
  *                           0 - if initialization was unsuccessful.
 *******************************************************************************/
-unsigned char   MS5192T_Init(void)
+unsigned char MS5192T_Init(void)
 { 
 	unsigned char status = 0x1;
     
-    if((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
+    if ((MS5192T_GetRegisterValue(MS5192T_REG_ID, 1, 1) & 0x0F) != MS5192T_ID)
 	{
 		status = 0x0;
 	}
     
-	return(status);
+	return status;
 }
 
+
 /***************************************************************************//**
  * @brief Sends 32 consecutive 1's on SPI in order to reset the part.
  *
@@ -30,12 +31,14 @@ unsigned char   MS5192T_Init(void)
 *******************************************************************************/
 void MS5192T_Reset(void)
 {
-	unsigned char dataToSend[5] = {0xff, 0xff, 0xff, 0xff};
+	unsigned char dataToSend[5] = {0x03, 0xff, 0xff, 0xff, 0xff};
 	
     MS5192T_CS_LOW;	    
-	SPI_Write(dataToSend,4);
+	SPI_Write(dataToSend, 4);
 	MS5192T_CS_HIGH;	
 }
+
+
 /***************************************************************************//**
  * @brief Reads the value of the selected register
  *
@@ -54,14 +57,17 @@ unsigned long MS5192T_GetRegisterValue(unsigned char regAddress,
     
 	data[0] = 0x01 * modifyCS;
 	data[1] = MS5192T_COMM_READ | MS5192T_COMM_ADDR(regAddress); 
-	SPI_Read(data,(1 + size));
-	for(i = 1;i < size + 1;i ++)
-    {
+    
+	SPI_Read(data, (1 + size));
+    
+	for (i = 1; i < size + 1; i++) {
         receivedData = (receivedData << 8) + data[i];
     }
     
-    return (receivedData);
+    return receivedData;
 }
+
+
 /***************************************************************************//**
  * @brief Writes the value to the register
  *
@@ -164,14 +170,36 @@ void MS5192T_SetGain(unsigned long gain)
     command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
                                       2,
                                       1); // CS is modified by SPI read/write functions.
+    
     command &= ~MS5192T_CONF_GAIN(0xFF);
     command |= MS5192T_CONF_GAIN(gain);
+    
+    MS5192T_SetRegisterValue(
+            MS5192T_REG_CONF,
+            command,
+            2,
+            1); // CS is modified by SPI read/write functions.
+}
+
+
+void MS5192T_SetPolar(unsigned long polar)
+{
+    unsigned long command;
+    
+    command = MS5192T_GetRegisterValue(MS5192T_REG_CONF,
+                                      2,
+                                      1); // CS is modified by SPI read/write functions.
+    
+    command &= ~(1 << 12);
+    command |= polar;
+    
     MS5192T_SetRegisterValue(
             MS5192T_REG_CONF,
             command,
             2,
             1); // CS is modified by SPI read/write functions.
 }
+ 
 /***************************************************************************//**
  * @brief Sets the reference source for the ADC.
  *

+ 3 - 0
fw/modules/adc/ms5192t.h

@@ -135,6 +135,9 @@ void MS5192T_SetChannel(unsigned long channel);
 /* Sets the gain of the In-Amp. */
 void MS5192T_SetGain(unsigned long gain);
 
+/* Unipolar/Bipolar Enable */
+void MS5192T_SetPolar(unsigned long polar);
+
 /* Sets the reference source for the ADC. */
 void MS5192T_SetIntReference(unsigned char type);
 

+ 3 - 1
fw/modules/io/analog_input.c

@@ -121,6 +121,8 @@ void ai_mode_test(void)
 {
     for (uint8_t i = 1; i < 13; i++)
     {
-        ai_set_mode(MEAS_CURRENT, i);
+        ai_set_mode(MEAS_VOLTAGE, i);
     }
 }
+
+

+ 6 - 5
fw/user/main.c

@@ -56,9 +56,9 @@ int main(void)
     
     xTaskCreate(button_task, "button_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
  
-    //xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    xTaskCreate(adc_task, "adc_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
     
-    xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
+    //xTaskCreate(dac_task, "dac_task", 2*configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL);
     
     taskEXIT_CRITICAL();
     
@@ -153,12 +153,13 @@ void init_task(void *argument)
     
 // Тесты аналоговых входов    
     //ai_connect_test();
-    //ai_mode_test();
-    //ai_connect_channel(V_ISO);  
+    ai_mode_test();
+    ai_connect_channel(V_ISO);  
+    ai_connect_channel(AN_INP_8);  
     
 // -------------------------------------------------------------------------- //    
 // DAC    
-    dac_gpio_init();
+    //dac_gpio_init();
     //dac_test(CH_DAC_1, 0);  
 /*    
     dac_test(CH_DAC_2, 10000);  

BIN
output/fw.bin


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 342 - 350
project/ewarm/iap/iap.dep


La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 839 - 834
project/ewarm/module_universal_io.dep


Algunos archivos no se mostraron porque demasiados archivos cambiaron en este cambio