I am designing a system that drives on relay. I have to control the relay with ON/OFF button.
Once I press ON the relay should close and drive the circuit for 1 second and wait for 30 seconds before it loops for 1000 times.
I know how to design the electronics, I am planning to drive the relay by driving FET or transistor in series to relay and ground,
but I am having hard time to decide if I should use one of the crystal clocks, PWM function or delay commands.
I am learning so extra help like links or code examples would be appreciated.
The PWM actually sounds like a pretty good option here. If you used a crystal clock (?) it sounds like you would end up using the core to set the pin manually to drive your relay(s) and then you would have to wake up from a low power mode to do that. Plus, you would have to use a pretty low oscillator value (or prescale your timer module) and thus your current consumption would be less than optimal.
You could use the RTC, as it gives you an accurate 1 second interrupt, but you would be waking up a lot and your current consumption would again be less than optimal.
If you use the PWM, the LPO would have the resolution you would want and you could get pretty even seconds level granularity. Plus, you would be able to set the part into a low power mode, let the module run and you can probably get the best power consumption out of this configuration. Plus, you would have the processor free to do whatever you like.
The KL25_SC sample code has some TPM configuration examples for "baremetal" code and Processor Expert code. They don't do exactly what you want to do but it would give you a starting point. The Processor Expert example (TPM_Demo) definitely uses the TPM in a low power mode if you are interested in that route.
You can find this code here: http://www.freescale.com/files/32bit/software/KL25_SC.exe?WT_TYPE=BSDL&WT_VENDOR=FREESCALE&WT_FILE_F...
Hope this helps,
What is your thinking behind a PWM?
Your description is to turn on the relay, then delay in a LONG loop and repeat.
PWMs are used to control things "quickly" like managing current by pulsing quickly (many times per second)
If you want to use "baremetal" then fins a millisecond delay loop. Typically there will be one that uses the system tick.
IF you aren't concerned about power (i mean really low power) then it doesn't really matter much if you use a crystal for a clock or one of the internal oscillators.
IF you can invest the time to figure it out, try using MQX-Lite, This will let you setup a clock source, give you a delay function, and let you create a task to turn things on and off on a schedule. AND this will be a solid basis for additional projects in the future.