⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usart1.c

📁 DSPic33FJ64GP206单片机的应用
💻 C
字号:

#include "includes.h"


#define SYSCLK 7372870/2 		//XT 
#define BAUDRATE2 9600

#define UART1_TX_TRIS   TRISFbits.TRISF3
#define UART1_RX_TRIS   TRISFbits.TRISF2
#define BAUDRATEREG2 	SYSCLK/16/BAUDRATE2 -1
#define uart1QUEUE_SIZE		1

void AppsUSART1Task( void *);
OS_EVENT * Quart1;
//void * OSUART1Message[40];
/*****************************************************************************
 * Function: UART1Init
 *
 * Precondition: None.
 *
 * Overview: Setup UART1 module.
 *
 * Input: None.
 *
 * Output: None.
 *
 *****************************************************************************/
void Init_UART1()
{
    // Set directions of UART IOs
	UART1_TX_TRIS = 0;
	UART1_RX_TRIS = 1;
	U1BRG = BAUDRATEREG2;

	U1MODE = 0;
	U1STA = 0;
	U1MODE = 0x8800 ;
	U1STA = 0x2400 ;

	

}

/*****************************************************************************
 * Function: UART1PutChar
 *
 * Precondition: UART1Init must be called before.
 *
 * Overview: Wait for free UART transmission buffer and send a byte.
 *
 * Input: Byte to be sent.
 *
 * Output: None.
 *
 *****************************************************************************/
void  UART1PutChar(char Ch){
    // wait for empty buffer  
    while(U1STAbits.UTXBF == 1);
      U1TXREG = Ch;
}



/*****************************************************************************
 * Function: UART1PutDec
 *
 * Precondition: UART1Init must be called before.
 *
 * Overview: This function converts decimal data into a string
 * and outputs it into UART.
 *
 * Input: Binary data.
 *
 * Output: None.
 *
 *****************************************************************************/
void  UART1PutDec(unsigned int Dec){
unsigned int Res;
    Res = Dec;

    if(Res/1000) 
		UART1PutChar(Res/1000+'0');
    Res = Res - (Res/1000)*1000;
	if(Res/100) 
        UART1PutChar(Res/100+'0');
    Res = Res - (Res/100)*100;

    if(Res/10) 
        UART1PutChar(Res/10+'0');
    Res = Res - (Res/10)*10;
 
    UART1PutChar(Res+'0');
}

/***************************************************************************
* Function Name     : UART1puts                                            *
* Description       : This function puts the data string to be transmitted *
*                     into the transmit buffer (till NULL character)       *
* Parameters        : unsigned int * address of the string buffer to be    *  
*                     transmitted                                          *
* Return Value      : None                                                 *
***************************************************************************/

void UART1puts(unsigned int *buffer)
{
    char * temp_ptr = (char *) buffer;

    /* transmit till NULL character is encountered */

    if(U1MODEbits.PDSEL == 3)        /* check if TX is 8bits or 9bits */
    {
        while(*buffer != '\0') 
        {
            while(U1STAbits.UTXBF); /* wait if the buffer is full */
            U1TXREG = *buffer++;    /* transfer data word to TX reg */
        }
    }
    else
    {
        while(*temp_ptr != '\0')
        {
            while(U1STAbits.UTXBF);  /* wait if the buffer is full */
            U1TXREG = *temp_ptr++;   /* transfer data byte to TX reg */
        }
    }
}


/*****************************************************************************
 * EOF
 *****************************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -