PCF8591 DA驅(qū)動(dòng)程序
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define disdata P0 //顯示數(shù)據(jù)碼輸出口
bit flag;
sbit SCL=P1^2; // 將p1.0口模擬時(shí)鐘口
sbit SDA=P1^3; // 將p1.3口模擬數(shù)據(jù)口
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; //啟動(dòng)健
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; //從機(jī)錯(cuò)誤標(biāo)志位
uchar N=0x00,k_count;
/*********************** PCF8591專用變量定義 ***********************/
#definePCF8591_WRITE0x90
#definePCF8591_READ 0x91
#define NUM 4 //接收和發(fā)送緩存區(qū)的深度
uchar idata receivebuf[NUM]; //數(shù)據(jù)接收緩沖區(qū)
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
};
/*******************************************************************/
/* */
/* 延時(shí)函數(shù) */
/* */
/*******************************************************************/
void delayB(uchar x) //x*0.14MS
{
uchar i;
while(x--)
{
for (i=0; i<13; i++)
{ }
}
}
/*******************************************************************/
/* */
/* 延時(shí)函數(shù) */
/* */
/*******************************************************************/
void delay(int ms)
{
while(ms--)
{
uchar i;
for(i=0;i<250;i++)
{
_nop_();
_nop_();
_nop_();
_nop_();
}
}
}
/*******************************************************************/
/* */
/* 蜂鳴器函數(shù) */
/* */
/*******************************************************************/
void beep()
{
uchar j;
for (j=0;j<180;j++)
{
delayB(5);
BEEP=!BEEP; //BEEP取反
}
BEEP=1; //關(guān)閉蜂鳴器
delay(100);
}
/*******************************************************************/
/* */
/*檢查LCD忙狀態(tài) */
/*lcd_busy為1時(shí),忙,等待。lcd-busy為0時(shí),閑,可寫指令與數(shù)據(jù)。 */
/* */
/*******************************************************************/
bit lcd_busy()
{
bit result;
LCD_RS = 0;
LCD_RW = 1;
LCD_EN = 1;
delayNOP();
result = (bit)(P0&0x80);
LCD_EN = 0;
return(result);
}
/*******************************************************************/
/* */
/*寫指令數(shù)據(jù)到LCD */
/*RS=L,RW=L,E=高脈沖,D0-D7=指令碼。 */
/* */
/*******************************************************************/
void lcd_wcmd(uchar cmd)
{
while(lcd_busy());
LCD_RS = 0;
LCD_RW = 0;
LCD_EN = 0;
_nop_();
_nop_();
P0 = cmd;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/*寫顯示數(shù)據(jù)到LCD */
/*RS=H,RW=L,E=高脈沖,D0-D7=數(shù)據(jù)。 */
/* */
/*******************************************************************/
void lcd_wdat(uchar dat)
{
while(lcd_busy());
LCD_RS = 1;
LCD_RW = 0;
LCD_EN = 0;
P0 = dat;
delayNOP();
LCD_EN = 1;
delayNOP();
LCD_EN = 0;
}
/*******************************************************************/
/* */
/* LCD初始化設(shè)定 */
/* */
/*******************************************************************/
void lcd_init()
{
delay(15);
lcd_wcmd(0x38); //16*2顯示,5*7點(diǎn)陣,8位數(shù)據(jù)
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x38);
delay(5);
lcd_wcmd(0x0c); //顯示開,關(guān)光標(biāo)
delay(5);
lcd_wcmd(0x06); //移動(dòng)光標(biāo)
delay(5);
lcd_wcmd(0x01); //清除LCD的顯示內(nèi)容
delay(5);
}
/*******************************************************************/
/* */
/* 設(shè)定顯示位置 */
/* */
/*******************************************************************/
void lcd_pos(uchar pos)
{
lcd_wcmd(pos | 0x80); //數(shù)據(jù)指針=80+地址變量
}
//-------------------------------------------------------------------
// 函數(shù)名稱: iic_start()
// 函數(shù)功能: 啟動(dòng)I2C總線子程序
//-------------------------------------------------------------------
void iic_start(void)
{ //時(shí)鐘保持高,數(shù)據(jù)線從高到低一次跳變,I2C通信開始
SDA = 1;
SCL = 1;
delayNOP(); // 延時(shí)5us
SDA = 0;
delayNOP();
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: iic_stop()
// 函數(shù)功能: 停止I2C總線數(shù)據(jù)傳送子程序
//-------------------------------------------------------------------
void iic_stop(void)
{
SDA = 0; //時(shí)鐘保持高,數(shù)據(jù)線從低到高一次跳變,I2C通信停止
SCL = 1;
delayNOP();
SDA = 1;
delayNOP();
SCL = 0;
}
//------------------------------------------------------------------
// 函數(shù)名稱: iicInit_()
// 函數(shù)功能: 初始化I2C總線子程序
//------------------------------------------------------------------
void iicInit(void)
{
SCL = 0;
iic_stop();
}
//-------------------------------------------------------------------
// 函數(shù)名稱: slave_ACK
// 函數(shù)功能: 從機(jī)發(fā)送應(yīng)答位子程序
//-------------------------------------------------------------------
void slave_ACK(void)
{
SDA = 0;
SCL = 1;
delayNOP();
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: slave_NOACK
// 函數(shù)功能: 從機(jī)發(fā)送非應(yīng)答位子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
//-------------------------------------------------------------------
void slave_NOACK(void)
{
SDA = 1;
SCL = 1;
delayNOP();
SDA = 0;
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: check_ACK
// 函數(shù)功能: 主機(jī)應(yīng)答位檢查子程序,迫使數(shù)據(jù)傳輸過程結(jié)束
//-------------------------------------------------------------------
void check_ACK(void)
{
SDA = 1; // 將p1.1設(shè)置成輸入,必須先向端口寫1
SCL = 1;
flag = 0;
delayNOP();
if(SDA == 1) // 若SDA=1表明非應(yīng)答,置位非應(yīng)答標(biāo)志flag
flag = 1;
SCL = 0;
}
//-------------------------------------------------------------------
// 函數(shù)名稱: IICSendByte
// 入口參數(shù): ch
// 函數(shù)功能: 發(fā)送一個(gè)字節(jié)
//-------------------------------------------------------------------
void IICSendByte(uchar ch)
{
unsigned char idata n=8; // 向SDA上發(fā)送一位數(shù)據(jù)字節(jié),共八位
while(n--)
{
if((ch&0x80) == 0x80) // 若要發(fā)送的數(shù)據(jù)最高位為1則發(fā)送位1
{
SDA = 1; // 傳送位1
SCL = 1;
delayNOP();
//SDA = 0;
SCL = 0;
}
else
{
SDA = 0; // 否則傳送位0
SCL = 1;
delayNOP();
SCL = 0;
}
ch = ch<<1; // 數(shù)據(jù)左移一位
}
}
//-------------------------------------------------------------------
// 函數(shù)名稱: IICreceiveByte
// 返回接收的數(shù)據(jù)
// 函數(shù)功能: 接收一字節(jié)子程序
//-------------------------------------------------------------------
uchar IICreceiveByte(void)
{
uchar idata n=8; // 從SDA線上讀取一上數(shù)據(jù)字節(jié),共八位
uchar tdata=0;
while(n--)
{
SDA = 1;
SCL = 1;
tdata =tdata<<1; //左移一位
if(SDA == 1)
tdata = tdata|0x01; // 若接收到的位為1,則數(shù)據(jù)的最后一位置1
else
tdata = tdata&0xfe; // 否則數(shù)據(jù)的最后一位置0
SCL = 0;
}
return(tdata);
}
//-------------------------------------------------------------------
// 函數(shù)名稱: DAC_PCF8591
// 入口參數(shù): slave_add從機(jī)地址,n要發(fā)送的數(shù)據(jù)個(gè)數(shù)
// 函數(shù)功能: 發(fā)送n位數(shù)據(jù)子程序
//-------------------------------------------------------------------
void DAC_PCF8591(uchar controlbyte)
{
iic_start(); // 啟動(dòng)I2C
delayNOP();
IICSendByte(PCF8591_WRITE); // 發(fā)送地址位
check_ACK(); // 檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; // 若非應(yīng)答,置錯(cuò)誤標(biāo)志位
}
IICSendByte(controlbyte&0x77);//Control byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; // 若非應(yīng)答,置錯(cuò)誤標(biāo)志位
}
}
//-------------------------------------------------------------------
// 函數(shù)名稱: ADC_PCF8591
// 入口參數(shù): controlbyte控制字
// 函數(shù)功能: 連續(xù)讀入4路通道的A/D轉(zhuǎn)換結(jié)果到receivebuf
//-------------------------------------------------------------------
/*
void ADC_PCF8591(uchar controlbyte)
{
uchar idata i; //,receive_da;
iic_start();
IICSendByte(PCF8591_WRITE);//控制字
check_ACK();
if(flag == 1)
{
SystemError = 1;
return;
}
IICSendByte(controlbyte);//控制字
check_ACK();
if(flag == 1)
{
SystemError = 1;
return;
}
iic_start(); //重新發(fā)送開始命令
IICSendByte(PCF8591_READ);//控制字
check_ACK();
if(flag == 1)
{
SystemError = 1;
return;
}
IICreceiveByte(); //空讀一次,調(diào)整讀順序
slave_ACK(); //收到一個(gè)字節(jié)后發(fā)送一個(gè)應(yīng)答位
//while(i<4)
for(i=0;i<4;i++)
{
// receive_da=IICreceiveByte();
// receivebuf[i]=receive_da;
receivebuf[i] = IICreceiveByte();
slave_ACK(); //收到一個(gè)字節(jié)后發(fā)送一個(gè)應(yīng)答位
}
slave_NOACK(); //收到最后一個(gè)字節(jié)后發(fā)送一個(gè)非應(yīng)答位
iic_stop();
}*/
/*********************************************************
輸出三角波
*********************************************************/
void Triangle()
{
uchar l;
for(l=0;l<255;l++)
{
IICSendByte(l); //data byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; //置錯(cuò)誤標(biāo)志位SystemError
}
}
for(l=255;l>1;l--)
{
IICSendByte(l); //data byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; //置錯(cuò)誤標(biāo)志位SystemError
}
}
}
/*********************************************************
輸出方波函數(shù)
*********************************************************/
void Square()
{
IICSendByte(N); //data byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; //置錯(cuò)誤標(biāo)志位SystemError
}
delay(15);
N=~N;
}
/*********************************************************
輸出正弦波函數(shù)
*********************************************************/
void sin()
{
uint i;
for(i=0; i<256; i++)
{
IICSendByte(sin_tab[i]); //data byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; //置錯(cuò)誤標(biāo)志位SystemError
}
}
}
/*********************************************************
輸出鋸齒波函數(shù)
*********************************************************/
void Sawtooth()
{
uchar l;
for(l=0;l<255;l++)
{
IICSendByte(l); //data byte
check_ACK(); //檢查應(yīng)答位
if(flag == 1)
{
SystemError = 1;
return; //置錯(cuò)誤標(biāo)志位SystemError
}
}
}
/*********************************************************
功能健函數(shù)
*********************************************************/
void keyscan()
{
uchar m;
if((K2==0)&(!START)) //啟動(dòng)輸出后,屏蔽K2健
{
k_count++;
beep();
switch (k_count)
{
case 1:
lcd_pos(0x00);
for(m=0;m<16;m++)
lcd_wdat(dis2[m]);
lcd_pos(0x40);
for(m=0;m<16;m++)
lcd_wdat(dis5[m]);
out1=1;
out4=0;
break;
case 2:
lcd_pos(0x40);
for(m=0;m<16;m++)
lcd_wdat(dis6[m]);
out1=0;
out2=1;
break;
case 3:
lcd_pos(0x40);
for(m=0;m<16;m++)
lcd_wdat(dis7[m]);
out2=0;
out3=1;
break;
case 4:
lcd_pos(0x40);
for(m=0;m<16;m++)
lcd_wdat(dis8[m]);
k_count=0;
out3=0;
out4=1;
break;
default:
out4=0; out3=0; out2=0; out1=0;
break;
}
}
if(K3==0) //啟動(dòng)輸出
{
START=1;
beep();
}
if(K4==0) //停止輸出
{
START=0;
beep();
}
}
/*******************************************************************/
/* */
/* 主函數(shù) */
/* */
/*******************************************************************/
main()
{
uchar i;
delay(10); //延時(shí)
lcd_init(); //初始化LCD
lcd_pos(0); //設(shè)置顯示位置為第一行
for(i=0;i<16;i++)
lcd_wdat(dis1[i]); //顯示字符
lcd_pos(0x40); //設(shè)置顯示位置為第二行
for(i=0;i<16;i++)
lcd_wdat(dis2[i]); //顯示字符
delay(1500);
lcd_pos(0); //設(shè)置顯示位置為第一行
for(i=0;i<16;i++)
lcd_wdat(dis3[i]); //顯示字符
lcd_pos(0x40); //設(shè)置顯示位置為第二行
for(i=0;i<16;i++)
lcd_wdat(dis4[i]); //顯示字符
DAC_PCF8591(0x40);
while(1)
{
keyscan();
while(out1&START)
{
sin(); //輸出正弦波
keyscan();
}
while(out2&START)
{
Square(); //輸出方波
keyscan();
}
while(out3&START)
{
Triangle(); //輸出三角波
keyscan();
}
while(out4&START)
{
Sawtooth(); //輸出鋸齒波
keyscan();
}
}
}
//*********************************************************
評(píng)論