0% found this document useful (0 votes)
37 views6 pages

Name: Nguyen Nhat Duy ID: EEEEIU18025 Homework 5 Embedded Real-Time System Master Code

The document contains code for a master-slave embedded system that communicates over UART. The master code initializes UART transmission and sends button press data from three buttons by toggling the buttons and sending codes over UART. The slave code initializes UART reception in interrupt mode and displays the received data on an LED port. When data is received, the interrupt service routine reads the data from the receive buffer and displays it on the LEDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views6 pages

Name: Nguyen Nhat Duy ID: EEEEIU18025 Homework 5 Embedded Real-Time System Master Code

The document contains code for a master-slave embedded system that communicates over UART. The master code initializes UART transmission and sends button press data from three buttons by toggling the buttons and sending codes over UART. The slave code initializes UART reception in interrupt mode and displays the received data on an LED port. When data is received, the interrupt service routine reads the data from the receive buffer and displays it on the LEDs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Name: NGUYEN NHAT DUY

ID: EEEEIU18025
HOMEWORK 5
EMBEDDED REAL-TIME SYSTEM
Master code:
#include <xc.h>

#include <stdint.h>

#include "config.h"

#define _XTAL_FREQ 4000000

//--------------------------------

// IO Pins Defines (Mappings)

#define UP RB0

#define Down RB1

#define Send RB2

//--------------------------------

// Functions Declarations

void UART_TX_Init(void);

void UART_Write(uint8_t);

//--------------------------------

// Main Routine

void main(void)

//--[ Peripherals & IO Configurations ]--

UART_TX_Init(); // Initialize The UART in Master Mode @ 9600bps

uint8_t Data = 0; // The Data Byte

TRISB = 0x00; // RB0, RB1 & RB2 Are Input Pins (Push Buttons)

PORTB = 0x00; // Initially OFF

//---------------------------
while(1)

RB0=1;

UART_Write(0x01);

__delay_ms(100);

RB1=1;

UART_Write(0x03);

__delay_ms(100);

RB2=1;

UART_Write(0x07);

__delay_ms(100);

RB0=0;

UART_Write(0x06);

__delay_ms(100);

RB1=0;

UART_Write(0x04);

__delay_ms(100);

RB2=0;

UART_Write(0x00);

__delay_ms(100);

//--------------------------------

// Functions Definitions
void UART_TX_Init(void)

BRGH = 1; // Set For High-Speed Baud Rate

SPBRG = 25; // Set The Baud Rate To Be 9600 bps

//--[ Enable The Ascynchronous Serial Port ]--

SYNC = 0;

SPEN = 1;

//--[ Set The RX-TX Pins to be in UART mode (not io) ]--

TRISC6 = 1; // As stated in the datasheet

TRISC7 = 1; // As stated in the datasheet

TXEN = 1; // Enable UART Transmission

void UART_Write(uint8_t data)

while(!TRMT);

TXREG = data;

Slave code:
#include <xc.h>

#include <stdint.h>

#include "config.h"

#define _XTAL_FREQ 4000000

//--------------------------------

// Functions Declarations

void UART_RX_Init(void);

uint8_t UART_Read(void);

// Globals

uint8_t UART_Buffer = 0;

//--------------------------------
// Main Routine

void main(void)

//--[ Peripherals & IO Configurations ]--

UART_RX_Init(); // Initialize The UART in Master Mode @ 9600bps

TRISB = 0x00; // Output Port (4-Pins)

PORTB = 0x00; // Initially OFF

//---------------------------

while(1)

// Stay Idle, Everything is handled in the ISR !

return;

//--------------------------------

// Functions Definitions

void UART_RX_Init()

BRGH = 1; // Set For High-Speed Baud Rate

SPBRG = 25; // Set The Baud Rate To Be 9600 bps

// Enable The Ascynchronous Serial Port

SYNC = 0;

SPEN = 1;

// Set The RX-TX Pins to be in UART mode (not io)

TRISC6 = 1; // As stated in the datasheet

TRISC7 = 1; // As stated in the datasheet

//--[ Enable UART Receiving Interrupts ]--

RCIE = 1; // UART Receving Interrupt Enable Bit

PEIE = 1; // Peripherals Interrupt Enable Bit


GIE = 1; // Global Interrupt Enable Bit

//------------------

CREN = 1; // Enable Data Continous Reception

void __interrupt() ISR (void)

if (RCIF == 1)

UART_Buffer = RCREG; // Read The Received Data Buffer

PORTB = UART_Buffer; // Display The Received Data On LEDs

RCIF = 0; // Clear The Flag

Result:

You might also like