搜索
bottom↓
回复: 8

C8051F系列的单片机为什么SMBUS的寄存器都不一样啊

[复制链接]

出0入0汤圆

发表于 2014-7-5 09:28:32 | 显示全部楼层 |阅读模式
看了C8051F350        和C8051F320的SMBUS寄存器,发现有好多地方不一样啊,350从机地址寄存器,也就是说做从机的时候,你只需要对ACK操作就可以了,比如主机发地址以后,你就发一个ACK就代表地址匹配了,但是320就不一样了,人家有从机地址寄存器,而且大部分的MCU做从机的时候都有地址寄存器的,350完全是玩非主流啊!

而且看了SILICON LAB提供的DEMO。SCL,SDA是哪两个引脚我到现在还稀里糊涂,P01,P00还是P06,P07.
关键是网上的朋友用硬件iic的少之又少,资料也不全,看来这次调试只能自己看规格书了。

阿莫论坛20周年了!感谢大家的支持与爱护!!

月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!

出0入0汤圆

 楼主| 发表于 2014-7-5 10:09:49 | 显示全部楼层
  1. #include <C8051F350.h>
  2. bit flag_timer3;
  3. unsigned char smbusdata;
  4. bit DATA_READY = 0;  


  5. #define  WRITE          0x00           // SMBus WRITE command
  6. #define  READ           0x01           // SMBus READ command

  7. #define  SLAVE_ADDR     0xF0           // Device addresses (7 bits,
  8.                                        // lsb is a don't care)

  9. // Status vector - top 4 bits only
  10. #define  SMB_SRADD      0x20           // (SR) slave address received
  11.                                        //    (also could be a lost
  12.                                        //    arbitration)
  13. #define  SMB_SRSTO      0x10           // (SR) STOP detected while SR or ST,
  14.                                        //    or lost arbitration
  15. #define  SMB_SRDB       0x00           // (SR) data byte received, or
  16.                                        //    lost arbitration
  17. #define  SMB_STDB       0x40           // (ST) data byte transmitted
  18. #define  SMB_STSTO      0x50           // (ST) STOP detected during a
  19.                                        //    transaction; bus error
  20. // End status vector definition

  21. #define  SYSCLK         24500000       // System clock frequency in Hz
  22. #define  SMB_FREQUENCY  10000
  23. // 16-bit SFR declarations
  24. sfr16    TMR3RL   = 0x92;              // Timer3 reload registers
  25. sfr16    TMR3     = 0x94;              // Timer3 counter registers


  26. void  Port_Init()
  27. {
  28.           P0MDOUT = 0x00;                     // All P0 pins open-drain output
  29.            XBR0 = 0x04;                        // Enable SMBus pins
  30.            XBR1 = 0x40;                        // Enable crossbar and weak pull-ups
  31.     P0 = 0xFF;
  32. }

  33. void Timer1_Init (void)
  34. {

  35. // Make sure the Timer can produce the appropriate frequency in 8-bit mode
  36. // Supported SMBus Frequencies range from 10kHz to 100kHz.  The CKCON register
  37. // settings may need to change for frequencies outside this range.
  38. #if ((SYSCLK/SMB_FREQUENCY/3) < 255)
  39.    #define SCALE 1
  40.       CKCON |= 0x08;                   // Timer1 clock source = SYSCLK
  41. #elif ((SYSCLK/SMB_FREQUENCY/4/3) < 255)
  42.    #define SCALE 4
  43.       CKCON |= 0x01;
  44.       CKCON &= ~0x0A;                  // Timer1 clock source = SYSCLK / 4
  45. #endif

  46.    TMOD = 0x20;                        // Timer1 in 8-bit auto-reload mode

  47.    // Timer1 configured to overflow at 1/3 the rate defined by SMB_FREQUENCY
  48.    TH1 = -(SYSCLK/SMB_FREQUENCY/SCALE/3);

  49.    TL1 = TH1;                          // Init Timer1

  50.    TR1 = 1;                            // Timer1 enabled
  51. }

  52. void Timer3_Init (void)
  53. {
  54.    TMR3CN = 0x00;                      // Timer3 configured for 16-bit auto-
  55.                                        // reload, low-byte interrupt disabled

  56.    CKCON &= ~0x40;                     // Timer3 uses SYSCLK/12

  57.    TMR3RL = -(SYSCLK/12/40);           // Timer3 configured to overflow after
  58.    TMR3 = TMR3RL;                      // ~25ms (for SMBus low timeout detect):
  59.                                        // 1/.025 = 40

  60.    EIE1 |= 0x80;                       // Timer3 interrupt enable
  61.    TMR3CN |= 0x04;                     // Start Timer3
  62. }

  63. void SMBus_Init (void)
  64. {
  65.    SMB0CF = 0x1D;                      // Use Timer1 overflows as SMBus clock
  66.                                        // source;
  67.                                        // Enable slave mode;
  68.                                        // Enable setup & hold time
  69.                                        // extensions;
  70.                                        // Enable SMBus Free timeout detect;
  71.                                        // Enable SCL low timeout detect;

  72.    SMB0CF |= 0x80;                     // Enable SMBus;
  73. }

  74. void main()
  75. {
  76.          PCA0MD &= ~0x40;                                      //禁止看门狗
  77.          OSCICN |= 0x03;                                      //时钟设置成24.5MHZ
  78.          Port_Init();
  79.          Timer3_Init();                                                //定时器3超时检测
  80.          Timer1_Init();                                                //timer1配置成SCL时钟
  81.          SMBus_Init();
  82.          EIE1 |= 0x01;         
  83.          EA = 1;                                                       //全局中断使能
  84.          smbusdata = 0xFD;                            //初始化SMBUS数据
  85.          while(1)
  86.          {
  87.                   if(flag_timer3==1)
  88.                  {
  89.                          flag_timer3=0;

  90.                  }
  91.          }

  92. }

  93. void Timer3_ISR (void) interrupt 14
  94. {
  95. //          TMR3 = -(SYSCLK/12/40);         
  96.         SMB0CF &= ~0x80;                    // Disable SMBus
  97.         SMB0CF |= 0x80;                     // Re-enable SMBus
  98.            TMR3CN &= ~0x80;                    // Clear Timer3 interrupt-pending flag
  99.     flag_timer3=1;

  100. }

  101. void SMBus_ISR (void) interrupt 7
  102. {
  103.    if (ARBLOST == 0)
  104.    {
  105.       switch (SMB0CN & 0xF0)           // Decode the SMBus status vector
  106.       {
  107.          // Slave Receiver: Start+Address received
  108.          case  SMB_SRADD:

  109.             STA = 0;                   // Clear STA bit
  110.             if((SMB0DAT&0xFE) == (SLAVE_ADDR&0xFE)) // Decode address
  111.             {                          // If the received address matches,
  112.                ACK = 1;                // ACK the received slave address
  113.                if((SMB0DAT&0x01) == READ) // If the transfer is a master READ,
  114.                {
  115.                   SMB0DAT = 0x41;  // Prepare outgoing byte
  116.                }
  117.             }
  118.             else                       // If received slave address does not
  119.             {                          // match,
  120.                ACK = 0;                // NACK received address
  121.             }
  122.             break;

  123.          // Slave Receiver: Data received
  124.          case  SMB_SRDB:

  125.             smbusdata = SMB0DAT;        // Store incoming data
  126.             DATA_READY = 1;            // Indicate new data received
  127.             ACK = 1;                   // ACK received data

  128.             break;

  129.          // Slave Receiver: Stop received while either a Slave Receiver or
  130.          // Slave Transmitter
  131.          case  SMB_SRSTO:

  132.             STO = 0;                   // STO must be cleared by software when
  133.                                        // a STOP is detected as a slave
  134.             break;

  135.          // Slave Transmitter: Data byte transmitted
  136.          case  SMB_STDB:
  137.                                        // No action required;
  138.                                        // one-byte transfers
  139.                                        // only for this example
  140.             break;

  141.          // Slave Transmitter: Arbitration lost, Stop detected
  142.          //
  143.          // This state will only be entered on a bus error condition.
  144.          // In normal operation, the slave is no longer sending data or has
  145.          // data pending when a STOP is received from the master, so the TXMODE
  146.          // bit is cleared and the slave goes to the SRSTO state.
  147.          case  SMB_STSTO:

  148.             STO = 0;                   // STO must be cleared by software when
  149.                                        // a STOP is detected as a slave
  150.             break;

  151.          // Default: all other cases undefined
  152.          default:

  153.             SMB0CF &= ~0x80;           // Reset communication
  154.             SMB0CF |= 0x80;
  155.             STA = 0;
  156.             STO = 0;
  157.             ACK = 0;
  158.             break;
  159.       }
  160.    }
  161.    // ARBLOST = 1, Abort failed transfer
  162.    else
  163.    {
  164.       STA = 0;
  165.       STO = 0;
  166.       ACK = 0;
  167.    }

  168.    SI = 0;                             // Clear SMBus interrupt flag
  169. }


复制代码


终于给我研究出来了!

有几个地方我给大家提示一下哈,

出0入4汤圆

发表于 2014-7-5 10:12:22 | 显示全部楼层
它的硬件功能管脚,是可以顺序排列下来的。不是固定哪个脚。
中断,只能是 P0。

每个型号,都有配置表和配置软件,你好好看看。

出0入0汤圆

 楼主| 发表于 2014-7-5 10:22:34 | 显示全部楼层
第一点:
从机地址匹配寄存器,怎么说呢?当时看到这里觉得好奇怪,为什么这个片子和其他的MCU不一样呢。后来发现原来是主机发地址后,作为从机的话只需要发一个ACK就代表地址匹配了,其实和其他MCU差不多了,因为机制都是一样的,都会和自身的地址进行比对,如果是一样的话,就发ack给主机
第二点,发送数据,我想这一个是很多朋友不太明白的地方,其实当时我调试的时候都遇到了这类情况,就是说一些MCU做从机的时候呢,不能发送数据,只能接受数据,后来我一琢磨,其实发不了数据是因为从机发数据的时候需要一个CLK.就是当前的时钟不止是主机提供,其实这个手册已经讲的很清楚,而我们很多都忽略了这个地方!

出0入0汤圆

 楼主| 发表于 2014-7-5 10:23:21 | 显示全部楼层
我再测测 会不会死机和其他的一些问题吧!

出0入0汤圆

发表于 2016-8-23 23:20:11 | 显示全部楼层
?C8051F350和C8051F320都没有地址寄存器的啊,比如C8051F020和C8051F040都有地址寄存器SMB0ADR,用地址寄存器根据例程我调通了C8051F040的SMBUS主从通信。现在想用C8051F340来做从机,通过SMBUS来接收数据,但是不知道怎么实现了。不知道楼主以上的程序是否调通,该代码是作为从机的吗?

出0入0汤圆

发表于 2016-8-23 23:33:13 | 显示全部楼层
此贴不会沉了吧

出0入0汤圆

 楼主| 发表于 2016-8-24 20:39:27 | 显示全部楼层

你现在是什么问题,我调通了

出0入0汤圆

发表于 2016-8-24 22:39:14 | 显示全部楼层
我糗大了!我原来使用040来调的,当把程序移植到340上后,才注意到SMBUS的寄存器不一样,缺少地址寄存器,原来调好程序根本没法在340上用,又需要从头来做程序,但是没找到340相关的SMBUS主从通讯例程,还无从下手。正好看到你的帖子,就来请教了
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-25 19:34

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表