How to make a nanosecond timer
Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
5 posts
Page 1 of 1
This is a tutorial that shows you how to make a nanosecond timer.
A nanosecond is one billionth of a second.
.000000001 or 10^-9
_________________________________________________________________________________
Form
_________________________________________________________________________________
Create a new project. Add the following controls:
Button1 (Start)
Button2 (Reset)
Label1
Timer1 _________________________________________________________________________________
Code
_________________________________________________________________________________
Imports:
Downloadable Source
_________________________________________________________________________________
A nanosecond is one billionth of a second.
.000000001 or 10^-9
_________________________________________________________________________________
Form
_________________________________________________________________________________
Create a new project. Add the following controls:
Button1 (Start)
Button2 (Reset)
Label1
Timer1 _________________________________________________________________________________
Code
_________________________________________________________________________________
Imports:
Code: Select all
Add this code to the project:
Imports System.Diagnostics.Stopwatch
Code: Select all
_________________________________________________________________________________ Dim StopWatch As Stopwatch = New System.Diagnostics.Stopwatch 'Creates the variable StopWatch, the main component of this project
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Button1.Text = "Start" Then 'If Button1's text is "Start", then...
Button1.Text = "Pause" 'Changes Button1's text to "Pause"
StopWatch.Start() 'Starts Stopwatch
Else 'Else
Button1.Text = "Start" 'Changes Button1's text to "Start
StopWatch.Stop() 'Pauses Stopwatch
End If
Label1.Text = (1 ^ 9 / StopWatch.Frequency * StopWatch.ElapsedTicks) / 1 ^ 9 'Gets the ticks from the stopwatch, then converts to nanoseconds
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = (1 ^ 9 / StopWatch.Frequency * StopWatch.ElapsedTicks) / 1 ^ 9 'Gets the ticks from the stopwatch, then converts to nanoseconds
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
StopWatch.Reset() 'Resets the stopwatch to 0
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True 'Enables Timer1
Timer1.Interval = 1 'Changes tick interval to 1 millisecond
End Sub
Downloadable Source
_________________________________________________________________________________
You do not have the required permissions to view the files attached to this post.
You could also find the nanosecond count with:
(1000000000 * StopWatch.ElapsedTicks / Stopwatch.Frequency)
(1000000000 * StopWatch.ElapsedTicks / Stopwatch.Frequency)
They both do the same thing, so I guess you could use that too.
I'm no mathematician, but there does seem to be a different level of accuracy between these methods.
5 posts
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023