CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Understanding and adapting some functions for CCS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
tonkostz



Joined: 07 May 2011
Posts: 56
Location: Bulgaria

View user's profile Send private message

Understanding and adapting some functions for CCS
PostPosted: Fri Jan 10, 2025 9:35 am     Reply with quote

I need some on coverting some functions from Arduino project to adapt them for CCS compiler.
The first one. I think i understand all here except where i have to define By0, By1, By2...etc?
Code:
void sendRDS(char By0,char By1,char By2,char By3,char By4,char By5,char By6,char By7){
   rdsSentStatus = read1Byte(STATUS_REG) & 8;
   write1Byte(RDSD0_REG,By0);
   write1Byte(RDSD1_REG,By1);
   write1Byte(RDSD2_REG,By2);
   write1Byte(RDSD3_REG,By3);
   write1Byte(RDSD4_REG,By4);
   write1Byte(RDSD5_REG,By5);
   write1Byte(RDSD6_REG,By6);
   write1Byte(RDSD7_REG,By7);
   if(rdsReady==4){
      rdsReady = 0;
   }else{
      rdsReady = 4;
   }
}


Second function. I don't understand some things here. What is SN.length()?
And these too: char char_array[str_len]; and SN.toCharArray(char_array, str_len);
Code:
void sendStationName(String SN){
   int str_len = SN.length() + 1;
   str_len += str_len%2; //making it multiple of 2
   char char_array[str_len];
   SN.toCharArray(char_array, str_len);
   
   for(int i=0;i<str_len;i+=2){
      sendRDS(0x64,0x00,0x02,0x68+(i/2),0xE0,0xCD,char_array[i],char_array[i+1]);
      waitForRDSSend();
   }
   
}


What kind of data is *rdsRawData?
Code:
void QND_RDSLoadData(UINT8 *rdsRawData, UINT8 upload)
{
   UINT8 i;
   UINT8 temp;
    if (upload)
   {      //TX MODE
        for (i = 0; i <= 7; i++)
      {
            QND_WriteReg(RDSD0 + i, rdsRawData[i]);
        }   
    }
   else
   {
      //RX MODE
        for (i = 0; i <= 7; i++)
      {
            temp = QND_ReadReg(RDSD0 + i);
            rdsRawData[i] = temp;
        }
    }
}


These are functions for RDS data sending for QN8007 fm radio transmitter.
Thanks!
Ttelmah



Joined: 11 Mar 2010
Posts: 19649

View user's profile Send private message

PostPosted: Fri Jan 10, 2025 10:58 am     Reply with quote

Do a search about strings in C. The Arduino, is implementing a string
type, which C does not have. A string in C is just a character array. A string
type exists in newer languages like C++.

Pass the data as a char *

strlen(SN)
then gives the length of the string.

rdsRawData, is a pointer to an array of unsigned int8 values.
Just call it with the name of the array declared in the code calling the
function.

The Read1Byte call is equivalent to
Code:

    I2C_Start(); //send a bus start
    I2C_write(DEVICE_ADDRESS); //select device
    I2C_write(STATUS_REG); //specify register address
    I2C_start();  //send a restart
    I2C_write(DEVICE_ADDRESS+1); //send address flagged to read
    rdsSentStatus = I2C_read();
    I2C_stop();


The Write1Byte calls need:
Code:

    I2C_Start(); //send a bus start
    I2C_write(DEVICE_ADDRESS); //select device
    I2C_write(REGISTER_REQUIRED); //specify register address
    I2C_write(BYTE_TO_WRITE); //send byte
    I2C_stop();
tonkostz



Joined: 07 May 2011
Posts: 56
Location: Bulgaria

View user's profile Send private message

PostPosted: Sun Jan 12, 2025 4:27 am     Reply with quote

Thanks!

Would you help me with this code which i am trying to convert from Arduino?
Original Arduino code:
Code:
  // RDS PS Section 

  for (uint8_t pslc = 0; pslc < 4; pslc++)   // pslc = PS Letter Counter for PS twin letters
  {
   
  uint16_t rdsGrp_0B = 0b0000100000000000; // raw group 0B data. 0B was chosen as there are no alternative frequencies

  //Explanation for the 16 group 0B bits:
  //0000->Group#, 1 = Bo -> B (0 = A), 0->TP, 00000->PTY, 0->TA, 0->M/S, 0->DI, xx->PS letter counter 00...11
  //15-12         11                   10     9 - 5       4      3       2      10  Bit number MSB....LSB

  uint16_t pty = myPTY << 5; // Move PTY bits to the correct position.

  Wire.requestFrom(0x2B, 32);
  for (uint8_t i = 0; i < 32; i++) {rbuf[i] = Wire.read ();} // rbuf = Read Buffer for register values read from QN8007
 
  rdsGrp_0B =  rdsGrp_0B + pslc + pty; // Add Letter counter and PTY data to group
     
  Wire.beginTransmission(0x2B);
  Wire.write(0x10);                 // First RDS register address
  Wire.write(rdsPI_h);              // Send data for Register 0x10 PI High Byte
  Wire.write(rdsPI_l);              // Send data for Register 0x11 PI Low Byte
  Wire.write(highByte(rdsGrp_0B));  // Send data for Register 0x12 Group High Byte
  Wire.write(lowByte(rdsGrp_0B));   // Send data for Register 0x13 Group Low Byte
  Wire.write(rdsPI_h);              // Send data for Register 0x14 PI High Byte repeat
  Wire.write(rdsPI_l);              // Send data for Register 0x15 PI Low Byte repeat
  Wire.write(ps[2*pslc]);           // Send data for Register 0x16 First letter
  Wire.write(ps[2*pslc+1]);         // Send data for Register 0x17 Second letter
  Wire.endTransmission();
     
  rbuf[3] = rbuf[3] ^ 0b00000100; // Toggle RDS Ready bit from register 0x01

  Wire.beginTransmission(0x2B);
  Wire.write(0x01);
  Wire.write(rbuf[3]); //Send toggled bit to move PS data to QN8007 RDS tx data buffer
  Wire.endTransmission();

  }


My code:
Code:
// RDS PS Section 
void sendRDS()
{
  for(int8 pslc = 0; pslc < 4; pslc++)   // pslc = PS Letter Counter for PS twin letters
  {
  int16 rdsGrp_0B = 0b0000100000000000; // raw group 0B data. 0B was chosen as there are no alternative frequencies

  //Explanation for the 16 group 0B bits:
  //0000->Group#, 1 = Bo -> B (0 = A), 0->TP, 00000->PTY, 0->TA, 0->M/S, 0->DI, xx->PS letter counter 00...11
  //15-12         11                   10     9 - 5       4      3       2      10  Bit number MSB....LSB

  int16 pty = myPTY << 5; // Move PTY bits to the correct position.
   
  for(int8 i = 0; i < 32; i++)
  {
    i2c_transfer_in(0x56,rbuf[i],32);
   } // rbuf = Read Buffer for register values read from QN8007
 
  rdsGrp_0B =  rdsGrp_0B + pslc + pty; // Add Letter counter and PTY data to group
     
  qn8007_write(QN8007_RDSD0,rdsPI_h); // Send data for Register 0x10 PI High Byte
  qn8007_write(QN8007_RDSD1,rdsPI_l); // Send data for Register 0x11 PI Low Byte
  qn8007_write(QN8007_RDSD2,(rdsGrp_0B)>>8); //highByte(rdsGrp_0B)); // Send data for Register 0x12 Group High Byte           
  qn8007_write(QN8007_RDSD3,(rdsGrp_0B)<<8); //lowByte(rdsGrp_0B)); // Send data for Register 0x13 Group Low Byte           
  qn8007_write(QN8007_RDSD4,rdsPI_h); // Send data for Register 0x14 PI High Byte repeat
  qn8007_write(QN8007_RDSD5,rdsPI_l); // Send data for Register 0x15 PI Low Byte repeat
  qn8007_write(QN8007_RDSD6,ps[2*pslc]); // Send data for Register 0x16 First letter
  qn8007_write(QN8007_RDSD7,ps[2*pslc+1]); // Send data for Register 0x17 Second letter           
                   
  rbuf[3] = rbuf[3] ^ 0b00000100; // Toggle RDS Ready bit from register 0x01

  qn8007_write(QN8007_SYSTEM2,0x04); //Send toggled bit to move PS data to QN8007 RDS tx data buffer
 
  }
}


Some defines:
Code:
char ps[] = "Hi Lucy!";
int16 myPTY = 1;
int16 rbuf[31];
int8 rdsPI_h = 0b00010010;
int8 rdsPI_l = 0b00100010;

////////////////////////WRITE/////////////////////////////
void qn8007_write(int Reg, int val)
{
 i2c_start();
 i2c_write(0x56); //QN8007_ADDR_WRITE
 i2c_write(Reg); //register name/address
 i2c_write(val); //data to be written
 i2c_stop();   
}
       
////////////////////////READ/////////////////////////////
int qn8007_read(int Reg)
{
 //unsigned int data = 0;
 //int buffer; 
 i2c_start();
 i2c_write(0x56); //QN8007_ADDR_WRITE
 i2c_write(Reg);  //register name/address
 i2c_start();     //restart
 i2c_write(0x57); //QN8007_ADDR_READ
 buffer = i2c_read(0); // read and store the data in a variable
 i2c_stop();
 return buffer;
}


I think that this section is not OK?
Code:
  for(int8 i = 0; i < 32; i++)
  {
    i2c_transfer_in(0x56,rbuf[i],32);
  } // rbuf = Read Buffer for register values read from QN8007
temtronic



Joined: 01 Jul 2010
Posts: 9370
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Jan 12, 2025 7:12 am     Reply with quote

1 ) post a link to the Arduino code, so everyone trying to help you can read the entire code and hopefully understand 'how it works'

2) have you got a small program that writes to a register and then you CAN read it back properly ?

3) that last 'code' appears to read 32 bytes from the device BUT I have NO idea WHERE the data is stored. Need to SEE the 'I2C_transfer_in(......) function as well as any supporting documentation. The //comment says 'rbuf' is where the data goes BUT 'rbuf' to me is a single byte location in RAM, 'rbuf[] ' would be an ARRAY of bytes that could hold the data

4) Whenever you code... USE //comments to almost every line you create !!

comments cost nothing, in program space but tel everyone WHAT is going on ,now, tomorrow, a year from now. Yes, some code is obvious but in the case of #3 above, without that comment NOBODY would KNOW where the data was sent.
Ttelmah



Joined: 11 Mar 2010
Posts: 19649

View user's profile Send private message

PostPosted: Sun Jan 12, 2025 12:16 pm     Reply with quote

Hint. You are doing 32*32 byte transfers......
I2C_read() is equivalent to Wire.read ();
Wire.requestFrom(
is equivalent to
i2c_start();
i2c_write(0x56); //QN8007_ADDR_WRITE
i2c_write(Reg); //register name/address
i2c_start(); //restart
i2c_write(0x57); //QN8007_ADDR_READ

With you then keeping the count, and sending an I2C_stop after the last
transfer. (and nacking the last byte).
I2C_transfer_in, should do the whole transfer, but is sometimes unreliable
on the older I2C peripherals, and may well give problems with this target
chip.

This thread shows why I2C_transfer may be dubious. It has got better:
[url]
http://192.96.219.76/forum/viewtopic.php?p=216979
[/url]
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group