S32k342 Using Pit Ip to track milliseconds.

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

S32k342 Using Pit Ip to track milliseconds.

跳至解决方案
1,769 次查看
pb632146
Contributor V

This is a little bit more of a C question, but I am trying to emulate the functionality of the arduino millis() command which returns how long the program has been on in milliseconds. I figure to do that I would want to use the PIT_IP to increment a global variable every millisecond, but I am having the issue that for a simple delay function where I call a function to return the value to set the starting point like I would with OsIf_GetCounter, the value will update when the timer does, rather than holding the current value of the timer. 

 

As a result the delay function I want to make does not work as well as other functions I have that want to use the current value as a timestamp in a similar function OsIf_GetCounter.

 

pb632146_0-1737692102722.pngpb632146_0-1737692102722.png

 

The interrupt works, I just do not know how to disconnect the variable's value from directly referencing the global.

pb632146_1-1737692160853.pngpb632146_1-1737692160853.png

pb632146_2-1737692167414.pngpb632146_2-1737692167414.png

Are there alternatives to this? I want to avoid OsIf_GetCounter as it only uses 32 bits at the real clock speed which would overflow too often for my needs.

 

0 项奖励
回复
1 解答
1,720 次查看
pb632146
Contributor V
Turning the PIT timer off and on as a delay function is interesting, I will keep it in my mind for the future.

Strangely the issues seemed to be as a result of the debugger/compiler optimizations, as the issue would change from updating the startTime variable when the global was updated to not updating the global at all with the interrupt presumably not triggering.

But the end goal was for using the counter for timestamping to keep track of on/off times rather than delaying and it seems to work correctly in that situation.

在原帖中查看解决方案

0 项奖励
回复
2 回复数
1,734 次查看
RomanVR
NXP Employee
NXP Employee

Hi @pb632146.

To assist you more effectively, could you please clarify the specific functionality you need to accomplish? Are you looking for a delay function using the PIT, or are you trying to measure execution time?

If you need to implement a blocking delay with the PIT, I suggest to follow the structure shown in the image below. It was tested for a 1 second delay and it works as expected:

RomanVR_0-1738014764334.pngRomanVR_0-1738014764334.png

The PIT is configured to interrupt every millisecond which updates the "counter" variable. The PitDelay function starts the timer and continuously checks for the "counter" to reach the delay value. Once it does, the variable restarts, and the PIT is stopped.

If you need to measure execution time, you can update a counter variable each time the PIT gets into the interruption. This will provide an accurate estimation of the running time of your program.

Please let me know if this helps.

- RomanVR.

Best Regards!
0 项奖励
回复
1,721 次查看
pb632146
Contributor V
Turning the PIT timer off and on as a delay function is interesting, I will keep it in my mind for the future.

Strangely the issues seemed to be as a result of the debugger/compiler optimizations, as the issue would change from updating the startTime variable when the global was updated to not updating the global at all with the interrupt presumably not triggering.

But the end goal was for using the counter for timestamping to keep track of on/off times rather than delaying and it seems to work correctly in that situation.
0 项奖励
回复
%3CLINGO-SUB%20id%3D%22lingo-sub-2033589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3ES32k342%20%E4%BD%BF%E7%94%A8%20Pit%20Ip%20%E8%B7%9F%E8%B8%AA%E6%AF%AB%E7%A7%92%E3%80%82%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2033589%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3E%E8%BF%99%E6%9B%B4%E5%83%8F%E6%98%AF%E4%B8%80%E4%B8%AA%20C%20%E8%AF%AD%E8%A8%80%E9%97%AE%E9%A2%98%EF%BC%8C%E4%BD%86%E6%88%91%E8%AF%95%E5%9B%BE%E6%A8%A1%E6%8B%9F%20arduino%20millis()%20%E5%91%BD%E4%BB%A4%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%8C%E8%AF%A5%E5%91%BD%E4%BB%A4%E4%BB%A5%E6%AF%AB%E7%A7%92%E4%B8%BA%E5%8D%95%E4%BD%8D%E8%BF%94%E5%9B%9E%E7%A8%8B%E5%BA%8F%E8%BF%90%E8%A1%8C%E7%9A%84%E6%97%B6%E9%97%B4%E3%80%82%E6%88%91%E6%83%B3%E5%81%9A%E5%88%B0%E8%BF%99%E4%B8%80%E7%82%B9%EF%BC%8C%E6%88%91%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8%20PIT_IP%20%E6%AF%8F%E6%AF%AB%E7%A7%92%E5%A2%9E%E5%8A%A0%E4%B8%80%E4%B8%AA%E5%85%A8%E5%B1%80%E5%8F%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E6%A0%B7%E5%B7%A5%E4%BD%9C%E3%80%82%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_0-1737692102722.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_0-1737692102722.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321166iDF5B9025C7DE8D6A%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_0-1737692102722.png%22%20alt%3D%22pb632146_0-1737692102722.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_0-1737692102722.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%E4%B8%AD%E6%96%AD%E6%9C%89%E6%95%88%EF%BC%8C%E6%88%91%E5%8F%AA%E6%98%AF%E4%B8%8D%E7%9F%A5%E9%81%93%E5%A6%82%E4%BD%95%E6%96%AD%E5%BC%80%E5%8F%98%E9%87%8F%E5%80%BC%E4%B8%8E%E5%85%A8%E5%B1%80%E7%9A%84%E7%9B%B4%E6%8E%A5%E5%BC%95%E7%94%A8%E3%80%82%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_1-1737692160853.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_1-1737692160853.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321167i2D6DE174A7970782%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_1-1737692160853.png%22%20alt%3D%22pb632146_1-1737692160853.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_1-1737692160853.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22pb632146_2-1737692167414.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22pb632146_2-1737692167414.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321168iDF85D9D39BA685CE%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22pb632146_2-1737692167414.png%22%20alt%3D%22pb632146_2-1737692167414.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Epb632146_2-1737692167414.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%E8%BF%98%E6%9C%89%E5%85%B6%E4%BB%96%E5%8A%9E%E6%B3%95%E5%90%97%EF%BC%9F%E6%88%91%E6%83%B3%E9%81%BF%E5%85%8D%E4%BD%BF%E7%94%A8%20OsIf_GetCounter%EF%BC%8C%E5%9B%A0%E4%B8%BA%E5%AE%83%E5%9C%A8%E5%AE%9E%E9%99%85%E6%97%B6%E9%92%9F%E9%80%9F%E5%BA%A6%E4%B8%8B%E4%BB%85%E4%BD%BF%E7%94%A8%2032%20%E4%BD%8D%EF%BC%8C%E8%BF%99%E4%BC%9A%E7%BB%8F%E5%B8%B8%E6%BA%A2%E5%87%BA%EF%BC%8C%E4%B8%8D%E7%AC%A6%E5%90%88%E6%88%91%E7%9A%84%E9%9C%80%E8%A6%81%E3%80%82%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2034867%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9AS32k342%20%E4%BD%BF%E7%94%A8%20Pit%20Ip%20%E8%B7%9F%E8%B8%AA%E6%AF%AB%E7%A7%92%E3%80%82%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2034867%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%B0%86%20PIT%20%E8%AE%A1%E6%97%B6%E5%99%A8%E4%BD%9C%E4%B8%BA%E5%BB%B6%E8%BF%9F%E5%8A%9F%E8%83%BD%E6%89%93%E5%BC%80%E5%92%8C%E5%85%B3%E9%97%AD%E5%BE%88%E6%9C%89%E8%B6%A3%EF%BC%8C%E6%88%91%E4%BC%9A%E5%9C%A8%E5%B0%86%E6%9D%A5%E8%AE%B0%E4%BD%8F%E5%AE%83%E3%80%82%3CBR%20%2F%3E%3CBR%20%2F%3E%E5%A5%87%E6%80%AA%E7%9A%84%E6%98%AF%EF%BC%8C%E8%BF%99%E4%BA%9B%E9%97%AE%E9%A2%98%E4%BC%BC%E4%B9%8E%E6%98%AF%E8%B0%83%E8%AF%95%E5%99%A8%2F%E7%BC%96%E8%AF%91%E5%99%A8%E4%BC%98%E5%8C%96%E7%9A%84%E7%BB%93%E6%9E%9C%EF%BC%8C%E5%9B%A0%E4%B8%BA%E9%97%AE%E9%A2%98%E4%BC%9A%E4%BB%8E%E5%9C%A8%E5%85%A8%E5%B1%80%E6%9B%B4%E6%96%B0%E6%97%B6%E6%9B%B4%E6%96%B0%20startTime%20%E5%8F%98%E9%87%8F%E5%8F%98%E4%B8%BA%E6%A0%B9%E6%9C%AC%E4%B8%8D%E6%9B%B4%E6%96%B0%E5%85%A8%E5%B1%80%E5%8F%98%E9%87%8F%EF%BC%8C%E5%B9%B6%E4%B8%94%E4%B8%AD%E6%96%AD%E5%8F%AF%E8%83%BD%E4%B8%8D%E4%BC%9A%E8%A7%A6%E5%8F%91%E3%80%82%3CBR%20%2F%3E%3CBR%20%2F%3E%E4%BD%86%E6%9C%80%E7%BB%88%E7%9B%AE%E6%A0%87%E6%98%AF%E4%BD%BF%E7%94%A8%E8%AE%A1%E6%95%B0%E5%99%A8%E8%BF%9B%E8%A1%8C%E6%97%B6%E9%97%B4%E6%88%B3%E6%9D%A5%E8%B7%9F%E8%B8%AA%E5%BC%80%E5%90%AF%2F%E5%85%B3%E9%97%AD%E6%97%B6%E9%97%B4%E8%80%8C%E4%B8%8D%E6%98%AF%E5%BB%B6%E8%BF%9F%EF%BC%8C%E5%B9%B6%E4%B8%94%E5%AE%83%E5%9C%A8%E8%BF%99%E7%A7%8D%E6%83%85%E5%86%B5%E4%B8%8B%E4%BC%BC%E4%B9%8E%E5%8F%AF%E4%BB%A5%E6%AD%A3%E5%B8%B8%E5%B7%A5%E4%BD%9C%E3%80%82%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2034798%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%E5%9B%9E%E5%A4%8D%EF%BC%9AS32k342%20%E4%BD%BF%E7%94%A8%20Pit%20Ip%20%E8%B7%9F%E8%B8%AA%E6%AF%AB%E7%A7%92%E3%80%82%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2034798%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F230291%22%20target%3D%22_blank%22%3E%40pb632146%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%E4%B8%BA%E4%BA%86%E6%9B%B4%E6%9C%89%E6%95%88%E5%9C%B0%E5%B8%AE%E5%8A%A9%E6%82%A8%EF%BC%8C%E6%82%A8%E8%83%BD%E5%90%A6%E6%BE%84%E6%B8%85%E4%B8%80%E4%B8%8B%E6%82%A8%E9%9C%80%E8%A6%81%E5%AE%8C%E6%88%90%E7%9A%84%E5%85%B7%E4%BD%93%E5%8A%9F%E8%83%BD%EF%BC%9F%E6%82%A8%E6%98%AF%E5%9C%A8%E5%AF%BB%E6%89%BE%E4%BD%BF%E7%94%A8%20PIT%20%E7%9A%84%E5%BB%B6%E8%BF%9F%E5%87%BD%E6%95%B0%EF%BC%8C%E8%BF%98%E6%98%AF%E8%AF%95%E5%9B%BE%E6%B5%8B%E9%87%8F%E6%89%A7%E8%A1%8C%E6%97%B6%E9%97%B4%EF%BC%9F%3C%2FP%3E%0A%3CP%3E%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E4%BD%BF%E7%94%A8PIT%E5%AE%9E%E7%8E%B0%E9%98%BB%E5%A1%9E%E5%BB%B6%E8%BF%9F%EF%BC%8C%E6%88%91%E5%BB%BA%E8%AE%AE%E9%81%B5%E5%BE%AA%E4%B8%8B%E5%9B%BE%E6%89%80%E7%A4%BA%E7%9A%84%E7%BB%93%E6%9E%84%E3%80%82%E7%BB%8F%E8%BF%87%201%20%E7%A7%92%E5%BB%B6%E8%BF%9F%E6%B5%8B%E8%AF%95%EF%BC%8C%E5%85%B6%E5%B7%A5%E4%BD%9C%E6%AD%A3%E5%B8%B8%EF%BC%9A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22RomanVR_0-1738014764334.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cspan%20class%3D%22lia-inline-image-display-wrapper%22%20image-alt%3D%22RomanVR_0-1738014764334.png%22%20style%3D%22width%3A%20400px%3B%22%3E%3Cimg%20src%3D%22https%3A%2F%2Fcommunity.nxp.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F321574i38FBFD6B4A2159C0%2Fimage-size%2Fmedium%3Fv%3Dv2%26amp%3Bpx%3D400%22%20role%3D%22button%22%20title%3D%22RomanVR_0-1738014764334.png%22%20alt%3D%22RomanVR_0-1738014764334.png%22%20%2F%3E%3Cspan%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3ERomanVR_0-1738014764334.png%3C%2Fspan%3E%3C%2Fspan%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EPIT%20%E9%85%8D%E7%BD%AE%E4%B8%BA%E6%AF%8F%E6%AF%AB%E7%A7%92%E4%B8%AD%E6%96%AD%E4%B8%80%E6%AC%A1%EF%BC%8C%E4%BB%A5%E6%9B%B4%E6%96%B0%E2%80%9C%E8%AE%A1%E6%95%B0%E5%99%A8%E2%80%9D%E5%8F%98%E9%87%8F%E3%80%82PitDelay%20%E5%87%BD%E6%95%B0%E5%90%AF%E5%8A%A8%E8%AE%A1%E6%97%B6%E5%99%A8%E5%B9%B6%E4%B8%8D%E6%96%AD%E6%A3%80%E6%9F%A5%E2%80%9C%E8%AE%A1%E6%95%B0%E5%99%A8%E2%80%9D%E6%98%AF%E5%90%A6%E8%BE%BE%E5%88%B0%E5%BB%B6%E8%BF%9F%E5%80%BC%E3%80%82%E4%B8%80%E6%97%A6%E5%8F%91%E7%94%9F%E8%BF%99%E7%A7%8D%E6%83%85%E5%86%B5%EF%BC%8C%E5%8F%98%E9%87%8F%E5%B0%B1%E4%BC%9A%E9%87%8D%E6%96%B0%E5%90%AF%E5%8A%A8%EF%BC%8C%E5%B9%B6%E4%B8%94%20PIT%20%E5%B0%B1%E4%BC%9A%E5%81%9C%E6%AD%A2%E3%80%82%3C%2FP%3E%0A%3CP%3E%E5%A6%82%E6%9E%9C%E9%9C%80%E8%A6%81%E6%B5%8B%E9%87%8F%E6%89%A7%E8%A1%8C%E6%97%B6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