Cortex-M3 (NXP LPC1788)之IIC應(yīng)用--PCA9532進(jìn)行IO擴(kuò)展和LED亮度控制
做為從設(shè)備,他的8位地址的高四位固定為1100,最低位為數(shù)據(jù)的方向位,剩下的3位有硬件連線確定他的地址。PCA9532共有10個寄存器來配置他的輸出狀態(tài)。
本文引用地址:http://cafeforensic.com/article/201611/318456.htm其中INPUT0 INPUT1在管腳配置成普通IO時候用于讀入IO腳的狀態(tài)。PSC0 PWM0 PSC1 PWM1用于設(shè)置兩路PWM波的周期和占空比。LS0~LS3用于選擇每個管腳的功能,包括通用LED OFF、LED ON、 PWM0、 PWM1。
知道了需要配置的寄存器,那怎么通過I2C通信來配置這幾個寄存器呢?當(dāng)LPC1788發(fā)出PCA9532的地址得到應(yīng)答后,需要發(fā)送一個字節(jié)的數(shù)據(jù)用于配置控制寄存器,他們第四位為B3~B0位,比如發(fā)送的字節(jié)第4位為0,即B3~B0為0則他接下去收到的數(shù)據(jù)用來配置INPUT0。配置寄存器的第4位為AI,即autoincrease,表示接收到一個字節(jié)的配置數(shù)據(jù)后,是否自動的將B3~B0加1,方便配置下一個表中的寄存器。
開發(fā)板上的PCA9532的電路圖如下
程序中配置LED0~7為GPIO用于檢測按鍵,LED8~LED11配置成PWM輸出,將LED RED做出漸亮漸暗的效果,LED12~LED15根據(jù)按鍵值設(shè)置成LED ON 或LED OFF。按鍵值讀取PCA9532的INPUT0得到。程序如下
- #definePCLK60000000
- #defineI2C0SCK100000
- #definePCA9532_ADDRESS0x60
- #definerI2C0CONSET(*(volatileunsigned*)(0x4001C000))
- #definerI2C0CONCLR(*(volatileunsigned*)(0x4001C018))
- #definerI2C0STAT(*(volatileunsigned*)(0x4001C004))
- #definerI2C0DAT(*(volatileunsigned*)(0x4001C008))
- #definerI2C0SCLH(*(volatileunsigned*)(0x4001C010))
- #definerI2C0SCLL(*(volatileunsigned*)(0x4001C014))
- #definerIOCON_P0_27(*(volatileunsigned*)(0x4002C06C))
- #definerIOCON_P0_28(*(volatileunsigned*)(0x4002C070))
- #definerPCONP(*(volatileunsigned*)(0x400FC0C4))
- unsignedcharconfig[11],read_data[1];
- voidI2C0_Init()
- {
- rIOCON_P0_27=(rIOCON_P0_27&(~0x7))|0x1;//I2C0_SDA
- rIOCON_P0_28=(rIOCON_P0_28&(~0x7))|0x1;//I2C0_SCL
- rPCONP|=0x1<<7;//I2C0PowerEnable
- rI2C0SCLH=PCLK/I2C0SCK/2;//setI2C0frequency100khz
- rI2C0SCLL=PCLK/I2C0SCK/2;
- rI2C0CONSET|=0x1<<6;//I2C接口使能
- rI2C0CONCLR=0x1<<3|0x1<<5;//清除SISTA
- }
- unsignedcharI2C0_Start()
- {
- rI2C0CONCLR=0x1<<3;//清除SI標(biāo)志
- rI2C0CONSET|=0x1<<5;//置位STA進(jìn)入主發(fā)送模式
- while(!(rI2C0CONSET&(0x1<<3)));//起始條件發(fā)送完成
- rI2C0CONCLR=0x1<<5;//清除STA標(biāo)志
- return(rI2C0STAT&0xF8);
- }
- voidI2C0_Stop()
- {
- rI2C0CONCLR=0x1<<5;//清除STA標(biāo)志
- rI2C0CONSET|=0x1<<4;//發(fā)送STO標(biāo)志
- rI2C0CONCLR=0x1<<3;//清除SI標(biāo)志
- }
- unsignedcharI2C0_SentByte(unsignedchardata)
- {
- rI2C0DAT=data;
- rI2C0CONCLR=0x1<<3;//清除SI標(biāo)志
- while(!(rI2C0CONSET&(0x1<<3)));//發(fā)送完數(shù)據(jù)得到了應(yīng)答
- return(rI2C0STAT&0xF8);
- }
- unsignedcharI2C0_GetByte(unsignedchar*data,unsignedcharack_flag)
- {
- if(ack_flag)
- {
- rI2C0CONSET|=0x1<<2;//主接收模式,接收到一個字節(jié)返回應(yīng)答
- }
- else
- {
- rI2C0CONCLR=0x1<<2;//主接收模式,接收最后一個字節(jié)時,不返回應(yīng)答
- }
- rI2C0CONCLR=0x1<<3;//清除SI標(biāo)志
- while(!(rI2C0CONSET&(0x1<<3)));//發(fā)送完數(shù)據(jù)得到了應(yīng)答
- *data=(unsignedchar)rI2C0DAT;
- return(rI2C0STAT&0xF8);
- }
- intI2C0_MasterTransfer(unsignedcharslave_address,unsignedchar*transfer_data,unsignedinttransfer_count,
- unsignedchar*receive_data,unsignedintreceive_count)
- {
- unsignedcharstatus;
- unsignedinti;
- status=I2C0_Start();
- while(status!=0x08);
- status=I2C0_SentByte(slave_address<<1);
- while(status!=0x18);
- for(i=0;i
- {
- status=I2C0_SentByte(*(transfer_data+i));
- while(status!=0x28);
- }
- if(receive_data!=(void*)0&&receive_count!=0)
- {
- //進(jìn)入主接收模式
- status=I2C0_Start();
- while(status!=0x10);
- status=I2C0_SentByte((slave_address<<1)|0x1);
- while(status!=0x40);
- for(i=0;i
- {
- if(i
- {
- status=I2C0_GetByte(receive_data,1);
- while(status!=0x50);
- }
- else
- {
- status=I2C0_GetByte(receive_data,0);
- while(status!=0x58);
- }
- receive_data++;
- }
- }
- I2C0_Stop();
- return1;
- }
- voidPCA9532_Config()
- {
- config[0]=0x1<<4;//讀寫控制寄存器后低四位自動增加
- config[1]=0;//input0
- config[2]=0;//input1
- config[3]=0;//PSC0PWM0的周期6.5ms
- config[4]=0;//PWM0PWM0占空比設(shè)置成0%
- config[5]=0;//PSC1PWM1的周期為6.5ms
- config[6]=0;//PWM1PWM1占空比設(shè)置成0%
- config[7]=0;//LS0
- config[8]=0;//LS1LED0~7設(shè)置成GPIOS
- config[9]=0xFA;//LS211111010,LED8,9->blinksPWM0;LED10,11->blinksPWM1
- config[10]=0;//LS3LED12~LED15,LEDoff
- }
- intmain(void)
- {
- unsignedcharflag=1,data=0;
- unsignedinti;
- I2C0_Init();
- PCA9532_Config();
- while(1)
- {
- I2C0_MasterTransfer(PCA9532_ADDRESS,config,sizeof(config),0,0);
- I2C0_MasterTransfer(PCA9532_ADDRESS,&data,1,read_data,1);
- if(flag)
- {
- config[4]++;
- config[6]++;
- }
- else
- {
- config[4]--;
- config[6]--;
- }
- if(config[4]==255||config[4]==0)
- {
- flag=!flag;
- }
- for(i=0;i<4;i++)
- {
- if(read_data[0]&(0x1<
- {
- config[10]&=~(0x3<<(i*2));
- }
- else
- {
- config[10]|=(0x1<<(i*2));
- }
- }
- }
- }
程序調(diào)試過程中遇到如下問題,要注意:
1,I2C控制清除寄存器為只讀,因此不能進(jìn)行|=操作,否則狀態(tài)寄存器的值異常。不知道為什么不是產(chǎn)生異常復(fù)位,之前EEPROM也對只讀寄存器進(jìn)行該操作會產(chǎn)生系統(tǒng)異常進(jìn)入異常中斷。
2,在主發(fā)送模式切換到主接收模式的過程中,一定要先清除SI標(biāo)志。開始沒注意,發(fā)送重復(fù)起始表示后的狀態(tài)一直是0x28,把這個重復(fù)起始標(biāo)志單數(shù)據(jù)發(fā)送?
評論