This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Arduino DSC PC1500RK Interface

By: caitsith2 on Jul 27th, 2012  |  syntax: C++  |  size: 5.06 KB  |  views: 1,960  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.         The code is to interface a DSC PC1500RK alarm keypad.  
  3.         It has 15 keys, 11 LEDs, and a Beeper that can be controlled.  
  4.         This code cycles through all the LEDs in a top-down fashion.  
  5.         The beeper is used to acknowledge that a key has been pressed.  
  6.         If the key was pressed successfully, its character is output on the serial line, at 9600 bps.
  7.  
  8.         I was originally communicating at 115200, but due to the read speed, and also the tendency to
  9.         quickly overfill the read buffer if lots of commands are sent at once, I slowed it down to 9600.
  10.         It is still possible to overfill the buffer, but that is mainly if you sends tons of flag
  11.         altering commands, or if you send a bunch of beep/delay commands all at once.  
  12.         The general thing, is Hurry up and wait. :).
  13.                 (In other words, maintain a slower pace whenever possible.)
  14.  
  15.         The fastest that this keypad can be read out, is ~30 times a second.  I tried doing 500
  16.         microsecond delays between clock cycles, and things started becoming unresponsive.
  17.        
  18.         Wiring is simple.  Red is Vcc, hooked to 5V
  19.         Black is Gnd.
  20.         Yellow is the Clock line. In this configuration, it is on Digital Pin 3.
  21.         Green is Data, and is on Digital Pin 2.
  22.        
  23.         Beeper          0x0001
  24.         unknown         0x0002
  25.         unknown         0x0004
  26.         Trouble LED     0x0008
  27.         By-Pass LED     0x0010
  28.         Memory LED      0x0020
  29.         Armed LED       0x0040
  30.         Ready LED       0x0080
  31.         unknown         0x0100
  32.         unknown         0x0200
  33.         Zone 6 LED      0x0400
  34.         Zone 5 LED      0x0800
  35.         Zone 4 LED      0x1000
  36.         Zone 3 LED      0x2000
  37.         Zone 2 LED      0x4000
  38.         Zone 1 LED      0x8000
  39.          
  40.         Matrix output is in the format of RCCCRRRR.
  41.         Matrix Output   Key             Output ^ 0xFF
  42.         11111111        -Nothing        0x00
  43.         10111110        1               0x41
  44.         11011110        2               0x21
  45.         11101110        3               0x11
  46.         10111101        4               0x42
  47.         11011101        5               0x22
  48.         11101101        6               0x12
  49.         10111011        7               0x44
  50.         11011011        8               0x24
  51.         11101011        9               0x14
  52.         10110111        *               0x48
  53.         11010111        0               0x28
  54.         11100111        #               0x18
  55.         00111111        F               0xC0
  56.         01011111        E               0xA0
  57.         01101111        P               0x90
  58. */
  59.  
  60. #define CLOCKPIN 3
  61. #define DATAPIN 2
  62.  
  63. #define NO_FLAGS 0x0000
  64. #define BUZZER 0x0001
  65. #define TROUBLE 0x0008
  66. #define BYPASS 0x0010
  67. #define MEMORY 0x0020
  68. #define ARMED 0x0040
  69. #define READY 0x0080
  70. #define ZONE_1 0x8000
  71. #define ZONE_2 0x4000
  72. #define ZONE_3 0x2000
  73. #define ZONE_4 0x1000
  74. #define ZONE_5 0x0800
  75. #define ZONE_6 0x0400
  76.  
  77. #define NO_KEY_PRESSED 0
  78.  
  79.  
  80. void setup() {
  81.   //Pin 2 is Data, and is bidirectional.
  82.   //Pin 3 is Clock, and is an output.
  83.   pinMode(DATAPIN, OUTPUT);
  84.   pinMode(CLOCKPIN, OUTPUT);
  85.   Serial.begin(9600);    //Any faster, and we run the risk of doing hurry up and wait.
  86. }
  87.  
  88. int readdata(int control)
  89. {
  90.  
  91.   int i,j=0,k=control;
  92.   int bitcount=0;
  93.  
  94.   for(i=0;i<8;i++)
  95.   {
  96.     j<<=1;
  97.     digitalWrite(DATAPIN,HIGH);
  98.     digitalWrite(CLOCKPIN,LOW);
  99.     delayMicroseconds(625);
  100.     //delay(1);
  101.     if(digitalRead(DATAPIN)==LOW)
  102.       j|=1;
  103.     digitalWrite(CLOCKPIN,HIGH);
  104.     delayMicroseconds(625);
  105.     //delay(1);
  106.   }
  107.   for(i=0;i<16;i++)
  108.   {
  109.     if(k&0x8000)
  110.       digitalWrite(DATAPIN,HIGH);
  111.     else
  112.       digitalWrite(DATAPIN,LOW);
  113.     digitalWrite(CLOCKPIN,LOW);
  114.     delayMicroseconds(625);
  115.     //delay(1);
  116.     digitalWrite(CLOCKPIN,HIGH);
  117.     delayMicroseconds(625);
  118.     //delay(1);
  119.     k<<=1;
  120.   }
  121.   if(j==0x41) return '1';
  122.   if(j==0x42) return '4';
  123.   if(j==0x44) return '7';
  124.   if(j==0x48) return '*';
  125.   if(j==0x21) return '2';
  126.   if(j==0x22) return '5';
  127.   if(j==0x24) return '8';
  128.   if(j==0x28) return '0';
  129.   if(j==0x11) return '3';
  130.   if(j==0x12) return '6';
  131.   if(j==0x14) return '9';
  132.   if(j==0x18) return '#';
  133.   if(j==0x90) return 'P';
  134.   if(j==0xA0) return 'E';
  135.   if(j==0xC0) return 'F';
  136.   return 0;
  137. }
  138.  
  139. void loop() {
  140.   int i;
  141.  
  142.  
  143.   static int last_key_pressed=0,key_just_pressed;
  144.   static unsigned int flags=0x80;
  145.   static unsigned int buzzer_counter=0;
  146.   int breakloop=0;
  147.  
  148.   while(Serial.available() && (breakloop==0))
  149.   {
  150.     i=Serial.read();
  151.     if((i>='1')&&(i<='6'))
  152.       flags ^= (ZONE_1 >> (i-'1'));
  153.      
  154.     if((i=='r')||(i=='R'))
  155.       flags ^= READY;
  156.     if((i=='a')||(i=='A'))
  157.       flags ^= ARMED;
  158.     if((i=='m')||(i=='M'))
  159.       flags ^= MEMORY;
  160.     if((i=='b')||(i=='B'))
  161.       flags ^= BYPASS;
  162.     if((i=='t')||(i=='T'))
  163.       flags ^= TROUBLE;
  164.     if(i=='S')
  165.     {
  166.       flags |= BUZZER;
  167.       buzzer_counter+=16;
  168.       break;
  169.     }
  170.     if(i=='s')
  171.     {
  172.       flags |= BUZZER;
  173.       buzzer_counter++;
  174.       break;
  175.     }
  176.     if(i==' ')
  177.       break;
  178.   }
  179.   if(buzzer_counter)
  180.     buzzer_counter--;
  181.   else
  182.     flags &= ~BUZZER;
  183.  
  184.   i=readdata(flags ^ key_just_pressed);
  185.  
  186.  
  187.   if(last_key_pressed==NO_KEY_PRESSED)
  188.   {
  189.     if(i != NO_KEY_PRESSED)
  190.       Serial.print((char)(i));
  191.   }
  192.   if((last_key_pressed!=i)&&(last_key_pressed==NO_KEY_PRESSED))
  193.     key_just_pressed=BUZZER;
  194.   else
  195.     key_just_pressed=NO_FLAGS;
  196.   last_key_pressed=i;
  197.  
  198.   delayMicroseconds(3333);
  199. }
clone this paste RAW Paste Data