这是LM4F120H5QR I2C环回模式的程序,我使用PB2和PB3作为scl和sda。我无法从数据寄存器读取数据,MTPR(SCL CLOCK PERIOD)也从7变为1.
#include<stdio.h>
#include<stdint.h>
#include<lm4f120h5qr.h>
void transmit()
{
while(I2C0_MASTER_MCS_R & 0x1); // WAIT TILL BUSY BIT IS FREE
I2C0_MASTER_MSA_R=0x0; // RS BIT-0 FOR TRANSMIT
I2C0_MASTER_MDR_R=0x0A; // WRITING DATA
I2C0_MASTER_MCS_R =0x03; //START + RUN BIT ENABLE
while(I2C0_MASTER_MCS_R & 0x1); //WAIT
I2C0_MASTER_MCS_R = 0x01; // ENABLE TRANSMIT
while(I2C0_MASTER_MCS_R & 0x1); // WAIT
I2C0_MASTER_MCS_R =0x05; //STOP
while(I2C0_MASTER_MCS_R & 0x1); //WAIT
}
int main()
{
char ch=0;
SYSCTL_RCC_R = 0x01521540; // CLOCK
SYSCTL_RCC2_R = 0xC1004000;
SYSCTL_RCGCI2C_R |= 0x01; // CLOCK FOR I2C
SYSCTL_RCGCGPIO_R |=0x02; //CLOCK FOR GPIO
GPIO_PORTB_DIR_R &= ~0x0C; // DIRECTION
GPIO_PORTB_AFSEL_R |= 0x0C; //ALTERNATE FUNCTION
GPIO_PORTB_PCTL_R |= 0x3300; // PORT CONTROL
GPIO_PORTB_DR2R_R |= 0x0C; // 2 mA
GPIO_PORTB_PUR_R |= 0x0C; // pull up resistors
GPIO_PORTB_ODR_R |= 0x08; // open drain for sda
GPIO_PORTB_DEN_R |= 0x0C; // digital enable
I2C0_MASTER_MCR_R |= 0x01; // loopback mode enable
I2C0_MASTER_MTPR_R |= 0x07; // scl clock period 100 kbps
transmit();
while(I2C0_MASTER_MCS_R & 0x1); // wait
I2C0_MASTER_MSA_R =0x0; // RS bit is 0 to txt clock
I2C0_MASTER_MCS_R = 0x03; // start+run
while(I2C0_MASTER_MCS_R & 0x1); //wait
I2C0_MASTER_MCS_R = 0x05; //stop
while(I2C0_MASTER_MCS_R & 0x1); // wait
I2C0_MASTER_MSA_R |=0x1; //RS bit 1 to receive
I2C0_MASTER_MCS_R= 0x0B; // ack+start+run
while(I2C0_MASTER_MCS_R & 0x1); //wait
ch=I2C0_MASTER_MDR_R; //reading the data
while(I2C0_MASTER_MCS_R & 0x1); // wait
I2C0_MASTER_MCS_R=0x05; //stop
while(I2C0_MASTER_MCS_R & 0x1); //wait
}