1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- /*
- * potentiometers.c
- *
- * Created: 04.07.2019 19:29:15
- * Author: Radioman
- */
- #include "heater3.h"
- #include "potentiometers.h"
- extern heater_str heater[2];
- extern opamp_str opamp[2];
- void POT_SendData(uint8_t data, potnum_t potnum)
- {
- PORTB = (PORTB & 0xF8) | (potnum & 0x07);
- PORTB &= ~(1 << SS_BIT);
- SPDR = 0x11;
- while(!(SPSR & (1 <<SPIF)));
- SPDR = data;
- while(!(SPSR & (1 <<SPIF)));
- PORTB |= (1 << SS_BIT);
- }
- uint8_t SetOpampGain(uint8_t ch, float gain)
- {
- uint8_t potval;
- ch = ((ch & 0x01) * 2) + 1;
- potval = (uint8_t)((8.448 * gain) - 8.9088);
- POT_SendData(potval, ch);
- //return (((float)potval + 8.9088) / 8.448);
- return potval;
- }
- uint8_t SetOpampShift(uint8_t ch, float shift)
- {
- uint8_t potval;
- ch = (ch & 0x01) * 2;
- potval = (uint8_t)((shift * 255.0) / 2.037);
- POT_SendData(potval, ch);
- //return (((float)potval * 2.037) / 255.0);
- return potval;
- }
- uint8_t SetDriveVoltage(uint8_t ch, float voltage)
- {
- uint8_t potval;
- ch = POT_DRIVE1 + (ch & 0x01);
- potval = (uint8_t)(voltage * 25.76);
- POT_SendData(potval, ch);
- //return ((float)potval / 25.76);
- return potval;
- }
- void SetInputRange(uint8_t ch, float vlow, float vhigh)
- {
- heater[ch].gain = SetOpampGain(ch, (3.3 / (vhigh - vlow)));
- heater[ch].shift = SetOpampShift(ch, (0.758 * vlow));
- opamp[ch].gain = (((float)heater[ch].gain + 8.9088) / 8.448);
- opamp[ch].shift = (((float)heater[ch].shift * 2.037) / 255.0);
- }
- void SetGainData(uint8_t ch, uint8_t data)
- {
- POT_SendData(data, ((ch & 0x01) * 2) + 1);
- heater[ch].gain = data;
- opamp[ch].gain = (((float)data + 8.9088) / 8.448);
- }
- void SetShiftData(uint8_t ch, uint8_t data)
- {
- POT_SendData(data, (ch & 0x01) * 2);
- heater[ch].shift = data;
- opamp[ch].shift = (((float)data * 2.037) / 255.0);
- }
- void SetDriveData(uint8_t ch, uint8_t data)
- {
- heater[ch].drive = data;
- opamp[ch].drive = ((float)data / 25.76);
- }
- void SwitchDrive(uint8_t ch, uint8_t onoff)
- {
- uint8_t potnum = POT_DRIVE1 + (ch & 0x01);
- POT_SendData(heater[ch].drive * (onoff & 0x01), potnum);
- }
- void RestoreGainShift(uint8_t ch)
- {
- POT_SendData(heater[ch].gain, ((ch & 0x01) * 2) + 1);
- POT_SendData(heater[ch].shift, (ch & 0x01) * 2);
- }
|