Hi, i have a machines electronics card, shematic and software. I'm new to company and don't used shift registers and c++.
The engineer who designed these left the company and the job was left to me. I worked day and night, but the card did not work as expected. The engineer before me was also a good engineer and built a lot of machines for the company. But we cannot reach him.
Purpose and problems;
1-) When the electronic card is powered on, the DS305 indicator should read 15. It currently says 19.
2-) S232 and S224 will be used to increase or decrease the value written in DS305 by one. Now when you press s224 it becomes 18, when you press s224 it becomes 17, when you press s3 it becomes 18, when you press s224 it becomes 19, when you press it it becomes 28.
3-) When the electronic card is powered on, no segments light up. This is OK.
4-) When the electronic card is powered on, D421-422-423 and DS412-413-414 are off, all other LEDs are on.
5-) When the buttons on the far right are pressed, the 7-segment on that line should be opened and the power and speed values should be written in the segment with the buttons on the left. For example, when I press S208, all LEDs except D412-413-414 are on, but no segment is on.
D2 led work fine and i can change time it works as i command.
The combinations change a lot when you press the buttons. There are many more problems, but maybe someone with experience will understand the problem, I think the shift registers are working incorrectly. I suspect ui.ino 19. line. Because code was not compiling when i start, then i added timer and arx container lib. code was arx::map<.... and i added arx::stdx::map<...
I am adding the schematics and software. ;
328pb.ino
#include <arduino-timer.h>
#include <ArxContainer.h>
#define TOOL_STATE_OFF 0
#define TOOL_STATE_ON 1
#define TOOL_STATE_RUNNING 2
#define RED 0
#define GREEN 1
//milisecond resolution timer
Timer<10, millis> timer;
//microsecond resolution timer, used for ui and io
Timer<2, micros> himer;
void setup() {
io_setup();
ui_setup();
buzzer_setup();
led_setup();
timer_setup();
brush_setup();
drdc_setup();
hammer_setup();
vacuum_setup();
hifreq_setup();
sonic_setup();
buzzer_bep();
}
void loop() {
himer.tick<void>();
timer.tick<void>();
}
void tool_stop_all() {
brush_stop();
drdc_stop();
hammer_stop();
vacuum_stop();
hifreq_stop();
sonic_stop();
}
void tool_off_all() {
brush_off();
drdc_off();
hammer_off();
vacuum_off();
hifreq_off();
sonic_off();
}
io.ino
#define PIN_SRIO_OE PORTC5
#define PIN_SRIO_CLR PORTE1
#define PIN_SRIO_LOAD PORTE0
#define PIN_SRIO_DATA PORTE3
#define PIN_SRIO_CLK PORTE2
uint16_t io_data;
uint8_t io_elapsed;
uint8_t io_duty_map[16];
void io_setup() {
//initial io data,
io_data = 0b0000000000000000U;
io_elapsed = 0U;
memset(&io_duty_map[0], 0, 16 * sizeof(uint8_t));
//shift register control lines outputs
DDRC |= _BV(PIN_SRIO_OE);
DDRE |= _BV(PIN_SRIO_CLR) | _BV(PIN_SRIO_LOAD) | _BV(PIN_SRIO_DATA) | _BV(PIN_SRIO_CLK);
//disable shift register
PORTC |= _BV(PIN_SRIO_OE);
//initial control signals
PORTE &= ~_BV(PIN_SRIO_CLR) & ~_BV(PIN_SRIO_LOAD) & ~_BV(PIN_SRIO_DATA) & ~_BV(PIN_SRIO_CLK);
//clear SR
PORTE |= _BV(PIN_SRIO_CLR);
PORTE &= ~_BV(PIN_SRIO_CLR);
//shift out data, all zeros
PORTE &= ~_BV(PIN_SRIO_DATA);
for (uint8_t i = 0; i < 16; i++) {
PORTE |= _BV(PIN_SRIO_CLK);
PORTE &= ~_BV(PIN_SRIO_CLK);
}
//load
PORTE |= _BV(PIN_SRIO_LOAD);
PORTE &= ~_BV(PIN_SRIO_LOAD);
//enable shift register
PORTC &= ~_BV(PIN_SRIO_OE);
himer.every(1000, __io_shift);
}
bool __io_shift(void*) {
uint8_t i = 16U, val;
uint16_t data = io_data;
//clear SR
PORTE |= _BV(PIN_SRIO_CLR);
PORTE &= ~_BV(PIN_SRIO_CLR);
//shift out msb first
while (i--) {
if (io_duty_map[i] != 0U) {
val = io_elapsed < io_duty_map[i];
} else {
val = (data & 0x8000U) == 0x8000U;
}
//set the bit
if (val) PORTE |= _BV(PIN_SRIO_DATA);
else PORTE &= ~_BV(PIN_SRIO_DATA);
//clk out
PORTE |= _BV(PIN_SRIO_CLK);
PORTE &= ~_BV(PIN_SRIO_CLK);
data = data << 1;
}
//load
PORTE |= _BV(PIN_SRIO_LOAD);
PORTE &= ~_BV(PIN_SRIO_LOAD);
io_elapsed = (io_elapsed + 1) % 20U;
return true;
}
void io_digitalWrite(uint8_t pin, uint8_t state) {
io_duty_map[pin] = 0; //cancle pwm
if (state) {
io_data |= 0x0001U << pin;
} else {
io_data &= ~(0x0001U << pin);
}
}
uint8_t io_digitalRead(uint8_t pin) {
return io_data & (0x0001U << pin);
}
//val 0-7
void io_analogWrite(uint8_t val) {
//only first tree bits is meaningfull A0,A1,A2
bitWrite(io_data, 6, bitRead(val, 0));
bitWrite(io_data, 5, bitRead(val, 1));
bitWrite(io_data, 4, bitRead(val, 2));
}
//pwm 0-100; @50Hz
void io_pwm(uint8_t pin, uint8_t duty) {
io_duty_map[pin] = duty / 5;
}
iu.ino
#define PIN_SRUI_OE PORTC5
#define PIN_SRUI_CLR PORTB3
#define PIN_SRUI_LOAD PORTB2
#define PIN_SRUI_DATA PORTB5
#define PIN_SRUI_CLK PORTB4
#define PIN_KBDR0 PINC0
#define PIN_KBDR1 PINC1
#define PIN_KBDR2 PINC2
#define PIN_KBDR3 PINC3
#define PIN_KBDR4 PINC4
uint8_t ui_cur_seg;
/////////////////////////// 0 , 1 , 2 , 3 ,4, ,5 ,6 ,7 ,8 ,9//
const uint8_t bcd_mat[] = { 0x40, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0x10 };
const uint8_t bcd_mal[] = { 0xFF, 0x79, 0x24, 0x30, 0x19, 0x12, 0x02, 0x78, 0x00, 0xFF };
uint8_t ui_7seg_buffer[14];
uint8_t ui_key_code, ui_last_key_code;
**arx::stdx::map<uint8_t, void (*)(void), 20> ui_key_map;**
**//std::map<uint8_t, void (*)(void), 20> ui_key_map;**
/*
*/
void ui_setup() {
ui_cur_seg = 0;
ui_key_code = ui_last_key_code = 0xFFU;
memset(&ui_7seg_buffer[0], 0xFFU, 14 * sizeof(uint8_t));
//shift register control lines outputs
DDRC |= _BV(PIN_SRUI_OE);
DDRB |= _BV(PIN_SRUI_CLR) | _BV(PIN_SRUI_LOAD) | _BV(PIN_SRUI_DATA) | _BV(PIN_SRUI_CLK);
//keyboard 5 rows input pullups
MCUCR &= ~_BV(PUD); //pull ups
PORTC |= _BV(PIN_KBDR0) | _BV(PIN_KBDR1) | _BV(PIN_KBDR2) | _BV(PIN_KBDR3);
DDRC &= ~_BV(PIN_KBDR0) & ~_BV(PIN_KBDR1) & ~_BV(PIN_KBDR2) & ~_BV(PIN_KBDR3);
//seven seg driver pins outputs
DDRD = 0xFFU;
PORTD = 0xFF;
//disable shift register
PORTC |= _BV(PIN_SRUI_OE);
//initial control signals
PORTB &= ~_BV(PIN_SRUI_CLR) & ~_BV(PIN_SRUI_LOAD) & ~_BV(PIN_SRUI_DATA) & ~_BV(PIN_SRUI_CLK);
//clear SR
PORTB |= _BV(PIN_SRUI_CLR);
PORTB &= ~_BV(PIN_SRUI_CLR);
//shift out all one's
PORTB |= _BV(PIN_SRUI_DATA);
for (uint8_t i = 0; i < 16; i++) {
//clk out
PORTB |= _BV(PIN_SRUI_CLK);
PORTB &= ~_BV(PIN_SRUI_CLK);
}
//load
PORTB |= _BV(PIN_SRUI_LOAD);
PORTB &= ~_BV(PIN_SRUI_LOAD);
//enable shift register
PORTC &= ~_BV(PIN_SRUI_OE);
himer.every(1000, __ui_scan);
}
/*
* walks thru all the segments
*/
static bool __ui_scan(void*) {
//first read-in the key code from previous cycle
//this way, it allows the signals settle out
//keypad colums are seg0 to seg7
if ((ui_cur_seg > 0) && (ui_cur_seg < 9)) {
uint8_t key_row = PINC & 0x1FU; //filter row pins
if (key_row != 0x1FU) {
//if any key pressed in current column
//key code structure: MSB 3 bits -> column number, LSB 5 bits -> row number
ui_key_code = (((ui_cur_seg - 1) & 0x07U) << 5) | key_row; //construct key code
}
}
if (ui_cur_seg == 0) {
//handle key presses, onece every 14 segments scanned
//hence on multiple presses, last key counts
if (ui_key_code == 0xFFU) {
//key released
ui_last_key_code = 0xFFU;
} else if (ui_key_code != ui_last_key_code) {
//new key pressed
ui_last_key_code = ui_key_code;
//buzzer_beep();
ui_handle_key(ui_key_code);
}
ui_key_code = 0xFFU;
//clear SR
PORTB &= ~_BV(PIN_SRUI_CLR);
PORTB |= _BV(PIN_SRUI_CLR);
//scan from beginning, put a zero into shift register
PORTB &= ~_BV(PIN_SRUI_DATA);
}
//shift out, data should be 1 except when cur_seg = 0;
//1111111111111110, shifted in repeadetly, zero walks from LSB to MSB
PORTB |= _BV(PIN_SRUI_CLK);
PORTB &= ~_BV(PIN_SRUI_CLK);
//make sure data line is high for the next cycle
PORTB |= _BV(PIN_SRUI_DATA);
//before loading register. turn off all leds,
//otherwise it might bleed to next segment
PORTD = 0xFFU;
//load the register
PORTB |= _BV(PIN_SRUI_LOAD);
PORTB &= ~_BV(PIN_SRUI_LOAD);
//light up the new leds
PORTD = ui_7seg_buffer[ui_cur_seg];
//goto next segment in the next cycle
ui_cur_seg = (ui_cur_seg + 1) % 14;
return true;
}
/*
* writes 1-8 to a 7segment display, 0 and 9 turns off the display
* this is for power level singel digit 7segment sdisplays
*/
void ui_7segWrite(uint8_t seg, uint8_t lvl, uint8_t dp) {
ui_7seg_buffer[seg] = bcd_mal[lvl] | (dp ? 0x00U : 0x80U);
}
/*
* write two digit time value
*/
void ui_7segWriteTime(uint8_t min, uint8_t dp) {
uint8_t bcd0 = 0xFFU;
uint8_t bcd1 = 0xFFU;
bcd1 = min / 10 ? bcd_mat[min / 10] : 0xFFU; //turn off if zero
bcd0 = bcd_mat[min - ((min / 10) * 10)];// do not turn off if zero
ui_7seg_buffer[7] = bcd1 | 0x80U; //filter dp
ui_7seg_buffer[8] = bcd0 | (dp ? 0x00 : 0x80U); //place dp if active
}
/*
* contols leds,
* leds numbered from 0-23
* 0-7 : only red, color parameter ignored, seg9
* 8-15 : red/green, red is segment 10, green is segment 11
* 16-23 : red green, red is segment 12, green is segment 13
*/
void ui_ledWrite(uint8_t led, uint8_t val, uint8_t color) {
uint8_t seg_on, seg, seg_off, dat;
if (led < 8) {
seg = seg_off = 9;
} else if (led < 16) {
seg = (color == RED) ? 10 : 11;
seg_off = (color == RED) ? 11 : 10;
led -= 8;
} else if (led < 24) {
seg = (color == RED) ? 12 : 13;
seg_off = (color == RED) ? 13 : 12;
led -= 16;
}
//turn off other color
dat = ui_7seg_buffer[seg_off] | (0x01U << led);
ui_7seg_buffer[seg_off] = dat;
//light up this color, depending on new value
dat = val ? ui_7seg_buffer[seg] & ~(0x01U << led) : ui_7seg_buffer[seg] | (0x01U << led);
ui_7seg_buffer[seg] = dat;
}
/*
* registes a callback function as a key code handler
*/
void ui_registerKey(uint8_t key, void (*handler)(void)) {
ui_key_map.insert(key, handler);
}
/*
* calls the handler callback function when key pressed
*/
static void ui_handle_key(uint8_t key) {
if (ui_key_map.find(key) != ui_key_map.end()) {
ui_key_map[key]();
}
}
ui 02 - Kopya.pdf (204.4 KB)