Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: MagneticEncoder.cpp
- Revision:
- 2:67cf01beacc1
- Child:
- 3:1b3354f31db4
diff -r b0fc2c9a5d06 -r 67cf01beacc1 MagneticEncoder.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MagneticEncoder.cpp Fri Nov 11 00:54:37 2016 +0000 @@ -0,0 +1,193 @@ +#include "mbed.h" + +PwmOut mypwm(PWM_OUT); +Serial serial(USBTX, USBRX); +DigitalOut myled(LED1); + +int main() { + + mypwm.period_ms(10); + mypwm.pulsewidth_ms(1); + + serial.printf("pwm set to %.2f %%\n", mypwm.read() * 100); + + while(1) { + myled = !myled; + wait(1); + } +} + +/* + +#define + SPI_CMD_READ 0x4000 +/*!< flag indicating read attempt when using SPI in +terfac*//* +#define + SPI_REG_AGC 0x3ffd +/*!< agc register when using SPI */ /* +#define + SPI_REG_MAG 0x3ffe +/*!< magnitude register when using SPI */ /* +#define + SPI_REG_DATA 0x3fff +/*!< data register when using SPI */ /* +#define + SPI_REG_CLRERR 0x1 +/*!< clear error register when using SPI */ + +/*! + ************************************************** +*************************** + * Reads out chip data via SPI interface + * + * This function is used to read out cordic value +from chips supporting SPI + * interface. + ************************************************** +*************************** + *//* + +void + spiReadData() +{ + u16 dat; +// 16-bit data buffer for SPI communication + ushort angle, agcreg; + ubyte agc; + ushort value; + bit alarmHi, alarmLo; +/* Send READ AGC command. Received data is thrown a +way: this data comes from the precedent +command (unknown)*/ /* + dat = SPI_CMD_READ | SPI_REG_AGC; + dat |= spiCalcEvenParity(dat) << 15; + spiTransfer((u8*)&dat, +sizeof +(u16)); +/ /* Send READ MAG command. Received data is the AG +C value: this data comes from the +precedent command (unknown)*/ /* + dat = SPI_CMD_READ | SPI_REG_MAG; + dat |= spiCalcEvenParity(dat) << 15; + spiTransfer((u8*)&dat, +sizeof +(u16)); +magreg = dat; +/* Send READ ANGLE command. Received data is the MA +G value, from the precedent command */ /* + dat = SPI_CMD_READ | SPI_REG_DATA; + dat |= spiCalcEvenParity(dat) << 15; + spiTransfer((u8*)&dat, +sizeof +(u16)); + agcreg = dat; + /* Send NOP command. Received data is the ANGLE + value, from the precedent command *//* + dat = 0x0000; +// NOP command. + spiTransfer((u8*)&dat, +sizeof +(u16)); + angle = dat >> 2; + } +if + ((dat & 0x4000) || (agcreg & 0x4000) || (magreg & +0x4000)) + { +/* error flag set - need to reset it */ /* + dat = SPI_CMD_READ | SPI_REG_CLRERR; + dat |= spiCalcEvenParity(dat)<<15; + spiTransfer((u8*)&dat, +sizeof(u16)); + } + else + { + agc = agcreg & 0xff + value = dat & (currentChipDescription->positions - 31 - 1); + angle = (dd->value * 360) / currentChipDescription->positions; + magnitude = magreg & (currentChipDescription->positions - 31 - 1); + alarmLo = (agcreg >> 10) & 0x1; + alarmHi = (agcreg >> 11) & 0x1; + } +} +/*! +*************************************************** +************************** +* Calculate even parity of a 16 bit unsigned integ +er +* +* This function is used by the SPI interface to ca +lculate the even parity +* of the data which will be sent via SPI to the en +coder. +* +* \param[in] value : 16 bit unsigned integer whose + parity shall be calculated +* +* \return : Even parity +* +*************************************************** +************************** +*/ /* +static + u8 spiCalcEvenParity(ushort value) +{ + u8 cnt = 0; + u8 i; +for + (i = 0; i < 16; i++) + { +if + (value & 0x1) + { + cnt++; + } + value >>= 1; + } +return + cnt & 0x1; +} +/*! + ************************************************** +*************************** + * Calculate even parity of a 16 bit unsigned inte +ger + * + * This function is used by the SPI interface to c +alculate the even parity + * of the data which will be sent via SPI to the e +ncoder. + * + * \param[in] value : 16 bit unsigned integer whos +e parity shall be calculated + * + * \return : Even parity + * + ************************************************** +*************************** + */ /* +static + u8 spiCalcEvenParity(ushort value) +{ + u8 cnt = 0; + u8 i; +for + (i = 0; i < 16; i++) + { +if + (value & 0x1) + { + cnt++; + } + value >>= 1; + } +return + cnt & 0x1; +} + + + + + +*/ \ No newline at end of file