This is a library to use HV507 boards as a switching circuitry which is a module used with an electrical stimulator made by kaji-lab.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers KajiLabHV507B.cpp Source File

KajiLabHV507B.cpp

Go to the documentation of this file.
00001 /**
00002  *  \file   "KajiLabHV507B.cpp"
00003  *  \Author Akifumi TAKAHASHI
00004  */
00005 
00006 #include "KajiLabHV507B.h"
00007 
00008 HV507::HV507(const int arg_num_board):
00009     m_DIOB(p14),    //  Pin to transfer Data input to HV507
00010     m_BL(p13),      //  Pin to transfer signal -BL(Blaker)
00011     m_POL(p12),     //  Pin to transfer signal POL
00012     m_CLK(p11),     //  Pin to transfer signal Clock
00013     m_LE(p10),      //  Pin to transfer signal -LE(Latch Enable)
00014     m_num_pins(arg_num_board * HV507_NUM_PINS)
00015 {
00016     init();
00017 }
00018 
00019 void HV507::DEBUG_ALLPIN_ON()
00020 {
00021     m_DIOB  = 1;
00022     m_BL    = 1;
00023     m_POL   = 1;
00024     m_CLK   = 1;
00025     m_LE    = 1;
00026 }
00027 void HV507::DEBUG_ALLPIN_OFF()
00028 {
00029     m_DIOB  = 0;
00030     m_BL    = 0;
00031     m_POL   = 0;
00032     m_CLK   = 0;
00033     m_LE    = 0;
00034 }
00035 
00036 //HV507 init
00037 void HV507::init()   // argument: the number of Hv507. #aktk150715
00038 {
00039 
00040     //  First, Rapidly Temporarily Make All Outputs Off (Low)
00041     m_POL = 1;  //  data in latches is reflected to the output as is (normal mode).
00042     m_BL  = 0;  //  L == ALL OFF (ShiftReg not cahnges, HVouts be all Low, DATAout not changes)
00043     m_LE  = 0;  //  Close the latches
00044 
00045     m_DIOB = 0; //  input data = L
00046     for(int pin = 0; pin < m_num_pins; pin++) putSRCLKahead();
00047     m_pos_stim = 0;
00048     
00049     updateLatches();
00050 
00051     m_BL = 1;
00052 }
00053 
00054 void HV507::shiftSRBits_by(int arg_num_shifting)
00055 {
00056     //  Load S/R Mode ON
00057     m_BL = 1;
00058     m_LE = 0;
00059 
00060     //  Fill 0 into ShitRegisters before supposing node to Shift 1 to the node.
00061     m_DIOB = 0;
00062     for(int itr = 0; itr < arg_num_shifting; itr++) putSRCLKahead();
00063     
00064     m_pos_stim += arg_num_shifting; 
00065     if(m_pos_stim > m_num_pins) m_pos_stim = 0;
00066 }
00067 
00068 void HV507::setHtoSR()
00069 {
00070     m_BL = 1;
00071     m_LE = 0;
00072     
00073     m_DIOB = 1;
00074     putSRCLKahead();
00075     m_pos_stim = 1;
00076 }
00077 
00078 void HV507::setLtoSR()
00079 {
00080     m_BL = 1;
00081     m_LE = 0;
00082     
00083     m_DIOB = 0;
00084     putSRCLKahead();
00085     m_pos_stim >= m_num_pins? m_pos_stim = 0 : m_pos_stim++;
00086     
00087 }
00088 
00089 void HV507::putSRCLKahead()
00090 {
00091     m_CLK = 0;  //  Clock down(L)
00092     m_CLK = 0;  //
00093     //  Load a datum to the SR(s) when clock rising
00094     m_CLK = 1;  //  Clock up(H)
00095     m_CLK = 1;  //  (twice substitution is for mbed faster than HV507.
00096 }
00097 
00098 
00099 
00100 //  Fill out 0 into all ShiftRegs.
00101 void HV507::clearSR()
00102 {
00103     if(m_pos_stim != 0) shiftSRBits_by(m_num_pins + 1 - m_pos_stim);
00104 }
00105 
00106 void HV507::setCh(const int arg_ch)
00107 {
00108     if(arg_ch <= 0 || m_num_pins < arg_ch)
00109         clearSR();
00110     else {
00111         if(arg_ch < m_pos_stim){
00112             clearSR();
00113             setHtoSR();
00114             shiftSRBits_by(arg_ch - 1);
00115         }
00116         else
00117             shiftSRBits_by(arg_ch - m_pos_stim);
00118     }
00119     updateLatches();
00120 }
00121 
00122 void HV507::updateLatches()
00123 {
00124     //  Make data to be stored in Latches
00125     //  Reflect the data in SR(s) to Latches
00126     m_LE = 1;   
00127     m_LE = 1;
00128     //  Close latch and store the data in it
00129     m_LE = 0;   
00130     m_LE = 0;
00131 }
00132 
00133 
00134 /*--------------------------------------------------------
00135     Example Main Functin For Simple Comunication
00136 ----------------------------------------------------------*/
00137 /*
00138 DigitalOut dout(p14); //trigger signal for check
00139 Serial pc(USBTX, USBRX); // tx, rx
00140 AnalogIn volume(p15); //volume
00141 #define HV507_NUM 1
00142 #define ELECTRODE_NUM 61
00143 #define PC_MBED_STIM_PATTERN 0xFF
00144 #define PC_MBED_MEASURE_REQUEST 0xFE
00145 #define MBED_PC_MEASURE_RESULT 0xFF
00146 
00147 int main()
00148 {
00149     int pin;
00150     short PulseHeight, AD;
00151     unsigned char stim_pattern[ELECTRODE_NUM]={0},uc;
00152     unsigned char impedance[ELECTRODE_NUM]={0};
00153 
00154     pc.baud(921600);
00155     DAADinit();
00156     hv507Init(1); //initialize one HV507
00157 
00158     while (1) {
00159         //pc.printf("%d\n", (int)PulseHeight);
00160         if (pc.readable()) { //check serial buffer
00161             uc=pc.getc();
00162             if (uc == PC_MBED_STIM_PATTERN) {//if PC requires stimulation
00163                 for (pin=0; pin<ELECTRODE_NUM; pin++) {//read data
00164                     stim_pattern[pin] =pc.getc();
00165                     if(stim_pattern[pin]>100)stim_pattern[pin]=100; //limit
00166                 }
00167                 PulseHeight = (short) (volume*1024.0); //0-5mA
00168                 hv507FastScan(0);
00169                 for (pin=0; pin<ELECTRODE_NUM; pin++) {
00170                     if(stim_pattern[pin]!=0) {
00171                         if(pin!=0) {
00172                             hv507FastScan(pin);
00173                         }
00174                         m_BL = 1;
00175                         m_LE = 1;
00176                         m_LE = 0;
00177 
00178                         //Cathodic Stimulation
00179                         //m_POL = 0;
00180 
00181                         //50us stimulation
00182                         AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
00183                         wait_us(stim_pattern[pin]); //0-100us
00184                         AD = DAAD(0);
00185                         m_BL = 0;
00186                         wait_us(100-stim_pattern[pin]); //wait
00187                     }
00188                 }
00189                 hv507Clear(HV507_NUM);    //cleaning
00190                 m_LE = 1;
00191                 m_LE = 0;
00192                 m_BL = 0;
00193             }else if (uc == PC_MBED_MEASURE_REQUEST) {//if PC requires stimulation
00194                 PulseHeight = 100.0; //0.5mA
00195                 hv507FastScan(0);
00196                 //50us stimulation
00197                 m_BL = 1;
00198                 m_LE = 1;
00199                 m_LE = 0;
00200                 AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
00201                 wait_us(30);
00202                 AD = DAAD(0);
00203                 m_BL = 0;
00204                 wait_us(70);
00205                 for (pin=0; pin<ELECTRODE_NUM; pin++) {
00206                         if(pin!=0) {
00207                             hv507FastScan(pin);
00208                         }
00209                         m_BL = 1;
00210                         m_LE = 1;
00211                         m_LE = 0;
00212 
00213                         //50us stimulation
00214                         AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
00215                         wait_us(30);
00216                         AD = DAAD(0);
00217                         impedance[pin]=AD>>3;
00218                         if(impedance[pin]>200)impedance[pin]=200;
00219                         m_BL = 0;
00220                         wait_us(70);
00221                 }
00222                 hv507Clear(HV507_NUM);    //cleaning
00223                 m_LE = 1;
00224                 m_LE = 0;
00225                 m_BL = 0;
00226                 pc.putc(MBED_PC_MEASURE_RESULT);
00227                 for (pin=0; pin<ELECTRODE_NUM; pin++) {
00228                     pc.putc(impedance[pin]);
00229                 }
00230             }
00231         }
00232     }
00233 }
00234 */