找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1494|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

求求大神們看看代碼哪里出錯(cuò),編譯沒(méi)問(wèn)題,舵機(jī)不動(dòng)stm32+pca9685

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:886875 發(fā)表于 2021-2-27 15:55 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
  1. #include "iic.h"
  2. #include "gpio.h"
  3. #include "delay.h"

  4. #define TRUE 1
  5. #define FALSE 0
  6.                


  7. /**************************實(shí)現(xiàn)函數(shù)********************************************
  8. *函數(shù)原型:                void IIC_Start(void)
  9. *功  能:                產(chǎn)生IIC起始信號(hào)
  10. *******************************************************************************/
  11. uint8_t IIC_Start(void)
  12. {
  13.         SDA_H;
  14.         SCL_H;
  15.         delay_ms(5);
  16.         if(!SDA_read)
  17.                 return FALSE;       
  18.         SDA_L;
  19.         delay_ms(5);
  20.         if(SDA_read)
  21.                 return FALSE;       
  22.         SDA_L;
  23.         delay_ms(5);
  24.         return TRUE;
  25. }

  26. /**************************實(shí)現(xiàn)函數(shù)********************************************
  27. *函數(shù)原型:                void IIC_Stop(void)
  28. *功  能:            //產(chǎn)生IIC停止信號(hào)
  29. *******************************************************************************/
  30. void IIC_Stop(void)
  31. {
  32.         SCL_L;
  33.         delay_ms(5);
  34.         SDA_L;
  35.         delay_ms(5);
  36.         SCL_H;
  37.         delay_ms(5);
  38.         SDA_H;
  39.         delay_ms(5);
  40. }

  41. /**************************實(shí)現(xiàn)函數(shù)********************************************
  42. *函數(shù)原型:                uint8_t IIC_Wait_Ack(void)
  43. *功  能:            等待應(yīng)答信號(hào)到來(lái)
  44. //返回值:1,接收應(yīng)答失敗
  45. //        0,接收應(yīng)答成功
  46. *******************************************************************************/
  47. uint8_t IIC_Wait_Ack(void)        
  48. {
  49.         SCL_L;
  50.         delay_ms(5);
  51.         SDA_H;
  52.         delay_ms(5);
  53.         SCL_H;
  54.         delay_ms(5);
  55.         if(SDA_read)
  56.         {
  57.     SCL_L;
  58.           delay_ms(5);
  59.       return FALSE;
  60.         }
  61.         SCL_L;
  62.         delay_ms(5);
  63.         return TRUE;
  64. }

  65. /**************************實(shí)現(xiàn)函數(shù)********************************************
  66. *函數(shù)原型:                void IIC_Ack(void)
  67. *功  能:            產(chǎn)生ACK應(yīng)答
  68. *******************************************************************************/
  69. void IIC_Ack(void)
  70. {
  71.         SCL_L;
  72.         delay_ms(5);
  73.         SDA_L;
  74.         delay_ms(5);
  75.         SCL_H;
  76.         delay_ms(5);
  77.         SCL_L;
  78.         delay_ms(5);
  79. }

  80. /**************************實(shí)現(xiàn)函數(shù)********************************************
  81. *函數(shù)原型:                void IIC_NAck(void)
  82. *功  能:            產(chǎn)生NACK應(yīng)答
  83. *******************************************************************************/
  84. void IIC_NAck(void)
  85. {
  86.         SCL_L;
  87.         delay_ms(5);
  88.         SDA_H;
  89.         delay_ms(5);
  90.         SCL_H;
  91.         delay_ms(5);
  92.         SCL_L;
  93.         delay_ms(5);
  94. }

  95. /**************************實(shí)現(xiàn)函數(shù)********************************************
  96. *函數(shù)原型:                void IIC_Send_Byte(uint8_t txd)
  97. *功  能:            IIC發(fā)送一個(gè)字節(jié)
  98. *******************************************************************************/
  99. void IIC_Send_Byte(uint8_t SendByte)
  100. {
  101.     uint8_t i=8;
  102.     while(i--)
  103.     {
  104.                         SCL_L;
  105.                         delay_ms(5);
  106.                         if(SendByte&0x80)
  107.                                 SDA_H;
  108.                         else
  109.                                 SDA_L;
  110.                         SendByte<<=1;
  111.                         delay_ms(5);
  112.                         SCL_H;
  113.                    delay_ms(5);
  114.     }
  115.     SCL_L;
  116. }

  117. /**************************實(shí)現(xiàn)函數(shù)********************************************
  118. *函數(shù)原型:                uint8_t IIC_Read_Byte(unsigned char ack)
  119. *功  能:            //讀1串字節(jié),ack=1時(shí),發(fā)送ACK,ack=0,發(fā)送nACK
  120. *******************************************************************************/
  121. unsigned char IIC_Read_Byte(void)  
  122. {
  123.     uint8_t i=8;
  124.     uint8_t ReceiveByte=0;

  125.     SDA_H;
  126.     while(i--)
  127.     {
  128.                         ReceiveByte<<=1;
  129.                         SCL_L;
  130.                         delay_ms(5);
  131.                         SCL_H;
  132.                         delay_ms(5);
  133.                         if(SDA_read)
  134.                         {
  135.                                 ReceiveByte|=0x01;
  136.                         }
  137.     }
  138.     SCL_L;
  139.     return ReceiveByte;
  140. }

  141. /**************************實(shí)現(xiàn)函數(shù)********************************************
  142. *函數(shù)原型:                unsigned char IIC_ReadOneByte(unsigned char I2C_Addr,unsigned char addr)
  143. *功  能:            讀取指定設(shè)備 指定寄存器的一個(gè)值
  144. 輸入        I2C_Addr  目標(biāo)設(shè)備地址
  145.                 addr           寄存器地址
  146. 返回   讀出來(lái)的值
  147. *******************************************************************************/
  148. uint8_t IIC_ReadOneByte(uint8_t SlaveAddress,uint16_t REG_Address,uint8_t* data)
  149. {
  150.         if(!IIC_Start())
  151.                         return FALSE;
  152.     IIC_Send_Byte(SlaveAddress);
  153.     if(!IIC_Wait_Ack())
  154.                 {
  155.                         IIC_Stop();
  156.                         return FALSE;
  157.                 }
  158.     IIC_Send_Byte((uint8_t) REG_Address>>8);   
  159.     IIC_Wait_Ack();
  160.     IIC_Send_Byte((uint8_t) REG_Address & 0x00ff);   
  161.     IIC_Wait_Ack();
  162.     IIC_Start();
  163.     IIC_Send_Byte(SlaveAddress+1);
  164.     IIC_Wait_Ack();

  165.                 *data= IIC_Read_Byte();
  166.     IIC_NAck();
  167.     IIC_Stop();
  168.     return TRUE;
  169. }


  170. /**************************實(shí)現(xiàn)函數(shù)********************************************
  171. *函數(shù)原型:                uint8_t IICreadBytes(uint8_t dev, uint8_t reg, uint8_t length, uint8_t *data)
  172. *功  能:            讀取指定設(shè)備 指定寄存器的 length個(gè)值
  173. 輸入        dev  目標(biāo)設(shè)備地址
  174.                 reg          寄存器地址
  175.                 length 要讀的字節(jié)數(shù)
  176.                 *data  讀出的數(shù)據(jù)將要存放的指針
  177. 返回   讀出來(lái)的字節(jié)數(shù)量
  178. *******************************************************************************/
  179. uint8_t IICreadBytes(uint8_t SlaveAddress,uint16_t REG_Address,uint8_t len,uint8_t *data)
  180. {
  181.                 uint8_t i = 0;
  182.                 if(!IIC_Start())
  183.                         return FALSE;
  184.     IIC_Send_Byte(SlaveAddress);
  185.     if(!IIC_Wait_Ack())
  186.                 {
  187.                         IIC_Stop();
  188.                         return FALSE;
  189.                 }
  190.     IIC_Send_Byte((uint8_t) REG_Address>>8);   
  191.     IIC_Wait_Ack();
  192.     IIC_Send_Byte(REG_Address&0x00ff);   
  193.     IIC_Wait_Ack();
  194.     IIC_Start();
  195.     IIC_Send_Byte(SlaveAddress+1);
  196.     IIC_Wait_Ack();

  197.                 for(i = 0;i<len;i++)
  198.                 {
  199.                         if(i != (len -1))
  200.                         {
  201.                                 data[i]= IIC_Read_Byte();
  202.                                 IIC_Ack();
  203.                         }
  204.                         else
  205.                         {
  206.                                 data[i]= IIC_Read_Byte();
  207.                                 IIC_NAck();
  208.                         }
  209.                 }
  210.                 IIC_Stop();
  211.                 return len;
  212. }

  213. /**************************實(shí)現(xiàn)函數(shù)********************************************
  214. *函數(shù)原型:                uint8_t IICwriteBytes(uint8_t dev, uint8_t reg, uint8_t length, uint8_t* data)
  215. *功  能:            將多個(gè)字節(jié)寫入指定設(shè)備 指定寄存器
  216. 輸入        dev  目標(biāo)設(shè)備地址
  217.                 reg          寄存器地址
  218.                 length 要寫的字節(jié)數(shù)
  219.                 *data  將要寫的數(shù)據(jù)的首地址
  220. 返回   返回寫入長(zhǎng)度
  221. *******************************************************************************/
  222. uint8_t IICwriteBytes(uint8_t dev, uint16_t reg, uint16_t length, uint8_t* data)
  223. {

  224.         uint8_t count = 0;
  225.         IIC_Start();
  226.         IIC_Send_Byte(dev);          
  227.         IIC_Wait_Ack();
  228.         IIC_Send_Byte(reg>>8);   
  229.     IIC_Wait_Ack();
  230.         IIC_Send_Byte(reg & 0x00ff);  
  231. LEDx_GPIO_Config();
  232.                 GPIO_SetBits(LED_GPIO_PORT,LED_PIN);       
  233.     IIC_Wait_Ack();
  234.         for(count=0;count<length;count++)
  235.         {
  236.                 IIC_Send_Byte(data[count]);
  237.                 IIC_Wait_Ack();
  238.          }
  239.         IIC_Stop();

  240.     return length; //status == 0;
  241. }


  242. /**************************實(shí)現(xiàn)函數(shù)********************************************
  243. *函數(shù)原型:                unsigned char IICwriteByte(unsigned char dev, unsigned char reg, unsigned char data)
  244. *功  能:            寫入指定設(shè)備 指定寄存器一個(gè)字節(jié)
  245. 輸入        dev  目標(biāo)設(shè)備地址
  246.                 reg           寄存器地址
  247.                 data  將要寫入的字節(jié)
  248. 返回   1
  249. *******************************************************************************/
  250. uint8_t IICwriteByte(uint8_t dev, uint16_t reg, uint8_t data)
  251. {
  252.     return IICwriteBytes(dev, reg, 1, &data);
  253. }


  254. /**************************實(shí)現(xiàn)函數(shù)********************************************
  255. *函數(shù)原型:                uint8_t IICwriteBit(uint8_t dev, uint8_t reg, uint8_t bitNum, uint8_t data)
  256. *功  能:            讀 修改 寫 指定設(shè)備 指定寄存器一個(gè)字節(jié) 中的1個(gè)位
  257. 輸入        dev  目標(biāo)設(shè)備地址
  258.                 reg           寄存器地址
  259.                 bitNum  要修改目標(biāo)字節(jié)的bitNum位
  260.                 data  為0 時(shí),目標(biāo)位將被清0 否則將被置位
  261. 返回   成功 為1
  262.                 失敗為0
  263. *******************************************************************************/
  264. uint8_t IICwriteBit(uint8_t dev, uint16_t reg, uint8_t bitNum, uint8_t data)
  265. {
  266.     uint8_t b;
  267.     IIC_ReadOneByte(dev, reg, &b);
  268.     b = (data != 0) ? (b | (1 << bitNum)) : (b & ~(1 << bitNum));
  269.     return IICwriteByte(dev, reg, b);
  270. }
復(fù)制代碼
  1. #ifndef _IIC_H
  2. #define _IIC_H

  3. #include "stm32f4xx.h"
  4. #include "pca9685.h"


  5. #define I2C_SLAVE_ADDRESS7     0x80


  6. #define SCL_H         GPIO_WriteBit(GPIOF,GPIO_Pin_1,Bit_SET)
  7. #define SCL_L         GPIO_WriteBit(GPIOF,GPIO_Pin_1,Bit_RESET)
  8.                                          
  9. #define SDA_H         GPIO_WriteBit(GPIOF,GPIO_Pin_0,Bit_SET)
  10. #define SDA_L         GPIO_WriteBit(GPIOF,GPIO_Pin_0,Bit_RESET)

  11. #define SCL_read      GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_1)
  12. #define SDA_read      GPIO_ReadOutputDataBit(GPIOF,GPIO_Pin_0)

  13. void IIC_Init(void);                //初始化IIC的IO口


  14. uint8_t IIC_ReadOneByte(uint8_t SlaveAddress,uint16_t REG_Address,uint8_t* data);
  15. unsigned char IICwriteByte(unsigned char dev, uint16_t reg, unsigned char data);
  16. uint8_t IICwriteBytes(uint8_t dev, uint16_t reg, uint16_t length, uint8_t* data);
  17. uint8_t IICwriteBit(uint8_t dev,uint16_t reg,uint8_t bitNum,uint8_t data);
  18. uint8_t IICreadBytes(uint8_t SlaveAddress,uint16_t REG_Address,uint8_t len,uint8_t *data);

  19. uint8_t IIC_Start(void);                               
  20. void IIC_Stop(void);                         
  21. void IIC_Send_Byte(uint8_t txd);       
  22. uint8_t IIC_Read_Byte(void);
  23. uint8_t IIC_Wait_Ack(void);        
  24. void IIC_Ack(void);                               
  25. void IIC_NAck(void);
  26. #endif
復(fù)制代碼
  1. #ifndef __PCA_H
  2. #define        __PCA_H
  3. #include "stm32f4xx.h"
  4. #define PCA9685_MODE1   0x0
  5. #define PCA9685_SUBADR1 0x2
  6. #define PCA9685_SUBADR2 0x3
  7. #define PCA9685_SUBADR3 0x4
  8. #define pca_adrr    0x80

  9. #define LED0_ON_L 0x6
  10. #define LED0_ON_H 0x7
  11. #define LED0_OFF_L 0x8
  12. #define LED0_OFF_H 0x9

  13. #define ALLLED_ON_L 0xFA
  14. #define ALLLED_ON_H 0xFB
  15. #define ALLLED_OFF_L 0xFC
  16. #define ALLLED_OFF_H 0xFD
  17. #define PCA9685_PRESCALE 0xFE

  18. void pca_write(uint8_t adrr,uint8_t data);
  19. uint8_t pca_read(uint8_t adrr);
  20. void pca_setfreq(float freq);
  21. void setpwm(uint8_t num, uint32_t on, uint32_t off);
  22. void pca_setpwm(uint8_t num,uint8_t off);
  23. uint8_t transform(uint8_t angel);
  24. #endif<div class="blockcode"><blockquote>
復(fù)制代碼



  1. #include "iic.h"
  2. #include "pca9685.h"
  3. #include "math.h"
  4. #include "delay.h"
  5. void pca_setfreq(float freq)
  6. {
  7.         float prescale = 25000000.0;
  8. freq *= 1.024;
  9. prescale = prescale/(4096*freq)-1;
  10.         IICwriteByte(pca_adrr,PCA9685_MODE1,0x10);
  11.    delay_ms(2);
  12.         IICwriteByte(pca_adrr,PCA9685_PRESCALE,(uint8_t) prescale);       
  13.         delay_ms(2);
  14.         IICwriteByte(pca_adrr,PCA9685_MODE1,0x00);       
  15.    delay_ms(2);
  16.         }
  17. void setpwm(uint8_t num, uint32_t on, uint32_t off)
  18. {
復(fù)制代碼
  1. #include "delay.h"
  2. void delay_us(uint32_t time){

  3.    uint16_t i=0;

  4. while(time--){

  5. i=10;//自己定義
  6. while(i--) ;
  7. }
  8. }

  9. //毫秒級(jí)的延時(shí)
  10. void delay_ms(uint16_t time){
  11.         uint16_t i=0;
  12.         while(time--){

  13. i=12000;//自己定義
  14. while(i--) ;
  15.         }
  16. }
復(fù)制代碼
  1. #ifndef __LED_H
  2. #define __LED_H
  3. #include "stm32f4xx.h"

  4. void delay_Init(u8 SYSCL);
  5. void delay_ms(u16 nms);
  6. void delay_us(u32 nus);
  7. #endif
復(fù)制代碼



分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:827243 發(fā)表于 2021-2-27 16:34 | 只看該作者
這個(gè)要有檢查的思路,首先確認(rèn)PCA9685硬件的地址是否是0x80
#define PCA9685_adrr 0x80//  1+A5+A4+A3+A2+A1+A0+w/r
                        //片選地址,將焊接點(diǎn)置1可改變地址,
                        // 當(dāng)IIC總 呱嫌 多片PCA9685或相同地址時(shí)才需焊接

然后確認(rèn)IIC是否工作正常,用讀命令讀PCA9685模式狀態(tài),看看能否讀出。
最后確定寫入?yún)?shù)是否正確。
回復(fù)

使用道具 舉報(bào)

板凳
ID:410541 發(fā)表于 2021-3-10 14:36 | 只看該作者
樓主解決了沒(méi)?
我的pca9685無(wú)法通訊
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表