How to control the generation of fixed points within a fixed period of 1ms and gradually generate points (waveforms) according to the progress of the period (time)?
1 个评论
采纳的回答
@Peter,
I did encounter a similar problem when working on one of my client projects. T address your query, I implemented a time-based interrupt-driven system that triggers the generation of waveform points at regular intervals. Hope, this will give you clue to help started with your project.Here is a simplified example to illustrate this concept:
#include stdio.h
#include unistd.h
#include signal.h
#include sys/time.h // Include sys/time.h for struct itimerval and timer functions
int waveform_points = 0;
void generateWaveformPoint() {
// Generate waveform point based on progress
printf("Generated waveform point: %d\n", waveform_points);
waveform_points++; }
void timerHandler(int signal) {
generateWaveformPoint(); }
int main() {
// Set up timer interrupt to trigger every 1ms
struct itimerval timer;
signal(SIGALRM, timerHandler);
timer.it_value.tv_sec = 0;
timer.it_value.tv_usec = 1000; // 1ms
timer.it_interval = timer.it_value;
setitimer(ITIMER_REAL, &timer, NULL);
// Simulate other tasks within the 1ms cycle
while (1) {
// Perform other tasks
usleep(500); // Simulate task taking 0.5ms }
return 0; }
Please see attached results.
![](/matlabcentral/answers/uploaded_files/1743681/IMG_7247.jpeg)
So, The generateWaveformPoint() function generates a waveform point based on the progress of the time period, then the timerHandler() function is called when the timer interrupt occurs (every 1ms) and triggers the generation of a waveform point and the main() function sets up a timer interrupt to trigger every 1ms and simulates other tasks within the 1ms cycle. By using interrupts and handling waveform generation in a time-driven manner, you can ensure that new points are generated at regular intervals without missing or overwriting existing points.
更多回答(0 个)
另请参阅
类别
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!