//0832各种波型输出演示 //P1.1P1.0 = 00时 正弦波 //P1.1P1.0 = 01时 锯齿波 //P1.1P1.0 = 10时 方波 //P1.1P1.0 = 11时 三角波 //需要连接的导线://(1)译码器Y0接DAC0832的CS //(2)P3.6即XWR插孔接DAC0832的WR //(3)8芯的排线连接8排针的P0口和DAC0832的D7-D0 //(4)串并转换区的+5v接DAC0832的参考电压Vref插孔#include <reg51.h>#include <ABSACC.H>//absacc.h是C51中绝对地址访问函数的头文件 #define daport XBYTE[0x8000]//将局唯daport定义为耐饥外部I/O口,地址8000H,则74ls138的Y0有效 sbit P1_1=P1^1sbit P1_0=P1^0 unsigned char i,j,k bit updown //三角波的上升/下降 unsigned char code sinn[64]={ 198,204,210,216,222,228,233,237,242,245,249,251,253,255,255,255, 255,254,252,250,247,243,239,235,230,224,219,213,207,201,194,188, 181,175,169,163,158,152,147,143,139,136,133,131,129,128,128,128, 129,130,132,135,138,142,146,151,156,162,168,174,180,186,193,199 }//正弦的数值在128-255之间,所以输出的正弦幅值在0-5v void main(void) { updown=0 i=128 j=0 while(1) {if (P1_1==1){ if (P1_0==0) //输出占空比为25%的方波 {if (j<=40) i=255//i=255时输出的电压为+5velse if(j<=160) i=0/昌腊返/i=0时输出的电压为-5v else j=0 } else //输出-5v—+5v的三角波 { if (updown==0) //上升 { if (i==255) updown=1 else i=i+5//i++} else //下降 { if (i==0) updown=0//if(i==128) updown=0i=128输出的电压为0 else i=i-5//i--}} } else{ if (P1_0==0) //正弦波 { i=sinn[j] if (j>=63) j=0 } else //锯齿波 { if (i==255) i=128 else i++ } } daport=i for (k=0k<2k++) j++ }}
//有C语言的。#include"common.h"
#include"intrins.h"
#define FAILURE 0
#define SUCCESS 1
#define READ_PCF8591 0x91 //AD器件PCF8591读地址
#define WRITE_PCF8591 0x90 //AD器件PCF8591写地址
unsigned char AD_Control=0x00 //选择AD
unsigned int buffer[3]
//#define Vref 500
//unsigned int buffer[3]
sbit SDA=P2^1
sbit SCL=P2^0
//extern unsigned char buffer[3]
void I2CStart(void)
{
SDA = 1
SCL = 1
_nop_()
_nop_()
_nop_()
_nop_()
SDA = 0
_nop_()
_nop_()
_nop_()
SCL = 0
}
void I2CStop(void)
{
SCL = 0
SDA = 0
SCL = 1
_nop_()
SDA = 1
}
bit I2CWaitAck(void)
{
unsigned char cErrTime = 255
SDA = 1
_nop_()
_nop_()
_nop_()
SCL = 1
while(SDA)
{
cErrTime--
_nop_()
_nop_()
if (0 == cErrTime)
{
I2CStop()
return FAILURE
SDA=1
}
}
SCL = 0
return SUCCESS
}
void I2CSendAck(void)
{
SDA = 0
_nop_()
SCL = 1
_nop_()
SCL = 0
}
void I2CSendNotAck(void)
{
SDA = 1
_nop_()
SCL = 1
_nop_()
SCL = 0
}
void I2CSendByte(unsigned char cSendByte)
{
char i = 8
while (i--)
{
SCL = 0
_nop_()
_nop_()
_nop_()
_nop_()
ACC=cSendByte
ACC <<=1
SDA=CY
cSendByte=ACC
_nop_()
SCL = 1
_nop_()
_nop_()
_nop_()
_nop_()
}
SCL = 0//退出时,其SCL为低
}
unsigned char I2CReceiveByte(void)
{
unsigned char data i = 8
unsigned char data cR_Byte = 0
SDA = 1
while (i--)
{
cR_Byte += cR_Byte
SCL = 0
_nop_()/顷胡/0xff是一个比较特殊的数据
_nop_()//如喊乎扮果没有这些_nop_()来放慢读取速度,就会发生读取0xff之后的郑灶数据的读取错误
_nop_()
_nop_()
_nop_()
SCL = 1
_nop_()//如果没有这些_nop_()来放慢读取速度,就会发生读取0xff之后的数据的读取错误
_nop_()
_nop_()
_nop_()
_nop_()
cR_Byte |= (unsigned char)SDA
}
SCL = 0
return cR_Byte
}
unsigned char Send_PCF8591(unsigned char channel)
{
//unsigned char i
unsigned char cCheckSum = 0
I2CStart()
I2CSendByte(WRITE_PCF8591)
if (I2CWaitAck() == SUCCESS)
{
I2CSendByte((AD_Control|channel))
if (I2CWaitAck() != SUCCESS)
{
return FAILURE
}
}
I2CStop()
return SUCCESS
}
unsigned char Recieve_PCF8591(void)
{
unsigned char cCnt
unsigned char i
unsigned char cCheckSum = 0
for (cCnt=0cCnt<0xFFcCnt++)
{
_nop_()
I2CStart()
I2CSendByte(READ_PCF8591)
if (I2CWaitAck() == SUCCESS) break
}
if (0xFF == cCnt) return FAILURE
/*********读三次取平均值****************/
i=3
for (cCnt=0cCnt<icCnt++)
{
buffer[cCnt] = I2CReceiveByte()
I2CSendAck()
_nop_()
_nop_()
_nop_()
_nop_()
_nop_()
}
//i=I2CReceiveByte()
//I2CSendNotAck()
I2CStop()
//i=buffer[1]
i = (unsigned char)((buffer[0]+buffer[1]+buffer[2])/3)
return (i)
}
/*****************AD采样*****************************/
//channel为选择AD通道
unsigned char AD_Sample(unsigned char channel)
{
unsigned char temp
Send_PCF8591(channel)
temp=Recieve_PCF8591()
return (temp)
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)