I2C qulundan alınan 2 baytı seriala köçürmək üçün yəqin ki, sadə kod parçası ilə mübarizə aparıram ki, mən daha sonra INT-yə çevirə və verilənlər üzərində riyaziyyat həyata keçirə bilim.
Bu kod uğurla (I2C analizatoruma görə) 16 bitlik ADC-də bir registr ünvanından 2 ardıcıl baytı oxuyur.
void ADC_Initialise( void )
{
_DINT() ; // disable all maskable interrupts
I2C_SCL_HIGH() ;
I2C_SCL_OUTPUT() ;
I2C_SDA_HIGH() ;
I2C_SDA_OUTPUT() ;
I2C_Write_Register_3B( ADC, 0x01, 0xC0, 0x83) ; // Write 2 bytes to ADC Config Register
for(i=0; i < 10000; i++);
I2C_Read_Register( ADC, 0x00 ); // Read the 2 byte ADC value
for(i=0; i < 10000; i++);
_EINT() ; // re-enable the interrupts
}
static unsigned char I2C_Read_Register( char Device_Address, char Register_Address )
{
unsigned char Value ;
I2CM_Start( ) ;
if( I2CM_Out( Device_Address << 1 ) ) //send write control byte + chip address
return 0 ;
else if( I2CM_Out( Register_Address ) ) //send register number
return 0 ;
I2CM_Start( ) ; // Restart
if( I2CM_Out(( Device_Address << 1 ) | 0x01 )) //send read control byte + chip address
return 0 ;
I2CM_In( &Value, 2 ) ; //RJ 6.8.19 input 2 byte ADC value to 'buf'
// return Value ; //rj placed after IC2M_In
I2CM_Stop( ) ;
return Value ; //rj placed after IC2M_Stop
}
static void I2CM_In( unsigned char* buf, int count )
{
unsigned char data ;
for( ; count--; ) // How do I store these 2 bytes in a char[] or INT
{
data = 0 ;
I2C_SDA_INPUT() ;
volatile unsigned int i = 0 ;
for( ; i < 8 ; ++i )
{
//Set Clock High
I2C_SCL_HIGH() ;
//shift the bit over
data <<= 1 ;
if( I2C_SDA_IS_HIGH() )
{
data |= 0x01 ;
}
//Set Clock Low
brief_pause( 0x04 ) ;
I2C_SCL_LOW() ;
}
//put the input data byte into the buffer, inc buffer pointer
*buf++ = data ;
//take sda to output ack
I2C_SDA_OUTPUT() ;
//Set Clock High
I2C_SCL_HIGH() ;
//Set Clock Low
brief_pause( 0x04 ) ;
I2C_SCL_LOW() ;
}
}
Beləliklə, ehtimal ki, nəticədə oxunanları toplamaq / saxlamaq üçün bir sıra (imzasız char adcarray [2]) lazımdır, ona görə də cəhd etdim -
*buf++ = data ;
adcarray[] = data ;
adcarray[] = buf ;
'gözlənilən ifadə', ehtimal ki, hər keçiddə massivi doldurmaq üçün for loopunun hansısa formasına ehtiyacım olduğunu bildirir, baxmayaraq ki, *buf xətti artıq count 2 dövrəsinin içərisindədir.
Yardım yüksək qiymətləndirildi (MSP430 və ADS1115 ADC-də IAR-da C). Hörmətlə, Ralf
adcarray
və yaValue
nədir - siz bu dəyişənlər üçün bəyannamələri tərk etmisiniz. - person Chris Turner   schedule 07.08.2019Value
nədir? O, 'I2CM_In. What's wrong with the content of
Value' tərəfindən oxunan 2 baytdan ibarət olmalıdır? Analizatorunuzda gördüyünüzdən fərqlidirmi? - person Gerhardh   schedule 07.08.2019unsigned char Value;
Bu necə 2 bayt tutmalıdır? - person Gerhardh   schedule 07.08.2019buf
arxasındakı faktiki dəyişən dirValue
, yalnız bir bayt yaddaş ayırır. Dəyər yəqin ki, massiv və ya 16 bit int olmalıdır vəI2C_Read_Register
funksiyası 16 bit int qaytarmalıdır. Əgər kəsmə nöqtəsini*buf++ = data ;
nöqtəsinə itələsəniz, analizatorunuzun gördüyü eyni şeyi görürsünüzmü? - person yhyrcanus   schedule 07.08.2019