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.

Committer:
aktk
Date:
Wed Nov 28 06:43:18 2018 +0000
Branch:
document_branch
Revision:
6:96df8bca04c4
Parent:
0:26eea8042420
neko

Who changed what in which revision?

UserRevisionLine numberNew contents of line
aktk 0:26eea8042420 1 /**
aktk 0:26eea8042420 2 * \file "KajiLabHV507B.cpp"
aktk 0:26eea8042420 3 * \Author Akifumi TAKAHASHI
aktk 0:26eea8042420 4 */
aktk 0:26eea8042420 5
aktk 0:26eea8042420 6 #include "KajiLabHV507B.h"
aktk 0:26eea8042420 7
aktk 0:26eea8042420 8 HV507::HV507(const int arg_num_board):
aktk 0:26eea8042420 9 m_DIOB(p14), // Pin to transfer Data input to HV507
aktk 0:26eea8042420 10 m_BL(p13), // Pin to transfer signal -BL(Blaker)
aktk 0:26eea8042420 11 m_POL(p12), // Pin to transfer signal POL
aktk 0:26eea8042420 12 m_CLK(p11), // Pin to transfer signal Clock
aktk 0:26eea8042420 13 m_LE(p10), // Pin to transfer signal -LE(Latch Enable)
aktk 0:26eea8042420 14 m_num_pins(arg_num_board * HV507_NUM_PINS)
aktk 0:26eea8042420 15 {
aktk 0:26eea8042420 16 init();
aktk 0:26eea8042420 17 }
aktk 0:26eea8042420 18
aktk 0:26eea8042420 19 void HV507::DEBUG_ALLPIN_ON()
aktk 0:26eea8042420 20 {
aktk 0:26eea8042420 21 m_DIOB = 1;
aktk 0:26eea8042420 22 m_BL = 1;
aktk 0:26eea8042420 23 m_POL = 1;
aktk 0:26eea8042420 24 m_CLK = 1;
aktk 0:26eea8042420 25 m_LE = 1;
aktk 0:26eea8042420 26 }
aktk 0:26eea8042420 27 void HV507::DEBUG_ALLPIN_OFF()
aktk 0:26eea8042420 28 {
aktk 0:26eea8042420 29 m_DIOB = 0;
aktk 0:26eea8042420 30 m_BL = 0;
aktk 0:26eea8042420 31 m_POL = 0;
aktk 0:26eea8042420 32 m_CLK = 0;
aktk 0:26eea8042420 33 m_LE = 0;
aktk 0:26eea8042420 34 }
aktk 0:26eea8042420 35
aktk 0:26eea8042420 36 //HV507 init
aktk 0:26eea8042420 37 void HV507::init() // argument: the number of Hv507. #aktk150715
aktk 0:26eea8042420 38 {
aktk 0:26eea8042420 39
aktk 0:26eea8042420 40 // First, Rapidly Temporarily Make All Outputs Off (Low)
aktk 0:26eea8042420 41 m_POL = 1; // data in latches is reflected to the output as is (normal mode).
aktk 0:26eea8042420 42 m_BL = 0; // L == ALL OFF (ShiftReg not cahnges, HVouts be all Low, DATAout not changes)
aktk 0:26eea8042420 43 m_LE = 0; // Close the latches
aktk 0:26eea8042420 44
aktk 0:26eea8042420 45 m_DIOB = 0; // input data = L
aktk 0:26eea8042420 46 for(int pin = 0; pin < m_num_pins; pin++) putSRCLKahead();
aktk 0:26eea8042420 47 m_pos_stim = 0;
aktk 0:26eea8042420 48
aktk 0:26eea8042420 49 updateLatches();
aktk 0:26eea8042420 50
aktk 0:26eea8042420 51 m_BL = 1;
aktk 0:26eea8042420 52 }
aktk 0:26eea8042420 53
aktk 0:26eea8042420 54 void HV507::shiftSRBits_by(int arg_num_shifting)
aktk 0:26eea8042420 55 {
aktk 0:26eea8042420 56 // Load S/R Mode ON
aktk 0:26eea8042420 57 m_BL = 1;
aktk 0:26eea8042420 58 m_LE = 0;
aktk 0:26eea8042420 59
aktk 0:26eea8042420 60 // Fill 0 into ShitRegisters before supposing node to Shift 1 to the node.
aktk 0:26eea8042420 61 m_DIOB = 0;
aktk 0:26eea8042420 62 for(int itr = 0; itr < arg_num_shifting; itr++) putSRCLKahead();
aktk 0:26eea8042420 63
aktk 0:26eea8042420 64 m_pos_stim += arg_num_shifting;
aktk 0:26eea8042420 65 if(m_pos_stim > m_num_pins) m_pos_stim = 0;
aktk 0:26eea8042420 66 }
aktk 0:26eea8042420 67
aktk 0:26eea8042420 68 void HV507::setHtoSR()
aktk 0:26eea8042420 69 {
aktk 0:26eea8042420 70 m_BL = 1;
aktk 0:26eea8042420 71 m_LE = 0;
aktk 0:26eea8042420 72
aktk 0:26eea8042420 73 m_DIOB = 1;
aktk 0:26eea8042420 74 putSRCLKahead();
aktk 0:26eea8042420 75 m_pos_stim = 1;
aktk 0:26eea8042420 76 }
aktk 0:26eea8042420 77
aktk 0:26eea8042420 78 void HV507::setLtoSR()
aktk 0:26eea8042420 79 {
aktk 0:26eea8042420 80 m_BL = 1;
aktk 0:26eea8042420 81 m_LE = 0;
aktk 0:26eea8042420 82
aktk 0:26eea8042420 83 m_DIOB = 0;
aktk 0:26eea8042420 84 putSRCLKahead();
aktk 0:26eea8042420 85 m_pos_stim >= m_num_pins? m_pos_stim = 0 : m_pos_stim++;
aktk 0:26eea8042420 86
aktk 0:26eea8042420 87 }
aktk 0:26eea8042420 88
aktk 0:26eea8042420 89 void HV507::putSRCLKahead()
aktk 0:26eea8042420 90 {
aktk 0:26eea8042420 91 m_CLK = 0; // Clock down(L)
aktk 0:26eea8042420 92 m_CLK = 0; //
aktk 0:26eea8042420 93 // Load a datum to the SR(s) when clock rising
aktk 0:26eea8042420 94 m_CLK = 1; // Clock up(H)
aktk 0:26eea8042420 95 m_CLK = 1; // (twice substitution is for mbed faster than HV507.
aktk 0:26eea8042420 96 }
aktk 0:26eea8042420 97
aktk 0:26eea8042420 98
aktk 0:26eea8042420 99
aktk 0:26eea8042420 100 // Fill out 0 into all ShiftRegs.
aktk 0:26eea8042420 101 void HV507::clearSR()
aktk 0:26eea8042420 102 {
aktk 0:26eea8042420 103 if(m_pos_stim != 0) shiftSRBits_by(m_num_pins + 1 - m_pos_stim);
aktk 0:26eea8042420 104 }
aktk 0:26eea8042420 105
aktk 0:26eea8042420 106 void HV507::setCh(const int arg_ch)
aktk 0:26eea8042420 107 {
aktk 0:26eea8042420 108 if(arg_ch <= 0 || m_num_pins < arg_ch)
aktk 0:26eea8042420 109 clearSR();
aktk 0:26eea8042420 110 else {
aktk 0:26eea8042420 111 if(arg_ch < m_pos_stim){
aktk 0:26eea8042420 112 clearSR();
aktk 0:26eea8042420 113 setHtoSR();
aktk 0:26eea8042420 114 shiftSRBits_by(arg_ch - 1);
aktk 0:26eea8042420 115 }
aktk 0:26eea8042420 116 else
aktk 0:26eea8042420 117 shiftSRBits_by(arg_ch - m_pos_stim);
aktk 0:26eea8042420 118 }
aktk 0:26eea8042420 119 updateLatches();
aktk 0:26eea8042420 120 }
aktk 0:26eea8042420 121
aktk 0:26eea8042420 122 void HV507::updateLatches()
aktk 0:26eea8042420 123 {
aktk 0:26eea8042420 124 // Make data to be stored in Latches
aktk 0:26eea8042420 125 // Reflect the data in SR(s) to Latches
aktk 0:26eea8042420 126 m_LE = 1;
aktk 0:26eea8042420 127 m_LE = 1;
aktk 0:26eea8042420 128 // Close latch and store the data in it
aktk 0:26eea8042420 129 m_LE = 0;
aktk 0:26eea8042420 130 m_LE = 0;
aktk 0:26eea8042420 131 }
aktk 0:26eea8042420 132
aktk 0:26eea8042420 133
aktk 0:26eea8042420 134 /*--------------------------------------------------------
aktk 0:26eea8042420 135 Example Main Functin For Simple Comunication
aktk 0:26eea8042420 136 ----------------------------------------------------------*/
aktk 0:26eea8042420 137 /*
aktk 0:26eea8042420 138 DigitalOut dout(p14); //trigger signal for check
aktk 0:26eea8042420 139 Serial pc(USBTX, USBRX); // tx, rx
aktk 0:26eea8042420 140 AnalogIn volume(p15); //volume
aktk 0:26eea8042420 141 #define HV507_NUM 1
aktk 0:26eea8042420 142 #define ELECTRODE_NUM 61
aktk 0:26eea8042420 143 #define PC_MBED_STIM_PATTERN 0xFF
aktk 0:26eea8042420 144 #define PC_MBED_MEASURE_REQUEST 0xFE
aktk 0:26eea8042420 145 #define MBED_PC_MEASURE_RESULT 0xFF
aktk 0:26eea8042420 146
aktk 0:26eea8042420 147 int main()
aktk 0:26eea8042420 148 {
aktk 0:26eea8042420 149 int pin;
aktk 0:26eea8042420 150 short PulseHeight, AD;
aktk 0:26eea8042420 151 unsigned char stim_pattern[ELECTRODE_NUM]={0},uc;
aktk 0:26eea8042420 152 unsigned char impedance[ELECTRODE_NUM]={0};
aktk 0:26eea8042420 153
aktk 0:26eea8042420 154 pc.baud(921600);
aktk 0:26eea8042420 155 DAADinit();
aktk 0:26eea8042420 156 hv507Init(1); //initialize one HV507
aktk 0:26eea8042420 157
aktk 0:26eea8042420 158 while (1) {
aktk 0:26eea8042420 159 //pc.printf("%d\n", (int)PulseHeight);
aktk 0:26eea8042420 160 if (pc.readable()) { //check serial buffer
aktk 0:26eea8042420 161 uc=pc.getc();
aktk 0:26eea8042420 162 if (uc == PC_MBED_STIM_PATTERN) {//if PC requires stimulation
aktk 0:26eea8042420 163 for (pin=0; pin<ELECTRODE_NUM; pin++) {//read data
aktk 0:26eea8042420 164 stim_pattern[pin] =pc.getc();
aktk 0:26eea8042420 165 if(stim_pattern[pin]>100)stim_pattern[pin]=100; //limit
aktk 0:26eea8042420 166 }
aktk 0:26eea8042420 167 PulseHeight = (short) (volume*1024.0); //0-5mA
aktk 0:26eea8042420 168 hv507FastScan(0);
aktk 0:26eea8042420 169 for (pin=0; pin<ELECTRODE_NUM; pin++) {
aktk 0:26eea8042420 170 if(stim_pattern[pin]!=0) {
aktk 0:26eea8042420 171 if(pin!=0) {
aktk 0:26eea8042420 172 hv507FastScan(pin);
aktk 0:26eea8042420 173 }
aktk 0:26eea8042420 174 m_BL = 1;
aktk 0:26eea8042420 175 m_LE = 1;
aktk 0:26eea8042420 176 m_LE = 0;
aktk 0:26eea8042420 177
aktk 0:26eea8042420 178 //Cathodic Stimulation
aktk 0:26eea8042420 179 //m_POL = 0;
aktk 0:26eea8042420 180
aktk 0:26eea8042420 181 //50us stimulation
aktk 0:26eea8042420 182 AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
aktk 0:26eea8042420 183 wait_us(stim_pattern[pin]); //0-100us
aktk 0:26eea8042420 184 AD = DAAD(0);
aktk 0:26eea8042420 185 m_BL = 0;
aktk 0:26eea8042420 186 wait_us(100-stim_pattern[pin]); //wait
aktk 0:26eea8042420 187 }
aktk 0:26eea8042420 188 }
aktk 0:26eea8042420 189 hv507Clear(HV507_NUM); //cleaning
aktk 0:26eea8042420 190 m_LE = 1;
aktk 0:26eea8042420 191 m_LE = 0;
aktk 0:26eea8042420 192 m_BL = 0;
aktk 0:26eea8042420 193 }else if (uc == PC_MBED_MEASURE_REQUEST) {//if PC requires stimulation
aktk 0:26eea8042420 194 PulseHeight = 100.0; //0.5mA
aktk 0:26eea8042420 195 hv507FastScan(0);
aktk 0:26eea8042420 196 //50us stimulation
aktk 0:26eea8042420 197 m_BL = 1;
aktk 0:26eea8042420 198 m_LE = 1;
aktk 0:26eea8042420 199 m_LE = 0;
aktk 0:26eea8042420 200 AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
aktk 0:26eea8042420 201 wait_us(30);
aktk 0:26eea8042420 202 AD = DAAD(0);
aktk 0:26eea8042420 203 m_BL = 0;
aktk 0:26eea8042420 204 wait_us(70);
aktk 0:26eea8042420 205 for (pin=0; pin<ELECTRODE_NUM; pin++) {
aktk 0:26eea8042420 206 if(pin!=0) {
aktk 0:26eea8042420 207 hv507FastScan(pin);
aktk 0:26eea8042420 208 }
aktk 0:26eea8042420 209 m_BL = 1;
aktk 0:26eea8042420 210 m_LE = 1;
aktk 0:26eea8042420 211 m_LE = 0;
aktk 0:26eea8042420 212
aktk 0:26eea8042420 213 //50us stimulation
aktk 0:26eea8042420 214 AD = DAAD(PulseHeight); //0-5mA. Simultaneous DA and AD. 2.0us
aktk 0:26eea8042420 215 wait_us(30);
aktk 0:26eea8042420 216 AD = DAAD(0);
aktk 0:26eea8042420 217 impedance[pin]=AD>>3;
aktk 0:26eea8042420 218 if(impedance[pin]>200)impedance[pin]=200;
aktk 0:26eea8042420 219 m_BL = 0;
aktk 0:26eea8042420 220 wait_us(70);
aktk 0:26eea8042420 221 }
aktk 0:26eea8042420 222 hv507Clear(HV507_NUM); //cleaning
aktk 0:26eea8042420 223 m_LE = 1;
aktk 0:26eea8042420 224 m_LE = 0;
aktk 0:26eea8042420 225 m_BL = 0;
aktk 0:26eea8042420 226 pc.putc(MBED_PC_MEASURE_RESULT);
aktk 0:26eea8042420 227 for (pin=0; pin<ELECTRODE_NUM; pin++) {
aktk 0:26eea8042420 228 pc.putc(impedance[pin]);
aktk 0:26eea8042420 229 }
aktk 0:26eea8042420 230 }
aktk 0:26eea8042420 231 }
aktk 0:26eea8042420 232 }
aktk 0:26eea8042420 233 }
aktk 0:26eea8042420 234 */