Recently, I've been pretty much obsessed with my micro. In my last entry, I spoke of some of the hardware issues, this one I'm going to talk about some software stuff.
The sinewave had to exceed a certain frequency. Now, considering the speed of my MCU (microcontroller), this wasn't a super high frequency.
I wasn't satisfied to just meet the specified frequency. I wanted... no, I NEEDED to get the highest frequency possible.
My initial code was slow. I wrote it how the instructor suggested. I then began to optimize the code.
I needed to generate a 32 point sinewave. Each point had to be sent to the digital to analog converter. The MCU could send data to the SPI (serial peripheral interface) at a maximum rate of 614400 bits per second. This meant that it took 1.627 micro seconds per bit. I had to transfer two 8 bit bytes to the SPI for each data point. So transferring 16 bits would take 26.041 micro seconds and, since the MCU takes 813.8 nano seconds per cycle, this meant that it would take about 32 cycles. Each 8 bit transfer took 16 cycles. I pre-calculated each of the 32 datapoints. I eliminated all stacking and unstacking operations. I eliminated the use of subroutines. My code went from like 40 lines to somewhere around 900 lines. I moved the registers to memory location 0x0000 so I could use direct addressing, eliminating even more cycles. I even went through the output of the assembler, making sure that the the opcodes I wanted were being used.
I sent the first half of a data point to the SPI. Then did 8 NOPs (no operation, takes 2 cycles). This allowed the transfer to complete. I then loaded the SPSR (SPI status register) into accumulator A to clear the spif bit. Then I sent the second half, did 8 NOPs, cleared the spif bit. (the spif bit had to be cleared before I could continue). I then toggled SS (slave select (PORTD PD5 (pin 5, which was tied to the load line on the DAC))), causing the DAC to convert the digital code into an analog voltage. As soon as that was done, I sent the next point of data the same way, etc. Once all 32 points were output, I jumped back to where I sent the first datapoint and continued from there. This made a very nice sinewave, especially when I looked at the filtered output. Nice and smooth and I think it was somewhere over 690Hz. I was very satisfied because I am very confident that the only way the code could be faster would be increasing the clockspeed of the MCU. And my prof went over my code and agreed that it was as fast as it could be.
My micro course is definitely the course I am most passionate about. I am almost as passionate about windows programming although I'd rather be using c++ than using c#. I can't seem to get as passionate about web development. Being able to write facebook from scratch just doesn't quite excite me the same way my micro does. Since I'm so deep into the MCU, my prof gave me a hardcopy of the MCU reference manual (the full manual, with all details in it... it's bigger than some of my textbooks), and the MCU technical specifications manual (about half as big), and I already had the quick reference book. I've already read most of the reference and technical specification manuals and I've had them for a week. I kinda consider my micro board as a pet... or maybe my baby... and... I guess I love it. I know it inside and out.
I got the analog to digital converter working and I bought an extra ref02 voltage reference and a capacitor and resistor and ten turn 10k trimpot, just so I could calibrate it so the step size of the ADC would be 20mV (it's only an 8 bit ADC, unfortunately). I could now write a program that uses my microboard as a voltmeter. The next project part will be to interface an lm335 temperature sensor to it and I'm really excited to do this. Then I can code a thermometer program for my micro.
In windows programming, I'm writing a program that will generate barcodes and allows the user to print them. And making a more useful graphics manipulation program.
Speaking of windows programming, I have a lab exam in that tomorrow so I should probably get some sleep.
Thursday, September 25, 2008
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment