Advertisement
Guest User

giobernardi

a guest
Jul 17th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. // DEVCFG2
  2. #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (1x Divider)
  3. #pragma config FPLLMUL = MUL_20 // PLL Multiplier (24x Multiplier)
  4. #pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (12x Divider)
  5. #pragma config UPLLEN = OFF // USB PLL Enable (Disabled and Bypassed)
  6. #pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 256)
  7. // DEVCFG1
  8. #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (Primary Osc w/PLL (XT+,HS+,EC+PLL))
  9. #pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled)
  10. #pragma config IESO = ON // Internal/External Switch Over (Enabled)
  11. #pragma config POSCMOD = HS // Primary Oscillator Configuration (HS osc mode)
  12. #pragma config OSCIOFNC = ON // CLKO Output Signal Active on the OSCO Pin (Enabled)
  13. #pragma config FPBDIV = DIV_8 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
  14. #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
  15. #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576)
  16. #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
  17. // DEVCFG0
  18. #pragma config DEBUG = OFF // Background Debugger Enable (Debugger is disabled)
  19. #pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (ICE EMUC2/EMUD2 pins shared with PGC2/PGD2)
  20. #pragma config PWP = OFF // Program Flash Write Protect (Disable)
  21. #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled)
  22. #pragma config CP = OFF // Code Protect (Protection Disabled)
  23.  
  24. #include <p32xxxx.h>
  25. #include <plib.h> // Include the PIC32 Peripheral Library.
  26.  
  27. #define SYSCLK 80000000L
  28. #define DESIRED_BAUDRATE 9600
  29. #define StatePOR 0x00
  30. #define StateIDLE 0x01
  31. #define StateSEND 0x02
  32. #define StateWAITING 0x03
  33.  
  34. #define LED LATDbits.LATD1
  35. #define BUT PORTDbits.RD11
  36. #define UARTFLAG LATDbits.LATD6
  37. #define UARTEmpty while (BusyUART1())
  38.  
  39. #include <xc.h>
  40.  
  41.  
  42. char oldBtnIndex = 1;
  43. char state = StateIDLE;
  44. char data = 0;
  45. char dataReceived = 0;
  46. void SendChar(char toSend);
  47. void initializeUART();
  48. char CheckButton();
  49.  
  50.  
  51. int main() {
  52. SYSTEMConfigPerformance(SYSCLK);
  53. initializeUART();
  54. INTEnableSystemMultiVectoredInt();
  55. TRISDbits.TRISD6 = 0; //UARTFLAG
  56. TRISDbits.TRISD1 = 0; //LED2
  57. TRISDbits.TRISD11 = 1; //BUTTON
  58.  
  59. UARTFLAG = 0;
  60. LED = 1;
  61. while (1) {
  62. switch (state) {
  63. case StatePOR:
  64. state = StateIDLE;
  65. break;
  66. case StateIDLE:
  67. if(CheckButton() == 1){
  68. SendChar('a');
  69. }
  70. if (dataReceived) {
  71. dataReceived = 0;
  72. state = StateSEND;
  73. LED = 0;
  74. }
  75. break;
  76. case StateSEND:
  77. SendChar('a');
  78. state = StateIDLE;
  79. break;
  80. case StateWAITING:
  81. state = StateIDLE;
  82. break;
  83. }
  84. }
  85. return 0;
  86. }
  87.  
  88. void __ISR(_UART1_VECTOR, ipl2) IntUart1Handler(void) {
  89. // Is this an RX interrupt?
  90. if (mU1RXGetIntFlag()) {
  91. // Clear the RX interrupt Flag
  92. mU1RXClearIntFlag();
  93. // Read data from Rx
  94. data = (char) ReadUART1();
  95. dataReceived = 1;
  96. }
  97. //chiama l'interrupt quando ha finito di trasmettere
  98. if (mU1TXGetIntFlag()) {
  99. mU1TXClearIntFlag();
  100. //sendRdy = 1;
  101. }
  102. }
  103.  
  104. void SendChar(char toSend) {
  105. UARTFLAG = 1;
  106. putcUART1(toSend);
  107. while (BusyUART1());
  108. UARTFLAG = 0;
  109. }
  110.  
  111. char CheckButton() {
  112. char temp = 0;
  113. if (oldBtnIndex & !BUT) {
  114. temp = 1;
  115. }
  116. oldBtnIndex = BUT;
  117. return temp;
  118. }
  119.  
  120. void initializeUART() {
  121. // Optimize PIC32 performance and return peripheral bus frequency
  122. unsigned int pbClk = SYSTEMConfig(SYSCLK, SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);
  123. // Abilita UART1 and set baud rate to DESIRED_BAUDRATE=9600
  124. OpenUART1(UART_EN | UART_ODD_PAR_8BIT, UART_RX_ENABLE | UART_TX_ENABLE, pbClk / 16 / DESIRED_BAUDRATE - 1);
  125. // Configure UART1 RX Interrupt
  126. ConfigIntUART1(UART_INT_PR2 | UART_RX_INT_EN | UART_TX_INT_EN);
  127. while (BusyUART1());
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement