51单片机PCA9685驱动板驱动舵机
单片机源程序如下:
- #include<reg52.h>
- #include <intrins.h>
- #include <stdio.h>
- #include <math.h>
- typedef unsigned char uchar;
- typedef unsigned int uint;
- unsigned char buf;
- sbit scl=P2^6;
- sbit sda=P2^7;
- #define PCA9685_adrr 0x80// 1+A5+A4+A3+A2+A1+A0+w/r
- #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 SERVOMIN 115 // this is the 'minimum' pulse length count (out of 4096)
- #define SERVOMAX 590 // this is the 'maximum' pulse length count (out of 4096)
- #define SERVO000 130 //0度
- #define SERVO180 520 //180度
- #define SERVO80 284 //80度
- #define SERVO110 340//110度
- void delayms(uint z)
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=148;y>0;y--);
- }
- void delayus() //大于4.7us
- {
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- _nop_();
- }
- /*---------------------------------------------------------------
- IIC初始化
- ----------------------------------------------------------------*/
- void init()
- {
- sda=1; //sda scl使用前被拉高
- delayus();
- scl=1;
- delayus();
- }
- /*---------------------------------------------------------------
- IIC????????
- ----------------------------------------------------------------*/
- void start()
- {
- sda=1;
- delayus();
- scl=1; //scl高 sda拉低 ????IIC启动
- delayus();
- sda=0;
- delayus();
- scl=0;
- delayus();
- }
- /*---------------------------------------------------------------
- IIC停止
- ----------------------------------------------------------------*/
- void stop()
- {
- sda=0;
- delayus();
- scl=1; //scl??? sda??????? ????IIC??
- delayus();
- sda=1;
- delayus();
- }
- /*---------------------------------------------------------------
- IIC应答
- ----------------------------------------------------------------*/
- void ACK()
- {
- uchar i;
- scl=1;
- delayus();
- while((sda=1)&&(i<255))
- i++;
- scl=0;
- delayus();
- }
- /*---------------------------------------------------------------
- 写字节
- ----------------------------------------------------------------*/
- void write_byte(uchar byte)
- {
- uchar i,temp;
- temp=byte;
- for(i=0;i<8;i++)
- {
- temp=temp<<1;
- scl=0;
- delayus();
- sda=CY;
- delayus();
- scl=1;
- delayus();
- }
- scl=0;
- delayus();
- sda=1;
- delayus();
- }
- /*---------------------------------------------------------------
- 肚子节
- ----------------------------------------------------------------*/
- uchar read_byte()
- {
- uchar i,j,k;
- scl=0;
- delayus();
- sda=1;
- delayus();
- for(i=0;i<8;i++)
- {
- delayus();
- scl=1;
- delayus();
- if(sda==1)
- {
- j=1;
- }
- else j=0;
- k=(k<< 1)|j;
- scl=0;
- }
- delayus();
- return k;
- }
- /*---------------------------------------------------------------
- ??PCA9685?????
- ----------------------------------------------------------------*/
- /*---------------------------------------------------------------
- ?PCA9685????,??
- ----------------------------------------------------------------*/
- void PCA9685_write(uchar address,uchar date)
- {
- start();
- write_byte(PCA9685_adrr); //PCA9685?????
- ACK();
- write_byte(address); //???????
- ACK();
- write_byte(date); //???
- ACK();
- stop();
- }
- /*---------------------------------------------------------------
- 从PCA9685读数据有返回值
- ----------------------------------------------------------------*/
- uchar PCA9685_read(uchar address)
- {
- uchar date;
- start();
- write_byte(PCA9685_adrr); //PCA9685?????
- ACK();
- write_byte(address);
- ACK();
- start();
- write_byte(PCA9685_adrr|0x01); //?????????????,?????
- ACK();
- date=read_byte();
- stop();
- return date;
- }
- /*---------------------------------------------------------------
- PCA9685复位
- ----------------------------------------------------------------*/
- void reset(void)
- {
- PCA9685_write(PCA9685_MODE1,0x0);
- }
- void begin(void)
- {
- reset();
- }
- /*---------------------------------------------------------------
- PCA9685修改频率
- ----------------------------------------------------------------*/
- void setPWMFreq(float freq)
- {
- uint prescale,oldmode,newmode;
- float prescaleval;
- freq *= 0.92; // Correct for overshoot in the frequency setting
- prescaleval = 25000000;
- prescaleval /= 4096;
- prescaleval /= freq;
- prescaleval -= 1;
- prescale = floor(prescaleval + 0.5);
-
- 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);
- delayms(2);
- PCA9685_write(PCA9685_MODE1, oldmode | 0xa1);
- }
- /*---------------------------------------------------------------
- PCA9685??????
- num:??PWM????0~15,on:PWM?????0~4096,off:PWM?????0~4096
- ??PWM????4096?,?0??+1??,??on???????,?????off?
- ??????,????4096????????on???0?????,?on??0?,
- off/4096????PWM?????
- ----------------------------------------------------------------*/
- void setPWM(uint num, uint on, uint 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);
- }
- void main()
- {
- init();
- begin();
- setPWMFreq(50); //设置50hz
-
- SCON=0x50; //设定串口工作方式
- PCON=0x00; //波特率不倍增
- TMOD=0x20; //定时器1工作于8位自动重载模式, 用于产生波特率
- EA=1;
- ES = 1; //允许串口中断
- TL1=0xfd;
- TH1=0xfd; //波特率9600
- TR1=1;
- // delayms(1000);
- //60度=0.5ms+(60/180)*(2.5ms-0.5ms)=1.1666ms
- //利用占空比=1.1666ms/20ms=off/4096,off=239,50hz对应周期20ms
-
- setPWM(0, 0, SERVOMAX);
- setPWM(1, 0, SERVOMAX);
- setPWM(2, 0, SERVOMAX);
- setPWM(3, 0, SERVOMAX);
- setPWM(4, 0, SERVOMAX);
- setPWM(5, 0, SERVOMAX);
- setPWM(6, 0, SERVOMAX);
- setPWM(7, 0, SERVOMAX);
- setPWM(8, 0, SERVOMAX);
- while(1)
- {
- switch(buf)
- {
- case 0x11: setPWM(0, 0, SERVO110); break;
- case 0x12: setPWM(0, 0, SERVOMAX); break;
- case 0x13: setPWM(1, 0, SERVO110); break;
- case 0x14: setPWM(1, 0, SERVOMAX); break;
- case 0x15: setPWM(2, 0, SERVO110); break;
- case 0x16: setPWM(2, 0, SERVOMAX); break;
- case 0x17: setPWM(3, 0, SERVO110); break;
- case 0x18: setPWM(3, 0, SERVOMAX); break;
- case 0x19: setPWM(4, 0, SERVO110); break;
- case 0x20: setPWM(4, 0, SERVOMAX); break;
- case 0x21: setPWM(5, 0, SERVO110); break;
- case 0x22: setPWM(5, 0, SERVOMAX); break;
- case 0x23: setPWM(6, 0, SERVO110); break;
- case 0x24: setPWM(6, 0, SERVOMAX); break;
- case 0x25: setPWM(7, 0, SERVO110); break;
- case 0x26: setPWM(7, 0, SERVOMAX); break;
- case 0x27: setPWM(8, 0, SERVO110); break;
- case 0x28: setPWM(8, 0, SERVOMAX); break;
- default:break; //接受到其它数据,蜂鸣器响
- }
- }
- }
- void serial() interrupt 4
- {
- ES = 0; //关闭串行中断
- RI = 0; //清除串行接受标志位
- buf = SBUF; //从串口缓冲区取得数据
-
- ES = 1; //允许串口中断
- }
复制代码
以上代码51hei下载(请大家多多指教):
51单片机PCA9685驱动板驱动舵机.zip
(35.92 KB, 下载次数: 90)
|