Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

timestamp from a microcontroller in millisecond

Status
Not open for further replies.

asif45

New Member
I am doing a project in keil uvision. I need to send timestamp from microcontroller (stm32f103rbt6) in Milliseconds to check events using C. I already found a program similar to this n it`s already in seconds n i need to change it from seconds to millisecond. i am new to microcontroller programming. If anyone can help me with this i will really appreciate. Please ask if anyone need more information.
 

Attachments

  • 1.jpg
    1.jpg
    71.5 KB · Views: 543
Can someone please tell me how can i change "u32Time" from that program in zip file. it is currently sending timestamp in second..i need to change it from second to millisecond. thanks
 
That is a hex-file, used for programming the device.
You need the source-file, used for generating the hex-file.

And don't post .zip-files... they are suspect as sources of malware.
 
I don't have time to take a look at the code, but I think you need to use Systick Timer
**broken link removed**

You could try asking the question in the Keil forums (search the forum before posting): https://www.keil.com/forum/
 
Last edited:
I changed " if (TimeDiff(u32Prescaler) >= 1000) " to " if (TimeDiff(u32Prescaler) >= 1) " for millisecond. Is this correct?



[c]

void TaskMain(void)
{
if (TimeDiff(u32Prescaler) >= 1) // updated clock
{
u32Prescaler = ReadSysTime();
u32Time++;
}
[c]
 
It does make sense that it could change the time resolution from 1 second to 1 millisecond (if the check is done often enough). Test it and you know.

If the variable 'u32Prescaler' allready holds the millisecond count, then you could just use that variable as it is.

EDIT: Actually, it looks like the ReadSysTime(); function returns exactly what you want. A millisecond resolution system time.

The function says:
C:
// *****************************************************************************
/// @brief        Função que lê o relógio do sistema
/// @fn            u32 ReadSysTime(void)   
/// @retval        timer @brief Valor do relógio do sistema (ms)   
// *****************************************************************************
u32 ReadSysTime(void)
{
    u32 u32Temp;

    do
        u32Temp = u32SystemTime;
    while (u32Temp != u32SystemTime);
    return u32Temp;
}
 
Last edited:
Thanks for the reply . Really appreciate it. If it's not much trouble for u can u plz show me how to do it please.

I tried to use ReadSysTime but was showing error .

u32 GetSystemTime = ReadSysTime;


u32 GetSystemTime()
{
return u32SystemTime;
}
 
The function ReadSysTime() returns the time value in milliseconds.

I assume that your communication works and you are able to send timestamps successfully.

C:
void TaskMain(void)
{
    
    u32Time = ReadSysTime(); // Read time in milliseconds

    /* do rest of the Task... */
}
 
Can you please tell me if integer 32 bit (u32) is enough to store in millisecond or i need to change this to integer 64 (u64) ?
 
Can you please tell me if integer 32 bit (u32) is enough to store in millisecond or i need to change this to integer 64 (u64) ?

32 bit variable rolls over after 7 weeks.
64 bit variable rolls over after 580 million years.

I don't know your application, but I would stay with 32 bit variable. If 7 weeks is not enough, you can add an 8 bit variable to count how many times the 32 bit variable has rolled over.
But, also, if you want to go with 64 bit variable that is ok also. Not much performance penalty there.
 
Last edited:
I am using this for check events in a I/O terminal in Industrial floor to collect data

Well, then maybe the device will be used more than 7 weeks without reboot. In that case maybe 64 bit variable is a good solution. It is up to you to decide.

You have to take account also how much data you are collectin and what you are doing with it. Do you need to compare datasamples that are more than 7 weeks apart? Does the system need 7 week old data?
 
Last edited:
I need 7 weeks old data. I tried to use 64 bit variable but then i need to change the whole firmware. so i tried use counter (i underlined the part below how i used it) but not to sure how to use properly. Can you please have a look ?

and also i am getting this error in command prompt when i am running the program "Cannot access Memory (@ 0xa0000060, Read, Acc Size: 4 Byte) ". Is this problem with initialization?

Code (C):

/// @brief Task of filtering the digital inputs
/// @fn void TaskFilter(void)
// *****************************************************************************
void TaskFilter(void)
{
u8 n, u8DigInp;
static u8 u8DigInpOld;
static u32 u32FilterTimer[INPUT_NUMBER], u32EventTime[INPUT_NUMBER], u32FilterSample;
static ESTATE_FILTER eFilterState[INPUT_NUMBER];
u32 u32Counter[2];


Code (C):
 
Last edited:
It is very difficult to follow your thoughts and code. I can see you are not familiar with C or microcontrollers. That makes this problem hard.

An "overflow counter" -solution goes something like this:
C:
u32 u32Time = 0; /* Variable to hold the time in milliseconds */
u8 u8TimeOverflowCounter = 0; /* Variable to count u32Time overflows */

void TaskMain(void)
{
    /* Check if the millisecond counter has overflown */
    if (u32Time > ReadSysTime())
    {
        u8TimeOverflowCounter++;
    }

    u32Time = ReadSysTime(); // Read time in milliseconds

   /* do rest of the Task... */
   /* When you send the timestamp, you need to send the overflow counter and the millisecond time value. */
   /* And, when you compare time values, you need to take account for the overflow counter also. */
}

That is the simple idea. You have to work out the implementation details yourself.
 
Last edited:
Yes i am new at this. my concept are not yet totally clear about some aspects. But i am trying to learn quickly. Thanks for your patience and attention.
 
Last edited:
There is a great website where you can try simple C programs and print output etc.
https://www.tutorialspoint.com/compile_c_online.php

Try this code with it. Copy paste it to the compiler, then hit "compile" and "execute".
C:
#include <stdio.h>
#include <stdint.h>

uint32_t time_ms = 0;
uint8_t overflow_counter = 0;
uint64_t combined_timestamp;

int main()
{
    /* Give some test values to the variables (hexadecimal values) */
    time_ms = 0x00982347;
    overflow_counter = 0xAB;

    /* Combine the two variables into a single value (at the receiving end) */
    combined_timestamp = 0;
    combined_timestamp = ((uint64_t)overflow_counter<<32) + time_ms;

    /* Print the result in hexadesimal */
    printf("combined_timestamp: 0x%lX \n", combined_timestamp);

    return 0;
}
This is very useful tool for learning C.. testing a piece of code etc.
You can also share direct links to the code you have written, this is useful for forum discussions. https://goo.gl/QVM2Na
 
Last edited:
Thanks for the link. That tool is simple but very effective.

Instead of using counter i used another variable with millisecond part u16EventTimeMS.

Then i send both u32 and u16 together. i had to make change in fifo stack also. in the image i tried to explain the basic idea.

i ran it n is working.
 

Attachments

  • m .jpg
    m .jpg
    61.2 KB · Views: 439
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top