#include "p33FJ64GP206.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "Usart1.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
xQueueHandle xStartUSART1Task( void );
xQueueHandle xUART1Queue;
/*****************************************************************************
* 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 */
}
}
}
xQueueHandle xStartUART1Task( void )
{
/* Create the queue used by the LCD task. Messages for display on the LCD
are received via this queue. */
xUART1Queue = xQueueCreate( uart1QUEUE_SIZE, sizeof( xUART1Message ) );
/* Start the task that will write to the LCD. The LCD hardware is
initialised from within the task itself so delays can be used. */
xTaskCreate( vUSART1Task, ( signed portCHAR * ) "UART", configMINIMAL_STACK_SIZE*2, NULL, tskIDLE_PRIORITY , NULL );
return xUART1Queue;
}
static void vUSART1Task( void *pvParameters )
{
xUART1Message xMessage;
/* Initialise the hardware. This uses delays so must not be called prior
to the scheduler being started
*/
Init_UART1();
volatile int a;
for(a=0;a<0x4000;a++);
/* Welcome message. */
UART1puts("www.FreeRTOS.org ");
for( ;; )
{
/* Wait for a message to arrive that requires displaying. */
while( xQueueReceive( xUART1Queue, &xMessage, portMAX_DELAY ) != pdPASS );
/* Clear the current display value. */
/* Switch rows each time so we can see that the display is still being
updated. */
UART1puts( xMessage.pcMessage );
/* Delay the requested amount of time to ensure the text just written
to the LCD is not overwritten. */
vTaskDelay( xMessage.xMinDisplayTime );
}
}
/*****************************************************************************
* EOF
*****************************************************************************/