IIC从机
-
能否提供一个IIC作为从机的代码
-
void iic_init(enum iic_channel_t channel, uint16_t speed, uint16_t slave_addr)
{
volatile struct iic_reg_t *iic_reg;if(channel == IIC_CHANNEL_0) { iic_reg = IIC0_REG_BASE; } else { iic_reg = IIC1_REG_BASE; } iic_reg->clkdiv.clk_div = (system_get_pclk_config()*1000/speed-10)/2; iic_reg->control.soft_reset = 1; iic_reg->control.seven_bit = 1; iic_reg->address.slv_addr = slave_addr; iic_byte_period[channel] = 1000/speed + 1; //enable interrupt iic_reg->control.rec_noemp_ie = 1;//接收fifo非空中断 iic_reg->control.slv_noful_ie = 1;//从机发送fifo不满中断 NVIC_EnableIRQ(IIC0_IRQn);
}
void iic0_isr(void)
{
volatile struct iic_reg_t *iic_reg = IIC0_REG_BASE;
uint8_t data;if(iic_reg->status.rec_emp == 0)//接收fifo非空 { iic_test_reg_addr = iic_reg->data;//从机接收数据 co_printf("%d rec_data = %d \r\n",iic_master_data_rx_cnt++,iic_test_reg_addr); } else if(iic_reg->status.slv_trans_ful ==0){//从机发送fifo不满 while(iic_reg->status.slv_trans_ful ==0){ data = rand() & 0xFF;//从机发送随机数据 iic_reg->slavedata.slave_data = data;//iic_slave_data_Tx_buffer[iic_slave_data_Tx_cnt++]; co_printf("send %d.\r\n", data); } }
}