Eric Smith wrote:
Typical bit-banged UART Rx works slightly differently
depending on
whether you get an interrupt for the leading edge of the start bit. If
you do, you start sampling every bit once, starting about 1/2 bit time
after the interrupt. If you don't get a leading edge interrupt, you
need to sample continuously at a rate higher than twice the bit rate.
So I split the difference with my bit-banged uart. :)
I just constantly poll the data line until I see a start bit, and then
count 1.5*bit_period instructions until I get to the middle of the first
bit, and then my loop takes exactly 1 bit_period from read to read.
Even at 2mbps, this works pretty good.
I've done some testing with it, and it worked reliably sending almost 3
gigabits of traffic through it without a single bit error.
I happen to only need to receive and transmit at particular times when
the uC is sitting there doing nothing. Meaning I don't need to be
interrupted......
My transmitting code works good too, although I ran into an interesting
situation where MOVing a binary-0 to the port takes slightly more (or
less? I forget) than MOVing a binary-1 to the port. MOV on my
controller is just a mnemonic --- the actual machine code is made up of
"skip next instructions", setb, and clrb....... This results in a couple
clocks difference based on the data that needs transmitting. UARTs are
forgiving enough, so there was no real issue, but interesting nonetheless.
Since I have no formal training on this stuff, it's neat to compare my
first attempt at things, and see how they differ from the "correct" or
"most accepted" way of doing things. Sometimes, I'm right on the money.
And most times, I'm WAY off. :)
Keith