Advertisement
Guest User

Shift register sketch

a guest
Jan 30th, 2015
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.29 KB | None | 0 0
  1. #include <DcsBios.h>
  2. #include <Servo.h>
  3.  
  4.  
  5. //DcsBios::RotaryEncoderSR vhfamFreq1("VHFAM_FREQ1", "DEC", "INC", 2, 3);
  6. //const byte ahcpAltScePins[3] = {0, 1, 2};
  7. //DcsBios::SwitchMultiPosSR ahcpAltSce("AHCP_ALT_SCE", ahcpAltScePins, 3);
  8. //DcsBios::ActionButton ahcpHudModeToggle("AHCP_HUD_MODE", "TOGGLE", 44);
  9. //DcsBios::Switch2PosSR ahcpCicu("AHCP_CICU", 1);
  10.  
  11.  
  12. int ploadPin        = 2;  // Connects to Parallel load pin the 165
  13. int dataPin         = 3; // Connects to the Q7 pin the 165
  14. int clockPin        = 4; // Connects to the Clock pin the 165
  15.  
  16.  
  17. unsigned int pinValues = read_shift_regs();
  18. unsigned int oldPinValues;
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26. unsigned int read_shift_regs()
  27. {
  28.     byte bitVal;
  29.     unsigned int bytesVal = 0;
  30.  
  31.     /* Trigger a parallel Load to latch the state of the data lines,
  32.     */
  33.     digitalWrite(ploadPin, LOW);
  34.     delayMicroseconds(5);
  35.     digitalWrite(ploadPin, HIGH);
  36.  
  37.     /* Loop to read each bit value from the serial out line
  38.      * of the SN74HC165N.
  39.     */
  40.     for(int i = 0; i < 8; i++)
  41.     {
  42.         bitVal = digitalRead(dataPin);
  43.  
  44.         /* Set the corresponding bit in bytesVal.
  45.         */
  46.         bytesVal |= (bitVal << ((8-1) - i));
  47.  
  48.         /* Pulse the Clock (rising edge shifts the next bit).
  49.         */
  50.         digitalWrite(clockPin, HIGH);
  51.         delayMicroseconds(5);
  52.         digitalWrite(clockPin, LOW);
  53.     }
  54.  
  55.     return(bytesVal);
  56. }
  57.  
  58.  
  59. DcsBios::ProtocolParser parser;
  60.  
  61. void setup() {
  62.   Serial.begin(500000);
  63.  
  64. /* Initialize our digital pins...
  65.     */
  66.     pinMode(ploadPin, OUTPUT);
  67.     pinMode(clockPin, OUTPUT);
  68.     pinMode(dataPin, INPUT);
  69.  
  70.     digitalWrite(clockPin, LOW);
  71.     digitalWrite(ploadPin, HIGH);
  72.  
  73.     /* Read in and display the pin states at startup.
  74.     */
  75.     pinValues = read_shift_regs();
  76.    
  77.     oldPinValues = pinValues;
  78.    
  79.    
  80. }
  81. /*
  82. Your main loop needs to pass data from the DCS-BIOS export
  83. stream to the parser object you instantiated above.
  84.  
  85. It also needs to call DcsBios::PollingInput::pollInputs()
  86. to detect changes in the state of connected controls and
  87. pass them on to DCS.
  88. */
  89. void loop() {
  90.  
  91.   // feed incoming data to the parser
  92.   while (Serial.available()) {
  93.       parser.processChar(Serial.read());
  94.   }
  95.     // poll inputs
  96.   DcsBios::PollingInput::pollInputs();
  97.  
  98.  
  99.    pinValues = read_shift_regs();
  100.    oldPinValues = pinValues;
  101.    
  102.  
  103.    
  104.    
  105.    
  106.    
  107.    
  108.     }
  109.    
  110. /*
  111. You need to define
  112. void sendDcsBiosMessage(const char* msg, const char* arg)
  113. so that the string msg, followed by a space, the string arg
  114. and a newline gets sent to the DCS-BIOS import stream.
  115.  
  116. In this example we send it to the serial port, so you need to
  117. run socat to read the data from the serial port and send it
  118. over UDP to DCS-BIOS.
  119.  
  120. If you are using an Ethernet Shield, you would probably want
  121. to send a UDP packet from this subroutine.
  122. */
  123. void sendDcsBiosMessage(const char* msg, const char* arg) {
  124.   Serial.write(msg);
  125.   Serial.write(' ');
  126.   Serial.write(arg);
  127.   Serial.write('\n');
  128. }
  129.  
  130. /*
  131. This subroutine gets called every time a message is received
  132. from the export stream (you need to define it even if it
  133. does nothing).
  134.  
  135. Use this to handle outputs which are not covered by the
  136. DcsBios Arduino library (e.g. displays).
  137. */
  138. void onDcsBiosWrite(unsigned int address, unsigned int value) {
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement