ericek111

Untitled

Nov 17th, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.64 KB | None | 0 0
  1. package me.lixko.serialcomm;
  2.  
  3. import com.fazecast.jSerialComm.SerialPort;
  4.  
  5. public class SerialComm {
  6.  
  7.     public static final int SERIN = 0;
  8.     public static final int SRCK = 1;
  9.     public static final int RCK = 2;
  10.     public static final int SRCLR = 3;
  11.     public static final boolean HIGH = true;
  12.     public static final boolean LOW = false;
  13.  
  14.     byte[] gpioa = new byte[1];
  15.     public SerialPort sp;
  16.  
  17.     public SerialComm(String port) {
  18.         sp = SerialPort.getCommPort(port);
  19.     }
  20.  
  21.     public void setPin(int pin, int value) {
  22.         if (value == 0)
  23.             gpioa[0] = (byte) (gpioa[0] & ~(1 << pin));
  24.         else
  25.             gpioa[0] = (byte) (gpioa[0] | (1 << pin));
  26.         sp.writeBytes(gpioa, 1);
  27.     }
  28.  
  29.     public void setPin(int pin, boolean value) {
  30.         if (value)
  31.             gpioa[0] = (byte) (gpioa[0] | (1 << pin));
  32.         else
  33.             gpioa[0] = (byte) (gpioa[0] & ~(1 << pin));
  34.         sp.writeBytes(gpioa, 1);
  35.         try {
  36.             Thread.sleep(0, 1);
  37.         } catch (InterruptedException e) {
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.    
  42.     public void pulsePin(int pin) {
  43.         setPin(pin, LOW);
  44.         setPin(pin, HIGH);
  45.     }
  46.  
  47.     public void setPort(int value) {
  48.         sp.writeBytes(new byte[] { (byte) value }, 1);
  49.     }
  50.  
  51.     public void run() throws InterruptedException {
  52.         byte[] valarr = {
  53.                 (byte) 0b11001001, 0, 1, 0x0F, (byte) 0b10101010, (byte) 0xFF, 0x55
  54.         };
  55.        
  56.         while(true) {
  57.             while (!sp.isOpen()) {
  58.                 sp.openPort();
  59.                 Thread.sleep(100);
  60.             }
  61.             for(byte val : valarr) {
  62.                 pulsePin(SRCLR);       
  63.                 for(int i = 0; i < 8; i++) {
  64.                     setPin(SERIN, val & (1 << i));
  65.                     pulsePin(SRCK);
  66.                 }
  67.                 pulsePin(RCK);
  68.                 Thread.sleep(500);
  69.             }
  70.         }
  71.        
  72.  
  73.     }
  74.  
  75.     public void demo1() throws InterruptedException {
  76.         int p = 0;
  77.         while (true) {
  78.             setPort(1 << p);
  79.             p++;
  80.             if (p == 5)
  81.                 p = 0;
  82.             Thread.sleep(500);
  83.         }
  84.     }
  85.  
  86.     public void demo2() throws InterruptedException {
  87.         int p = 0;
  88.         boolean direction = true;
  89.         while (true) {
  90.             setPort(1 << p);
  91.             if (p < 5 && direction)
  92.                 p++;
  93.             else {
  94.                 direction = false;
  95.                 p--;
  96.             }
  97.             if (p == 0 && !direction)
  98.                 direction = true;
  99.             p++;
  100.             Thread.sleep(500);
  101.         }
  102.     }
  103. }
  104.  
  105. _________________________________-
  106. void setup() {
  107.   // http://www.stm32duino.com/viewtopic.php?t=1130
  108.   afio_cfg_debug_ports(AFIO_DEBUG_SW_ONLY); // release PB3 and PB5
  109.   Serial.begin(); // baudrate doesn't matter with USB
  110.   pinMode(PA0, OUTPUT);
  111.   pinMode(PA1, OUTPUT);
  112.   pinMode(PA2, OUTPUT);
  113.   pinMode(PA3, OUTPUT);
  114.   pinMode(PA4, OUTPUT);
  115.   pinMode(PA5, OUTPUT);
  116.   pinMode(PA6, OUTPUT);
  117.   pinMode(PA7, OUTPUT);
  118. }
  119.  
  120.  
  121. void loop() {  
  122.   if(Serial.available()) {
  123.     byte received = Serial.read();
  124.     GPIOA->regs->BRR = ~received;
  125.     GPIOA->regs->BSRR = received;
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment