Trying to duplicate/understand this switch circuit(SOLVED) - Page 1
Forum section: electronics / beginners

author: jerryk
author_rank: Regular Contributor
timestamp: January 09, 2024, 09:22:16 pm
content: I have a two identical BMS boards that have an override switch circuit per schematic in the first attachment. The only difference is the microcontroller on the functioning BMS is factory programmed and the other board has a newmicrocontrollerthat I'm trying to program to duplicate the factory original. I do not understand how the outputs on both PB1 and PC5 are configured from the factory nor to I get the why part of using the two signals together to create the combined stepped PWM? output in the second attachment. I removed R97 and scoped the output of PB1 and PB5 (third and fourth attachments respectively) I could only get the output of PB1 to stabilize with AC coupling. PC5 however would not stabilize under any setting I could think but at least the image capture shows the it's form.On the BMS board with the new microcontroller the I have set both pins to output high which is how I think they should be configured.Any direction in furthering my understanding of the techniques used in this switch circuit would be appreciated.Jerry

author: BillyO
author_rank: Super Contributor
timestamp: January 09, 2024, 10:47:38 pm
content: What is Vcc?

author: jerryk
author_rank: Regular Contributor
timestamp: January 09, 2024, 10:48:15 pm
content: vcc = 3.3V

author: BillyO
author_rank: Super Contributor
timestamp: January 09, 2024, 10:57:25 pm
content: Then part of what you are seeing in that bottom trace is that LED acting like a capacitor. It'd be great if could arrange the 2 bottom traces together at the same time with the resistor in on 2 different channels so we could see the timing of when each pin changes state, and I would probe at the pins.This is the working unit, right?

author: jerryk
author_rank: Regular Contributor
timestamp: January 09, 2024, 11:03:55 pm
content: I can try the second two together but not all three since I would need to reinstall R97 which results in the first scope capture in my original post. I was curious if it's possible that they are toggling the data direction register between input and output?Jerry

author: jerryk
author_rank: Regular Contributor
timestamp: January 09, 2024, 11:05:32 pm
content: Correct. These are from the working unit.Jerry

author: BillyO
author_rank: Super Contributor
timestamp: January 09, 2024, 11:07:44 pm
content: Quote from: jerryk on January 09, 2024, 11:03:55 pmIt might be, but we'd need to see those two traces with the resistor in.

author: jerryk
author_rank: Regular Contributor
timestamp: January 09, 2024, 11:30:54 pm
content: Thanks. It will be a moment, possibly tomorrow morning as I have to step away from the office.Jerry

author: WattsThat
author_rank: Frequent Contributor
timestamp: January 10, 2024, 03:03:26 am
content: The only way the mirco can tell the bypass switch is closed is to have PC5 become an input. That’s what exactly is shown in the fourth trace, that pulse with the rc decay is the pin changing from a high output to an input with its pull up enabled. The first trace shows the same thing and the differences in amplitude is change in PC5 from output to input.If you use two channels and observe both port pins at the same time, it will make sense, you cannot see them independently with just one input channel used.

author: jerryk
author_rank: Regular Contributor
timestamp: January 10, 2024, 05:08:07 pm
content: First attachment is the circuit with trace locations and I have left R97 in so I can activate the switch. Next are two three channel views. The first view depicts switch S3 open and the second view S3 is closed.I'm getting a better understanding of this but still can't see how I can detect the switch state of S3.Edit - Now I think I see that I have to read the trace at PC5 1ms after the output is changed to input??Jerry

author: jerryk
author_rank: Regular Contributor
timestamp: January 11, 2024, 07:26:54 pm
content: Thanks to the help provided I have been able to duplicate the logic to at least impersonate the factory board output with the switch open. When I close the switch there is some variation in the from the factory output. Traces from my coding of the test board are below.The code I came up to accomplish this:Code:void init_timer0(){_mf0e = 0x01; // enable Multi-function 0 interrupt_stm0ae = 0x01; // enable STM0A interrupt_stm0c0 = 0x30; // set clk = f(sys)/64_stm0c1 = 0xc1; // set TIMER MODE AND CLEAR COUNT_stm0af = 0x00; // clear STM0A interrupt flag_mf0f = 0x00; // clear Multi-function 0 flag_stm0al = 0x7d; // Matching value_stm0ah = 0x00;_st0on = 1; // start counting}void __attribute((interrupt(0x14))) ISR_TMR0 (void){//1ms pulsecount ++;subcount++;_stm0af = 0x00; // clear T2A interrupt flag_stm0al = 0x7d; // Matching value = 125 (8MHz/64/1000)_stm0ah = 0x00;// Logic for override switch detectif(subcount == 1){_pb1 = 1; // Pin PB1_pcc5 = 0; // sets _pc5 to output_pc5 = 0; // Pin PC5_pcpu5 = 0; // disable pullup on pin _pc5}if(subcount == 4){_pc5 = 1;_pcc5 = 1;}if(subcount == 5){_pb1 =0;_pc5 = 0;}if(subcount == 6){_pcpu5 = 1;_pc5 = 1;}if(subcount >= 6){subcount = 0;}if(count >= 63536){count = 0;}}So it seems like the goal of duplicating at least part of the logic to create the original signals has happened but I still don't understand how to code the detection logic required for a switch press detection. I'm not so much asking for the coding part but just need some direction on what part of the traces show a low signal after the switch press?Also, if I'm I should have posted this in the microcontroller forum let me know.Jerry

author: jerryk
author_rank: Regular Contributor
timestamp: January 12, 2024, 06:16:26 pm
content: Quote from: WattsThat on January 10, 2024, 03:03:26 amI did finally manage to emulate the factory original circuit exactly. I also had also set _pb1 to input with pull up enabled at what would be the 4th ms place (upper step of _pb1 trace) if you look at the whole period as 6ms. Right or wrong it works and all I have to is poll pin _pb1 for a low condition.FWIW here is the change in the interrupt that made it work:Code:void __attribute((interrupt(0x14))) ISR_TMR0 (void){//1ms pulsecount ++;subcount++;_stm0af = 0x00; // clear T2A interrupt flag_stm0al = 0x7d; // Matching value = 125 (8MHz/64/1000)_stm0ah = 0x00;// Logic for override switch detectif(subcount == 1){_pb1 = 1; // Pin PB1_pcc5 = 0; // sets _pc5 to output_pc5 = 0; // Pin PC5_pcpu5 = 0; // disable pullup on pin _pc5}if(subcount == 4){_pbc1 = 1;_pc5 = 1;_pcc5 = 1;}if(subcount == 5) // subcount 5 is the 4th ms in the 6ms period{_pbc1 = 0; // THE FIX - changed PB1 to input_pbpu1 = 1;// THE FIX - enabled PB1 pullup_pb1 =0;_pc5 = 0;}if(subcount == 6){_pcpu5 = 1;}if(subcount >= 6){subcount = 0;}Your post helped get me over the hump and now my override switch works.Jerry