Jump to content
43oh

timer routines / events / etc


Recommended Posts

Timing of events that happen at random times, and creating a timing library?

 

I not played with the Arduino yet, people talk about its library of functions.

I know some people are working on a Arduino compatible library for the 430.

 

I was thinking of creating some useful library of functions:

 

1. Real time clock - What do you think of the TI RTC library? better way to do this?

2. Software PWM - PWM_int( pin, freq, duty cycle), PWM_stop(pin)

3. Switch input with debounce SW_inputdb_int(pin) state change only after time is allowed for switch bounce. SW_inputdb_rd(pin) , maybe a low to high or high to low interrupt, holding down button function (after X seconds change function)

4. binary to BCD functions,

 

Just throwing some stuff out there.... maybe some of you have already created some of these routines.

Link to post
Share on other sites

Ok... For the initial part of my library, I am starting with basic RTC clock functions: hour, minute, seconds, milliseconds.

 

What is the best approach to mark time, using the Watchdog timer or timer_a to interrupt every 1 millisecond.

 

Also want to have other task going and this to happen in the back ground.

Link to post
Share on other sites

I going with timer A to keep track of time, but having trouble getting the interrupt to work for timer A.

 

I running linux and using gcc compiler.

 

my initializing of the timer A:

 

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

//1Mhz

BCSCTL1 = CALBC1_1MHZ; // Set range

DCOCTL = CALDCO_1MHZ; // Set DCO step + modulation */

CCTL0 = CCIE; // CCR0 interrupt enabled

CCR0 = 1000; // divide the system clock to get 1 millisecond

TACTL = TASSEL_2 + MC_2; // SMCLK, contmode

eint();

 

while(1)

{

displayLED(seconds);

}

}

 

 

my interrupt routine:

interrupt(TIMERA0_VECTOR) Timer_A (void)

{

milliseconds++;

if (milliseconds > 999)

{

seconds++;

milliseconds = 0;

}

if (seconds > 59)

{

minutes++;

seconds = 0;

}

if (minutes > 59 )

{

hours++;

minutes = 0;

}

if (hours > 13) hours = 1;

CCR0 += 1000;

}

Link to post
Share on other sites

Here is one of the routines I been working on: Binary to BCD. Suggestions, improvements?

 

Useage: binary2bcd16( one of 5 digit's, 16 bit binary number)

 

bcd = binary2bcd16( 3, 5432 ) will return third digit from the right.

 

bcd = 4

 

int binary2bcd16( int d, int t )  // 16bit
{
int i,bcd[5];

while( i < 5)
{
bcd[i] = 0;
i++;
}

    if ( t > 9999 ) bcd[4] = t / 10000;
    if ( t > 999  ) bcd[3] = t  / 1000 - bcd[4] * 10;
    if ( t > 99 ) bcd[2] = t / 100 - (bcd[3] * 10 + bcd[4] * 100); 
    if ( t > 9 ) bcd[1] = t / 10 - (bcd[2] * 10 + bcd[3] * 100 + bcd[4] * 1000);
    bcd[0] = t - (bcd[1] * 10 + bcd[2] * 100 + bcd[3] * 1000 + bcd[4] * 10000);      


return(bcd[d]);
}

 

Changes could be have it return all five digits in an array.

Link to post
Share on other sites

You could do something like this:

 

unsigned char d = 0;
while(binData > 0) {
     bcdArray[d] = binData % 10;
     binData /= 10;
     d++;
}
while(d  < bcdArraySize) {
   bcdArray[d] = 0;
   d++;
}

 

If you do not want to use array, you could do this:

 

 unsigned int multiplier = 1;      // temporary multiplier variable
        while(binary > 0) {               // convert binary to bcd
           bcdData += (binary % 10) * multiplier;   // add reminder * multiplier to bcdData
           binary /= 10;
           multiplier *= 16;
        }

 

If you need to get a specific digit, you can shift ((d-1) * 4) bits to the right and mask if with 0x0F

Link to post
Share on other sites

I will look into it, I forget a lot of coding stuff since I not in it 100% of the time.

Maybe would be good to have options on the BCD decoding , on the other hand you want to reduce cycle time doing redundant stuff. Right now I go through the whole process to retrieve each digit.

 

Maybe two functions 8 bit convert and 16 bit convert.

 

One problem I came across is I not sure how to pass an array from my function back to my main routine.

 

main loop

 

int bcd_data[5]; // to hold a 16 bit binary converted to bcd.

 

binary2bcd16( bcd_data, binary_number) // send a pointer to the address of my variable in this case do I use & or *

 

 

int binary2bcd( int *bcd[], int) // correct way to point back to my variable in the main program?

{

 

any pointers.... :D

Link to post
Share on other sites
int binary2bcd( int *bcd, int) // correct way to point back to my variable in the main program? 
{

}

void main(void)
{
  int bcd_data[5]; // to hold a 16 bit binary converted to bcd
  int binary_number = 5;

  binary2bcd16( bcd_data, binary_number) 
}


Good explanation here:
http://stackoverflow.com/questions/1106957/pass-array-by-reference-in-c


Link to post
Share on other sites

Thanks Bluehash... that fixed the problem.

 

Just an over view of what I have done so far.

 

I created a timing routine that controls the timing of events:

 

1. Real Time clock

2. Refresh display - now done in back ground of the main program.

3. Control to make a digit blink.

 

Binary to BCD conversion

 

Driving the 74hc595 8-bit shift register.

 

Seven segment table - include Hex code characters and a few special characters.

 

To do:

Clean up code.

more code remark's

Switch de-bounce

Switch up time/down time

I sure a few more things will come to mind.

 

 


Good explanation here:
http://stackoverflow.com/questions/1106957/pass-array-by-reference-in-c


Link to post
Share on other sites

Keep in mind, that for simple programs you can just use global vars, you do not need to use references (which require additional memory and at 128 RAM things might get tight.) On top of that, each function call requires space on stack (taken out of your available RAM,) which in turn may lead to out of memory conditions if you not careful with your calls.

Also, since you are using 595s, the second code snipped in my previous post was written just for that reason, to convert binary into BCD encoded integer which then can be sent to shift register.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...