Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Step 1: define a single struct adc_t for all the registers

step 2: assign a pointer to the adc_t struct at the memory-mapped base address

step 3: create a uint16_t pot_read(void), dividing the adc sample by 4 to put 12-bit value into the range from 0 to 1023

step 4: call above function in main

What I have tried:

THIS IS WHAT I HAVE SO FAR.
any help would be appreciated

#include <stdint.h>
#include "led.h"
#include <assert.h>

/*!
* @brief Delay for some number of milliseconds, by wasting CPU cycles.
*
* @param[in] ms  Number of milliseconds to busy-wait.
* @return void
*/


// a single struct for all the registers 
typedef struct {

  uint8_t AD_Control_Register ;
  
  uint8_t unused1[3];
  uint16_t AD_Channel_Select_Register_0;
  
  uint16_t AD_Channel_Select_Register_1;
  
  uint16_t AD_Converted_Value_Addition_Mode_Select_Register_0;

  uint16_t  AD_Converted_Value_Addition_Mode_Select_Register_1;
  
  uint8_t AD_Converted_Value_Addition_Count_Select_Register;
   
  uint16_t AD_Control_Extended_Register ;
  
  uint8_t AD_Start_Trigger_Select_Register;
  
  uint16_t AD_Converted_Extended_Input_Control_Register;
  
  uint16_t AD_Temperature_Sensor_Data_Register;
  
  uint16_t AD_Internal_Reference_Voltage_Data_Register;
  
  uint16_t AD_Data_Register_y;
  
  uint8_t unused2[11];
  uint16_t AD_Sampling_State_Register_01 ;
  
  uint8_t unused3[9];
  uint16_t AD_Sampling_State_Register_23;
  

} adc_t;


// assign a pointer to the memory-mapped base address 
adc_t* adc_regs = (adc_t *)adc_regs_base_address;


/* new function:
   this function will divide the raw ADC sample by 4 to put the 12 bit value into the range 
   0 to 1023 ms
*/
uint16_t pot_read()
{
    // adc_t *adc_regs = get mapped address here;
  
    // create the 12-bit value if necessary (if the registers are 8-bit wide), and divide by four.
    uint16_t adc_value = ((adc_regs -> value_lo) | ((adc_regs -> value_hi & 0x0f) << 8));
    
    // unmap here if necessary
    
    return adc_value / 4;
    
} 
Posted
Updated 25-Aug-16 12:30pm
v2

1 solution

This can't be answered without additional information.

The structure name adc_t indicates that it refers to some kind of ADC (Analogue to Digital Converter) registers. These are usually specified in the datasheet of the embedded system.

Assigning a pointer to this structure requires to know the address. Depending on the system it might be sufficient to just assign the address using casting:
C++
adc_t* adc_regs = (adc_t *)adc_regs_base_address;

where adc_regs_base_address is the hard coded address defined as constant (e.g. by a define statement or as hex address).

If this is not possible there might be a system function to map the address (e.g. mmap(2): map/unmap files/devices into memory - Linux man page[^] with Linux).

The third step requires knowing the ADC registers. With a typical system, use the above mapped memory to read the corresponding register(s), create the 12-bit value if necessary (if the registers are 8-bit wide), and divide by four.
A typical code example might be:
C++
uint16_t pot_read()
{
    //adc_t *adc_regs = get mapped address here;
    uint16_t adc_value = adc_regs->value_lo | 
        ((adc_regs->value_hi & 0x0f) << 8);
    // unmap here if necessary
    return adc_value / 4;
}
 
Share this answer
 
Comments
Jochen Arndt 26-Aug-16 2:30am    
I noticed that you have updated your question by defining the ADC register names and inserting the code snippets from my answer. For others reading the updated question my answer seems to be useless now.

So where is the problem of replacing the register names of my example with the name of the data register from the adc_t struct in the pot_read() function (it is a 16 bit register making the function even simpler)?

The mapping problem can't still be solved without knowing the system.

You have also accepted my answer and undone that some hours later. This is and editing the question in a way that my answer seems useless is rude behaviour.

Your question looks like a homework assignment. For such a question I gave you more information than you would have got from most others here at CodeProject.

If it is not a homework assignment and you have to develop the system you have to learn how to read datasheets and system specifications to get the information that is required to implement such tasks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900