Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <xc.h>
- #include <plib.h>
- #include "fubmini.h"
- #define SYS_FREQ (48000000L)
- #define GetPeripheralClock() (SYS_FREQ /(1 << OSCCONbits.PBDIV))
- typedef unsigned int UINT;
- //CONFIGS
- #pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
- #pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
- #pragma config FUSBIDIO = OFF // USB USID Selection (Controlled by Port Function)
- #pragma config FVBUSONIO = OFF // USB VBUS ON Selection (Controlled by Port Function)
- #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider)
- #pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier)
- #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider)
- #pragma config UPLLEN = ON // USB PLL Enable (Enabled)
- #pragma config FPLLODIV = DIV_2 // System PLL Output Clock Divider (PLL Divide by 2)
- #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
- #pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled)
- #pragma config IESO = OFF // Internal/External Switch Over (Disabled)
- #pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode)
- #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled)
- #pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1)
- #pragma config FCKSM = CSECME // Clock Switching and Monitor Selection (Clock Switch Enable, FSCM Enabled)
- #pragma config WDTPS = PS1024 // Watchdog Timer Postscaler (1:1024)
- #pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
- #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
- #pragma config FWDTWINSZ = WISZ_25 // Watchdog Timer Window Size (Window Size is 25%)
- #pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled)
- #pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1)
- #pragma config PWP = OFF // Program Flash Write Protect (Disable)
- #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
- #pragma config CP = OFF // Code Protect (Protection Disabled)
- #define ALL_BITS 0xFFFFFFFF
- void init(void);
- void configIO(void);
- //----------------
- BYTE UART1_RX(void);
- void UART1_TX(BYTE data);
- void UART1Init(void);
- void UART1ConfigPins(void);
- void UART1_RX_STR(char *buff, int len, char end);
- void UART1_RX_BUFF(char *buff, int len);
- void UART1_TX_STRING(char *string);
- void UART1_TX_BUFF(char *string, int len);
- //----------------
- char inBuff[128];
- char outBuff[128];
- void delay()
- {
- unsigned int tt;
- tt = 10;
- while(tt--)
- Nop();
- }
- void main ()
- {
- init();
- configIO();
- UART1Init();
- UART1ConfigPins();
- sprintf(outBuff, "Hello!!\r\n\0");
- UART1_TX_STRING(outBuff);
- while(1)
- {
- UART1_RX_STR(inBuff,0,1);
- sprintf(outBuff, "You Wrote: %s\r\n\0", inBuff);
- UART1_TX_STRING(outBuff);
- }
- }
- void configIO(void)
- {
- //ALL INPUT TO AVOID ISSUES
- PORTSetPinsDigitalIn(IOPORT_A, ALL_BITS);
- PORTSetPinsDigitalIn(IOPORT_B, ALL_BITS);
- PORTSetPinsDigitalIn(IOPORT_C, ALL_BITS);
- }
- void init(void)
- {
- SYSTEMConfig(SYS_FREQ, SYS_CFG_ALL);
- OSCConfig(OSC_POSC_PLL, OSC_PLL_MULT_24,OSC_PLL_POST_2,0);
- mOSCSetPBDIV( OSC_PB_DIV_1 ); // Configure the PB bus to run at 1/4 the CPU frequency
- }
- void UART1ConfigPins(void)
- {
- SYSKEY = 0x0; // Make sure SYSKEY is locked
- SYSKEY = 0xAA996655; // SYSKEY unlock sequence
- SYSKEY = 0x556699AA;
- PPSInput(3,U1RX,RPB2); //RX - P9
- PPSOutput(1,RPC0,U1TX); //TX - P11
- SYSKEY = 0x0; // Relock SYSKEY
- PORTSetPinsDigitalIn(FUB_P9);
- PORTSetPinsDigitalOut(FUB_P11);
- }
- void UART1Init(void)
- {
- UARTConfigure(UART1 , UART_ENABLE_PINS_TX_RX_ONLY);
- UARTSetFifoMode(UART1 , 0);//UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
- UARTSetLineControl(UART1 , UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
- UARTSetDataRate(UART1 , GetPeripheralClock(), 57600);
- UARTEnable(UART1 , UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));
- }
- void UART1_TX(BYTE data)
- {
- while(UARTTransmitterIsReady(UART1)==0);
- UARTSendDataByte(UART1, data);
- }
- BYTE UART1_RX(void)
- {
- BYTE character;
- while(UARTReceivedDataIsAvailable(UART1)==0);
- character = UARTGetDataByte(UART1);
- return character;
- }
- void UART1_TX_BUFF(char *string, int len)
- {
- char inTemp;
- while(len--)
- {
- UART1_TX(*string++);
- }
- }
- void UART1_TX_STRING(char *string)
- {
- while(*string)
- {
- UART1_TX(*string++);
- }
- }
- void UART1_RX_BUFF(char *buff, int len)
- {
- while(len--)
- {
- *buff++ = UART1_RX();
- }
- }
- void UART1_RX_STR(char *buff, int len, char end)
- {
- char inTemp;
- if(!end)
- {
- if(!len)
- {
- do
- {
- inTemp = UART1_RX();
- *buff++ = inTemp;
- }while(inTemp != 0);
- } else {
- while(len--)
- *buff++ = UART1_RX();
- }
- } else {
- while(inTemp != '\n')
- {
- inTemp = UART1_RX();
- if(inTemp != '\n')
- *buff++ = inTemp;
- else
- *buff++ = 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement