Reply
Posts: 5
Registered: ‎10-07-2016

transmit a packet every 100ms

I am using the SI1060 development kit. I want to transmit a packet every 100ms using the Timer operation. I know that the clock frequency is 24.5Mhz but I am not sure how to initialise the Timer register such as Timer 0 or Timer 3. Does anyone have the code to generate this timer of 100ms?. Thanks

Posts: 488
Registered: ‎02-21-2014

Re: transmit a packet every 100ms

In the old 8-bit IDE, there are several examples for how to configure the timers. I've attached the Timer2_16bitReloadTimer.c file that toggles and LED every 50ms. You could easily adapt this to interrupt at 100ms instead for your transmission.

Posts: 5
Registered: ‎10-07-2016

Re: transmit a packet every 100ms

Hi, thanks for replying. However I cant compile it.  Having some problem. I am trying to configure my Timer0, I want to create a 50 ms second delay. Testing it out on the LED 2 on my board but it doesnt blink according. It just stays lighted up. Please help. You can refer to the SI 106x-108x datasheet for reference. Thanks

 

 */
void main(void)
{

    int i=0;

  while (TRUE)
  {
      //for(i=0;i<1000;i++)
    //{    
          vCio_SetLed(0x02);
          delay_t0();
    //}

    //for(i=0;i<1000;i++)
    //{
        vCio_ClearLed(0x02);
        delay_t0();
    //}

          
 

   }

 }

 

 

void delay_timer0();
{
    CKCON=0x02;  clock control( select sysclk/48 for timer 0);
    TMOD=0x01; (16bit timer)
    TH0=0x9C;  (high byte)
    TL0=0x4F;  (Low byte)
    ET0=1;        (Enable interrupt)
    TR0=1;       (Enable timer run)
    EA=1;
    while(TF0==0);  (wait here till overflow)
    TF0=0;  (Reset flag)
    TR0=0;   (Timer run )
}

 

Posts: 2,384
Registered: ‎10-14-2014

Re: transmit a packet every 100ms

[ Edited ]

@alighaus828

You code is not complete, delay_t0 was not declared/defined but was called.

Could you please upload the full code?

WeiguoLu
Posts: 3,153
Registered: ‎02-07-2002

Re: transmit a packet every 100ms

Further, where is the interrupt handler?

And if you're using interrupts then why are you polling for the TF0 flag?

Posts: 5
Registered: ‎10-07-2016

Re: transmit a packet every 100ms

Hi, thanks for replying. I have declared the delay_t0() function. I tried to fiddle around with the CLKSEL register, but to no avail as well. I have written this program in the bi-directional data transmission file. But I have commented out the code related to the bi-directional transmission. I just want to be able to first blink the LED 2 with a delay of 50 or 100ms using the Timer0.  Currently, the LED 2 just stays on. I am not sure if my Timer0 configuration is wrong. Do help me check. Thanks guys

 

void delay_t0(void);

void delay_t0()
{    
    //CLKSEL=0x98BD90;
    CKCON=0x02;
    TMOD=0x01;
    TH0=0x9C;
    TL0=0x4F;
    //ET0=1;
    TR0=1;
    //EA=1;
    while(TF0==0);
    TF0=0;
    TR0=0;
}



void main(void)
{

    int i=0;
  // Initialize the Hardware and Radio
  vInitializeHW();

#ifdef SILABS_LCD_DOG_GLCD
  /* Initialize graphic LCD */
  vLcd_InitLcd();

  /* Set logo location to center */
  bLcd_LcdSetPictureCursor(bLcd_Line1_c, 35u);

  /* Draw SiLabs logo */
  vLcd_LcdDrawPicture(silabs66x30);
#endif
  // Start RX with Packet handler settings
  vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber,0u);

  while (TRUE)
  {
      //for(i=0;i<1000;i++)
    //{    
          vCio_SetLed(0x02);
        delay_t0();
    //}

    //for(i=0;i<1000;i++)
    //{
        vCio_ClearLed(0x02);
        delay_t0();
    //}

          
    // The following Handlers requires care on invoking time interval
    /*if (wIsr_Timer2Tick)
    {
      if (lPer_MsCnt < 0xFFFF)
      {
        lPer_MsCnt++;
      }

      vHmi_LedHandler();
      vHmi_BuzzHandler();
      vHmi_PbHandler();

      wIsr_Timer2Tick = 0;
    }*/

    // Demo Application Poll-Handler function
    DemoApp_Pollhandler();
  }
}

Posts: 5
Registered: ‎10-07-2016

Re: transmit a packet every 100ms

Hi guys, for Si1060, how long is one machine cylce?

 

Like for standard 8051 microcontrollers, the frequency of clock is 11.059Mhz. and one machine cycle takes 12 pulses of the crystal.  To be able to use timer0 in 8051, the 11.059/12=921Khz with a period of 1,085 micro seconds is fed into timer0.

 

Similary, for SI1060 I need to know the length of one machine cycle and how many pulses does it take so I can find the frequency going into TImer0. Anyone know? The clock frequency here in this case is 24.5Mhz. Thanks.

Posts: 488
Registered: ‎02-21-2014

Re: transmit a packet every 100ms

SYSCLK is the clock frequency. In your case, 24.5 MHz. The datasheet has diagrams on how this clock is fed into the timer module. You can see here that you can select either SYSCLK (24.5 MHz), SYSCLK/12, RTC/8, or EXTCLK as the timer 2 clock sources.

 

timer2.PNG

Posts: 2,384
Registered: ‎10-14-2014

Re: transmit a packet every 100ms

[ Edited ]

@alighaus828

Check if below code could work on you device, I tested it on C8051F930 board and it works fine:

//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------

#include <compiler_defs.h>
#include <C8051F930_defs.h>            // SFR declarations

//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------

#define  SYSCLK         24500000       // System clock frequency in Hz


SBIT (YELLOW_LED, SFR_P1, 6);          // YELLOW_LED==LED_ON means ON



//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------

void T0_Wait_ms (U8 ms);

//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------

void main (void)
{
   volatile U8 dat;                    // Test counter
   U8 i;                               // Dummy variable counters

   PCA0MD &= ~0x40;                    // WDTE = 0 (watchdog timer enable bit)

   OSCICN |= 0x80;                     // Enable precision internal oscillator
   CLKSEL =  0x00;                     // Select precision internal oscillator
                                       // divided by 1 as system clock
	//Enter default mode
    XBR2 = 0x40; 
	while (1) {
		T0_Wait_ms (50);
		YELLOW_LED = !YELLOW_LED;

	}   
}


//-----------------------------------------------------------------------------
// T0_Wait_ms
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters   :
//   1) U8 ms - number of milliseconds to wait
//                        range is full range of character: 0 to 255
//
// Configure Timer0 to wait for <ms> milliseconds using SYSCLK as its time
// base.
//
//-----------------------------------------------------------------------------
void T0_Wait_ms (U8 ms)
{
   TCON &= ~0x30;                      // Stop Timer0; Clear TF0
   TMOD &= ~0x0f;                      // 16-bit free run mode
   TMOD |=  0x01;

   CKCON |= 0x04;                      // Timer0 counts SYSCLKs

   while (ms) {
      TR0 = 0;                         // Stop Timer0
      TH0 = ((-(SYSCLK/1000)) >> 8);   // Overflow in 1ms
      TL0 = ((-(SYSCLK/1000)) & 0xFF);
      TF0 = 0;                         // Clear overflow indicator
      TR0 = 1;                         // Start Timer0
      while (!TF0);                    // Wait for overflow
      ms--;                            // Update ms counter
   }

   TR0 = 0;                            // Stop Timer0
}

//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------

 

 

WeiguoLu
Posts: 3,153
Registered: ‎02-07-2002

Re: transmit a packet every 100ms

YELLOW_LED = ~YELLOW_LED;

That is a dangerous construction in C though Keil may do what you expected instead of what the C standard specifies. According to the standard the result is always a non-zero integer. If YELLOW_LED has type bool it will always be true.

Posts: 2,384
Registered: ‎10-14-2014

Re: transmit a packet every 100ms

@vanmierlo

Yes, I corrected that error in the post. thanks a lot!

WeiguoLu
Posts: 30
Registered: ‎03-24-2016

Re: transmit a packet every 100ms

FWIW re one's complement: K&R section 2.9, pp. 45 says

 

"The unary operator ~ yields the one's complement of an integer; that is, it converts each 1-bit into a 0-bit and vice versa."

 

As I understand it, this means that

~0x01=0xFE, ~0xFF=0x00, and ~0x00=0xFF

so the result of applying the one's complement operator can be zero. I would expect that, if the "integer" only has one bit, the result would be what the poster expected.

Posts: 3,153
Registered: ‎02-07-2002

Re: transmit a packet every 100ms

The C standard does not support an integer of only one bit. An integer must at least have 16 bits. And it also requires every operand to be upcast to integer if it has less bits. That is why this is dangerous code. To follow your example:

 

~0x01=~0x0001=0xFFFE
~0xFF=~0x00FF=0xFF00
~0x00=~0x0000=0xFFFF

When this result is assigned to a char or 1 bit sized bitfield the msb's are discarded, but when assigned to a bool it is compared against zero. The bit/sbit types are a non-standard extension and every compiler can choose how to implement it.

 

E.g. SDCC chose to make it behave like bool because that at least is specified. I don't remember it being explicitly specified for other compilers how they implement arithmetic operations on bit/sbit.

 

If instead of using ~ you use ! or ^1 the result is always defined.

Posts: 30
Registered: ‎03-24-2016

Re: transmit a packet every 100ms

You're right (drifting this thread even more). I found that, with gcc 4.7.2 on Linux for 64-bit Intel, the statement:

char a=~0xff;

does result in an overflow warning on compilation. The resulting executable produces the intended result; but it is still a warning message. Anyone who is truly determined can suppress the warning either with the -Wno-overflow option on the command line, or by casting:

char a=(char)~0xff;

Somewhat curiously, the variants

char a=~-1;
char a=~0,b=~a;

are accepted without murmur, and have the intended results.

 

You can tell I have a lot of time on my hands.