0% found this document useful (0 votes)
2 views2 pages

Code For TX

This document contains code for configuring UART communication and GPIO for an LED on a microcontroller. It initializes UART with a baud rate of 9600 and sets up an LED for output, toggling its state based on received data. The main application loop continuously transmits a string and checks for incoming data to control the LED's state.

Uploaded by

Saad Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

Code For TX

This document contains code for configuring UART communication and GPIO for an LED on a microcontroller. It initializes UART with a baud rate of 9600 and sets up an LED for output, toggling its state based on received data. The main application loop continuously transmits a string and checks for incoming data to control the LED's state.

Uploaded by

Saad Mughal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*********************************************************************************

This code is for TX.


*********************************************************************************/
#include "driver/gpio.h"
#include "driver/uart.h"

#define TXD_PIN GPIO_NUM_17


#define RXD_PIN GPIO_NUM_16

#define UART_NUM UART_NUM_2


#define BAUD 9600

#define LED_PIN GPIO_NUM_2 // Define the GPIO pin for the LED

static const int RX_BUF_SIZE = 128;

void UART_init(void)
{
const uart_config_t uart_config = {
.baud_rate = BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
.source_clk = UART_SCLK_DEFAULT,
};

// We won't use a buffer for sending data.


uart_driver_install(UART_NUM, RX_BUF_SIZE * 2, 0, 0, NULL, 0);
uart_param_config(UART_NUM, &uart_config);
uart_set_pin(UART_NUM, TXD_PIN, RXD_PIN, UART_PIN_NO_CHANGE,
UART_PIN_NO_CHANGE);
}

// Function to initialize GPIO


void initialize_gpio()
{
// Configure GPIOs for LED
gpio_config_t io_conf_led;
io_conf_led.intr_type = GPIO_INTR_DISABLE; // Disable interrupts
io_conf_led.mode = GPIO_MODE_OUTPUT; // Set as output mode
io_conf_led.pin_bit_mask = (1ULL << LED_PIN); // Set the pin mask
io_conf_led.pull_down_en = GPIO_PULLDOWN_DISABLE; // Disable pull-down resistor
io_conf_led.pull_up_en = GPIO_PULLUP_DISABLE; // Disable pull-up resistor
gpio_config(&io_conf_led);
}

void UART_tx(unsigned char byte)


{
//uint8_t* data = (uint8_t*) malloc(4);
//data = &byte;
uart_write_bytes(UART_NUM, &byte, 1);
//free(data);
}

unsigned char UART_rx()


{
uint8_t data;
uart_read_bytes(UART_NUM, &data, 1, portMAX_DELAY);
return (unsigned char)data;
}

// Delay for milliseconds


void delay_ms(unsigned int i){
vTaskDelay (i / portTICK_PERIOD_MS);
}

// Delay for microseconds


void delay_us(unsigned int i){
esp_rom_delay_us(i);
}

//******************************************************************************
void app_main(){
uint8_t cnt = 0;
char str[64] =
"ABCDEFGHIragtihihfsifyhTGFYETH*ragtihihfsifyhTTusXtd
nxwYZ";

UART_init();
initialize_gpio();
gpio_set_level(LED_PIN, 1);
delay_ms(500);
gpio_set_level(LED_PIN, 0);

for(;;){
for(int j=0; j<64; j++){
UART_tx(str[j]);
delay_us(500);
}

cnt = UART_rx();
if(cnt >= 32){
gpio_set_level(LED_PIN, 1);
delay_ms(2000);
gpio_set_level(LED_PIN, 0);

}
delay_ms(5000);
}
}

You might also like