daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Sep 12th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. PySerial and microcontroller
  2. putsU1("string");
  3.  
  4. // print a string
  5.  
  6.     int putsU1(char *s) {
  7.      while(*s)
  8.         putU1(*s++);
  9.     }
  10.  
  11. // print a character
  12.  
  13.     int putU1(int c) {
  14.       while (_UTXBF);
  15.         U1TXREG = c;
  16.       return c;
  17.     }
  18.    
  19. In [13]: ser = serial.Serial('/dev/ttyACM0', 9600, bytesize=7, xonxoff=0)
  20.  
  21. In [14]: while True:                                                    
  22.             print(struct.unpack('s', ser.read()))
  23.          ....:    
  24. ('x7f',)
  25. ('c',)
  26. ('x11',)
  27. ('t',)
  28. ('e',)
  29. ('y',)
  30. ('=',)
  31. ('x7f',)
  32.    
  33. getsnU1(s, BUF_SIZE);
  34.  
  35.      char *getsnU1(char *s, int len) {
  36.         char *p = s;
  37.         int cc = 0;
  38.         do {
  39.            *s = getU1();
  40.            putU1(*s);
  41.  
  42.            if ((*s == 0x8) && (s > p)) {
  43.               putU1(' ');
  44.               putU1(0x8);
  45.               len++;
  46.               s--;
  47.               continue;
  48.            }
  49.  
  50.            if (*s == 'n')
  51.               continue;
  52.  
  53.            if (*s == 'r')
  54.               break;
  55.            s++;
  56.           len--;
  57.        } while (len > 1);
  58.  
  59.       *s = '';
  60.  
  61.       return p;
  62.     }
  63.    
  64. #define FOSC            8000000
  65. #define FCY             (FOSC/2)
  66. #define BAUD            9600
  67. #define U1BRGValue      (((FCY/BAUD)/16)-1)
  68.  
  69. void initUART1(void) {
  70. /* general UART config */
  71. RPINR18bits.U1RXR = U1RXPIN;            // set RP5 as UART1 RX
  72. _RP6R = 0b00011;                        // set RP6 as UART Tx pin
  73. U1RXPINTRIS = 1;                        // set RP5 (RB5) as input
  74. U1MODEbits.PDSEL = DATAPARITY;           // set data bits and parity
  75. U1MODEbits.STSEL = STOPBITS;            // set stop bits
  76. U1MODEbits.ABAUD = 0;                   // auto-baud disabled
  77. U1MODEbits.BRGH = 0;                    // low speed baud rate
  78. U1BRG = U1BRGValue;                     // set baud value
  79.  
  80.  
  81. /* enable interrupt on error */
  82. IEC4bits.U1EIE = 1;                     // enable UART1 error interrupt
  83. IPC16bits.U1EIP = 6;                     // set interrupt priority to 6
  84.  
  85. /* enable RX buffer interrupt */
  86. IEC0bits.U1RXIE = 0;                    // enable UART1 RX interrupt
  87. U1STAbits.URXISEL = 0b00;                // interrupt flag is set when buffer is 3/4 full
  88. IPC2bits.U1RXIP = 6;                     // set interrupt priority to 6
  89.  
  90. /* enable TX interrupt */
  91. IEC0bits.U1TXIE = 0;
  92.  
  93.  
  94. /* enable UART1 module */
  95. U1MODEbits.UEN = 0b00;
  96. U1MODEbits.UARTEN = 1;
  97. _UTXEN = 1;                             // enable UART Tx
  98.    
  99. def write(s):
  100.     for c in s:
  101.         ser.write(c)
  102.         time.sleep(0.001) # often not required
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top