Apaseo 3 Posted December 24, 2013 Share Posted December 24, 2013 Hello, I was wondering if someone could help me create or find a delay function. I was thinking a 1ms delay function could work. I would like to have this, so that I can have some timed prompts on an LCD that I have working. Below is a fraction of my main.c I hope you guys can help me. main(){ LcdClrDisp(); LcdMoveCursor(1,4); LcdDispStrg("It is"); //-->Delay Function Here LcdClrDisp(); LcdMoveCursor(2,4); LcdDispStrg("Working :)"); //-->Delay Function Here } Thanks Quote Link to post Share on other sites
chicken 630 Posted December 24, 2013 Share Posted December 24, 2013 _delay_cycles(), at least when working with CCS, might do the job. As it counts cycles, it's dependent on the CPU clock. For milliseconds, it would look something like this: _delay_cycles(MCU_clock_in_Hz / 1000 * wait_time_in_ms); Apaseo 1 Quote Link to post Share on other sites
Apaseo 3 Posted December 24, 2013 Author Share Posted December 24, 2013 I have been using that, but it is not very accurate. Here is what I was using: BCSCTL1 |= DIVA_3; // ACLK/8 BCSCTL3 |= XCAP_3; BCSCTL1 = CAL_BC1_1MHZ; // Set DCO to 1MHz DCOCTL = CAL_DCO_1MHZ; Having the external crystal installed, along witht this code would create a 1ms delay when the function is called as __delay_cycles(1); And assuming this works, would I go the same about creating a 1-second delay? Could I just add "1,000,000" to the function? Thanks for the help. I wish I had an oscilloscope or a logic analyzer to look at the signals. Quote Link to post Share on other sites
chicken 630 Posted December 24, 2013 Share Posted December 24, 2013 I wouldn't use _delay_cycles do build a clock or implement time sensitive communication protocols. But with 1MHz clock, _delay_cycles(1000) should be close enough to 1ms (and _delay_cycles(1000000) to 1 second) for basic delays, e.g. when blinking LEDs, waiting for some peripheral to do its thing etc. Note, that if there are other things going on, e.g. interrupt routines, _delay_cycles() might take longer than expected. If you need more precise timing, you will have to setup a timer interrupt and implement delay() similar to what Energia does. PS: @1MHz _delay_cycles(1) will wait 1 us (microsecond), not ms (millisecond). 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.