分享源代码,共同提高
本次试用活动过程还是写了不少的代码,在本帖子里面将其开源,共享。
液晶屏仅为数据显示,而数据的控制与采集全部来自LPC1768开发板。
以下是与INA219电流采样芯片IIC通讯的源代码,做为示例供大家参考:
int8_t openI2C(tI2CMasterClass *pDev, tI2CMasterMode mode)
{
switch(pDev->busNum)
{
case 0:
{
if(pDev->state == busInit)
{
LPC_SC->PCONP |= (1 << 7); /* shutdown I2C0 */
LPC_SC->PCONP |= (1 << 15);
LPC_PINCON->PINSEL1 &= ~(0x03 << 22); /*PINSEL1 bit23、22 01 SDA0*/
LPC_PINCON->PINSEL1 |= (0x01 << 22);
LPC_PINCON->PINMODE1 &= ~(0x03 << 22);
LPC_PINCON->PINMODE1 |= (0x01 << 22);
LPC_PINCON->PINMODE_OD0 |= (0x01 << 27);
LPC_PINCON->PINSEL1 &= ~(0x03 << 24); /*PINSEL1 bit25、24 01 SCL0*/
LPC_PINCON->PINSEL1 |= (0x01 << 24);
LPC_PINCON->PINMODE1 &= ~(0x03 << 24);
LPC_PINCON->PINMODE1 |= (0x01 << 24);
LPC_PINCON->PINMODE_OD0 |= (0x01 << 28);
LPC_PINCON->I2CPADCFG = 0x00;
/*--- Reset registers ---*/
LPC_I2C0->I2SCLL = (uint32_t)i2cBusRate[pDev->busSpeed].i2scllData;
LPC_I2C0->I2SCLH = (uint32_t)i2cBusRate[pDev->busSpeed].i2sclhData;
NVIC_EnableIRQ(I2C0_IRQn);
LPC_I2C0->I2CONSET = (0x01 << BIT_I2EN);
pDev->state = busIdle;
}
if(pDev->state != busIdle)
{
return (-2);
}
break;
}
default:
{
/* bus number is illegal */
return (-1);
}
}
pDev->busMode = mode;
return 0;
}
int8_t closeI2C(tI2CMasterClass *pDev)
{
if(pDev->state == busComplete)
{
pDev->state = busIdle;
return 0;
}
return (-1);
}
/**
* @brief
* @param
* @retval -1 = not idle;
* @date 2019-03-09 20:22
* @note first write regAddr *wBufPtr and then read *rdBufPtr rdLen bytes
*/
int8_t wrReadI2c(tI2CMasterClass *pDev, uint8_t *rdBufPtr, uint16_t rdLen, uint8_t *wBufPtr, uint16_t wLen)
{
if(pDev->state != busIdle)
{
return (-1);
}
pDev->pRcvBuf = rdBufPtr;
pDev->rcvBufLength = rdLen;
pDev->pSendBuf = wBufPtr;
pDev->sendBufLength = wLen;
pDev->busMode = busWR;
pDev->state = busWait;
LPC_I2C0->I2CONCLR = (1 << BIT_AA) | (1 << BIT_SI) | (1 << BIT_STA);
LPC_I2C0->I2CONSET = (1 << BIT_STA);
return 0;
}
int8_t writeI2c(tI2CMasterClass *pDev, uint8_t *wBufPtr, uint16_t wLen)
{
if(pDev->state != busIdle)
{
return (-1);
}
pDev->pSendBuf = wBufPtr;
pDev->sendBufLength = wLen;
pDev->busMode = busWrite;
pDev->state = busWait;
LPC_I2C0->I2CONCLR = (1 << BIT_AA) | (1 << BIT_SI) | (1 << BIT_STA);
LPC_I2C0->I2CONSET = (1 << BIT_STA);
return 0;
}
/**
* @brief
* @param
* @retval
* @date 2019-03-09 11:30
* @note
*/
void I2C0_IRQHandler(void)
{
static char enterCnt = 0;
uint8_t statVal;
statVal = LPC_I2C0->I2STAT & 0xf8;
switch(statVal)
{
case 0x08: /* STATUS_SENDSTART */
{
gI2cBus[0].state = busBusy;
switch(gI2cBus[0].busMode)
{
case busRead:
{
LPC_I2C0->I2DAT = gI2cBus[0].salveAddr | 0x01;
break;
}
case busWrite:
case busWR:
{
LPC_I2C0->I2DAT = gI2cBus[0].salveAddr & 0xFE;
break;
}
}
LPC_I2C0->I2CONSET = (1 << BIT_AA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI) | (1 << BIT_STA);
break;
}
case 0x10: /* STATUS_REPEATSTART */
{
LPC_I2C0->I2DAT = gI2cBus[0].salveAddr | 0x01;
LPC_I2C0->I2CONSET = (1 << BIT_AA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI) | (1 << BIT_STA);
break;
}
case 0x20:
{
LPC_I2C0->I2CONSET = (1 << BIT_AA) | (1 << BIT_STO);
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
break;
}
case 0x18:
case 0x28:
{
switch(gI2cBus[0].busMode)
{
case busWrite:
case busWR:
{
if(gI2cBus[0].sendBufLength > 0)
{
LPC_I2C0->I2DAT = *(gI2cBus[0].pSendBuf);
gI2cBus[0].pSendBuf++;
gI2cBus[0].sendBufLength--;
LPC_I2C0->I2CONSET = (1 << BIT_AA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
}
else
{
if(gI2cBus[0].busMode == busWR)
{
LPC_I2C0->I2CONSET = (1 << BIT_STA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI) | (1 << BIT_AA);
}
else
{
LPC_I2C0->I2CONSET = (1 << BIT_STO) | (1 << BIT_AA); /* send stop flag */
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
gI2cBus[0].state = busComplete;
}
}
break;
}
case busRead:
{
Debug_Info("loop here!\r\n", statVal);
break;
}
default:
{
Debug_Info("default here!\r\n", statVal);
break;
}
}
break;
}
case 0x40:
{
if(gI2cBus[0].rcvBufLength <= 1)
{
LPC_I2C0->I2CONCLR = (1 << BIT_AA) | (1 << BIT_SI);
}
else
{
LPC_I2C0->I2CONSET = (1 << BIT_AA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
}
break;
}
case 0x48:
{
LPC_I2C0->I2CONSET = (1 << BIT_STO) | (1 << BIT_AA); /* send stop flag */
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
gI2cBus[0].state = busError;
break;
}
case 0x50:
{
*gI2cBus[0].pRcvBuf++ = LPC_I2C0->I2DAT;
gI2cBus[0].rcvBufLength--;
if(gI2cBus[0].rcvBufLength == 1)
{
LPC_I2C0->I2CONCLR = (1 << BIT_AA) | (1 << BIT_SI);
}
else
{
LPC_I2C0->I2CONSET = (1 << BIT_AA);
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
}
break;
}
case 0x58:
{
*gI2cBus[0].pRcvBuf++ = LPC_I2C0->I2DAT;
gI2cBus[0].rcvBufLength--;
LPC_I2C0->I2CONSET = (1 << BIT_STO) | (1 << BIT_AA); /* send stop flag */
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
gI2cBus[0].state = busComplete;
break;
}
default:
{
LPC_I2C0->I2CONSET = (1 << BIT_STO) | (1 << BIT_AA); /* send stop flag */
LPC_I2C0->I2CONCLR = (1 << BIT_SI);
break;
}
}
}
亲爱的网友们,具体的工程文件请参见我的代码仓库
https://gitee.com/jobszheng5/learn1768