刚买的pca9685写的代码一直驱动不了舵机,小白求解

新手小白求问
刚买了pca9685模块,看数据手册没有中文的只好找了些别人stm32的代码改过来用在51上面,但是一直都驱动不了舵机,不是知道代码出了什么问题,求解
以下就是我用的所有源码。

#ifndef __IIC_H
#define __IIC_H
sbit IIC_SDA = P3^6;
sbit IIC_SCL = P3^7;

#define u8 unsigned char
#define u16 char
//IIC所有操作函数
void delay_us(u8 i);
void IIC_Start(void);               //发送IIC开始信号
void IIC_Stop(void);                //发送IIC停止信号
void IIC_Send_Byte(u8 txd);         //IIC发送一个字节
u8 IIC_Read_Byte(unsigned char ack);//IIC读取一个字节
u8 IIC_Wait_Ack(void);              //IIC等待ACK信号
void IIC_Ack(void);                 //IIC发送ACK信号
void IIC_NAck(void);                //IIC不发送ACK信号
//void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 dat);
//u8 IIC_Read_One_Byte(u8 daddr,u8 addr);     

#endif
#include <stc12c5a60s2.h>
#include <IIC.h>
#include <intrins.h>



void delay_us(u8 i)
{
    u8 j;
    for(j=i;j>0;j--)
    {_nop_();}
}
//产生IIC起始信号
void IIC_Start(void)
{
//  SDA_OUT();     //sda线输出
    IIC_SDA=1;        
    IIC_SCL=1;
    delay_us(4);
    IIC_SDA=0;//START:when CLK is high,DATA change form high to low 
    delay_us(4);
    IIC_SCL=0;//钳住I2C总线,准备发送或接收数据 
}     
//产生IIC停止信号
void IIC_Stop(void)
{
//  SDA_OUT();//sda线输出
    IIC_SCL=0;
    IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
    delay_us(4);
    IIC_SCL=1; 
    IIC_SDA=1;//发送I2C总线结束信号
    delay_us(4);                                
}
//等待应答信号到来
//返回值:1,接收应答失败
//        0,接收应答成功
u8 IIC_Wait_Ack(void)
{
    u8 ucErrTime=0;
//  SDA_IN();      //SDA设置为输入  
    IIC_SDA=1;delay_us(1);     
    IIC_SCL=1;delay_us(1);   
    while(IIC_SDA)
    {
        ucErrTime++;
        if(ucErrTime>250)
        {
            IIC_Stop();
            return 1;
        }
    }
    IIC_SCL=0;//时钟输出0      
    return 0;  
} 
//产生ACK应答
void IIC_Ack(void)
{
    IIC_SCL=0;
//  SDA_OUT();
    IIC_SDA=0;
    delay_us(2);
    IIC_SCL=1;
    delay_us(2);
    IIC_SCL=0;
}
//不产生ACK应答            
void IIC_NAck(void)
{
    IIC_SCL=0;
//  SDA_OUT();
    IIC_SDA=1;
    delay_us(2);
    IIC_SCL=1;
    delay_us(2);
    IIC_SCL=0;
}                                        
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答           
void IIC_Send_Byte(u8 txd)
{                        
    u8 t;   
//  SDA_OUT();      
    IIC_SCL=0;//拉低时钟开始数据传输
    for(t=0;t<8;t++)
    {              
        IIC_SDA=(txd&0x80)>>7;
        txd<<=1;      
        delay_us(2);   //对TEA5767这三个延时都是必须的
        IIC_SCL=1;
        delay_us(2); 
        IIC_SCL=0;  
        delay_us(2);
    }    
}       
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK   
u8 IIC_Read_Byte(unsigned char ack)
{
    unsigned char i,receive=0;
//  SDA_IN();//SDA设置为输入
    for(i=0;i<8;i++ )
    {
        IIC_SCL=0; 
        delay_us(2);
        IIC_SCL=1;
        receive<<=1;
        if(IIC_SDA)receive++;   
        delay_us(1); 
    }                    
    if (!ack)
        IIC_NAck();//发送nACK
    else
        IIC_Ack(); //发送ACK   
    return receive;
}
#include <stc12c5a60s2.h>
#include <IIC.h>
#include <intrins.h> 
#include <pca8574.h>
#define uchar unsigned char          
#define uint unsigned int           


sbit scl=P3^6;                    //时钟输入线
sbit sda=P3^7;                   //数据输入/输出端

void PCA9685_write(unsigned char reg,unsigned char dat);
u8 PCA9685_read(unsigned char reg);
void setPWMFreq(u8 freq);
void setPWM(u8 num, u16 on, u16 off);
void down();
void up();

void delay_ms(u8 xms)
{
    u8 i,j;
    for(i=xms;i>0;i--)
    for (j=200;j>0;j--);
}
void PCA9685_write(unsigned char reg,unsigned char dat)
{
    IIC_Start();
    IIC_Send_Byte(PCA9685_adrr);
    IIC_Wait_Ack();
    IIC_Send_Byte(reg);
    IIC_Wait_Ack();
    IIC_Send_Byte(dat);
    IIC_Wait_Ack();
    IIC_Stop();
}
u8 PCA9685_read(unsigned char reg)
{
    u8 res;
    IIC_Start();
    IIC_Send_Byte(PCA9685_adrr);
    IIC_Wait_Ack();
    IIC_Send_Byte(reg);
    IIC_Wait_Ack();    
    IIC_Start();                
    IIC_Send_Byte(PCA9685_adrr|0X01);
    IIC_Wait_Ack();
    res=IIC_Read_Byte(0);       
    IIC_Stop();             
    return res;  
}
void setPWMFreq(u8 freq)
{
   u8 prescale,oldmode,newmode;
   double prescaleval;
   prescaleval = 25000000.0/(4096*freq*0.915);
   prescale = (u8)(prescaleval+0.5)-1;

   oldmode = PCA9685_read(PCA9685_MODE1);
   newmode = (oldmode&0x7F) | 0x10; // sleep
   PCA9685_write(PCA9685_MODE1, newmode); // go to sleep
   PCA9685_write(PCA9685_PRESCALE, prescale); // set the prescaler
   PCA9685_write(PCA9685_MODE1, oldmode);
   delay_ms(5);
   PCA9685_write(PCA9685_MODE1, oldmode | 0xa1); 
}
void setPWM(u8 num, u16 on, u16 off) 
{
    PCA9685_write(LED0_ON_L+4*num,on);
    PCA9685_write(LED0_ON_H+4*num,on>>8);
    PCA9685_write(LED0_OFF_L+4*num,off);
    PCA9685_write(LED0_OFF_H+4*num,off>>8);
}
u16 calculate_PWM(u8 angle)
{
    return (int)(204.8*(0.5+angle*1.0/90));
}
void down()
    {
    u16 pwm = calculate_PWM(0);
    setPWM(0x0,0,pwm);
    delay_ms(1);
    setPWM(0x1,0,pwm);
    delay_ms(1);
    setPWM(0x2,0,pwm);
    delay_ms(1);
    setPWM(0x3,0,pwm);
    delay_ms(1);
    setPWM(0x4,0,pwm);
    delay_ms(1);
    setPWM(0x5,0,pwm);
    delay_ms(1);
    setPWM(0x6,0,pwm);
    delay_ms(1);
    setPWM(0x7,0,pwm);
}

void up()
    {
    u16 pwm = calculate_PWM(90);
    setPWM(0x0,0,pwm);
    delay_ms(1);
    setPWM(0x1,0,pwm);
    delay_ms(1);
    setPWM(0x2,0,pwm);
    delay_ms(1);
    setPWM(0x3,0,pwm);
    delay_ms(1);
    setPWM(0x4,0,pwm);
    delay_ms(1);
    setPWM(0x5,0,pwm);
    delay_ms(1);
    setPWM(0x6,0,pwm);
    delay_ms(1);
    setPWM(0x7,0,pwm);
}


void main()
{
    PCA9685_write(PCA9685_MODE1,0x0);//PCA9685复位
    setPWMFreq(50);
    while(1)
    {
    down();
    up();
    }
}   




#ifndef __PCF8574_H
#define __PCF8574_H
#include <stc12c5a60s2.h>

#define PCA9685_adrr 0x40
#define PCA9685_SUBADR1 0x2
#define PCA9685_SUBADR2 0x3
#define PCA9685_SUBADR3 0x4

#define PCA9685_MODE1 0x0
#define PCA9685_PRESCALE 0xFE


#define LED0_ON_L 0x6
#define LED0_ON_H 0x7
#define LED0_OFF_L 0x8
#define LED0_OFF_H 0x9

#define ALLLED_ON_L 0xFA
#define ALLLED_ON_H 0xFB
#define ALLLED_OFF_L 0xFC
#define ALLLED_OFF_H 0xFD

#define u8 unsigned char
#define u16 char

1个回答

我这有能驱动的PCA9685的程序,之前我也自己写过,不过都失败,后来查了很久,都是因为IIC的问题,导致和mcu不能通讯,就控制不了

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问