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 support@ccsinfo.com

Passing string as parameter and calculating parity bit

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



Joined: 29 Mar 2008
Posts: 26
Location: Ribeirão Preto, SP, Brasil

View user's profile Send private message Visit poster's website ICQ Number

Passing string as parameter and calculating parity bit
PostPosted: Sat Mar 29, 2008 3:48 pm     Reply with quote

Hi.

I have a function to write some texts to my car's display. In this function, i need to receive a text (by parameter) and send char by char using i2c (+somethings).

My problem is: how can i pass a string by parameter?

Also, is there any easy way to calculate parity bit? I need to receive a string, get 10 chars from this string (is less, add a blank char) and send these 10 chars to my display using i2c_write() function.....but not only char, it's char + parity bit...so it's 9 bits.
Now, i'm using a global variable for string and then i call EscreveDisplay(), which scans the global variable and send char-by-char. The chars are pre-calculated by me (i have a chars table in switch).

This is how i call the function:

Code:

sprintf(e,"Test string");
EscreveDisplay();

where 'e' is a global variable (char[20] e="").

This is the function:


Code:

void EscreveDisplay(){
   long i=0,t=0;
   int bitcount=0;

   #ifndef DEBUG
      Initi2c();
   #endif

   MRQ = 0;
   delay_ms(1);
   MRQ = 1;
   delay_us(200);

   #ifndef DEBUG
      Starti2c();
      i2c_write(0x9b);     // Endereço do display
   #endif

   MRQ = 0;
   delay_us(250);

   #ifndef DEBUG
   // Display symbols
   
   // Faz a contagem de bits=1 para calcular a paridade
   for (i=1;i<7;i++) {
      if (bit_test(b1,i)) {
         bitcount++;
      }
   }
   // Se o numero de bits for impar, o bit de paridade é 0
   // Caso contrario, é 1
   if (EvenOrOdd(bitcount)) {
      bit_clear(b1,0);
   } else {
      bit_set(b1,0);
   }
   
   delay_ms(1);
//   i2c_write(0x01);
   i2c_write(b1);
   delay_ms(1);
   i2c_write(0x01);
   delay_ms(1);
   i2c_write(0x01);
   delay_ms(1);
   #endif

   t = strlen(e);
   #ifdef DEBUG
      printf("Tamanho da string: %u\n\r",strlen(e));
   #endif

   for (i=0;i<t;i++) {

    if (i<10) {

     if (e[i]==NULL) {
        #ifdef DEBUG
          printf(" Achei nulo\n\r");
        #endif
        e[i]=" ";
     }

      #ifdef DEBUG
         printf("  Escrevendo caracter %c\n\r",e[i]);
      #endif

         #ifndef DEBUG
         switch( e[i] ) {
            case ' ': i2c_write(0x40); break;
            case '@': i2c_write(0x80); break;
            case '~': i2c_write(0xFD); break;
            case ',': i2c_write(0x58); break;
            case '+': i2c_write(0x57); break;
            case '/': i2c_write(0x5E); break;
            case '\\': i2c_write(0xB9); break;
            case '|': i2c_write(0xF8); break;
            case '{': i2c_write(0xF7); break;
            case '}': i2c_write(0xFB); break;
            case '"': i2c_write(0x45); break;
            case '#': i2c_write(0x46); break;
            case '$': i2c_write(0x49); break;
            case '&': i2c_write(0x4C); break;
            case '\'': i2c_write(0x4F); break;
            case ';': i2c_write(0x76); break;
            case '<': i2c_write(0x79); break;
            case '>': i2c_write(0x7C); break;
            case '.': i2c_write(0x5D); break;
            case ':': i2c_write(0x75); break;
            case ')': i2c_write(0x52); break;
            case '(': i2c_write(0x51); break;
            case '%': i2c_write(0x4A); break;
            case '=': i2c_write(0x7A); break;
            case '?': i2c_write(0x7F); break;
            case '-': i2c_write(0x5B); break;
            case '*': i2c_write(0x54); break;
            case '!': i2c_write(0x43); break;


            case '0': i2c_write(0x61); break;
            case '1': i2c_write(0x62); break;
            case '2': i2c_write(0x64); break;
            case '3': i2c_write(0x67); break;
            case '4': i2c_write(0x68); break;
            case '5': i2c_write(0x6B); break;
            case '6': i2c_write(0x6D); break;
            case '7': i2c_write(0x6E); break;
            case '8': i2c_write(0x70); break;
            case '9': i2c_write(0x73); break;

            case 'a': i2c_write(0xC2); break;
            case 'b': i2c_write(0xC4); break;
            case 'c': i2c_write(0xC7); break;
            case 'd': i2c_write(0xC8); break;
            case 'e': i2c_write(0xCB); break;
            case 'f': i2c_write(0xCD); break;
            case 'g': i2c_write(0xCE); break;
            case 'h': i2c_write(0xD0); break;
            case 'i': i2c_write(0xD3); break;
            case 'j': i2c_write(0xD5); break;
            case 'k': i2c_write(0xD6); break;
            case 'l': i2c_write(0xD9); break;
            case 'm': i2c_write(0xDA); break;
            case 'n': i2c_write(0xDC); break;
            case 'o': i2c_write(0xDF); break;
            case 'p': i2c_write(0xE0); break;
            case 'q': i2c_write(0xE3); break;
            case 'r': i2c_write(0xE5); break;
            case 's': i2c_write(0xE6); break;
            case 't': i2c_write(0xE9); break;
            case 'u': i2c_write(0xEA); break;
            case 'v': i2c_write(0xEC); break;
            case 'w': i2c_write(0xEF); break;
            case 'x': i2c_write(0xF1); break;
            case 'y': i2c_write(0xF2); break;
            case 'z': i2c_write(0xF4); break;

            case 'A': i2c_write(0x83); break;
            case 'B': i2c_write(0x85); break;
            case 'C': i2c_write(0x86); break;
            case 'D': i2c_write(0x89); break;
            case 'E': i2c_write(0x8A); break;
            case 'F': i2c_write(0x8C); break;
            case 'G': i2c_write(0x8F); break;
            case 'H': i2c_write(0x91); break;
            case 'I': i2c_write(0x92); break;
            case 'J': i2c_write(0x94); break;
            case 'K': i2c_write(0x97); break;
            case 'L': i2c_write(0x98); break;
            case 'M': i2c_write(0x9B); break;
            case 'N': i2c_write(0x9D); break;
            case 'O': i2c_write(0x9E); break;
            case 'P': i2c_write(0xA1); break;
            case 'Q': i2c_write(0xA2); break;
            case 'R': i2c_write(0xA4); break;
            case 'S': i2c_write(0xA7); break;
            case 'T': i2c_write(0xA8); break;
            case 'U': i2c_write(0xAB); break;
            case 'V': i2c_write(0xAD); break;
            case 'W': i2c_write(0xAE); break;
            case 'X': i2c_write(0xB0); break;
            case 'Y': i2c_write(0xB3); break;
            case 'Z': i2c_write(0xB5); break;

            default:  i2c_write(0x40); break;
         }
      #endif


      delay_ms(2);
    } else {
      #ifdef DEBUG
         printf("=> Passou do limite!Ignorando caracter %c\n\r",e[i]);
      #endif
    }

   }

   if (t<10) {
      for (i=0;i<10-t;i++) {
         #ifdef DEBUG
            printf("----> Completando caracter %Lu\n\r",i);
         #endif

         #ifndef DEBUG
            i2c_write(0x40);
         #endif
         delay_ms(2);
      }
   }



   MRQ = 1; // MRQ = 1 // (Entrada?)
   delay_us(200);

   #ifndef DEBUG
//      Stopi2c();
   #endif

   #ifdef DEBUG
      printf("Fim da escrita no display!\n\r");
   #endif

   return;

}


If is possible, i like to just call: EscreveDisplay("My text");

Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Mar 30, 2008 1:59 pm     Reply with quote

Quote:
Also, is there any easy way to calculate parity bit?

See the find_parity() function at the end of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=21363
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