重庆科技学院单片机实验课程指导new - 图文

unsigned char Data_Buffer[4]={1,2,3,4}; uchar

5e,0x79,0x71,0x76};

sbit P24=P2^4; sbit P25=P2^5; sbit P26=P2^6; sbit P27=P2^7;

bit flag=0;

bit DACconversion(unsigned char sla,unsigned char c, unsigned char Val); bit ISendByte(unsigned char sla,unsigned char c); unsigned char IRcvByte(unsigned char sla); /******************************/ void main(void) //主程序 {

unsigned int v;

unsigned char AD_CHANNEL=0; unsigned int D=255;

TMOD=0x11; TH0=THCO; TL0=TLCO; TR0=1; ET0=1; EA=1; while(1) {

if(flag==1) {flag=0;

code

Duan[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x

//四个数码管的位码口定义

//设置定时器0工作模式,16位计数模式

//启动定时器

//使能定时器中断

//开总中断

DACconversion(PCF8591,0x40, D); //DAC 数模转换 D=255-D;//输出方波

37/39

}

Data_Buffer[1]=D/100; Data_Buffer[2]=D/10; } }

void timer0() interrupt 1 //定时器中断服务子程序 {

static unsigned int count=0;//软计时变量定义 static unsigned char Bit=0;

TH0=THCO; TL0=TLCO; Bit++;

//先关位码

if(Bit>=4)Bit=0; P2|=0xf0;

P0=Duan[Data_Buffer[Bit]]; //开段码 if(Bit==0)P0|=0x80; switch(Bit) {

case 0: P24=0;break; case 1: P25=0;break; case 2: P26=0;break; case 3: P27=0;break; }

count++;

if(count>=250) {

count=0;

38/39

Data_Buffer[3]=D; }

//静态变量,退出程序后,值保留

//送位码

//半S时间到

flag=1; } }

六、思考题

利用单片机控制PDF8591 DA转换器输出三角波,用示波器观察PCF8591输出的方波。

39/39

联系客服:779662525#qq.com(#替换为@)