Add I2C0 initialization and data write function

This commit is contained in:
Sanyog Nevase Nevase 2024-10-29 20:06:46 +05:30
parent 48897dbc0e
commit b6c18dbd2e
1 changed files with 50 additions and 16 deletions

66
main.c
View File

@ -1,29 +1,63 @@
#include <stdint.h>
#include "tm4c123gh6pm.h"
#define STCTRL *((volatile uint32_t *) 0xE000E010) // Control and status
#define STRELOAD *((volatile uint32_t *) 0xE000E014) // Reload value
#define STCURRENT *((volatile uint32_t *) 0xE000E018) // Current value
#define STCTRL *((volatile uint32_t *) 0xE000E010) // STATUS AND CONTROL REGISTER
#define STRELOAD *((volatile uint32_t *) 0xE000E014) // RELOAD VALUE REGISTER
#define STCURRENT *((volatile uint32_t *) 0xE000E018) // CURRENT VALUE REGISTER
#define ENABLE (1 << 0) // ENABLE TIMER BY SETTING BIT 0 OF CSR
#define CLKINT (1 << 2) // SPECIFY CPU CLOCK
#define CLOCK_HZ 16000000 // CLOCK FREQUENCY
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SYSTICK RELOAD VALUE CONVERSION IN MICROSECONDS
void I2C0_init(void) {
SYSCTL_RCGCI2C_R |= 0x01; // I2C0 CLOCK ENABLE
SYSCTL_RCGCGPIO_R |= 0x02; // GPIO PORTB CLOCK ENABLE
while ((SYSCTL_PRGPIO_R & 0x02) == 0)
{
// DELAY TO ENSURE CLOCK IS STABILIZED
};
GPIO_PORTB_AFSEL_R |= 0x0C; // ENABLE ALERNATE FUNCTION FOR PB2 AND PB3
GPIO_PORTB_ODR_R |= 0x08; // ENABLE OPEN DRAIN ON SDA
GPIO_PORTB_DEN_R |= 0x0C; // ENABLE DIGITAL FOR PB2 AND PB3
GPIO_PORTB_PCTL_R &= ~0xFF00; // CLEAR PCTL
GPIO_PORTB_PCTL_R |= 0x3300; // I2C FUNCTIONALITY FOR PB2 AND PB3
I2C0_MCR_R = 0x10; // I2C MASTER FUNCTION INITIALIZE
I2C0_MTPR_R = 0x07; // SET CLOCK
}
void I2C0_WRITE(uint8_t PERIPHERAL_ADDRESS, uint8_t ANALOG_SAMPLE_MSB, uint8_t ANALOG_SAMPLE_LSB)
{
I2C0_MSA_R = (PERIPHERAL_ADDRESS << 1); // SET PERIPHERAL ADDRESS, WRITE
I2C0_MDR_R = ANALOG_SAMPLE_MSB; // MSB
I2C0_MCS_R = 0x03; // SEND START AND RUN
while (I2C0_MCS_R & 0x01); // WAIT FOR TRANSMISSION TO FINISH
if (I2C0_MCS_R & 0x02) return; // RETURN IF ERROR OCCURS
I2C0_MDR_R = ANALOG_SAMPLE_LSB; // LSB
I2C0_MCS_R = 0x05; // SEND START AND RUN
while (I2C0_MCS_R & 0x01); // WAIT FOR TRANSMISSION TO FINISH
if (I2C0_MCS_R & 0x02) return; // RETURN IF ERROR OCCURS
}
#define ENABLE (1 << 0) // Bit 0 of CSR to enable the timer
#define CLKINT (1 << 2) // Bit 2 of CSR to specify CPU clock
#define CLOCK_HZ 16000000 // Clock frequency of EK-TM4C123GXL
#define SYSTICK_RELOAD_VALUE(us) ((CLOCK_HZ / 1000000) * (us) - 1) // SysTick reload value in microseconds based on clock frequency
void systick_setting(void)
{
STRELOAD = SYSTICK_RELOAD_VALUE(1000); // RELOAD VALUE FOR 1ms
STCTRL |= ENABLE | CLKINT; // Enable SysTick with system clock
STCURRENT = 0; // Clear current value
STRELOAD = SYSTICK_RELOAD_VALUE(1000); // RELOAD VALUE FOR 1mS
STCTRL |= ENABLE | CLKINT; // ENABLE SYSTICK WITH SYSTEM CLOCK
STCURRENT = 0; // CLEAR STCURRENT REGUSTER
}
void delay(int us)
{
STRELOAD = SYSTICK_RELOAD_VALUE(us); // RELOAD VALUE FOR REQUIRED DELAY
STCURRENT = 0; // Clear STCURRENT
STCTRL |= ENABLE | CLKINT; // Enable SysTick
while ((STCTRL & (1 << 16)) == 0); // Wait until flag is set
STCTRL &= ~ENABLE; // Stop the timer
STRELOAD = SYSTICK_RELOAD_VALUE(us); // RELOAD VALUE FOR REQUIRED DELAY
STCURRENT = 0; // CLEAR STCURRENT
STCTRL |= ENABLE | CLKINT; // ENABLE SYSTICK WITH SYSTEM CLOCK
while ((STCTRL & (1 << 16)) == 0); // WAIT FOR FLAG TO SET
STCTRL &= ~ENABLE; // STOP TIMER
}
int main(void)
@ -33,4 +67,4 @@ int main(void)
{
}
}
}