/*
 * File:   uart.c
 * Author: Vojta
 *
 * Created on 31. ?ervence 2022, 13:59
 */


#include "uart.h"

//************nepouziva se u finalniho vyrobku, bylo to jenom pro posilani pozadovanych hodnot pro zobrazeni*****
void uart_init(void) // pro fosc = 32 MHz !!!!!!!!!!!!!!!!!!
{
   
    TX1STAbits.TX9 = 0; //Selects 8-bit transmission
    TX1STAbits.TXEN = 1; // Transmit enabled.
    RC1STAbits.SPEN = 1; // Serial port is enabled (configures RX/DT and TX/CK pins as serial port pins)
    
    RC1STAbits.RX9 = 0; //Selects 8-bit reception
    RC1STAbits.CREN = 1; //Enables receiver
    
    
    //*****konfigurace na 9600 baudrate (9615) pro fosc = 32MHz
    TX1STAbits.SYNC = 0; // asynchronni mod
    TX1STAbits.BRGH = 0; // low speed
    BAUD1CONbits.BRG16 = 0;
    SP1BRG = 51; 

    
    RB7PPS = 0x0F; // TX na RB7
    return;
}

void uart_send(unsigned char uart_data)
{
   
    
    
  
  while(!PIR3bits.TX1IF)
      continue;
    TX1REG = uart_data;
     __delay_ms(10);

    

    return;   
}

void int_to_char_uart(unsigned int data1)
{
    unsigned char Lbit = 0,Hbit =0;
    Lbit |= data1;
    Hbit |= data1 >> 8; 
    
   // while(!TXIF);
   // TXREG = 255; //aby nebyly srance viz arduino kod


    uart_send(Hbit);
    
    uart_send(Lbit);
   
    
    __delay_ms(1000);
    
    
    
    return;
}