nexusone1984 32 Posted April 21, 2011 Share Posted April 21, 2011 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. Quote Link to post Share on other sites
bluehash 1,581 Posted April 21, 2011 Share Posted April 21, 2011 Hi Nexus, Gatesphere has the MSPhere library - You can build over it or you can create your own small library and share it. If you start, people can add to it once you have the architecture in. Quote Link to post Share on other sites
gatesphere 45 Posted April 21, 2011 Share Posted April 21, 2011 Nice ideas. I'm open to collaboration, should you like to work together. I haven't updated it in a long while since I've been in classes, but the semester ends in 3 weeks, so I'll be able to tinker again soon! There will be an update soon, I promise. Quote Link to post Share on other sites
nexusone1984 32 Posted April 22, 2011 Author Share Posted April 22, 2011 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. Quote Link to post Share on other sites
bluehash 1,581 Posted April 22, 2011 Share Posted April 22, 2011 - You can ask the user to setup their timer every 1ms and call a tick function in your library - Set up the watchdog as a 1ms timer as most people disable it and go for timer A Quote Link to post Share on other sites
nexusone1984 32 Posted April 22, 2011 Author Share Posted April 22, 2011 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; } Quote Link to post Share on other sites
zeke 693 Posted April 22, 2011 Share Posted April 22, 2011 I bet your variables aren't global and you don't clear the interrupt flag in your ISR. Quote Link to post Share on other sites
nexusone1984 32 Posted April 22, 2011 Author Share Posted April 22, 2011 I bet your variables aren't global and you don't clear the interrupt flag in your ISR. Strange I changed _eint() to _EINT() and my seconds from char to int, one of those two that made it work.... Quote Link to post Share on other sites
zeke 693 Posted April 22, 2011 Share Posted April 22, 2011 one is a macro and one isn't. Quote Link to post Share on other sites
nexusone1984 32 Posted April 23, 2011 Author Share Posted April 23, 2011 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. Quote Link to post Share on other sites
RobG 1,892 Posted April 23, 2011 Share Posted April 23, 2011 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 Quote Link to post Share on other sites
nexusone1984 32 Posted April 24, 2011 Author Share Posted April 24, 2011 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.... Quote Link to post Share on other sites
bluehash 1,581 Posted April 24, 2011 Share Posted April 24, 2011 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 Quote Link to post Share on other sites
nexusone1984 32 Posted April 24, 2011 Author Share Posted April 24, 2011 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 Quote Link to post Share on other sites
RobG 1,892 Posted April 24, 2011 Share Posted April 24, 2011 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. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.