Home > OS >  C language for C8051F120 Silicon Labs
C language for C8051F120 Silicon Labs

Time:12-06

I'm trying to control a pin and set its logic level to '1' and then '0' (basically blinking an LED) but for some reason I can't even get this basic thing to work.

From digging around I found that I first need to set the bit/port to output mode and only then change the output level of the pin.

Here's my full code, and any sort of guidance would be very greatly appreciated.

#include <stdio.h>
#include <string.h>

#include "inc\C8051F120.h"

sbit ON_BOARD_LED = P2^0;

void main(void)
{
    int i = 0;

    P2MDOUT = 0xFF;

    while(1)
    {

        for(i = 0; i < 10000; i  );

        ON_BOARD_LED = 0;

        for(i = 0; i < 10000; i  );

        ON_BOARD_LED = 1;
    }
}

Any ideas what I may be doing wrong or where I could get more relevant information?

Thank you in advance, Matt

CodePudding user response:

Consider changing the maximum value for the loop. I think it´s too fast to be seen if the C8051F120 runs at 100MHz. Change to at least 10^8 to blink one second on , another off.

Also check the following. From https://www.silabs.com/documents/public/data-sheets/C8051F12x-13x.pdf

"The output drivers on Ports 0 through 3 remain disabled until the Crossbar is enabled by setting XBARE (XBR2.4) to a logic 1." page 239

Try adding XBR2 = 0x40; before the P2MDOUT = 0xFF;

CodePudding user response:

From this example it seems you should use a larger count for 1 millisecond.

#define t 200000 // millisecond delay function constant;

delay(signed long y) {
    unsigned int i;
    for(i=0;i<y;i  ){
      ;
    }
}

delay(t);  // for 1 millisecond

From the Keil header file the port definition is:

sfr P2            = 0xA0; /* PORT 2 LATCH */

From the Keil compiler sbit page

sbit name = sfr-name ^ bit-position;
sbit name = sfr-address ^ bit-position;
sbit name = sbit-address;

So it seems your P2^0 is correct.

  • Related