So, A couple of days ago I posted in regards to Debouncing hardware button press using the Atmel family. The reason why I needed to do this was because I was planning to send a pulse width modulation signal to an H-bridge controller. Now in your Int Main you need to initialize the parameters of your pulse width modulation, you can do that with the following of declared below in my main. Once done I used a interrupt request to then trigger that I pressed on the button and in this case a gas pedal. I then needed to bounce a gas pedal like I spoke about previously then from there increment OCRoA register from 0 to 255. Once done I then check to see if they gas pedal has been released and then set the register back to zero to halt the H-bridge from powering the motor. The example below should be hopefully straightforward. Good Luck
int main(void)
{
TCCR0A = (1 << COM0A1) | (1 << WGM00); // phase correct PWM mode
OCR0A = 0x00; // initial PWM pulse width
TCCR0B = (1 << CS01); // clock source = CLK/8, start PWM
//https://thewanderingengineer.com/2014/08/11/pin-change-interrupts-on-attiny85/
GIMSK = 0b00100000; // turns on pin change interrupts AKA PCIE. Pg.60 also for INT0 for the button press
PCMSK = 0b00000001; // turn on interrupts on pin PCINT0 aka PB0 for Gas and reverse
sei(); // enables interrupts
}
ISR(PCINT0_vect)
{
//PCMSK = 0b00000000; // turn on interrupts on pin PCINT0 aka PB0
debouncer = 0;
if (REVSWITCH_PIN & (1 << REVSWITCH_BIT))
{ // is switch open? Forward and reverse Switch
DIR_PORT |= (1 << DIR_BIT); // Pin n goes high
}
else
{
DIR_PORT &= ~(1 << DIR_BIT); // Pin n goes low
}
if (GAS_PIN & (1 << GAS_BIT))
{ // is switch open?
while (debouncer != 100 )
{
if (!(GAS_PIN & (1 << GAS_BIT)))
{
lcd_write_instruction_4d(lcd_Clear);
_delay_us(80);
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineOne);
_delay_us(80);
lcd_write_string_4d(“Debounced”);
_delay_us(80);
return;
}
_delay_us(200);
debouncer++;
}
lcd_write_instruction_4d(lcd_Clear);
_delay_us(80);
for(int i=0;i<=0xff;i++)
{
if (!(GAS_PIN & (1 << GAS_BIT)))
{
return;// is switch open?
}
//_delay_ms(AccelerateSleepTimer);
OCR0A = i;
_delay_us(40); // 40 uS delay (min)
char str[4];
itoa(i, str, 10);
//lcd_write_instruction_4d(lcd_Clear);
//_delay_us(200); // 40 uS delay (min)
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineOne);
_delay_us(80);
if (REVSWITCH_PIN & (1 << REVSWITCH_BIT))
{
lcd_write_string_4d(“Accelerating REV”);
}
else
{
lcd_write_string_4d(“Accelerating”);
}
// 40 uS delay (min)
_delay_us(40); // 40 uS delay (min)
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);
_delay_us(40); // 40 uS delay (min)
lcd_write_string_4d(str);
}
//Clear the Pin Change Interrupt flag so it does not trigger a 2nd time.
GIFR |= (1<<PCIF0); //PCIFR |= (1<<PCIF2);
}
else
{
OCR0A = 0x00;
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineTwo);
_delay_us(80); // 40 uS delay (min)
lcd_write_string_4d(“Stopped”);
}
//PCMSK = 0b00000011; // turn on interrupts on pin PCINT0 aka PB0
}
Documenting Missing Security Groups in Active Directory
So in one of the environments, I am working in. There are alot of one off security groups that are undocumented in Active Directory that needs to be moved to a centralized database.
I used the following code to run through AD, Find users that have the same Dept and Job title information via the wwwHomepage attribute and compile a report to get the departments eyes on them for adding the documentation for the reasoning these groups are being added.
Should the Dept and Jobcode combination be ultimately stored in the wwwHomepage freetext field. The answer would be no but that is the way it is right now.
Dim Data As New Dictionary(Of String, Dictionary(Of String, Integer))
Dim DataSecurityCount As New Dictionary(Of String, Integer)
Dim DeptJobcode As New Dictionary(Of String, Integer)
Dim DeptJobcodeSecurityGroup As New Dictionary(Of String, Integer)
Private Sub LoadSecuritryGroupReport()
Dim enTry As System.DirectoryServices.DirectoryEntry = New System.DirectoryServices.DirectoryEntry(“LDAP://Domain.Com/DC=info,DC=sys”)
Dim mySearcher As System.DirectoryServices.DirectorySearcher = New System.DirectoryServices.DirectorySearcher(enTry)
mySearcher.Filter = “(objectCategory=person)” ‘Environment.UserName Environment.MachineName
mySearcher.PageSize = 10000
mySearcher.SizeLimit = 10000
Dim FindAll As SearchResultCollection = mySearcher.FindAll
If IsNothing(FindAll) Then
Debug.WriteLine(“Did not find user account”)
Exit Sub
End If
Dim I As Integer = 0
Dim searchResult As SearchResult
For Each searchResult In FindAll
I += 1
If searchResult.Properties.Contains(“wwwHomePage”) Then
Debug.WriteLine(searchResult.Properties(“wwwHomePage”)(0))
Debug.WriteLine(searchResult.Path)
Debug.WriteLine(searchResult.Properties(“memberof”))
If DeptJobcode.ContainsKey(searchResult.Properties(“wwwHomePage”)(0)) Then
DeptJobcode(searchResult.Properties(“wwwHomePage”)(0)) += 1
Else
DeptJobcode.Add(searchResult.Properties(“wwwHomePage”)(0), 1)
End If
If Data.ContainsKey(searchResult.Properties(“wwwHomePage”)(0)) Then
For Each MyGroup In searchResult.Properties(“memberof”)
If Data(searchResult.Properties(“wwwHomePage”)(0)).ContainsKey(MyGroup) Then
Data(searchResult.Properties(“wwwHomePage”)(0))(MyGroup) += 1
Else
Data(searchResult.Properties(“wwwHomePage”)(0)).Add(MyGroup, 1)
End If
Next
Else
Dim MyNewDictionary As New Dictionary(Of String, Integer)
For Each MyGroup In searchResult.Properties(“memberof”)
MyNewDictionary.Add(MyGroup, 1)
Next
Data.Add(searchResult.Properties(“wwwHomePage”)(0), MyNewDictionary)
End If
End If
‘End If
Next
For I = 2000 To 5 Step -1
For Each MyDict In DeptJobcode
If MyDict.Value = I Then
Debug.WriteLine(MyDict.Key & ” – ” & MyDict.Value)
For Each MyValue In Data(MyDict.Key)
For Ii = 2000 To 5 Step -1
If MyValue.Value = Ii Then
Debug.WriteLine(vbTab & MyValue.Key & ” ” & MyValue.Value)
End If
Next
Next
End If
Next
Next
Debug.WriteLine(I & ” accounts are active”)
End Sub
Software Button Debouncing Logic – Atmel
So and I’m kinda constructing a suped-up power wheels car. There’s been a couple bumps in a road but it’s been mostly a nice learning experience. There’s a lot of little things that pop up when working with upgrading inductive motors in one of was software Debouncing for the gas pedal so the logic below is just an example of what I use my code to resolve this issue. First when you detect that your switch is either open or closed, start a timer in some form. In my case I use a global variable, or a stack/local variable in the current function then I loop over it multiple times incrementing either a register or the value the variable. If each time I loop over the bun is still in a close position I can safely assume that it’s been Debounce and held down for at least some human period of time and it’s not some sort of electrical false positive. You can of course adjust the sleep time between checks or the amount of intervals you like to adjust through. In my case I felt that 200 µs 100 times was a reliable way to the sense that the signals Mindy bounced however if you want to tweak it you may.
if (GAS_PIN & (1 << GAS_BIT))
{ // is switch open?
while (debouncer != 100 )
{
if (!(GAS_PIN & (1 << GAS_BIT)))
{
lcd_write_instruction_4d(lcd_Clear);
_delay_us(80);
lcd_write_instruction_4d(lcd_SetCursor | lcd_LineOne);
_delay_us(80);
lcd_write_string_4d(“Opps, Debounced”);
_delay_us(80);
return;
}
_delay_us(200);
debouncer++;
}
//Do something
}
GoDaddy Backups
Well,
I got lucky today. Apparently, my hosting is so old GoDaddy actually offers 30 backup and file revisions and I got a copy of my old Database back. Guess I got lucky this time. It’s a shame the DB Backup’s copy itself using the same name. My bad copy ended up overwriting my good copy in 24 hours. Look’s like I may be in luck with importing my older work. 🙂
Reverse Engineering the RS485 protocol
Well this weekend I had quite an adventure with RS485.
I ran into a couple issues on the way.
One was Identifying the baud rate of what your vampire tapping into.
One way is to use an Oscilloscope and find the shortest pause and calculate the baud rate from that.
Take the shortest pulse, then measure it in Microseconds (US aka 10^-5 in scientific notation aka 0.00000X). Take that value then plug it in here, (1 / 0.000026 = 38461.53…) there for 38461 is your baud rate… where 26 Microseconds is your shortest pulse.
An example on how to eye out the shortest pulse is below.

Now afterward’s There’s an issue just by merely vampire tapping into the wires with RS-485. An issue that arises is Discovering which way the traffic flows. On RS-45 unlike RS-232 there is no RX in TX line is only A and B. So determining the direction which data is flowing on the line is not really possible Unless you already where the underlying Sender / Receiver protocol. So one way to get around this is to actually split the connections physically and run them into your laptop with a RS-485 the USB converter, Read the data then send the same data out of the port. You can do this with the rig like I made below. In this case I use a red wire to transfer power that is required for the receiver and green for the ground. This power usage is purely for the UC on the receiving side, RS-485 itself does not require ground reference or power reference to ran along in the communication even though sometimes it is included it. In picture three see the solder points bridging the two green connectors on the left side and then to solder points bridging the connections on the right side.







Here’s a chart I found.
| Time | Baud Rate |
| 3333µs (3.3ms) | 300 |
| 833µs | 1200 |
| 416µs | 2400 |
| 208µs | 4800 |
| 104µs | 9600 |
| 69µs | 14400 |
| 52µs | 19200 |
| 34µs | 28800 |
| 26µs | 38400 |
| 17.3µs | 57600 |
| 8µs | 115200 |
| 4.34µs | 230400 |
I wrote a MITM code using USB to RS485 modules and the following code below in VB.net
You can adjust the code below to the correct comport as needed.
Imports System.Text
Public Class Form1
Delegate Sub ListboxItem(data As String)
Dim func1 As ListboxItem = AddressOf Listbox1Item
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each MyPort In System.IO.Ports.SerialPort.GetPortNames()
ComboBox1.Items.Add(MyPort)
ComboBox2.Items.Add(MyPort)
Next
ComboBox1.SelectedIndex = 0
ComboBox2.SelectedIndex = 1
End Sub
Dim Buffer1Index As Integer = 0
Dim Buffer1(32) As Byte
Private Sub SerialPort1_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(func1, {“r1”})
Buffer1(Buffer1Index) = SerialPort1.ReadByte()
SerialPort2.Write(Buffer1(Buffer1Index))
If Buffer1Index > 1 Then
If (Buffer1(Buffer1Index) = &HAUS And Buffer1(Buffer1Index – 1) = &HDUS) Or Buffer1(Buffer1Index) = &HDUS Then
Dim strTemp As New StringBuilder(128)
strTemp.Append(“[” & SerialPort1.PortName.ToString & “]”)
For b = 0 To Buffer1Index
strTemp.Append(” ” & Conversion.Hex(Buffer1(b)))
Next
Me.Invoke(func1, {strTemp.ToString})
Buffer1Index = 0
Else
Buffer1Index += 1
End If
Else
Buffer1Index += 1
End If
End Sub
Dim Buffer2Index As Integer = 0
Dim Buffer2(32) As Byte
Private Sub SerialPort2_DataReceived(sender As Object, e As IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
Me.Invoke(func1, {“r2”})
Buffer2(Buffer2Index) = SerialPort2.ReadByte()
SerialPort1.Write(Buffer2(Buffer2Index))
If Buffer2Index > 1 Then
If Buffer2(Buffer2Index) = &HAUS And Buffer2(Buffer2Index – 1) = &HDUS Or Buffer2(Buffer2Index) = &HDUS Then
Dim strTemp As New StringBuilder(128)
strTemp.Append(“[” & SerialPort2.PortName.ToString & “]”)
For b = 0 To Buffer2Index
strTemp.Append(” ” & Conversion.Hex(Buffer2(b)))
Next
Me.Invoke(func1, {strTemp.ToString})
Buffer2Index = 0
Else
Buffer2Index += 1
End If
Else
Buffer2Index += 1
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.BaudRate = 1200
SerialPort2.BaudRate = 1200
SerialPort1.PortName = ComboBox1.SelectedItem
SerialPort2.PortName = ComboBox2.SelectedItem
‘SerialPort1.Open()
SerialPort2.Open()
End Sub
Sub Listbox1Item(ByVal data As String)
ListBox1.Items.Add(Now.TimeOfDay.ToString & ” ” & data)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim OutputString As String = “”
For Each MyLine In ListBox1.Items
OutputString &= MyLine
Next
Dim File1 = IO.File.CreateText(“Serial1.txt”)
File1.WriteLine(OutputString)
File1.Flush()
End
End Sub
End Class