Author Topic: 4 UART  (Read 1777 times)

kelvinpmlu

  • Newbie
  • *
  • Posts: 2
    • View Profile
4 UART
« on: November 08, 2012, 08:46:51 PM »
Hi All,

Newbie to Microchip,

Where to find following files of netcruzer?

serSer1.h
serSer2.h
serSer3.h
serSer4.h

Can anyone help ?


Tostikoning

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: 4 UART
« Reply #1 on: November 15, 2012, 02:16:10 AM »
Hi,

I don't believe there is  UART code in the netcruzer libary. What you can do is check the microchip application libary for UART code (MAL). I have some code for the SB66EC you can look into as an example. the comments are scarce. My reccomendation is, check the datasheet and family reference datasheet. You can find thos over here http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en547867

This is my UART code:

Code: [Select]
/* ----------- PROTOTYPES ----------- */
void UART_Init(void);
void UART_SendString(char *string[], char enter);
void UART_SendReg(int reg);
void UART_Hex(unsigned int long dec);
void UART_Dec(unsigned int long dec);
void UART_Bin(unsigned int long dec);
void UART_Send(char *string, int var1, int var2, int var3);

/* ----------- FUNCTIONS ----------- */
void UART_Interrupt()  iv IVT_ADDR_U1RXINTERRUPT
{
    int i = -1;
   
    timesec_buff[data_id] = seconds; // Save time
    timefrac_buff[data_id] = fracs;

    while(databuffer[data_id][i++] != '\n')
    {
        while(U1STAbits.URXDA == 0);
        databuffer[data_id][i] = U1RXREG;                // Save data
    }

    data_length[data_id] = i - 1;
   
    data_flag++;     // raise data flag
   
    if(data_id < BUFF_SIZE) // is the buffer full, start over... else add 1 to the data id
        data_id++;
    else
        data_id = 0;
       
    IFS0bits.U1RXIF = 0;    // Clear the Receive Interrupt Flag
}

void UART_Init()
{
    ANSG = 0;
    TRISGbits.TRISG7 = 0;
    TRISGbits.TRISG8 = 1;

    RPINR18 = 19; // Rx => RP19 => RG8
    RPOR13 = 3;   // Tx => RP26 => RG7

    /*  -- U1MODE  --
    15: UARTEN: 1  (enabled)
    14: 0
    13: USIDL:  0  (stop in idle mode)
    12: IRDA    0  (infrared)
    11: RTSMD:  1  (1=simplex, 0=flow control)
    10: 0
    9-8: UEN:   00
    7: WAKE:    0  (wake-up on start bit)
    6: LPBACK:  0
    5: ABAUD:   0
    4: RXINV:   1  (receive polarity imversion bit)
    3: BRGH:    1  (high baud rate enable)
    2-1: PDSEL: 00 (data bits and parity)
    0: STSEL    0  (stop bits) */

    U1MODE = 0x8008;
    U1BRG = ((FCY/BAUDRATE)/4)-1;  // Calculate BAUD Rate
    U1STA = 0x6040;    // interrupt on 1 character in buffer
}

void UART_inton(void)
{
    // set interrupt vector
    IPC2bits.U1RXIP = 5;    // Set interrupt level 6 for UART1 RX
    IFS0bits.U1RXIF = 0;    // Clear the Recieve Interrupt Flag
    IEC0bits.U1RXIE = 1;    // Enable Recieve Interrupts
}

void UART_Send(char *string, int var0, int var1, int var2)
{
    int i = 0;
    int vars[3];

    vars[0] = var0;
    vars[1] = var1;
    vars[2] = var2;

    while(*string != '\0')
    {
        if(*string == '%')
        {
            string++;

            switch (*string)
            {
                case 'h': UART_Hex(vars[i++]); break;
                case 'b': UART_Bin(vars[i++]); break;
                case 'd': UART_Dec(vars[i++]); break;
                case 'n': U1TXREG = 13; U1STAbits.UTXEN = 1; break;
            }
        }

        else
        {
            while(U1STAbits.TRMT == 0); // Has the last character been sent?
            U1TXREG = *string;
            U1STAbits.UTXEN = 1; // Send character
        }

        string++;
    }
}

void UART_SendString(char string[64], char enter)
{
    int i;

    if (enter)
    {
        while(U1STAbits.TRMT == 0);
        U1TXREG = 13;
        U1STAbits.UTXEN = 1;
    }

    for(i = 0;string[i];i++)
    {
         while(U1STAbits.TRMT == 0);
         U1TXREG = string[i];
         U1STAbits.UTXEN   = 1;
    }
}

void UART_Hex(unsigned int long dec)
{
    char str[11] = {'0', 'x'};
    char hex[16]= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    uart_temp = dec & 0xF0000000;
    uart_temp = uart_temp >> 28;
    str[2] = hex[uart_temp];

    uart_temp = dec & 0xF000000;
    uart_temp = uart_temp >> 24;
    str[3] = hex[uart_temp];

    uart_temp = dec & 0xF00000;
    uart_temp = uart_temp >> 20;
    str[4] = hex[uart_temp];

    uart_temp = dec & 0xF0000;
    uart_temp = uart_temp >> 16;
    str[5] = hex[uart_temp];

    uart_temp = dec & 0xF000;
    uart_temp = uart_temp >> 12;
    str[6] = hex[uart_temp];

    uart_temp = dec & 0xF00;
    uart_temp = uart_temp >> 8;
    str[7] = hex[uart_temp];

    uart_temp = dec & 0xF0;
    uart_temp = uart_temp >> 4;
    str[8] = hex[uart_temp];

    uart_temp = dec & 0xF;
    str[9] = hex[uart_temp];

    str[10] = '\0';   // End of string

    UART_SendString(str, 0);
}

void UART_Dec(unsigned long int dec)
{
    char str[7];

    IntToStrWithZeros(dec, str);

    UART_SendString(str, 0);

    dec = 0;
}

void UART_Bin(unsigned long int dec)
{
    char str[20];
    unsigned int bin = 32768;
    int i = 0;

    str[0] = '0';
    str[1] = 'b';
    for(i=2;i<19;i++)
    {
        if (i == 10) // add space between bit 7 and 8
            str[i] = ' ';
        else
        {
            str[i] = (dec >= bin) ? '1' : '0';
            dec = (dec >= bin) ? dec - bin : dec;
            bin = bin/2;
        }
    }

    str[19] = '\0';

    UART_SendString(str,0);

    dec = 0;
}