硬件I2C控制2线CH452-供参考

//LPC2214 //I2C接口速度在500HZ-200KHZ void CH452_Write(unsigned short command) //写命令 { I2CONCLR = 0x2C; I2CONSET = 0x60; //I2C HOST START while(I2STAT==0XF8); //WAIT START I2DAT=((unsigned char)(command>>7)&0x3E)|0x60; //LOAD ADDR I2CONCLR=0X28; //CLR SI while(I2STAT==0XF8); //WAIT I2DAT=(unsigned char)command; //LOAD DAT I2CONCLR=0X28; //CLR SI while(I2STAT==0XF8); //WAIT I2CONSET|=0X10; //SET STO while(I2STAT==0XF8); }

unsigned char CH452_Read( void ) //读取按键 { unsigned char k; I2CONCLR = 0x2C; I2CONSET = 0x60; //I2C HOST START while(I2STAT==0XF8); //WAIT START I2DAT=0X6F; //IF ADDR=0 I2DAT=0X4F; I2CONCLR=0X28; //CLR SI while(I2STAT==0XF8); //WAIT I2CONCLR = 0x2c; //RECV while(I2STAT==0XF8); //WAIT k=I2DAT; I2CONSET|=0X10; //SET STO while(I2STAT==0XF8); return(k); } void I2C_Init(unsigned long fi2c) { if(fi2c>200000) fi2c = 200000;

PINSEL0 = (PINSEL0&0xFFFFFF0F) | 0x50; I2SCLH = (Fpclk/fi2c + 1) / 2; I2SCLL = (Fpclk/fi2c) / 2; I2CONCLR = 0x2C; I2CONSET = 0x40; }

这个是硬件SPI操作452的例子程序: void spi_init(){ DDRB |= 0x2c;//SCK-OUT,MISO-IN,MOSI-OUT,SS-OUT DDRD &=0XFB;//PD2-IN SPCR = 0x73;//SPE=1,DORD=1(LSB FIRSET),MSTR=1,CPOL=CPOH=0,SPR0=SPR1=1 } void spi_send(unsigned short cData) {// unsigned char c; PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 mDelay1uS(); mDelay1uS(); cData =cData<<4; SPDR = cData&0xff; /* 等待传输结束 */ while(!(SPSR & (1< mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); // c=(unsigned char)(cData>>8); SPDR = (unsigned char)(cData>>8);; /* 等待传输结束 */ while(!(SPSR & (1< mDelay1uS(); mDelay1uS(); PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); // CH452_Write(0XA00|0X03); }

unsigned char spi_rec(void ) { unsigned char key,c; DDRB |= 0x2c;//SCK-OUT,MISO-IN,MOSI-OUT,SS-OUT SPCR = 0x53;//SPE=1,DORD=0(MSB FIRSET),MSTR=1,CPOL=CPOH=0,SPR0=SPR1=1 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 PORTB &=0xfb;//cs=0 mDelay1uS(); mDelay1uS(); SPDR = 0x0E; /* 等待传输结束 */ while(!(SPSR & (1< mDelay1uS(); mDelay1uS(); // c=SPDR; // CH452_Write(0x0a00|c&0x0f); // CH452_Write(0x0900|(c&0xf0>>4)); PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 PORTB |=0x04;//cs=1 mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); CH452_Write(0x0800|0X03); mDelay1uS(); mDelay1uS(); SPDR = 0x00;//接收数据 /* 等待传输结束 */ while(!(SPSR & (1< mDelay1uS(); mDelay1uS(); key=SPDR; // CH452_Write(0x0a00|key&0x0f); // CH452_Write(0x0900|(key&0xf0>>4)); key=key>>1; DDRB |= 0x2c;//SCK-OUT,MISO-IN,MOSI-OUT,SS-OUT SPCR = 0x73;//SPE=1,DORD=1(LSB FIRSET),MSTR=1,CPOL=CPOH=0,SPR0=SPR1=1 mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); mDelay1uS(); return key; }


为什么要这样对数据进行变换 cData =cData<<4; SPDR = cData&0xff;

SPDR = (unsigned char)(cData>>8);; ch452传输都是12位的,这样变换后不是相当于只传输了低四位和高四位吗???


只有登录才能回复,可以选择微信账号登录