 |
|
 |
Parallax Forums > Public Forums > SX Microcontrollers, SX/B Compiler and SX-Key Tool > (No Subject) | Forum Quick Jump
|
|  Mag748 Registered Member

       Date Joined Jul 2004 Total Posts : 94 | Posted 10/23/2006 3:56 PM (GMT -8) |   |
Hello,
I currently have a fully functioning SX program that I would like to upgrade. Right now, the only thing that it does is receive a serial stream and decodes the data, updating an LED display. I would like to add a function which will send a serial stream as well as listen to the original incoming serial stream for instructions. The serial stream that I would like to send is midi time code. So I would need some way of keeping track of time. I looked around for a ‘stopwatch’ chip or something that keeps time faster than just the DS1620 (at least 1/30 of a sec). Using the ISR that I already have (which I am using to receive the serial data) can I use it to send data as well? Luckily it’s the same baud and everything. So, ideally, is there a way to send, receive, AND keep track of time all in the same ISR?
I could probably figure out the keeping track of time part fairly easy. Simply knowing how many clock cycles equal one thirtieth of a second. And then keeping a counter or something. I am not that experienced with interrupts though.
Thank you for reading,
Marcus | | Back to Top | | |
 |  pjv Registered Member

       Date Joined Nov 2004 Total Posts : 1295 | Posted 10/23/2006 4:30 PM (GMT -8) |   | Hi Marcus;
Yes, it can readily be done, and simultaneously keep track of time as well..... I do lots of these concurrent task programs.
To do this, you will need to write it in assembler though.
Cheers,
Peter (pjv) | | Back to Top | | |
  |  pjv Registered Member

       Date Joined Nov 2004 Total Posts : 1295 | Posted 10/23/2006 5:15 PM (GMT -8) |   | Hi Marcus;
I'm sure I can point you in the right direction, and help if needed.
I trust you have someting programmable like an SX Tech or ProfDevelopment board and an SX-Key ?
To do simultaneous functions efficiently, you will want to use a scheduler.... kind of like a simple minded real-time operating system. You can find an example in my previous submission to the Parallax contest.
It is well commented, and if you are rusty on interrupts, then becoming familiar with that is an absolute "must". So study that example, and we'll go from there.
Cheers,
Peter (pjv) | | Back to Top | | |
     |  Yendor Registered Member
        Date Joined Aug 2006 Total Posts : 233 | Posted 11/10/2006 9:54 AM (GMT -8) |   | Marcus - like the sound of your project as I'm working on something similar. I'd like to sync with MTC as well as generate MTC, working on the sync'ing concepts now.
If you haven't already, I also recommend the following: Column #117: Timing is Everything in the Nuts&Volts section for some assembly explanation from Jon Williams on the Rx, as it is used in his Advance Midi Reciever project, which you are probably well aware of by now.
Also, I believe this code was borrowed from Chip Gracey's vitrual UART that is in Gunther's book that covers both Tx and Rx, in assembly. Every line has comments! See pgs 180-185, and the text downloads sections (TUT040 is the source code). Also, check out Paul Messick's web site.: http://www.maxmidi.com/diy/metro/index.html He has source code in there using a PIC16C54 that using incoming MTC for a metronome, and provides nice comments, as well. This will be an effective tool to work on for our drummer trying to play with a sequencer backing track, as we don't have a set of headphones big enough to wrap around his head, but then again he DOES get distracted with colored lights!
Bean - would it help that your favorite pest is interested in your sample, as well?
Rodney | | Back to Top | | |
 |  Bean (Hitt Consulting) Forum Moderator

       Date Joined Jul 2004 Total Posts : 5761 | Posted 11/10/2006 11:02 AM (GMT -8) |   | Marcus & Rodney, I have the code done, but I need to test it. Hopefully I'll be able to do that this weekend.
The interrupt runs at twice the baud rate. So if you need 31250 Baud, the interrupt will run at 62500. So at 20Mhz that gives about 320 clocks, the routines should only use about 100-ish clocks. So that is about 1/3 of the processors time. If you run at 50MHz (recommended), you will have 800 clocks per interrupt. So the routines will only use 1/8 of the processors time.
I'll post the code if someone wants to test it for me ;)
Bean.
Cheap used 4-digit LED display with driver IC www.hc4led.com
"People who are willing to trade their freedom for security deserve neither and will lose both." Benjamin Franklin
Post Edited (Bean (Hitt Consulting)) : 11/10/2006 7:18:32 PM GMT | | Back to Top | | |
  |  Phil Pilgrim (PhiPi) Registered Member

       Date Joined Feb 2006 Total Posts : 5998 | Posted 11/10/2006 8:13 PM (GMT -8) |   | Hey Bean,
I just read your post from 11/5.
I've always used 3x the baudrate for receive. The reason is this: at 2x, if the first low in the start bit is detected near the middle of its transmitted frame, the first data bit won't be sampled until very near the end of its frame. Any slight mismatch between baudrates, and you could already be into the next bit. And baud mismatches only get exacerbated as you near the end of the byte.
At 3x, this can't happen. Here, you wait for the start bit, then wait four timer interrupts to sample the first bit, then three thereafter. The first detected low in the start bit can't be more than 1/3 through the bit. Add 4 clocks to this, and you can't be more than 2/3 through the first data bit. At the other end, if the first start bit sample occurs right when the start bit goes low, the first data bit sample can't be less than 1/3 through the first data bit. So in every case, you're sampling data bits in their middle third.
At 3x, you've also got a chance to resample the start bit one interrupt after it's detected to see if it was just a noise glitch.
'Just a suggesiton...
-Phil | | Back to Top | | |
  |  Yendor Registered Member
        Date Joined Aug 2006 Total Posts : 233 | Posted 11/11/2006 7:58 AM (GMT -8) |   | Bean,
I bet your templates are better than most peoples source code - I'd take an alpha version off of you any day!
I wasn't expecting you to DO the code for us... But it is nice to see it in SX/B!
So, that does it! You're getting something for Christmas, but I just googled Hitt, and it seems to be from a Jewish decendant...So does this mean I need to send you 8? ;o)
Since we're discussing, referencing Jon's N&V's column #117 "Timing is Everything", he mentions that we should sample 4X per bit period for interrupt-driven serial input. So don't we need to sample 4X the bit period for one serial in or is that over engineering? Then the challenge is to make sure we're not Tx'ing when an interrupt occurs, or lose Rx bits when we're Tx'ing. In that point I can see to alternate portions of the interrupt which have a sampling of 4X and increasing the effective baud rate by 2 (or increasing the sampling to 8X - same thing). This seems like we can then change for every instance of serin/out needed (e.g. 3 instances = 12X), but let's not go there now...
Just for grins, I put together the chart below for 2X, 4X, 8X and 12X the bit rates while increasing the MIDI Baud.
Also, Bean, since we're mimicing Serial IN/OUT using SX/B bitbanging, we still need to have an Open Collector for output on Midi. So we're handling the "T" part of the baud rate option through code, and the "O" part, do we again just need to declare the pin as INPUT for serial out to make it Hi-Z? As in:
SerialOut PIN RA.1 INPUT 'MIDI Out - Open Collector/Hi-Z
Bean, we all appreciate how generous you are with your intellectual property and your support. You've really enlightend me! "I LOVE YOU, MAN!"
Rodney
| Baud Rate |
Bit Period |
Samping (2X) |
ClockFreq |
Instruction Rate |
Interrupt Rate (RTCC) |
Prescaler |
RTCC |
| 9,600 |
0.000104167 |
5.20833E-05 |
4,000,000 |
0.00000025 |
208.3 |
1 |
208 |
| 31,250 |
0.000032 |
0.000016 |
4,000,000 |
0.00000025 |
64.0 |
1 |
64 |
| 31,250 |
0.000032 |
0.000016 |
20,000,000 |
0.00000005 |
320.0 |
2 |
160 |
| 62,500 |
0.000016 |
0.000008 |
20,000,000 |
0.00000005 |
160.0 |
1 |
160 |
| 31,250 |
0.000032 |
0.000016 |
50,000,000 |
0.00000002 |
800.0 |
4 |
200 |
| 62,500 |
0.000016 |
0.000008 |
50,000,000 |
0.00000002 |
400.0 |
4 |
100 |
| 93,750 |
1.06667E-05 |
5.33333E-06 |
50,000,000 |
0.00000002 |
266.7 |
2 |
133 |
| 125,000 |
0.000008 |
0.000004 |
50,000,000 |
0.00000002 |
200.0 |
1 |
200 |
| Baud Rate |
Bit Period |
Samping (4X) |
ClockFreq |
Instruction Rate |
Interrupt Rate (RTCC) |
Prescaler |
RTCC |
| 9,600 |
0.000104167 |
2.60417E-05 |
4,000,000 |
0.00000025 |
104.2 |
1 |
104 |
| 31,250 |
0.000032 |
0.000008 |
4,000,000 |
0.00000025 |
32.0 |
1 |
32 |
| 31,250 |
0.000032 |
0.000008 |
20,000,000 |
0.00000005 |
160.0 |
1 |
160 |
| 62,500 |
0.000016 |
0.000004 |
20,000,000 |
0.00000005 |
80.0 |
1 |
80 |
| 31,250 |
0.000032 |
0.000008 |
50,000,000 |
0.00000002 |
400.0 |
2 |
200 |
| 62,500 |
0.000016 |
0.000004 |
50,000,000 |
0.00000002 |
200.0 |
1 |
200 |
| 93,750 |
1.06667E-05 |
2.66667E-06 |
50,000,000 |
0.00000002 |
133.3 |
1 |
133 |
| 125,000 |
0.000008 |
0.000002 |
50,000,000 |
0.00000002 |
100.0 |
1 |
100 |
|
|
|
|
|
|
|
|
| Baud Rate |
Bit Period |
Samping (8X) |
ClockFreq |
Instruction Rate |
Interrupt Rate (RTCC) |
Prescaler |
RTCC |
| 9,600 |
0.000104167 |
1.30208E-05 |
4,000,000 |
0.00000025 |
52.1 |
1 |
52 |
| 31,250 |
0.000032 |
0.000004 |
4,000,000 |
0.00000025 |
16.0 |
1 |
16 |
| 31,250 |
0.000032 |
0.000004 |
20,000,000 |
0.00000005 |
80.0 |
1 |
80 |
| 62,500 |
0.000016 |
0.000002 |
20,000,000 |
0.00000005 |
40.0 |
1 |
40 |
| 31,250 |
0.000032 |
0.000004 |
50,000,000 |
0.00000002 |
200.0 |
1 |
200 |
| 62,500 |
0.000016 |
0.000002 |
50,000,000 |
0.00000002 |
100.0 |
1 |
100 |
| 93,750 |
1.06667E-05 |
1.33333E-06 |
50,000,000 |
0.00000002 |
66.7 |
1 |
66 |
| 125,000 |
0.000008 |
0.000001 |
50,000,000 |
0.00000002 |
50.0 |
1 |
50 |
|
|
|
|
|
|
|
|
| Baud Rate |
Bit Period |
Samping (12X) |
ClockFreq |
Instruction Rate |
Interrupt Rate (RTCC) |
Prescaler |
RTCC |
| 9,600 |
0.000104167 |
8.68056E-06 |
4,000,000 |
0.00000025 |
34.7 |
1 |
34 |
| 31,250 |
0.000032 |
2.66667E-06 |
4,000,000 |
0.00000025 |
10.7 |
1 |
10 |
| 31,250 |
0.000032 |
2.66667E-06 |
20,000,000 |
0.00000005 |
53.3 |
1 |
53 |
| 62,500 |
0.000016 |
1.33333E-06 |
20,000,000 |
0.00000005 |
26.7 |
1 |
26 |
| 31,250 |
0.000032 |
2.66667E-06 |
50,000,000 |
0.00000002 |
133.3 |
1 |
133 |
| 62,500 |
0.000016 |
1.33333E-06 |
50,000,000 |
0.00000002 |
66.7 |
1 |
66 |
| 93,750 |
1.06667E-05 |
8.88889E-07 |
50,000,000 |
0.00000002 |
44.4 |
1 |
44 |
| 125,000 |
0.000008 |
6.66667E-07 |
50,000,000 |
0.00000002 |
33.3 |
1 |
33 | | | Back to Top | | |
  |  Yendor Registered Member
        Date Joined Aug 2006 Total Posts : 233 | Posted 11/11/2006 12:22 PM (GMT -8) |   | | Kramer,
It took me a minute to find the 16X you're mentioning, but I see what you're saying. Here's the chart with 16X sampling, and added the 19,200 baud from the VP UART code (the resultant RTCC for 19200 should be rounded up).
| Baud Rate |
Bit Period |
Samping (16X) |
ClockFreq |
Instruction Rate |
Interrupt Rate (RTCC) |
Prescaler |
RTCC |
| 9,600 |
0.000104167 |
6.51042E-06 |
4,000,000 |
0.00000025 |
26.0 |
1 |
26 |
| 31,250 |
0.000032 |
0.000002 |
4,000,000 |
0.00000025 |
8.0 |
1 |
8 |
| 31,250 |
0.000032 |
0.000002 |
20,000,000 |
0.00000005 |
40.0 |
1 |
40 |
| 62,500 |
0.000016 |
0.000001 |
20,000,000 |
0.00000005 |
20.0 |
1 |
20 |
| 31,250 |
0.000032 |
0.000002 |
50,000,000 |
0.00000002 |
100.0 |
1 |
100 |
| 62,500 |
0.000016 |
0.000001 |
50,000,000 |
0.00000002 |
50.0 |
1 |
50 |
| 93,750 |
1.06667E-05 |
6.66667E-07 |
50,000,000 |
0.00000002 |
33.3 |
1 |
33 |
| 125,000 |
0.000008 |
0.0000005 |
50,000,000 |
0.00000002 |
25.0 |
1 |
25 |
| 19,200 |
5.20833E-05 |
3.25521E-06 |
50,000,000 |
0.00000002 |
162.8 |
1 |
162 |
So we have 3X, 4X and 16X. Could it be a power of two problem?
One serial-in it's 41. For serin/serout it's 42? But it seems like that 42 would be needed for two rx's.
I find the whole thing very interesting...
Rodney | | Back to Top | | |
 | Forum Information | Currently it is Friday, November 20, 2009 10:41 PM (GMT -8) There are a total of 393,734 posts in 55,521 threads. In the last 3 days there were 82 new threads and 700 reply posts. View Active Threads
| | Who's Online | This forum has 17687 registered members. Please welcome our newest member, mark09. 49 Guest(s), 1 Registered Member(s) are currently online. Details Sal Ammoniac |
Forum powered by dotNetBB v2.42EC SP2.02 dotNetBB © 2000-2009 |
|
|