potentiometers.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * potentiometers.c
  3. *
  4. * Created: 04.07.2019 19:29:15
  5. * Author: Radioman
  6. */
  7. #include "heater3.h"
  8. #include "potentiometers.h"
  9. extern heater_str heater[2];
  10. extern opamp_str opamp[2];
  11. void POT_SendData(uint8_t data, potnum_t potnum)
  12. {
  13. PORTB = (PORTB & 0xF8) | (potnum & 0x07);
  14. PORTB &= ~(1 << SS_BIT);
  15. SPDR = 0x11;
  16. while(!(SPSR & (1 <<SPIF)));
  17. SPDR = data;
  18. while(!(SPSR & (1 <<SPIF)));
  19. PORTB |= (1 << SS_BIT);
  20. }
  21. uint8_t SetOpampGain(uint8_t ch, float gain)
  22. {
  23. uint8_t potval;
  24. ch = ((ch & 0x01) * 2) + 1;
  25. potval = (uint8_t)((8.448 * gain) - 8.9088);
  26. POT_SendData(potval, ch);
  27. //return (((float)potval + 8.9088) / 8.448);
  28. return potval;
  29. }
  30. uint8_t SetOpampShift(uint8_t ch, float shift)
  31. {
  32. uint8_t potval;
  33. ch = (ch & 0x01) * 2;
  34. potval = (uint8_t)((shift * 255.0) / 2.037);
  35. POT_SendData(potval, ch);
  36. //return (((float)potval * 2.037) / 255.0);
  37. return potval;
  38. }
  39. uint8_t SetDriveVoltage(uint8_t ch, float voltage)
  40. {
  41. uint8_t potval;
  42. ch = POT_DRIVE1 + (ch & 0x01);
  43. potval = (uint8_t)(voltage * 25.76);
  44. POT_SendData(potval, ch);
  45. //return ((float)potval / 25.76);
  46. return potval;
  47. }
  48. void SetInputRange(uint8_t ch, float vlow, float vhigh)
  49. {
  50. heater[ch].gain = SetOpampGain(ch, (3.3 / (vhigh - vlow)));
  51. heater[ch].shift = SetOpampShift(ch, (0.758 * vlow));
  52. opamp[ch].gain = (((float)heater[ch].gain + 8.9088) / 8.448);
  53. opamp[ch].shift = (((float)heater[ch].shift * 2.037) / 255.0);
  54. }
  55. void SetGainData(uint8_t ch, uint8_t data)
  56. {
  57. POT_SendData(data, ((ch & 0x01) * 2) + 1);
  58. heater[ch].gain = data;
  59. opamp[ch].gain = (((float)data + 8.9088) / 8.448);
  60. }
  61. void SetShiftData(uint8_t ch, uint8_t data)
  62. {
  63. POT_SendData(data, (ch & 0x01) * 2);
  64. heater[ch].shift = data;
  65. opamp[ch].shift = (((float)data * 2.037) / 255.0);
  66. }
  67. void SetDriveData(uint8_t ch, uint8_t data)
  68. {
  69. heater[ch].drive = data;
  70. opamp[ch].drive = ((float)data / 25.76);
  71. }
  72. void SwitchDrive(uint8_t ch, uint8_t onoff)
  73. {
  74. uint8_t potnum = POT_DRIVE1 + (ch & 0x01);
  75. POT_SendData(heater[ch].drive * (onoff & 0x01), potnum);
  76. }
  77. void RestoreGainShift(uint8_t ch)
  78. {
  79. POT_SendData(heater[ch].gain, ((ch & 0x01) * 2) + 1);
  80. POT_SendData(heater[ch].shift, (ch & 0x01) * 2);
  81. }