|
這個程序就是測試一下單片機(jī)的I2C程序,能不能和IO口擴(kuò)展芯片通訊使用。
各位大老,不知道問題出在哪里,燒進(jìn)去沒反應(yīng),接觸單片機(jī)時間不長,望大老有時間幫忙看看,在這里謝謝了!新年快樂!
單片機(jī)源程序如下:
- #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 //定義前提命令 -發(fā)送低8位IO口數(shù)據(jù)
- #define i2ch 0x46 //定義前提命令 -發(fā)送高8位IO口數(shù)據(jù)
- 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; //高/低位數(shù)據(jù)選擇開關(guān)
- sbit k2=P2^0; //點(diǎn)動測試開關(guān),數(shù)據(jù)有變化I2C就發(fā)送新的數(shù)據(jù)到IO擴(kuò)展IC
- sbit k14=P4^2; //清零開關(guān)
- bit busy; //I2C中斷標(biāo)志位
- void timer0Init() //中斷時間計(jì)數(shù)器函數(shù)
- {
- TMOD|=0x01;
- TH0=0xFF;
- TL0=0xA3;
- ET0=1;
- EA=1;
- TR0=1;
- }
- void Timer0() interrupt 1 //程序運(yùn)行依賴-中斷函數(shù)
- {
- TH0=0xFF;
- TL0=0xA3;
-
- y++;
- u++;
- v++;
- ys++;
-
- if(k2==0||k14==0)
- {x++; }
- }
- void I2C_Isr() interrupt 24 //I2C中斷函數(shù)-清零
- {
- char store;
- store = P_SW2;
- P_SW2 |= 0x80;
- if (I2CMSST & 0x40)
- {
- I2CMSST &= ~0x40; //清中斷標(biāo)志
- busy = 0;
- }
- P_SW2 = store;
- }
- //_______________________________ I2C各命令子函數(shù)
- void Start()
- {
- busy = 1;
- I2CMSCR = 0x81; //發(fā)送START命令
- while (busy);
- }
- void SendData(char dat)
- {
- I2CTXD = dat; //寫數(shù)據(jù)到數(shù)據(jù)緩沖區(qū)
- busy = 1;
- I2CMSCR = 0x82; //發(fā)送SEND命令
- while (busy);
- }
- void RecvACK()
- {
- busy = 1;
- I2CMSCR = 0x83; //發(fā)送讀ACK命令
- while (busy);
- }
- void Stop()
- {
- busy = 1;
- I2CMSCR = 0x86; //發(fā)送STOP命令
- while (busy);
- }
- //_________________________
- void i2c(char aa,char bb) //數(shù)據(jù)發(fā)送完整流程函數(shù)
- {
- Start(); //啟動
- SendData(aa); //發(fā)數(shù)據(jù)1 --設(shè)置命令
- RecvACK(); //應(yīng)答1
- SendData(bb); //發(fā)數(shù)據(jù)2 --驅(qū)動數(shù)據(jù)
- RecvACK(); //應(yīng)答2
- Stop(); //停止
- }
- void cx1()
- {
- e=c;f=d;
-
- if(k1==0) //高8位數(shù)據(jù)+1
- {if(k2==0&&x>50){while(!k2);c++;x=0;}} //x>50-為開關(guān)消抖
- if(k1==1) //低8位數(shù)據(jù)+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位數(shù)據(jù)有變化就發(fā)送到IO擴(kuò)展芯片
-
- if(f!=d){i2c(i2ch,d);} //低8位數(shù)據(jù)有變化就發(fā)送到IO擴(kuò)展芯片
-
- if(c>8){c=e=0;} //超過8位,清零
-
- if(d>8){d=f=0;} //超過8位,清零
- }
- main()
- {
-
- timer0Init();
-
-
- P_SW2 = 0x90; //使能I2C主機(jī)模式-并配置I2C腳位
- I2CCFG = 0xe0; //使能I2C主機(jī)模式
- I2CMSST = 0x00;
- i2c(0x48,0x10); //IO擴(kuò)展芯片配置清零
-
- P0=P1=P6=0x00; P2=P7=0x0F; P3=0xE2; P4=0x06;P5=0x03;
-
- P0M0 = 0x00; //設(shè)置P0.0~P0.7為準(zhǔn)雙向口模式
- P0M1 = 0x00;
- P1M0 = 0x00; //設(shè)置P1.0~P1.7為準(zhǔn)雙向口模式
- P1M1 = 0x00;
- P2M0 = 0x00; //設(shè)置P2.0~P2.7為準(zhǔn)雙向口模式
- P2M1 = 0x00;
- P3M0 = 0x00; //設(shè)置P3.0~P3.7為準(zhǔn)雙向口模式
- P3M1 = 0x00;
- P4M0 = 0x00; //設(shè)置P4.0~P4.7為準(zhǔn)雙向口模式
- P4M1 = 0x00;
- P5M0 = 0x00; //設(shè)置P5.0~P5.7為準(zhǔn)雙向口模式
- P5M1 = 0x00;
- P6M0 = 0x00; //設(shè)置P6.0~P6.7為準(zhǔn)雙向口模式
- P6M1 = 0x00;
- P7M0 = 0x00; //設(shè)置P7.0~P7.7為準(zhǔn)雙向口模式
- P7M1 = 0x00;
- a=0;
- b=0;
- c=0;
- i=0;
- while(1)
- {
- cx1();
- }
- }
復(fù)制代碼
|
|