808
Dependents: Chromatograph_Mobile
ads1248.cpp@1:54015254fc18, 2020-08-11 (annotated)
- Committer:
- nikmaos
- Date:
- Tue Aug 11 19:13:08 2020 +0000
- Revision:
- 1:54015254fc18
- Parent:
- 0:1b92bdad27f6
- Child:
- 2:ccfbc827eb2c
1108
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nikmaos | 0:1b92bdad27f6 | 1 | #include "ads1248.h" |
nikmaos | 0:1b92bdad27f6 | 2 | |
nikmaos | 0:1b92bdad27f6 | 3 | unsigned char ADS1248SleepCommand( void ) |
nikmaos | 0:1b92bdad27f6 | 4 | { |
nikmaos | 0:1b92bdad27f6 | 5 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 6 | SPI1MasterTransferByte(DS1248_SLEEP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 7 | DS1248_CS=1; |
nikmaos | 0:1b92bdad27f6 | 8 | DS1248_START = 0; |
nikmaos | 0:1b92bdad27f6 | 9 | return 0; |
nikmaos | 0:1b92bdad27f6 | 10 | } |
nikmaos | 0:1b92bdad27f6 | 11 | |
nikmaos | 0:1b92bdad27f6 | 12 | unsigned char ADS1248WakeupCommand( void ) |
nikmaos | 0:1b92bdad27f6 | 13 | { |
nikmaos | 0:1b92bdad27f6 | 14 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 15 | DS1248_START = 1; |
nikmaos | 0:1b92bdad27f6 | 16 | SPI1MasterTransferByte(DS1248_WAKEUP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 17 | DS1248_CS = 1; |
nikmaos | 0:1b92bdad27f6 | 18 | return 0; |
nikmaos | 0:1b92bdad27f6 | 19 | } |
nikmaos | 0:1b92bdad27f6 | 20 | |
nikmaos | 0:1b92bdad27f6 | 21 | unsigned char ADS1248ReadRegister( unsigned char reg, unsigned char num ) |
nikmaos | 0:1b92bdad27f6 | 22 | { |
nikmaos | 0:1b92bdad27f6 | 23 | char res[num]; |
nikmaos | 0:1b92bdad27f6 | 24 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 25 | SPI1MasterTransferByte(DS1248_RREG_COMMAND | ( reg & 0x0F )); |
nikmaos | 0:1b92bdad27f6 | 26 | SPI1MasterTransferByte(num - 1); |
nikmaos | 0:1b92bdad27f6 | 27 | while (num--) |
nikmaos | 0:1b92bdad27f6 | 28 | { |
nikmaos | 0:1b92bdad27f6 | 29 | res[num]=SPI1MasterTransferByte(DS1248_NOP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 30 | } |
nikmaos | 0:1b92bdad27f6 | 31 | DS1248_CS = 1; |
nikmaos | 0:1b92bdad27f6 | 32 | return res[0]; |
nikmaos | 0:1b92bdad27f6 | 33 | } |
nikmaos | 0:1b92bdad27f6 | 34 | |
nikmaos | 0:1b92bdad27f6 | 35 | unsigned char ADS1248WriteRegister( unsigned char reg, unsigned char num, uint8_t data) |
nikmaos | 0:1b92bdad27f6 | 36 | { |
nikmaos | 0:1b92bdad27f6 | 37 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 38 | SPI1MasterTransferByte(DS1248_WREG_COMMAND | ( reg & 0x0F )); |
nikmaos | 0:1b92bdad27f6 | 39 | SPI1MasterTransferByte(num - 1); |
nikmaos | 0:1b92bdad27f6 | 40 | SPI1MasterTransferByte(data); |
nikmaos | 0:1b92bdad27f6 | 41 | SPI1MasterTransferByte(DS1248_NOP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 42 | DS1248_CS = 1; |
nikmaos | 0:1b92bdad27f6 | 43 | return 0; |
nikmaos | 0:1b92bdad27f6 | 44 | } |
nikmaos | 0:1b92bdad27f6 | 45 | |
nikmaos | 0:1b92bdad27f6 | 46 | unsigned char ADS1248StartConversion( void ) |
nikmaos | 0:1b92bdad27f6 | 47 | { |
nikmaos | 0:1b92bdad27f6 | 48 | DS1248_START = 1; |
nikmaos | 0:1b92bdad27f6 | 49 | |
nikmaos | 0:1b92bdad27f6 | 50 | //delay_us ( 5 ); |
nikmaos | 0:1b92bdad27f6 | 51 | |
nikmaos | 0:1b92bdad27f6 | 52 | DS1248_START = 0; |
nikmaos | 0:1b92bdad27f6 | 53 | return 0; |
nikmaos | 0:1b92bdad27f6 | 54 | } |
nikmaos | 0:1b92bdad27f6 | 55 | |
nikmaos | 0:1b92bdad27f6 | 56 | unsigned long ADS1248ReadData( ADS1248_t * ads ) |
nikmaos | 0:1b92bdad27f6 | 57 | { |
nikmaos | 0:1b92bdad27f6 | 58 | |
nikmaos | 0:1b92bdad27f6 | 59 | union{ |
nikmaos | 0:1b92bdad27f6 | 60 | struct{ |
nikmaos | 1:54015254fc18 | 61 | uint8_t |
nikmaos | 1:54015254fc18 | 62 | b1,b2,b3; |
nikmaos | 1:54015254fc18 | 63 | }; |
nikmaos | 1:54015254fc18 | 64 | uint32_t all; |
nikmaos | 0:1b92bdad27f6 | 65 | }data; |
nikmaos | 0:1b92bdad27f6 | 66 | data.all=0; |
nikmaos | 0:1b92bdad27f6 | 67 | |
nikmaos | 0:1b92bdad27f6 | 68 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 69 | wait_us(1); |
nikmaos | 0:1b92bdad27f6 | 70 | SPI1MasterTransferByte(DS1248_RDATA_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 71 | data.b3=SPI1MasterTransferByte(DS1248_NOP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 72 | data.b2=SPI1MasterTransferByte(DS1248_NOP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 73 | data.b1=SPI1MasterTransferByte(DS1248_NOP_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 74 | wait_us(1); |
nikmaos | 0:1b92bdad27f6 | 75 | DS1248_CS = 1; |
nikmaos | 0:1b92bdad27f6 | 76 | |
nikmaos | 0:1b92bdad27f6 | 77 | return data.all; |
nikmaos | 0:1b92bdad27f6 | 78 | } |
nikmaos | 0:1b92bdad27f6 | 79 | |
nikmaos | 0:1b92bdad27f6 | 80 | unsigned char ADS1248SettingReg ( ADS1248_t * ads ) |
nikmaos | 0:1b92bdad27f6 | 81 | { |
nikmaos | 0:1b92bdad27f6 | 82 | DS1248_START = 1; |
nikmaos | 0:1b92bdad27f6 | 83 | DS1248_CS = 0; |
nikmaos | 0:1b92bdad27f6 | 84 | wait_us(1); |
nikmaos | 0:1b92bdad27f6 | 85 | SPI1MasterTransferByte(DS1248_RESET_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 86 | wait_us(600); |
nikmaos | 0:1b92bdad27f6 | 87 | SPI1MasterTransferByte(DS1248_SDATAC_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 88 | |
nikmaos | 0:1b92bdad27f6 | 89 | ADS1248WriteRegister ( DS1248_REGISTER_MUX0, 1, ads->MUX0.all ); |
nikmaos | 0:1b92bdad27f6 | 90 | ADS1248WriteRegister ( DS1248_REGISTER_VBIAS, 1,ads->VBIAS.all); |
nikmaos | 0:1b92bdad27f6 | 91 | ADS1248WriteRegister ( DS1248_REGISTER_MUX1, 1, ads->MUX1.all ); |
nikmaos | 0:1b92bdad27f6 | 92 | ADS1248WriteRegister ( DS1248_REGISTER_SYS0, 1, ads->SYS0.all ); |
nikmaos | 0:1b92bdad27f6 | 93 | ADS1248WriteRegister ( DS1248_REGISTER_IDAC0,1, ads->IDAC0.all); |
nikmaos | 0:1b92bdad27f6 | 94 | ADS1248WriteRegister ( DS1248_REGISTER_IDAC1,1, ads->IDAC1.all); |
nikmaos | 0:1b92bdad27f6 | 95 | ADS1248WriteRegister ( DS1248_REGISTER_FSC0,1, ads->FSC.FSC0); |
nikmaos | 0:1b92bdad27f6 | 96 | ADS1248WriteRegister ( DS1248_REGISTER_FSC1,1, ads->FSC.FSC1); |
nikmaos | 0:1b92bdad27f6 | 97 | ADS1248WriteRegister ( DS1248_REGISTER_FSC2,1, ads->FSC.FSC2); |
nikmaos | 0:1b92bdad27f6 | 98 | |
nikmaos | 0:1b92bdad27f6 | 99 | SPI1MasterTransferByte(DS1248_SYNC_COMMAND); |
nikmaos | 0:1b92bdad27f6 | 100 | wait_us(1); |
nikmaos | 0:1b92bdad27f6 | 101 | DS1248_CS = 1; |
nikmaos | 0:1b92bdad27f6 | 102 | return 0; |
nikmaos | 0:1b92bdad27f6 | 103 | |
nikmaos | 0:1b92bdad27f6 | 104 | } |
nikmaos | 1:54015254fc18 | 105 | |
nikmaos | 1:54015254fc18 | 106 | //вызывать вместо простого чтения АЦП |
nikmaos | 1:54015254fc18 | 107 | unsigned long ADS1248ReadCalibratedData(ADS1248_t * ads) |
nikmaos | 1:54015254fc18 | 108 | { |
nikmaos | 1:54015254fc18 | 109 | unsigned long result=0; |
nikmaos | 1:54015254fc18 | 110 | //проверка адекватности коэффициентов (не доделана) |
nikmaos | 1:54015254fc18 | 111 | if ((ads->Calibr.k>0)&&(ads->Calibr.b<0x8000000)) |
nikmaos | 1:54015254fc18 | 112 | result = ads->Calibr.k*ADS1248ReadData(ads)+ads->Calibr.b; |
nikmaos | 1:54015254fc18 | 113 | else return (0); |
nikmaos | 1:54015254fc18 | 114 | if (result & 0xFF000000) |
nikmaos | 1:54015254fc18 | 115 | /*Если результат переполнен из-за коэффициентов то данные испорчены. |
nikmaos | 1:54015254fc18 | 116 | Наверное можно просто отбросить старший байт или лучше приравнять максимальному 0x00FFFFFF*/ |
nikmaos | 1:54015254fc18 | 117 | return 0; |
nikmaos | 1:54015254fc18 | 118 | } |
nikmaos | 1:54015254fc18 | 119 | |
nikmaos | 1:54015254fc18 | 120 | //программная калибровка через терминал, без использования регистров микросхемы |
nikmaos | 1:54015254fc18 | 121 | void ADS1248CalibrateSoft(ADS1248_t * ads, /*костыль костылей*/float (*btf)(unsigned char*)) |
nikmaos | 1:54015254fc18 | 122 | { |
nikmaos | 1:54015254fc18 | 123 | //тут остановить все управляющие воздействия и считывание. Или перед использованием функции. |
nikmaos | 1:54015254fc18 | 124 | /* |
nikmaos | 1:54015254fc18 | 125 | |
nikmaos | 1:54015254fc18 | 126 | */ |
nikmaos | 1:54015254fc18 | 127 | UART.printf("ADS CALIBRATE:\r\n"); |
nikmaos | 1:54015254fc18 | 128 | __disable_irq (); |
nikmaos | 1:54015254fc18 | 129 | UART_gets(16); |
nikmaos | 1:54015254fc18 | 130 | UART.printf("%s\r\n",str); |
nikmaos | 1:54015254fc18 | 131 | if(str[0]=='k'){ |
nikmaos | 1:54015254fc18 | 132 | float result = btf(str); |
nikmaos | 1:54015254fc18 | 133 | UART.printf("%f\r\n",result); |
nikmaos | 1:54015254fc18 | 134 | } |
nikmaos | 1:54015254fc18 | 135 | __enable_irq (); |
nikmaos | 1:54015254fc18 | 136 | } |