#include <proc/p32mx795f512h.h>
#include <string.h>
#include <plib.h>
// Configuration Bits
#pragma config FNOSC = FRCPLL // Oscilador interno
#pragma config FPLLIDIV = DIV_1 // PLL Input Divider
#pragma config FPLLMUL = MUL_20 // PLL Multiplier
#pragma config FPLLODIV = DIV_1 // PLL Output Divider
#pragma config FPBDIV = DIV_1 // Peripheral Clock divisor
#pragma config FWDTEN = OFF // Watchdog Timer
#pragma config WDTPS = PS1 // Watchdog Timer Postscale
#pragma config FCKSM = CSECMD // Clock Switching & Fail Safe Clock Monitor
#pragma config OSCIOFNC = OFF // CLKO Enable
#pragma config IESO = OFF // Internal/External Switch-over
#pragma config FSOSCEN = OFF // Secondary Oscillator Enable
#pragma config CP = OFF // Code Protect
#pragma config BWP = OFF // Boot Flash Write Protect
#pragma config PWP = OFF // Program Flash Write Protec
#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select
#pragma config DEBUG = OFF // Debugger Disabled for Starter Kit
// *****************************************************************************
// *****************************************************************************
// Section: System Macros
// *****************************************************************************
// *****************************************************************************
#define GetSystemClock() (80000000ul)
#define GetPeripheralClock() (GetSystemClock()/(1 << OSCCONbits.PBDIV))
#define GetInstructionClock() (GetSystemClock())
// *****************************************************************************
// *****************************************************************************
// Section: Function Prototypes
// *****************************************************************************
// *****************************************************************************
void SendDataBuffer(const char *buffer, UINT32 size);
void delay_us(WORD us);
void delay_ms(WORD msec);
#define BAUDRATE 115200
#define UARTx UART6
const char cadenaHello[]="Hello World desde la G2...\r\n";
// *****************************************************************************
// int main (void)
// *****************************************************************************
int main (void)
{
int nc;
int pbFreq;
CheKseg0CacheOn();
mCheConfigure(CHE_CONF_WS3 | CHE_CONF_PF_DISABLE);
pbFreq=SYSTEMConfigPerformance(GetSystemClock());
UARTConfigure(UARTx, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UARTx, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UARTx, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UARTx, GetPeripheralClock(), BAUDRATE);
UARTEnable(UARTx, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
while (1) {
SendDataBuffer(cadenaHello, sizeof(cadenaHello)-1);;
delay_ms(1000);
}
}
// *****************************************************************************
// void delay_us(WORD us)
// *****************************************************************************
void delay_us(WORD us)
{
DWORD start = _CP0_GET_COUNT();
DWORD end = start + GetSystemClock() / 1000000 / 2 * us;
if (end > start) while (_CP0_GET_COUNT() < end);
else while (_CP0_GET_COUNT() > start || _CP0_GET_COUNT() < end);
}
// *****************************************************************************
// void delay_ms(WORD msec)
// *****************************************************************************
void delay_ms(WORD msec)
{
unsigned int i;
for(i=0; i<msec; i++)
delay_us(1000);
}
// *****************************************************************************
// void SendDataBuffer(char *buffer, UINT32 size)
// *****************************************************************************
void SendDataBuffer(const char *buffer, UINT32 size)
{
while(size)
{
while(!UARTTransmitterIsReady(UARTx))
;
UARTSendDataByte(UARTx, *buffer);
buffer++;
size--;
}
while(!UARTTransmissionHasCompleted(UARTx))
;
}