微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > PCF8591 DA驱动程序

PCF8591 DA驱动程序

时间:11-24 来源:互联网 点击:

#include
#include
#include

#define uchar unsigned char
#define uint unsigned int
#define disdata P0 //显示数据码输出口
bit flag;
sbit SCL=P1^2; // 将p1.0口模拟时钟口
sbit SDA=P1^3; // 将p1.3口模拟数据口

sbit LCD_RS = P2^0;
sbit LCD_RW = P2^1;
sbit LCD_EN = P2^2;

sbit K1 = P1^4;
sbit K2 = P1^5; //选择健
sbit K3 = P1^6; //启动健
sbit K4 = P1^7; //停止健
sbit BEEP = P3^6; //蜂鸣器

uchar code dis1[] = {" PCF-8591 "};
uchar code dis2[] = {" DA CONVERTER "};

uchar code dis3[] = {" K2: D/A CHOOSE "};
uchar code dis4[] = {"K3:ENTER K4:EXIT"};

uchar code dis5[] = {" SINE WAVE "};
uchar code dis6[] = {" SQUARE WAVE "};
uchar code dis7[] = {" TRIANGLE WAVE "};
uchar code dis8[] = {" SAWTOOTH WAVE "};

bit out1=0,out2=0,out3=0,out4=0,START=0;

#define delayNOP(); {_nop_();_nop_();_nop_();_nop_();};

bit bdata SystemError; //从机错误标志位

uchar N=0x00,k_count;
/*********************** PCF8591专用变量定义 ***********************/

#definePCF8591_WRITE0x90
#definePCF8591_READ 0x91
#define NUM 4 //接收和发送缓存区的深度
uchar idata receivebuf[NUM]; //数据接收缓冲区

uchar code sin_tab[] = //正弦波输出表
{
0x80,0x83,0x86,0x89,0x8D,0x90,0x93,0x96,0x99,0x9C,0x9F,0xA2,0xA5,0xA8,0xAB,0xAE,
0xB1,0xB4,0xB7,0xBA,0xBC,0xBF,0xC2,0xC5,0xC7,0xCA,0xCC,0xCF,0xD1,0xD4,0xD6,0xD8,
0xDA,0xDD,0xDF,0xE1,0xE3,0xE5,0xE7,0xE9,0xEA,0xEC,0xEE,0xEF,0xF1,0xF2,0xF4,0xF5,
0xF6,0xF7,0xF8,0xF9,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xF9,0xF8,0xF7,0xF6,
0xF5,0xF4,0xF2,0xF1,0xEF,0xEE,0xEC,0xEA,0xE9,0xE7,0xE5,0xE3,0xE1,0xDF,0xDD,0xDA,
0xD8,0xD6,0xD4,0xD1,0xCF,0xCC,0xCA,0xC7,0xC5,0xC2,0xBF,0xBC,0xBA,0xB7,0xB4,0xB1,
0xAE,0xAB,0xA8,0xA5,0xA2,0x9F,0x9C,0x99,0x96,0x93,0x90,0x8D,0x89,0x86,0x83,0x80,
0x80,0x7C,0x79,0x76,0x72,0x6F,0x6C,0x69,0x66,0x63,0x60,0x5D,0x5A,0x57,0x55,0x51,
0x4E,0x4C,0x48,0x45,0x43,0x40,0x3D,0x3A,0x38,0x35,0x33,0x30,0x2E,0x2B,0x29,0x27,
0x25,0x22,0x20,0x1E,0x1C,0x1A,0x18,0x16,0x15,0x13,0x11,0x10,0x0E,0x0D,0x0B,0x0A,
0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,
0x0A,0x0B,0x0D,0x0E,0x10,0x11,0x13,0x15,0x16,0x18,0x1A,0x1C,0x1E,0x20,0x22,0x25,
0x27,0x29,0x2B,0x2E,0x30,0x33,0x35,0x38,0x3A,0x3D,0x40,0x43,0x45,0x48,0x4C,0x4E,
0x51,0x55,0x57,0x5A,0x5D,0x60,0x63,0x66,0x69,0x6C,0x6F,0x72,0x76,0x79,0x7C,0x7E
};

/*******************************************************************/
/* */
/* 延时函数 */
/* */
/*******************************************************************/
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}

/*******************************************************************/
/* */
/* 延时函数 */
/* */
/*******************************************************************/
void delay(int ms)
{
while(ms--)
{
uchar i;
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}

/*******************************************************************/
/* */
/* 蜂鸣器函数 */
/* */
/*******************************************************************/
void beep()
{
uchar j;
for (j=0;j<180;j++)
{
delayB(5);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //关闭蜂鸣器
delay(100);
}

/*******************************************************************/
/* */
/*检查LCD忙状态 */
/*lcd_busy为1时,忙,等待。lcd-busy为0时,闲,可写指令与数据。 */
/* */
/*******************************************************************/

bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}

/*******************************************************************/
/* */
/*写指令数据到LCD */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。 */
/* */
/*******************************************************************/

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top