Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <xc.h>
  2. #include <stdint.h>
  3. #include <stdbool.h>
  4.  
  5. #define _XTAL_FREQ 48000000UL
  6.  
  7. #define RS485_READ 1
  8. #define RS485_WRITE 0
  9. #define IOF_8MHz 0b111
  10. #pragma config XINST = OFF
  11.  
  12. void clockInit(void);
  13. uint8_t high8(uint16_t x);
  14. uint8_t low8(uint16_t x);
  15.  
  16. void main(void) {
  17.  
  18. clockInit();
  19.  
  20. // disables analog-to-digital conversion
  21. // (enables digital IO on all AD pins)
  22. ANCON0 = 0x1F;
  23. ANCON1 = 0x1F;
  24.  
  25. // enables rs485 write (output)
  26. TRISAbits.TRISA2 = 0;
  27.  
  28. TXSTA1bits.SYNC = 0;
  29. TXSTA1bits.BRGH = 1; // set BRGH bit
  30. TXSTA1bits.TXEN = 1; // enable TX
  31.  
  32. RCSTA1bits.SPEN = 1;
  33. RCSTA1bits.CREN = 1;
  34.  
  35. TRISCbits.TRISC6 = 0;
  36. TRISCbits.TRISC7 = 1; // Rx1 set input
  37.  
  38. BAUDCON1bits.BRG16 = 1; // set 16 bits SPBRG
  39. BAUDCON1bits.RCIDL = 1; // set receive active
  40.  
  41. SPBRGH1 = high8(104); // set UART speed SPBRGH
  42. SPBRG1 = low8(104); // set UART speed SPBRGL
  43.  
  44. CM1CONbits.CON = 0; // disables comparator
  45.  
  46.  
  47. /* Loop forever */
  48. while (1) {
  49. LATAbits.LATA2 = RS485_WRITE;
  50.  
  51. TXREG1 = 0x55; // yes, send char
  52. while (!TXSTA1bits.TRMT); // Ready ?
  53.  
  54. LATAbits.LATA2 = RS485_READ;
  55. __delay_us(100);
  56. }
  57. }
  58.  
  59. /*----------------------------------------------------------------------------*/
  60. void clockInit(void) {
  61. // disables reference oscillator output
  62. REFOCONbits.ROON = 0;
  63.  
  64. // device enters IDLE mode on SLEEP instruction
  65. OSCCONbits.IDLEN = 1;
  66. // system clock is from primary oscillator
  67. OSCCONbits.SCS = 0;
  68. // selects base frequency as 8MHz
  69. OSCCONbits.IRCF = IOF_8MHz;
  70. // wait until oscillator is running
  71. while (!OSCCONbits.OSTS);
  72.  
  73. // low speed USB (6MHz clock)
  74. UCFGbits.FSEN = 0;
  75.  
  76. // enable PLL
  77. OSCTUNEbits.PLLEN = 1;
  78. }
  79.  
  80. uint8_t high8(uint16_t x) {
  81. return (uint8_t) (x >> 8);
  82. }
  83.  
  84. uint8_t low8(uint16_t x) {
  85. return (uint8_t) (x & 0xFF);
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement