This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Set Pin to high

Hello,

I want to set a GPIO Pin to high. Therefore I wrote the code below.

But when the software is written onto the nrf52811, I can not messure any voltage.

I do not want to use any library or external file for this.

//start of code

int main()
{

unsigned int base = 0x50000000; //base adress of GPIO
unsigned int out = 0x504; //offset
unsigned int outAdress = base + out;// adress of OUT Register

unsigned int write = 1; //set first Pin to high
unsigned int *point = outAdress; //Pointer on outAdress

*point = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001

while(1)
{
    //do nothing
}
}

//end of code

Thank you for your help.

  • Hi 

    I do not want to use any library or external file for this.

    May I ask what you are trying to do with the device?

    Unless all you need to do is toggle some pins then not using any external files will make things quite complicated ;)

    As for your code, by default all pins are set as inputs, and you need to write to the DIR register to enable a pin as an output. Can you try this and see if it works better?

    Best regards
    Torbjørn

  • Hi,

    thank you for your reply. I basically want to understand how these registers work, how they are programmed. I want to do some general tests - get into this stuff. 

    As you proposed, I have changed the code. But it still does not work.

    Also I wonder if I am even able to run the chip only with this main.c file. Do you know if there has to be some files, that must be written to chip to operate anything on it?

    The extended code:

    //start of code

    int main()
    {

    unsigned int base = 0x50000000; //base adress of GPIO
    unsigned int outOffset = 0x504; //offset of out
    unsigned int dirOffset = 0x514; //offset of dir

    unsigned int dirAdress = base + dirOffset;// adress of DIR Register
    unsigned int outAdress = base + outOffset;// adress of OUT Register

    unsigned int write = 1;

    unsigned int *dir = dirAdress; //Pointer on dirAdress
    unsigned int *out = outAdress; //Pointer on outAdress

    *dir = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001
    *out = write; //set outAdress to 0000 0000 0000 0000 0000 0000 0000 0001

    while(1)
    {

    }
    }

    //end of code

  • Hi 

    As a colleague of mine pointed out, in order to write to memory mapped registers it is important to define the variables as volatile. Otherwise the compiler could end up optimizing out your writes (or writing them to registers only), since the values you write don't get used anywhere else in the code. 

    The quickest way to 'manually' write directly to a memory mapped peripheral register is follows:

    // Setting the NRF_P0->DIR register to 1
    *((volatile int *)0x50000514) = 1;
    // Setting the NRF_P0->OUT register to 1
    *((volatile int *)0x50000504) = 1;

    Best regards
    Torbjørn

  • Hi

    I tested this code:

    //start of code

    int main() //first line

    {

    // Setting the NRF_P0->DIR register to 1
    *((volatile int *)0x50000514) = 1;
    // Setting the NRF_P0->OUT register to 1
    *((volatile int *)0x50000504) = 1;

    while(1)

    {

    }

    } //last line

    //end of code

    But it does not work. So I wonder if I am doing something wrong at building.

    So I have copied the example receiver from the SDK: examples\peripheral\radio\receiver

    The I opened the file receiver_pca10040.emProject with Segger Embedded Studio

    In this file I changed the main.c file to the code above.

    Then I clicked on Build -> build _pca10040

    Afterwards I upload the receiver_pca10040.hex file, I got in receiver\pca10040\blank\ses\Output\Release\Exe, which has the current date.

    For uploading I use nRF Connect v3.6.1

    Is their something wrong in my Method?

    Best regards
    Tobias

  • Hi Tobias

    What kind of hardware are you trying to program?

    Is it a standard development kit, or a custom board?

    Also, have you tried to flash the example directly from the SES interface, instead of using nRF Connect?

    The pca10040 project is designed for the nRF52832 device, not the nRF52811, so if you are targetting the nRF52811 this might be the problem. 

    Best regards
    Torbjørn

Related