1.硬件
PCA9685是一款基于IIC总线通信的12位精度16通道PWM波输出的芯片,该芯片最初由NXP推出时主要面向LED开关调光,但就目前国内的形式来看,好像在被Arduino在舵机控制领域使用的更广泛。
该模块由于主要活跃在Aruino周边,所以在使用Arduino开发其底层驱动库是十分完善的,但对于单片机开发人员就不太友好了,需要自行根据用户手册在单片机上编写底层的驱动。
其地址的分配是通过模块右上方的短接焊盘来确定的,从A0-A5表示地址的最低位到最高位,也就是最多可级联2^5=32个模块,地址为: 1+A5+A4+A3+A2+A1+A0+rw。如果不用短接的话Ax=0;短接的话Ax=1;rw为写的话rw=0;rw为读的话rw=1;所以写入数据不做短接则地址应该为1000 0000 =0x80,许多人包括淘宝商家误以为默认地址是0x40,坑爹啊。
2.IIC通信
SCL接PH4,SDA接PH5,VCC接3.3V,GND接,V+单纯只是供电,在驱动大功率舵机时不接,但是外接电源的两个端口要接上正负极,否则无法供电。
STM32单片机源程序如下: - #include "stm32f10x_lib.h"
- #include "userconfig.h"
- #include "appdefine.h"
- #include "appfuns.h"
- #include "appvar.h"
- #include "appfiles.h"
- u8 mysendbuff[100];
- u8 HandShakeToMaster[7] = {0xAA, 0xBB, 0x00, 0x00, 0x00, 0x00, 0x65};//握手
- u8 WaterLowLine[7] = {0xAA, 0xEE, 0x00, 0x00, 0x00, 0x00, 0x98}; //请加水
- u8 TurnOffComputer[7] = {0xAA, 0xFF, 0x00, 0x00,0x00,0x00,0xA9};//停电关机
- void ProcessUSART(void){
- USARTRecBuff *u;
- SystermTask *s;
- u16 u16tempreceve;
- MuxData temp;
- u16 Motordata,Motorline;
-
- u = GetUSARTRecAddress();
- s = GetTaskAddress();
- if(u->COMState == COMBUSY){//如有完整帧接收
- /*通讯协议*/
- if(u->RxBuff[0] == 0xAA){
- u16tempreceve = GenCheck(&u->RxBuff[0],6);
- if(u->RxBuff[6] != u16tempreceve){
- Send2USART2("err",3);
- }
- else{
- Send2USART2((u8*)HandShakeToMaster,7);
- DelayX10us(50);
- switch(u->RxBuff[1]){
- case 0x03:
- StopAllMotor();
- temp. u8_data[0]= u->RxBuff[3] ;
- temp. u8_data[1]= u->RxBuff[2] ;
- Motordata = temp.u16_data[0];
- temp. u8_data[2]= u->RxBuff[5] ;
- temp. u8_data[3]= u->RxBuff[4];
- Motorline = temp.u16_data[1];
- MotorControl(0x01,Motordata,Motorline);//开
- // MotorControl(0x01,u->RxBuff[3],u->RxBuff[5]);//开
- // TurnOnELS();
- s->MotorBit = 1;
- s->Medecinestate = Medecine_Out_Testing;
- break;
- case 0x04:
- MotorControl(0x00,u->RxBuff[3],u->RxBuff[5]);//关
- break;
- case 0x05:
- StopAllMotor();
- break;
- case 0x06:
- break;
- case 0x07:
- TurnOnELS();
- CheckElsOn();
- break;
- case 0x08:
- TurnOffELS();
- CheckElsOff();
- break;
- case 0x09:
- CheckEls();
- break;
-
- case 0x0E:
- ControlOut1_On();
- break;
- case 0x0F:
- ControlOut1_Off();
- break;
- default:
- break;
- }
-
- }
- }
- else if(u->RxBuff[0] == 0xFF){
- // ShowString (&(u->RxBuff[3]) ,u->RxBuff[2] ,202 ,80);
- u->length = u->RxBuff[2];
- u16tempreceve = GenCheck(&u->RxBuff[0],(u->RxBuff[2]+3));
- if(u->RxBuff[u->length+3] != u16tempreceve){
- Send2USART2("err",3);
- }
- else if(u->RxBuff[1] == 0x01){
- // ClearDisplay();
- Choicepage(1);
- ShowString (&(u->RxBuff[3]) ,u->RxBuff[2] ,202 ,100);
- }
- }
- else if(u->RxBuff[0] == 0xEE){
- u->length = u->RxBuff[2];
- u16tempreceve = GenCheck(&u->RxBuff[0],(u->RxBuff[2]+3));
- if(u->RxBuff[u->length+3] != u16tempreceve){
- Send2USART2("err",3);
- }
- else if(u->RxBuff[1] == 0x12){
- ShowString (&(u->RxBuff[3]) ,u->RxBuff[2] ,202 ,80);
- }
-
- }
- u->RxCount = 0;
- u->COMState = COMIDLE;
- }
- }
- void ProcessTask(void){
- Motor_Set_Ang(15,120);
- delay_ms(250);
- Motor_Set_Ang(15, 0);
- delay_ms(250);
- Motor_Set_OFF(15);
-
- Motor_Set_Ang(14,120);
- delay_ms(250);
- Motor_Set_Ang(14, 0);
- delay_ms(250);
- Motor_Set_OFF(14);
-
- Motor_Set_Ang(13,120);
- delay_ms(250);
- Motor_Set_Ang(13, 0);
- delay_ms(250);
- Motor_Set_OFF(13);
- Motor_Set_Ang(12,120);
- delay_ms(250);
- Motor_Set_Ang(12, 0);
- delay_ms(250);
- Motor_Set_OFF(12);
-
- Motor_Set_Ang(11,120);
- delay_ms(250);
- Motor_Set_Ang(11, 0);
- delay_ms(250);
- Motor_Set_OFF(11);
-
- Motor_Set_Ang(10,120);
- delay_ms(250);
- Motor_Set_Ang(10, 0);
- delay_ms(250);
- Motor_Set_OFF(10);
-
- Motor_Set_Ang(9,120);
- delay_ms(250);
- Motor_Set_Ang(9, 0);
- delay_ms(250);
- Motor_Set_OFF(9);
-
- Motor_Set_Ang(8,120);
- delay_ms(250);
- Motor_Set_Ang(8, 0);
- delay_ms(250);
- Motor_Set_OFF(8);
-
- Motor_Set_Ang(7,120);
- delay_ms(250);
- Motor_Set_Ang(7, 0);
- delay_ms(250);
- Motor_Set_OFF(7);
-
- Motor_Set_Ang(6,120);
- delay_ms(250);
- Motor_Set_Ang(6, 0);
- delay_ms(250);
- Motor_Set_OFF(6);
-
- Motor_Set_Ang(5,120);
- delay_ms(250);
- Motor_Set_Ang(5, 0);
- delay_ms(250);
- Motor_Set_OFF(5);
-
- Motor_Set_Ang(4,120);
- delay_ms(250);
- Motor_Set_Ang(4, 0);
- delay_ms(250);
- Motor_Set_OFF(4);
-
- Motor_Set_Ang(3,120);
- delay_ms(250);
- Motor_Set_Ang(3, 0);
- delay_ms(250);
- Motor_Set_OFF(3);
-
- Motor_Set_Ang(2,120);
- delay_ms(250);
- Motor_Set_Ang(2, 0);
- delay_ms(250);
- Motor_Set_OFF(2);
-
- Motor_Set_Ang(1,120);
- delay_ms(250);
- Motor_Set_Ang(1, 0);
- delay_ms(250);
- Motor_Set_OFF(1);
-
- Motor_Set_Ang(0,120);
- delay_ms(250);
- Motor_Set_Ang(0, 0);
- delay_ms(250);
- }
- void ProcessCtrl(void){
- }
- int main(void){
-
-
- SystermInit();
- I2C_PCA9685_Init();
- OE_Init();
-
- while(1){
- ProcessUSART();
- ProcessCtrl();
- ProcessTask();
- }
- }
复制代码
所有资料51hei提供下载:
SoftwareforPCA9685.rar
(640.96 KB, 下载次数: 56)
|