查看: 3300|回复: 3

AS5050 / AS5055 磁旋转编码器 通过 SPI 读取数据代码

[复制链接]
匿名  发表于 2015-5-4 16:39:05 |阅读模式
分享到:
又找到一个 ams 芯片的资料~~这么翻译下去,coco的英文水平要飚升了~~
这位楼主分享了自己的代码,表示自己是编程渣,希望有人可以帮助他 编程……

一楼提供的资料是那位楼主自己编写的程序(不知道是否有错),2楼的程序是一位热心网友提供的~(应该没错~~)

Hey people!

I'm trying to connect a AS5050 / AS5055 Magnetic Rotary Encoder to my arduino mega but I must admit I'm not a programming guy!
So I was wondering if someone could give me some help writing the code...?

This is the code in C... does anyone has a "arduino" translation

  1. /*!
  2. *****************************************************************************
  3. *  Reads out chip data via SPI interface
  4. *
  5. *  This function is used to read out cordic value from chips supporting SPI
  6. *  interface.
  7. *****************************************************************************
  8. */
  9. #define SPI_CMD_READ 0x8000     /*!< flag indicating read attempt when using SPI interface */
  10. #define SPI_REG_DATA  0x7ffe    /*!< data register when using SPI */
  11. #define SPI_REG_AGC   0x7ff0    /*!< agc register when using SPI */
  12. #define SPI_REG_CLRERR 0x6700   /*!< clear error register when using SPI */

  13. void spiReadData()
  14. {
  15.    u16 dat;   // 16-bit data buffer for SPI communication
  16.    ushort angle, agcreg;
  17.    ubyte agc;
  18.    ushort value;
  19.    bit alarmHi, alarmLo;

  20.    /* Send READ AGC command. Received data is thrown away: this data comes from the precedent      
  21. command (unknown)*/
  22.    dat  = SPI_CMD_READ | SPI_REG_AGC;
  23.    dat |= spiCalcEvenParity(dat);
  24.    spiTransfer((u8*)&dat, sizeof(u16));

  25.   /* Send READ ANGLE command. Received data is the AGC value, from the precedent command */
  26.    dat  = SPI_CMD_READ | SPI_REG_DATA;
  27.    dat |= spiCalcEvenParity(dat);
  28.    spiTransfer((u8*)&dat, sizeof(u16));
  29.    agcreg = dat;

  30.    /* Send NOP command. Received data is the ANGLE value, from the precedent command */
  31.    dat = 0x0000; // NOP command.
  32.    spiTransfer((u8*)&dat, sizeof(u16));
  33.    angle = dat >> 2;

  34.    }

  35. if (((dat >> 1) & 0x1) || ((agcreg >> 1) & 0x1))
  36.    {
  37.        /* error flag set - need to reset it */
  38.        dat  = SPI_CMD_READ | SPI_REG_CLRERR;
  39.        dat |= spiCalcEvenParity(dat);
  40.        spiTransfer((u8*)&dat, sizeof(u16));
  41.    }
  42.    else
  43.    {
  44.        agc = (agcreg >> 2) & 0x3f;  // AGC value (0..63)
  45.        value = (dat >> 2) & 0x3fff;  // Angle value (0..4095 for AS5055)
  46.        angle = (value * 360) / 4095;  // Angle value in degree (0..359.9°)
  47.        alarmLo = (dat >> 14) & 0x1;
  48.        alarmHi = (dat >> 15) & 0x1;
  49.    }
  50. }

  51. /*!
  52. *****************************************************************************
  53. *  Calculate even parity of a 16 bit unsigned integer
  54. *
  55. *  This function is used by the SPI interface to calculate the even parity
  56. *  of the data which will be sent via SPI to the encoder.
  57. *
  58. *  \param[in] value : 16 bit unsigned integer whose parity shall be calculated
  59. *
  60. *  \return : Even parity
  61. *
  62. *****************************************************************************
  63. */
  64. static u8 spiCalcEvenParity(ushort value)
  65. {
  66.    u8 cnt = 0;
  67.    u8 i;

  68.   for (i = 0; i < 16; i++)
  69.    {
  70.        if (value & 0x1)
  71.        {
  72.            cnt++;
  73.        }
  74.        value >>= 1;
  75.    }
  76.    return cnt & 0x1;
  77. }
复制代码




回复

使用道具

匿名  发表于 2015-5-4 16:42:01
然后有位网友给楼主提供了一份通过SPI读取数据的程序~真是可爱善良的小伙伴~

Here is a code for AS5055
This program reads data by SPI

  1. //AS5055 READ DATA PROGRAM
  2. //www.AleyRobotics.com, 2013
  3. //Email: info@AleyRobotics.com

  4. #include "SPI.h"
  5.   unsigned int result = 0;
  6.   unsigned int result1 = 0;
  7.   unsigned int result2 = 0;

  8. void setup ()
  9. {
  10. Serial.begin(9600);
  11. SPI.begin();
  12. SPI.setBitOrder(MSBFIRST);
  13. SPI.setClockDivider(SPI_CLOCK_DIV64); //you can chose faster SPI frequency
  14. }

  15. void loop () {
  16.   digitalWrite(SS, LOW);
  17.   result1 = SPI.transfer(0xff);
  18.   result2 = SPI.transfer(0xff);  
  19.   digitalWrite(SS, HIGH);
  20.   result1 &= 0b00111111;
  21.   result1 = result1 << 8;
  22.   result = (result1 | result2) >> 1;
  23.   Serial.print("Result: ");
  24.   Serial.print(" = ");
  25.   Serial.println(result, DEC);
  26.   delay(100);
  27. }
复制代码

另外这位网友还提供了AS5048的SPI程序,我将会在另一个帖子中分享~




回复 支持 反对

使用道具

  • TA的每日心情
    开心
    2024-3-15 14:34
  • 签到天数: 651 天

    连续签到: 1 天

    [LV.9]以坛为家II

    发表于 2015-5-5 11:35:35 | 显示全部楼层
    很赞,这就是技术渣的情操
    回复 支持 反对

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /4 下一条



    手机版|小黑屋|与非网

    GMT+8, 2024-10-21 03:40 , Processed in 0.139755 second(s), 22 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2024, Tencent Cloud.