Released mag_pic-0.1.asm

Released mag_pic-0.1.asm

This is 5000 lines of pic 18F14k22 asm, with CORDIC code by Pat Fairbank. My code is released under the GPL.

formatting link

Accuracy of the compass display is .1 degree, the CORDIC radians display show 0-65535 for 0-360 degrees. There are many more features.

This is actually a first test code, I did not have more time between lunch and dinner. It is beautiful code, yo ucan use teh thing remotely by sending character 'v' to it, and then it will display something like:

Clock separator = not flash Panteltje (c) mag_pic-0.1 fluxgate compass RS232 commands 115200 Bd 1 start bit, 8 data bits, 1 stop bit: BnnnENTER set LCD brightness, from 0(min) to 255(max), default 128, saved in EEPROM. E flash clock separator, default off, saved in EEPROM. e do not flash clock separator, default off, saved in EEPROM. GnnnENTER set clock calibration, set timer1 reload, default 175, saved in EEPROM. HnnENTER set hour. h help, this help. MnnENTER set minute. RintENTER set rotation 16 bit number, rotates compass, use 900 for 90 degrees, default 900, saved in EEPROM. v print status.

Have fun. Now back to the hardware.

Reply to
Jan Panteltje
Loading thread data ...

On a sunny day (Fri, 27 May 2011 16:21:19 GMT) it happened Jan Panteltje wrote in :

dinner.

degrees=292.2

PS in case you cannot get the same numbers, remember the x and y come from

10 bits 0-1023 ADCs, and 512 is subtracted to get a signed number. So you get x=-374 and y=157 CORDIC counts counter clockwise with zero in east, and is converted to compass that counts clockwise with zero in north.

compass counts clockwise North 360 / 0 y NW | 'r' is distance = hypotenuse phi 292.2-----. +157 | \r | West 270 -x -.--------------- +x 90 East -374 | | | -y 180 South

CORDIC counts counter-clockwise 16384 90 28720 | phi 157.8-----. | |

32768 180 -.--------------- 0 0 / 65536 | | | 180 49151

Oh what fun :-) LOL hehe

Reply to
Jan Panteltje

On a sunny day (Fri, 27 May 2011 16:21:19 GMT) it happened Jan Panteltje wrote in :

I have done some more soldering, and made a coil former from the top of some small microscope specimen bottle. Then added super glue (the thick variety), becomes messy. Made a vertical board for the coils that will be horizontal later. Separate analog an digital boards, things seem to work, need to be put together like a sandwich. The board is populated for 3 axis, but the coil only has two just now. That diagram is old I do not have a new one yet. Beautiful pictures!

formatting link

Reply to
Jan Panteltje

Your pictures are fine but your schematics are useless, if meant that someone else could use them, that is.

Reply to
LM

dinner.

compass degrees=292.2

Why not run 2's complement instead? That way the magnitude is the same for +ve and -ve numbers, and top bit is the sign.

Reply to
Grant

and dinner.

compass degrees=292.2

Huh? Wanna try that one again?

Reply to
krw

On a sunny day (Mon, 30 May 2011 09:18:36 +1000) it happened Grant wrote in :

Right, that is what comes out of the 32 bit signed subtraction:

-512 (= 0 - 512) to +511 (= 1023 - 512), sign bit is MSB. It is then further processed in the normal way. The PIC ADC can only do left or right justified, and I need to be able to subtract other values than 512 in the (still to be added) software calibration (set zero). So how would you do it faster / betetr / cheaper?

Reply to
Jan Panteltje

Hi,

Looks great! :)

cheers, Jamie

Reply to
Jamie

Signed integers are a natural data type in some processors, I'm not sure if PIC does them directly or with macros. It's getting on to a year now since I did any PIC programming :(

The advantage of working with signed values is obvious, I thought.

But I don't know how much is native to the CPU and how much you'd have to write macros for. Oh, you're programming in C? It should do signed numbers for you. I mainly program in assembler for microcontrollers.

That can change :) I only spent 15 years ignoring C, back when I was working. Conversion to 2's comp is easy, invert and increment.

Grant.

Reply to
Grant

On a sunny day (Wed, 01 Jun 2011 14:40:12 +1000) it happened Grant wrote in :

Why not look at the code before commenting?

formatting link
It is asm and I use the integer math copyright Peter Hemsley, and CORDIC atan code copyright Pat Fairbank

That 'integer math' is 32 bit signed, and the fastest way I know to get from an ADC output 0-1023 to a signed number with added calibration (this calibration is set via RS232 from EEPROM to compensate of any offsets in the system (opamp + sampling, plus residual magnetic fields). Did you know some resistors have metal caps that can be magnetised? Some SMDs too (zeners I have here).

This is what the latest code looks like: ; make signed z ; to make a signed number -511 to +511 from the ADC output 0-1023, z = z_h:z_l

- z_zero_h:z_zero_l (default 512) clrf REGA3 clrf REGA2

movfw z_h movwf REGA1

movfw z_l movwf REGA0 ; REGA = x_h:x_l clrf REGB3 clrf REGB2

movfw z_zero_h REGA return carry set if overflow ; REGA = z_h:z_l - z_sero_h:z_zero_l can be negative, range -511 to + 511, fits in 16 bits

For the small PICs I will wait to gcc supports it, without any idiotic casts and other stuff. As that will never happen (the architecture sucks), it is likely we will see the full Linux PC on a chip first with gcc in ROM or FLASH (so you can upgrade). LOL

One could argue you only need 16 bit signed integer math for the above, well you can download those libraries from the Microchip website. The only difference is the clrf REGA3 clrf REGA2, assuming you use the same variable names (it does not). I found the 32 bit math much more useful than the 16 bit for almost all cases, as in the Microchip libs you have to define if you want signed, and then you lose half the range to the sign bit, makes it useless for most serious applications where you really want to multiply with some large number before you divide to get any accuracy in fixed point arithmetic. For example in fixed point integer 100 / 5.5 = 20. But if you multiply the 100 first by 1000, then you get 100000 / 5.5 = 18181, now just print a '.' after the first 2 digits, 18.181. THAT is why I use 32 bits signed and not 16 bits. Anyways it is all in that code in the link above. I thought this was common knowledge, but then this is an electronics group, so maybe not. grml: ~ # ptlrc -d /dev/ttyS0 -b 115200 Panteltje ptlrc-0.6 using device /dev/ttyS0 Escape exits.

Panteltje (c) mag_pic-0.2 fluxgate compass RS232 commands 115200 Bd 1 start bit, 8 data bits, 1 stop bit: BnnnENTER set LCD brightness, from 0(min) to 255(max), default 128, saved in EEPROM. E flash clock separator, default off, saved in EEPROM. e do not flash clock separator, default off, saved in EEPROM. GnnnENTER set clock calibration, set timer1 reload, default 175, saved in EEPROM. HnnENTER set hour. h help, this help. MnnENTER set minute. RintENTER set rotation 16 bit number, rotates compass, use 900 for 90 degrees, default 900, saved in EEPROM. v print status. XintENTER set x axis (east west) zero, range 0-1023, default 512, saved in EEPROM. YintENTER set y axis (north south) zero, range 0-1023, default 512, saved in EEPROM. ZintENTER set z axis (earth sky) zero, range 0-1023, default 512, saved in EEPROM.

Come to think of it, this would fly a missile (clock + compass), still plenty of code space left. Oh, and we need altitude too, maybe for a next PIC project :-) Let them switch of GPS :-). That is what CORDIC came from anyways, navigation for the B52 bomber,

Reply to
Jan Panteltje

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.