|
这个程序就是测试一下单片机的I2C程序,能不能和IO口扩展芯片通讯使用。
各位大老,不知道问题出在哪里,烧进去没反应,接触单片机时间不长,望大老有时间帮忙看看,在这里谢谢了!新年快乐!
单片机源程序如下:
- #include<reg52.h>
- #include "intrins.h"
- #define unit unsigned int
- #define uchar unsigned char
-
- #define I2CCFG (*(unsigned char volatile xdata *)0xfe80)
- #define I2CMSCR (*(unsigned char volatile xdata *)0xfe81)
- #define I2CMSST (*(unsigned char volatile xdata *)0xfe82)
- #define I2CSLCR (*(unsigned char volatile xdata *)0xfe83)
- #define I2CSLST (*(unsigned char volatile xdata *)0xfe84)
- #define I2CSLADR (*(unsigned char volatile xdata *)0xfe85)
- #define I2CTXD (*(unsigned char volatile xdata *)0xfe86)
- #define I2CRXD (*(unsigned char volatile xdata *)0xfe87)
- #define i2cl 0x44 //定义前提命令 -发送低8位IO口数据
- #define i2ch 0x46 //定义前提命令 -发送高8位IO口数据
- unsigned int u=0;v=0;x=0; y=0; m=0;w=0;
- static unsigned char a,b,d,e,f,g,h,i,c,i2clh,i2cdata;
- sfr P0M0 = 0x94;
- sfr P0M1 = 0x93;
- sfr P1M0 = 0x92;
- sfr P1M1 = 0x91;
- sfr P2M0 = 0x96;
- sfr P2M1 = 0x95;
- sfr P3M0 = 0xb2;
- sfr P3M1 = 0xb1;
- sfr P4M0 = 0xb4;
- sfr P4M1 = 0xb3;
- sfr P5M0 = 0xca;
- sfr P5M1 = 0xc9;
- sfr P6M0 = 0xcc;
- sfr P6M1 = 0xcb;
- sfr P7M0 = 0xe2;
- sfr P7M1 = 0xe1;
- sfr P4 = 0xc0;
- sfr P5 = 0xc8;
- sfr P6 = 0xe8;
- sfr P7 = 0xf8;
- sfr P_SW2 = 0xba;
- sbit k1=P7^3; //高/低位数据选择开关
- sbit k2=P2^0; //点动测试开关,数据有变化I2C就发送新的数据到IO扩展IC
- sbit k14=P4^2; //清零开关
- bit busy; //I2C中断标志位
- void timer0Init() //中断时间计数器函数
- {
- TMOD|=0x01;
- TH0=0xFF;
- TL0=0xA3;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void Timer0() interrupt 1 //程序运行依赖-中断函数
- {
- TH0=0xFF;
- TL0=0xA3;
-
- y++;
- u++;
- v++;
- ys++;
-
- if(k2==0||k14==0)
- {x++; }
- }
- void I2C_Isr() interrupt 24 //I2C中断函数-清零
- {
- char store;
- store = P_SW2;
- P_SW2 |= 0x80;
- if (I2CMSST & 0x40)
- {
- I2CMSST &= ~0x40; //清中断标志
- busy = 0;
- }
- P_SW2 = store;
- }
- //_______________________________ I2C各命令子函数
- void Start()
- {
- busy = 1;
- I2CMSCR = 0x81; //发送START命令
- while (busy);
- }
- void SendData(char dat)
- {
- I2CTXD = dat; //写数据到数据缓冲区
- busy = 1;
- I2CMSCR = 0x82; //发送SEND命令
- while (busy);
- }
- void RecvACK()
- {
- busy = 1;
- I2CMSCR = 0x83; //发送读ACK命令
- while (busy);
- }
- void Stop()
- {
- busy = 1;
- I2CMSCR = 0x86; //发送STOP命令
- while (busy);
- }
- //_________________________
- void i2c(char aa,char bb) //数据发送完整流程函数
- {
- Start(); //启动
- SendData(aa); //发数据1 --设置命令
- RecvACK(); //应答1
- SendData(bb); //发数据2 --驱动数据
- RecvACK(); //应答2
- Stop(); //停止
- }
- void cx1()
- {
- e=c;f=d;
-
- if(k1==0) //高8位数据+1
- {if(k2==0&&x>50){while(!k2);c++;x=0;}} //x>50-为开关消抖
- if(k1==1) //低8位数据+1
- {if(k2==0&&x>50){while(!k2);d++;x=0;}}
- if(k14==0&&x>50) //按键清零
- {while(!k14);c=d=e=f=x=0;}
-
- if(e!=c){i2c(i2cl,c);} //高8位数据有变化就发送到IO扩展芯片
-
- if(f!=d){i2c(i2ch,d);} //低8位数据有变化就发送到IO扩展芯片
-
- if(c>8){c=e=0;} //超过8位,清零
-
- if(d>8){d=f=0;} //超过8位,清零
- }
- main()
- {
-
- timer0Init();
-
-
- P_SW2 = 0x90; //使能I2C主机模式-并配置I2C脚位
- I2CCFG = 0xe0; //使能I2C主机模式
- I2CMSST = 0x00;
- i2c(0x48,0x10); //IO扩展芯片配置清零
-
- P0=P1=P6=0x00; P2=P7=0x0F; P3=0xE2; P4=0x06;P5=0x03;
-
- P0M0 = 0x00; //设置P0.0~P0.7为准双向口模式
- P0M1 = 0x00;
- P1M0 = 0x00; //设置P1.0~P1.7为准双向口模式
- P1M1 = 0x00;
- P2M0 = 0x00; //设置P2.0~P2.7为准双向口模式
- P2M1 = 0x00;
- P3M0 = 0x00; //设置P3.0~P3.7为准双向口模式
- P3M1 = 0x00;
- P4M0 = 0x00; //设置P4.0~P4.7为准双向口模式
- P4M1 = 0x00;
- P5M0 = 0x00; //设置P5.0~P5.7为准双向口模式
- P5M1 = 0x00;
- P6M0 = 0x00; //设置P6.0~P6.7为准双向口模式
- P6M1 = 0x00;
- P7M0 = 0x00; //设置P7.0~P7.7为准双向口模式
- P7M1 = 0x00;
- a=0;
- b=0;
- c=0;
- i=0;
- while(1)
- {
- cx1();
- }
- }
复制代码
|
|