我还是参照相关资料写出程序,但是不适用于STC89C51单片机,需要构造外围电路,有大神能告诉我怎样构造外围电路来调方波振幅吗
单片机源程序如下:
- //三角波发生程序
- #include"reg51.h"
- #include <intrins.h>
- #define I2CDelay() {_nop_();_nop_();_nop_();_nop_();}
- sbit I2C_SCL = P3^7;
- sbit I2C_SDA = P3^6;
- sbit p_out=P1^0;
- unsigned char code triwave[]={0,16,32,48,64,80,96,112,
- 128,144,160,176,192,208,
- 224,240,255,240,224,208,
- 192,176,160,144,128,112,
- 96,80,64,48,32,16,};//三角波波表,选取32点
-
- unsigned char *pwave;//波表指针
- unsigned char T1RH = 1;
- unsigned char T1RL = 1;
- void I2CStart() //产生总线起始信号
- {
- I2C_SDA = 1; //首先确保SDA、SCL都是高电平
- I2C_SCL = 1;
- I2CDelay();
- I2C_SDA = 0; //先拉低SDA
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL
- }
- void I2CStop() //产生总线停止信号
- {
- I2C_SCL = 0; //首先确保SDA、SCL都是低电平
- I2C_SDA = 0;
- I2CDelay();
- I2C_SCL = 1; //先拉高SCL
- I2CDelay();
- I2C_SDA = 1; //再拉高SDA
- I2CDelay();
- }
- bit I2CWrite(unsigned char dat) //I2C总线写操作,待写入字节dat,返回值为应答状态
- {
- bit ack; //用于暂存应答位的值
- unsigned char mask; //用于探测字节内某一位值的掩码变量
- for (mask=0x80; mask!=0; mask>>=1) //从高位到低位依次进行
- {
- if ((mask&dat) == 0) //该位的值输出到SDA上
- I2C_SDA = 0;
- else
- I2C_SDA = 1;
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL,完成一个位周期
- }
- I2C_SDA = 1; //8位数据发送完后,主机释放SDA,以检测从机应答
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- ack = I2C_SDA; //读取此时的SDA值,即为从机的应答值
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL完成应答位,并保持住总线
- return (~ack); //应答值取反以符合通常的逻辑:0=不存在或忙或写入失败,1=存在且空闲或写入成功
- }
- unsigned char I2CReadNAK() //I2C总线读操作,并发送非应答信号,返回值为读到的字节
- {
- unsigned char mask;
- unsigned char dat;
- I2C_SDA = 1; //首先确保主机释放SDA
- for (mask=0x80; mask!=0; mask>>=1) //从高位到低位依次进行
- {
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- if(I2C_SDA == 0) //读取SDA的值
- dat &= ~mask; //为0时,dat中对应位清零
- else
- dat |= mask; //为1时,dat中对应位置1
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL,以使从机发送出下一位
- }
- I2C_SDA = 1; //8位数据发送完后,拉高SDA,发送非应答信号
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL完成非应答位,并保持住总线
- return dat;
- }
- unsigned char I2CReadACK() //I2C总线读操作,并发送应答信号,返回值为读到的字节
- {
- unsigned char mask;
- unsigned char dat;
- I2C_SDA = 1; //首先确保主机释放SDA
- for (mask=0x80; mask!=0; mask>>=1) //从高位到低位依次进行
- {
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- if(I2C_SDA == 0) //读取SDA的值
- dat &= ~mask; //为0时,dat中对应位清零
- else
- dat |= mask; //为1时,dat中对应位置1
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL,以使从机发送出下一位
- }
- I2C_SDA = 0; //8位数据发送完后,拉低SDA,发送应答信号
- I2CDelay();
- I2C_SCL = 1; //拉高SCL
- I2CDelay();
- I2C_SCL = 0; //再拉低SCL完成应答位,并保持住总线
- return dat;
- }
- //设置DAC的初始值,val是初始值
- void DA(unsigned char val)
- {
- I2CStart();
- if(!I2CWrite(0x48<<1))//寻址PCF8591,如未应答,停止操作并返回
- {
- I2CStop();
- return;
- }
- I2CWrite(0x40);
- I2CWrite(val);
- I2CStop();
- }
- //输出波形频率
- void setwavefreq(unsigned char freq)
- {
- unsigned long temp;
- temp=(12/12)/(freq*32);//定时器的计数频率,是波形频率的32倍
- temp=65536-temp;//定时器重载
- temp=temp+33;//修断中断响应延时造成的误差
- T1RH=(unsigned char)(temp>>8);
- T1RL=(unsigned char)temp;
- TMOD&=0x0F;
- TMOD|=0x10;
- TH1=T1RH;
- TL1=T1RL;
- ET1=1;
- PT1=1;
- TR1=1;
- }
- //中断T1服务函数
- void timer1() interrupt 3
- {
- static unsigned int i=0;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
三角波.rar
(30.75 KB, 下载次数: 37)
|