查看: 1113|回复: 3

分享一下AS5145的代码

[复制链接]

该用户从未签到

发表于 2015-5-21 15:12:39 | 显示全部楼层 |阅读模式
分享到:
分享一下AS5145的代码,希望大家用到这片子的时候少走一些弯路,采用51单片机来进行操作,下面是最简单的读写0~4095的值,清0的话在一下函数,自己读懂调用就可以了

  1. #include <STC12C5630AD.H>

  2. sbit CSN  = P3^7;

  3. sbit CLK  = P1^0;

  4. sbit DO   = P1^1;

  5. sbit PROG = P1^2;





  6. //Definitions: Macro

  7. #define CLEAR_CSN() do { CSN = 0; } while(0)

  8. #define SET_CSN() do { CSN = 1; } while(0)

  9. #define CLEAR_CLK() do { CLK = 0; } while(0)

  10. #define SET_CLK() do { CLK = 1; } while(0)

  11. #define CLEAR_PROG() do { PROG = 0; } while(0)

  12. #define SET_PROG() do { PROG = 1; } while(0)



  13. #define PROG_HIGH_IMPED() do { PROG = 1;} while(0) // Pushpull disabled, Hi-Z

  14. #define PROG_LOW_IMPED() do {  } while(0) // Pushpull enabled





  15. uint32        RD_as5311_data(void)

  16. {

  17.         uint32 Data=0;

  18.         uint8 i=0;

  19.         bit flags=0;

  20. //start

  21.         CLK=1;

  22.         CSN=1;

  23.         delay_500ns();

  24.         CSN=0;

  25.         delay_500ns();

  26.         delay_500ns();



  27. //read D0-D17

  28.          for(i=0;i<17;i++)

  29.         {

  30.                  CLK=0;

  31.                 delay_500ns();

  32.                 CLK=1;

  33.                 delay_500ns();

  34.                 Data=(Data<<1)|DO;

  35.                 if(flags==DO) flags=0;

  36.                         else  flags=1;

  37.         }

  38.                  CLK=0;

  39.                 delay_500ns();

  40.                 CLK=1;

  41.                 delay_500ns();

  42.                

  43.         if(DO!=flags)

  44.                 Data=0x10000000;



  45. //stop

  46.         CSN=1;

  47.         delay_500ns();



  48.         return (Data);

  49. }







  50. uint16   ssi_read(void)

  51. {         

  52.         uint32 rd_data=0;



  53.         rd_data=RD_as5311_data();



  54.         return((rd_data%0x20000)/0x20);

  55. }





  56. void main(void)

  57. {

  58.         uchar digit[10] = {'0','1','2','3','4','5','6','7','8','9'};

  59.         uchar buf[4]=0,i=0;

  60.         UART_Init();



  61.         while(1)

  62.         {

  63.                 P1 &= ~(1 << 2);

  64.                 P0 &= ~(1 << 1);

  65.                 angle = ssi_read();//读取0~4096的值

  66.                

  67.                 buf[0] = angle / 1000;

  68.                 buf[1] = angle % 1000 / 100;

  69.                 buf[2] = angle % 1000 % 100 / 10;

  70.                 buf[3] = angle % 10;

  71.                

  72.                 for(i=0;i<4;i++)

  73.                 {

  74.                         SBUF = digit[buf[i]];

  75.                         while(TI==0);TI=0;

  76.                 }        



  77.         }

  78. }

复制代码


以下的函数可以实现清0,OTP编程,读懂调用就可以了

  1. void delay_500ns(void)

  2. {        

  3.         uint8 x=15;

  4.         while(x--);

  5. }



  6. uint32        RD_as5311_data(void)

  7. {

  8.         uint32 Data=0;

  9.         uint8 i=0;

  10.         bit flags=0;

  11. //start

  12.         CLK=1;

  13.         CSN=1;

  14.         delay_500ns();

  15.         CSN=0;

  16.         delay_500ns();

  17.         delay_500ns();



  18. //read D0-D17

  19.          for(i=0;i<17;i++)

  20.         {

  21.                  CLK=0;

  22.                 delay_500ns();

  23.                 CLK=1;

  24.                 delay_500ns();

  25.                 Data=(Data<<1)|DO;

  26.                 if(flags==DO) flags=0;

  27.                         else  flags=1;

  28.         }

  29.                  CLK=0;

  30.                 delay_500ns();

  31.                 CLK=1;

  32.                 delay_500ns();

  33.                

  34.         if(DO!=flags)

  35.                 Data=0x10000000;



  36. //stop

  37.         CSN=1;

  38.         delay_500ns();



  39.         return (Data);

  40. }









  41. // Utility functions

  42. void pptrimDelay(volatile unsigned int value)

  43. {

  44.         for(value; value>0; value--);

  45.         {

  46.                 unsigned char foo = 30;

  47.                 while(foo--);

  48.         }

  49. }





  50. static void clkPulses(unsigned char num)

  51. {

  52.         unsigned char i;

  53.         for(i = 0; i < num; i++)

  54.         {

  55.                 SET_CLK(); pptrimDelay(PPTRIMDelay);

  56.                 CLEAR_CLK(); pptrimDelay(PPTRIMDelay);

  57.         }

  58. }



  59. unsigned char reversebits(unsigned char value) // Endian switch

  60. {

  61.         unsigned char i=0, result=0;

  62.         while (i<8)

  63.         {

  64.                 result += (value<<i)&0x80;

  65.                 if (i<7) result = result >> 1;

  66.                 i++;

  67.         }

  68.         return result;

  69. }



  70. //Setup and exit conditions

  71. static void setupCondition()

  72. {

  73.         CLEAR_CSN();

  74.         pptrimDelay(PPTRIMDelay);

  75.         CLEAR_CLK();

  76.         pptrimDelay(PPTRIMDelay);

  77.         SET_PROG();

  78.         pptrimDelay(PPTRIMDelay);

  79.         SET_CSN();

  80.         pptrimDelay(PPTRIMDelay);

  81.         CLEAR_CSN();

  82.         pptrimDelay(PPTRIMDelay);

  83.         SET_CLK();

  84.         pptrimDelay(PPTRIMDelay);

  85.         CLEAR_CLK();

  86.         pptrimDelay(PPTRIMDelay);

  87. }



  88. static void exitCondition()

  89. {

  90.         PROG_LOW_IMPED();

  91.         pptrimDelay(PPTRIMDelay);

  92.         CLEAR_CSN();

  93.         pptrimDelay(PPTRIMDelay);

  94.         SET_CLK();

  95.         pptrimDelay(PPTRIMDelay);

  96.         CLEAR_CLK();

  97.         pptrimDelay(PPTRIMDelay);

  98.         SET_CLK();

  99.         pptrimDelay(PPTRIMDelay);

  100.         SET_CSN();

  101.         pptrimDelay(PPTRIMDelay);

  102.         CLEAR_PROG();

  103.         pptrimDelay(PPTRIMDelay);

  104. }



  105. static void operationModeRead()

  106. {

  107.         CLEAR_PROG();

  108.         pptrimDelay(PPTRIMDelay);

  109.         SET_CLK();

  110.         pptrimDelay(PPTRIMDelay);

  111.         SET_CSN();

  112.         pptrimDelay(PPTRIMDelay);

  113.         CLEAR_CLK();

  114.         pptrimDelay(PPTRIMDelay);

  115.         clkPulses(1);

  116.         PROG_HIGH_IMPED();

  117. }



  118. static void operationModeWrite()

  119. {

  120.         SET_CSN();

  121.         pptrimDelay(PPTRIMDelay);

  122.         clkPulses(3);

  123. }

  124.         

  125. void pptrimRead(unsigned char *buffer, unsigned char num_bits)

  126. {

  127.         xdata unsigned char current_byte = 0;

  128.         xdata unsigned char current_bit = 0;

  129.         xdata unsigned char temp = 0;

  130.         if(!num_bits) return;

  131.         current_byte = num_bits >> 3;

  132.         current_bit = num_bits & ~0x07;

  133.         setupCondition();

  134.         operationModeRead();

  135.         clkPulses(1); // position the first bit to read

  136.         //-- read OTP Data --

  137.         temp = 0;

  138.         temp += (PROG) ? 1 : 0;

  139.         for(current_bit = num_bits; current_bit; current_bit--)

  140.         {

  141.                 if(((current_bit - 1) & 0x07) == 0)

  142.                 {

  143.                         buffer[current_bit >> 3] = temp;

  144.                         temp = 0;

  145.                 }

  146.                 if (current_bit)

  147.                 {

  148.                         temp <<= 1;

  149.                         SET_CLK();

  150.                         pptrimDelay(200);

  151.                         temp += (PROG) ? 1 : 0;

  152.                         CLEAR_CLK();

  153.                         pptrimDelay(200);

  154.                 }

  155.         }

  156.         exitCondition();

  157. }



  158. void pptrimWrite(unsigned char *buffer, unsigned char num_bits)

  159. {

  160.         xdata unsigned char *current_byte;

  161.         xdata unsigned char current_bit = 0;

  162.         xdata unsigned char temp = 0;

  163.         current_byte = buffer + ((num_bits-1)>>3);

  164.         temp = *current_byte;

  165.         if(num_bits % 8)

  166.         temp <<= 8 - (num_bits % 8);

  167.         setupCondition();

  168.         operationModeWrite();

  169.         //-- send OTP Data

  170.         for(current_bit = num_bits; current_bit; current_bit--)

  171.         {

  172.                 if(temp & 0x80)

  173.                 SET_PROG();

  174.                 else

  175.                 CLEAR_PROG();

  176.                 pptrimDelay(100);

  177.                 SET_CLK();

  178.                 pptrimDelay(300);// delay, tzapp=2us(typ.)

  179.                 CLEAR_CLK();

  180.                 pptrimDelay(PPTRIMDelay);

  181.                 temp <<= 1;

  182.                 if(((current_bit-1) & 0x07) == 0)

  183.                 {

  184.                         temp = *(--current_byte);

  185.                 }

  186.         }

  187.         SET_PROG();

  188.         pptrimDelay(100);

  189.         clkPulses(1); // data latched

  190.         // END OTP-Write

  191.         exitCondition();

  192. }



  193. uint16   ssi_read(void)

  194. {         

  195.         uint32 rd_data=0;



  196.         rd_data=RD_as5311_data();



  197.         return((rd_data%0x20000)/0x20);

  198. }



  199. void AS5145set_zero_last(void)

  200. {

  201.         unsigned char PPTrimBuffer[7];



  202.         EEPROM_read_n(EepromDataAddr,EepromData,3);

  203.         pptrimRead(PPTrimBuffer, 54);

  204.         PPTrimBuffer[4]=EepromData[0];

  205.         PPTrimBuffer[5]=EepromData[1];

  206.         pptrimDelay(100);

  207. //        if(((PPTrimBuffer[4]&0x03)==0) && ((PPTrimBuffer[5]&0x80)==0))

  208.         pptrimWrite(PPTrimBuffer, 54);

  209. }



  210. void AS5145set_zero(void)

  211. {

  212.         unsigned char PPTrimBuffer[7];

  213.         unsigned char ZeroTemp;

  214.         unsigned short angle;

  215.         uint32 rd_data=0;

  216.         EEPROM_SectorErase(EepromDataAddr);//擦除Eeprom

  217.         pptrimRead(PPTrimBuffer, 54);

  218.         PPTrimBuffer[4]=0;

  219.         PPTrimBuffer[5]=0;

  220.         EepromData[0] = PPTrimBuffer[4];//保存在写入Eeprom的数据当中

  221.         EepromData[1] = PPTrimBuffer[5];

  222.         pptrimWrite(PPTrimBuffer, 54);

  223.         pptrimDelay(100);

  224.         // Step 1: Read the 18 bit SSI value from the AS5145 (AS5140 uses 16 bit)

  225.         rd_data=RD_SSI_data();//ssiRead(SSIBuffer, 18);

  226.         angle =(rd_data%0x20000)/0x20;

  227. //        angle =0x0c00;

  228.         pptrimDelay(100);         

  229.         // Step 2: Read the 54 PPTRIM OTP bits from the AS5145

  230.         // Step 3: Write the actual angle to the zero position field of the OTP bits

  231.         ZeroTemp=(angle/0x100)%0x10;        //GET 4 MSB

  232.         ZeroTemp = reversebits(ZeroTemp);

  233.         PPTrimBuffer[4] = ZeroTemp;

  234. //        Byte_Program(0X0001, PPTrimBuffer[4]);

  235.         pptrimDelay(100);

  236.         ZeroTemp=angle%0x100; //GET 8 LSB

  237.         ZeroTemp = reversebits(ZeroTemp);

  238.         PPTrimBuffer[5] = ZeroTemp;

  239. //        Byte_Program(0X0002, PPTrimBuffer[5]);

  240.         EepromData[0] = PPTrimBuffer[4];//保存在写入Eeprom的数据当中

  241.         EepromData[1] = PPTrimBuffer[5];//保存在写入Eeprom的数据当中

  242.         EEPROM_SectorErase(EepromDataAddr);//擦除Eeprom

  243.         EEPROM_write_n(EepromDataAddr,EepromData,3);//写入Eeprom的数据当中

  244.         pptrimDelay(100);

  245.         // Step 4: Write back the 54 PPTRIM OTP bits containing the Zero position (which is the actual angle) to the AS5145

  246.         pptrimWrite(PPTrimBuffer, 54);

  247. }

复制代码

回复

使用道具 举报

该用户从未签到

 楼主| 发表于 2015-5-21 15:13:32 | 显示全部楼层
#include "AS5145.H"改成51单片机的头文件#include <STC12C5630AD.H>
回复 支持 反对

使用道具 举报

  • TA的每日心情
    开心
    2021-12-10 15:56
  • 签到天数: 2675 天

    连续签到: 1 天

    [LV.Master]伴坛终老

    发表于 2015-5-21 16:39:01 | 显示全部楼层
    学习一下,感谢分享
    回复 支持 反对

    使用道具 举报

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

    本版积分规则

    关闭

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



    手机版|小黑屋|与非网

    GMT+8, 2024-4-20 18:41 , Processed in 0.130607 second(s), 21 queries , MemCache On.

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

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.