Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DEVCFG2
- #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (1x Divider)
- #pragma config FPLLMUL = MUL_20 // PLL Multiplier (24x Multiplier)
- #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (12x Divider)
- #pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
- #pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 256)
- // DEVCFG1
- #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
- #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
- #pragma config IESO = ON // Internal/External Switch Over (Enabled)
- #pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)
- #pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
- #pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
- #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
- #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
- #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
- // DEVCFG0
- #pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
- #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
- #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)
- #include <p32xxxx.h>
- #include <plib.h> // Include the PIC32 Peripheral Library.
- #define SYSCLK 80000000L
- #define DESIRED_BAUDRATE 9600
- #define StatePOR 0x00
- #define StateIDLE 0x01
- #define StateSEND 0x02
- #define StateWAITING 0x03
- #define LED LATDbits.LATD1
- #define BUT PORTDbits.RD11
- #define UARTFLAG LATDbits.LATD6
- #define UARTEmpty while (BusyUART1())
- #include <xc.h>
- char oldBtnIndex = 1;
- char state = StateIDLE;
- char data = 0;
- char dataReceived = 0;
- void SendChar(char toSend);
- void initializeUART();
- char CheckButton();
- int main() {
- SYSTEMConfigPerformance(SYSCLK);
- initializeUART();
- INTEnableSystemMultiVectoredInt();
- TRISDbits.TRISD6 = 0; //UARTFLAG
- TRISDbits.TRISD1 = 0; //LED2
- TRISDbits.TRISD11 = 1; //BUTTON
- UARTFLAG = 0;
- LED = 1;
- while (1) {
- switch (state) {
- case StatePOR:
- state = StateIDLE;
- break;
- case StateIDLE:
- if(CheckButton() == 1){
- SendChar('a');
- }
- if (dataReceived) {
- dataReceived = 0;
- state = StateSEND;
- LED = 0;
- }
- break;
- case StateSEND:
- SendChar('a');
- state = StateIDLE;
- break;
- case StateWAITING:
- state = StateIDLE;
- break;
- }
- }
- return 0;
- }
- void __ISR(_UART1_VECTOR, ipl2) IntUart1Handler(void) {
- // Is this an RX interrupt?
- if (mU1RXGetIntFlag()) {
- // Clear the RX interrupt Flag
- mU1RXClearIntFlag();
- // Read data from Rx
- data = (char) ReadUART1();
- dataReceived = 1;
- }
- //chiama l'interrupt quando ha finito di trasmettere
- if (mU1TXGetIntFlag()) {
- mU1TXClearIntFlag();
- //sendRdy = 1;
- }
- }
- void SendChar(char toSend) {
- UARTFLAG = 1;
- putcUART1(toSend);
- while (BusyUART1());
- UARTFLAG = 0;
- }
- char CheckButton() {
- char temp = 0;
- if (oldBtnIndex & !BUT) {
- temp = 1;
- }
- oldBtnIndex = BUT;
- return temp;
- }
- void initializeUART() {
- // Optimize PIC32 performance and return peripheral bus frequency
- unsigned int pbClk = SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
- // Abilita UART1 and set baud rate to DESIRED_BAUDRATE=9600
- OpenUART1(UART_EN | UART_ODD_PAR_8BIT, UART_RX_ENABLE | UART_TX_ENABLE, pbClk / 16 / DESIRED_BAUDRATE - 1);
- // Configure UART1 RX Interrupt
- ConfigIntUART1(UART_INT_PR2 | UART_RX_INT_EN | UART_TX_INT_EN);
- while (BusyUART1());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement