ads1259

Dependents:   Chromatograph_Mobile Chromatograph_Mobile

Committer:
vitlog
Date:
Mon Jun 22 09:51:45 2020 +0000
Revision:
1:533ce2102fcc
Parent:
0:f9ba28ab9f4c
ne pomnyu chto pomenyal

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vitlog 0:f9ba28ab9f4c 1 #include "PGA280.h"
vitlog 0:f9ba28ab9f4c 2 #include <math.h>
vitlog 0:f9ba28ab9f4c 3
vitlog 0:f9ba28ab9f4c 4 /*Повторное объявление структур, из-за матюгов линковщика*/
vitlog 0:f9ba28ab9f4c 5 __PGA280_GPIO_t PGA280_GPIO;
vitlog 0:f9ba28ab9f4c 6 __PGA280_MUX_GAIN_t PGA280_MUX_GAIN;
vitlog 0:f9ba28ab9f4c 7 __PGA280_SPI_MODE_t PGA280_SPI_MODE;
vitlog 0:f9ba28ab9f4c 8 __PGA280_BUF_TIMEOUT_t PGA280_BUF_TIMEOUT;
vitlog 0:f9ba28ab9f4c 9 __PGA280_ERROR_t PGA280_ERROR;
vitlog 0:f9ba28ab9f4c 10 adress_t adress;
vitlog 0:f9ba28ab9f4c 11
vitlog 0:f9ba28ab9f4c 12 void pga280_setAdress ( unsigned char adr )
vitlog 0:f9ba28ab9f4c 13 /*Установить адрес микросемы на плате ТЭД-2 для работы*/
vitlog 0:f9ba28ab9f4c 14 {
vitlog 0:f9ba28ab9f4c 15 adress.adr=adr;
vitlog 0:f9ba28ab9f4c 16 ADR0 = adress.adr0;
vitlog 0:f9ba28ab9f4c 17 ADR1 = adress.adr1;
vitlog 0:f9ba28ab9f4c 18 ADR2 = adress.adr2;
vitlog 0:f9ba28ab9f4c 19 }
vitlog 0:f9ba28ab9f4c 20
vitlog 0:f9ba28ab9f4c 21 void pga280_resetAdress ( void )
vitlog 0:f9ba28ab9f4c 22 /*Сбросить адрес микросхемы на плате ТЭД-2*/
vitlog 0:f9ba28ab9f4c 23 {
vitlog 0:f9ba28ab9f4c 24 ADR0 = 0;
vitlog 0:f9ba28ab9f4c 25 ADR1 = 0;
vitlog 0:f9ba28ab9f4c 26 ADR2 = 0;
vitlog 0:f9ba28ab9f4c 27 }
vitlog 0:f9ba28ab9f4c 28
vitlog 0:f9ba28ab9f4c 29 char pga280_sendCommandDevice ( unsigned char command, unsigned char adr )
vitlog 0:f9ba28ab9f4c 30 /*Отправить устройству по адресу "adr" команду*/
vitlog 0:f9ba28ab9f4c 31 {
vitlog 0:f9ba28ab9f4c 32 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 33 CS = 0;
vitlog 0:f9ba28ab9f4c 34 SPI1MasterTransferByte(command);
vitlog 0:f9ba28ab9f4c 35 CS = 1;
vitlog 0:f9ba28ab9f4c 36 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 37
vitlog 0:f9ba28ab9f4c 38 return 0xff;
vitlog 0:f9ba28ab9f4c 39 }
vitlog 0:f9ba28ab9f4c 40
vitlog 0:f9ba28ab9f4c 41 unsigned char pga280_readOneRegisterDevice ( unsigned char reg, unsigned char adr)
vitlog 0:f9ba28ab9f4c 42 /*Прочитать регистр "reg" с устройства по адресу "adr"
vitlog 0:f9ba28ab9f4c 43 Функция возвращает значение регистра*/
vitlog 0:f9ba28ab9f4c 44 {
vitlog 0:f9ba28ab9f4c 45 unsigned char out_value = 0;
vitlog 0:f9ba28ab9f4c 46
vitlog 0:f9ba28ab9f4c 47 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 48
vitlog 0:f9ba28ab9f4c 49 CS = 0;
vitlog 0:f9ba28ab9f4c 50 SPI1MasterTransferByte ( READ_PGA280 | reg );
vitlog 0:f9ba28ab9f4c 51 out_value = SPI1MasterTransferByte(0x00 );
vitlog 0:f9ba28ab9f4c 52 CS = 1;
vitlog 0:f9ba28ab9f4c 53 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 54
vitlog 0:f9ba28ab9f4c 55 return out_value;
vitlog 0:f9ba28ab9f4c 56 }
vitlog 0:f9ba28ab9f4c 57
vitlog 0:f9ba28ab9f4c 58 void pga280_writeOneRegisterDevice (unsigned char reg, uint8_t data, unsigned char adr)
vitlog 0:f9ba28ab9f4c 59 /*Записать в регистр "reg" данные "data" в устройство по адресу "adr"*/
vitlog 0:f9ba28ab9f4c 60 {
vitlog 0:f9ba28ab9f4c 61 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 62
vitlog 0:f9ba28ab9f4c 63 CS = 0;
vitlog 0:f9ba28ab9f4c 64 SPI1MasterTransferByte ( WRITE_PGA280 | reg );
vitlog 0:f9ba28ab9f4c 65 SPI1MasterTransferByte ( data );
vitlog 0:f9ba28ab9f4c 66 CS = 1;
vitlog 0:f9ba28ab9f4c 67
vitlog 0:f9ba28ab9f4c 68 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 69 }
vitlog 0:f9ba28ab9f4c 70
vitlog 0:f9ba28ab9f4c 71 void pga280_writeBufOneRegisterDevice (unsigned char reg, unsigned char data, unsigned char adr)
vitlog 0:f9ba28ab9f4c 72 {
vitlog 0:f9ba28ab9f4c 73 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 74 CS = 0;
vitlog 0:f9ba28ab9f4c 75
vitlog 0:f9ba28ab9f4c 76 SPI1MasterTransferByte ( WRITE_BUF_PGA280 | reg );
vitlog 0:f9ba28ab9f4c 77 SPI1MasterTransferByte ( data );
vitlog 0:f9ba28ab9f4c 78
vitlog 0:f9ba28ab9f4c 79 CS = 1;
vitlog 0:f9ba28ab9f4c 80 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 81 }
vitlog 0:f9ba28ab9f4c 82
vitlog 0:f9ba28ab9f4c 83 void pga280_directCS ( unsigned char ex_cs, unsigned char adr )
vitlog 0:f9ba28ab9f4c 84 {
vitlog 0:f9ba28ab9f4c 85 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 86 CS = 0;
vitlog 0:f9ba28ab9f4c 87
vitlog 0:f9ba28ab9f4c 88 SPI1MasterTransferByte ( CS_DIRECT_PGA280 | ex_cs );
vitlog 0:f9ba28ab9f4c 89
vitlog 0:f9ba28ab9f4c 90 CS = 1;
vitlog 0:f9ba28ab9f4c 91 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 92 }
vitlog 0:f9ba28ab9f4c 93
vitlog 0:f9ba28ab9f4c 94 void pga280_resetDevice ( unsigned char adr )
vitlog 0:f9ba28ab9f4c 95 {
vitlog 0:f9ba28ab9f4c 96 pga280_writeOneRegisterDevice ( PGA280_RESET_ADR, 1, adr );
vitlog 0:f9ba28ab9f4c 97 }
vitlog 0:f9ba28ab9f4c 98
vitlog 0:f9ba28ab9f4c 99 void pga280_setMUX ( unsigned char mux, unsigned char adr )
vitlog 0:f9ba28ab9f4c 100 /*Выбрать канал АЦП "mux" на микросхеме по адресу "adr"*/
vitlog 0:f9ba28ab9f4c 101 {
vitlog 0:f9ba28ab9f4c 102 PGA280_MUX_GAIN.MUX=mux;
vitlog 0:f9ba28ab9f4c 103 pga280_writeOneRegisterDevice ( PGA280_MUX_GAIN_ADR, PGA280_MUX_GAIN.w, adr );
vitlog 0:f9ba28ab9f4c 104 }
vitlog 0:f9ba28ab9f4c 105
vitlog 0:f9ba28ab9f4c 106 void pga280_setGAIN ( unsigned char gain, unsigned char adr )
vitlog 0:f9ba28ab9f4c 107 /*Установить коэффициент усиления "gain" на устройстве (pga280)
vitlog 0:f9ba28ab9f4c 108 по адресу "adr" (коэффициенты написаны в заголовочном файле) */
vitlog 0:f9ba28ab9f4c 109 {
vitlog 0:f9ba28ab9f4c 110 PGA280_MUX_GAIN.GAIN=gain;
vitlog 0:f9ba28ab9f4c 111 pga280_writeOneRegisterDevice ( PGA280_MUX_GAIN_ADR, PGA280_MUX_GAIN.w, adr ); //запись в адрес регистра содержимого регистра
vitlog 0:f9ba28ab9f4c 112 }
vitlog 0:f9ba28ab9f4c 113
vitlog 0:f9ba28ab9f4c 114 void pga280_directCsBegin ( unsigned char exCS, unsigned char adr )
vitlog 0:f9ba28ab9f4c 115 {
vitlog 0:f9ba28ab9f4c 116 pga280_setAdress ( adr );
vitlog 0:f9ba28ab9f4c 117
vitlog 0:f9ba28ab9f4c 118 CS = 0;
vitlog 0:f9ba28ab9f4c 119
vitlog 0:f9ba28ab9f4c 120 SPI1MasterTransferByte ( CS_DIRECT_PGA280 | exCS );
vitlog 0:f9ba28ab9f4c 121 }
vitlog 0:f9ba28ab9f4c 122
vitlog 0:f9ba28ab9f4c 123 void pga280_directCsEnd ( void )
vitlog 0:f9ba28ab9f4c 124 {
vitlog 0:f9ba28ab9f4c 125 CS = 1;
vitlog 0:f9ba28ab9f4c 126 //RESET=0;
vitlog 0:f9ba28ab9f4c 127 //pga280_resetAdress ();
vitlog 0:f9ba28ab9f4c 128 }
vitlog 0:f9ba28ab9f4c 129
vitlog 0:f9ba28ab9f4c 130 void pga280_setGPIO ( unsigned char num, unsigned char adr )
vitlog 0:f9ba28ab9f4c 131 /*Установить в лог. 1 GPIO вывод с номером "num" на устройстве по адресу "adr"*/
vitlog 0:f9ba28ab9f4c 132 {
vitlog 0:f9ba28ab9f4c 133 PGA280_GPIO.GPIO |= 1 << num;
vitlog 0:f9ba28ab9f4c 134 pga280_writeOneRegisterDevice ( PGA280_GPIO_DATA_ADR, PGA280_GPIO.w, adr );
vitlog 0:f9ba28ab9f4c 135 }
vitlog 0:f9ba28ab9f4c 136
vitlog 0:f9ba28ab9f4c 137 void pga280_resetGPIO ( unsigned char num, unsigned char adr )
vitlog 0:f9ba28ab9f4c 138 /*Сбросить в лог. 0 GPIO вывод с номером "num" на устройстве по адресу "adr"*/
vitlog 0:f9ba28ab9f4c 139 {
vitlog 0:f9ba28ab9f4c 140 PGA280_GPIO.GPIO &= ~(1 << num);
vitlog 0:f9ba28ab9f4c 141 pga280_writeOneRegisterDevice ( PGA280_GPIO_DATA_ADR, PGA280_GPIO.w, adr );
vitlog 0:f9ba28ab9f4c 142 }