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

How to separate an 8 bit int

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



Joined: 07 Sep 2012
Posts: 17

View user's profile Send private message

How to separate an 8 bit int
PostPosted: Sun Nov 04, 2012 9:11 pm     Reply with quote

Hi, again asking for help, I have a problem separating an 8 bit int. I am doing a QAM modulator with PIC 18f452.

The 16 QAM modulation use 4 bits, if i want to modulate a PCM word, with 8 bits, i need to separate them in groups of 4, or individually and copy to a char, for be compared.

Example:

int x

x=rand(x)

x is now ( 1 0 1 0 0 0 1 0 ) for example

i need to separate

1 0 1 0

and

0 0 1 0

and copy :

c[i]=x(i)


I appreciate any help, of any master in this forum thanks to all
juanodiy



Joined: 07 Sep 2012
Posts: 17

View user's profile Send private message

PostPosted: Sun Nov 04, 2012 10:32 pm     Reply with quote

I think I could use these operators "<< >>", but if I want every bit in every position of a vector I don't know how, please any idea will be great.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Mon Nov 05, 2012 2:48 am     Reply with quote

Code:

int x, nibble;
x=rand(x);

nibble=x & 0xF;
//nibble is now the low nibble of x
swap(x);
nibble=x & 0xF;
//nibble is now the high nibble of x


Key is that the PIC has a single instruction (swap) which swaps the high and low nibbles of a byte. Much more efficient than a four bit rotation. CCS allows direct access to this instruction.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Nov 05, 2012 9:48 am     Reply with quote

The CCS manual does explain how you could do it in 'C'.

As Ttelmah has shown, you might as well use the built in Microchip instruction.

Mike
juanodiy



Joined: 07 Sep 2012
Posts: 17

View user's profile Send private message

PostPosted: Mon Nov 05, 2012 11:20 pm     Reply with quote

First of all, thanks for your time. I already have used the function swap, indeed it works, but I want to copy to a char vector. Here is my example:
Code:

#include <18f452.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP

#include <math.h>
#include <lcd.c>

int8 data=0,val,x,aux,i=0,m=0,y=0,p=0;
char c[8],high[4],low[4];
short res=0;
float ai,aq;

char s1[16][4]={
{'0','0','0','0'},
{'0','0','0','1'},
{'0','0','1','0'},
{'0','0','1','1'},
{'0','1','0','0'},
{'0','1','0','1'},
{'0','1','1','0'},
{'0','1','1','1'},
{'1','0','0','0'},
{'1','0','0','1'},
{'1','0','1','0'},
{'1','0','1','1'},
{'1','1','0','0'},
{'1','1','0','1'},
{'1','1','1','0'},
{'1','1','1','1'},
};

float compare()
{
while(low!=s1[p][])
  {
   p++;
  }
printf(lcd_putc,"encontro");
}

void main()
{
   lcd_init();
   setup_adc(ADC_CLOCK_DIV_32);//
   setup_adc_ports(RA0_RA1_RA3_ANALOG);
   set_adc_channel(0);
   lcd_putc("dato:");
 
   do {
      read_adc(ADC_START_ONLY); //start the ADC reading
      //Now wait for the ADC to complete
      while (!adc_done()) ;
      val=read_adc(ADC_READ_ONLY); //read the value
      delay_us(5); //ensure acquisition has completed
      lcd_putc("\f");
      lcd_gotoxy(1,1);
      printf(lcd_putc,"%u",val);

      for(x=0;x<=7;x++) //bucle for conversion dec to bin
      {
      res=val%2;
      aux=val/2;
      val=aux;
      if(res==0){
      c[x]='0';
      }
      else
      {
      c[x]='1';
      }
      }

      for(i=0;i<=3;i++) //copy LSB part from c
      {
      low[i]=c[i];
      }

      for(m=0;m<=3;m++) //copy MSB part from c
      {
      high[m]=c[m+4];
      }

      lcd_gotoxy(1,2);
      printf(lcd_putc,"%s",low);
      lcd_gotoxy(6,2);

      for(y=0;y<=3;y++)
      {
      printf(lcd_putc,"%c",high[y]);
      }

      compare(); //call function compare
      delay_ms(5000);
     
  } while(TRUE);
   
}


Now the idea is to compare to values of s1 with high and low, the combinations possible for a nibble. For this I create a function called compare, but it doesn't work. I don't know how to compare a matrix with a vector. Again thanks for your answers, any idea will be grateful.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 2:47 am     Reply with quote

I don't follow exactly what you're trying to do.

Please explain what you are attempting to compare.

Mike
juanodiy



Joined: 07 Sep 2012
Posts: 17

View user's profile Send private message

PostPosted: Tue Nov 06, 2012 11:23 pm     Reply with quote

Ok, thanks mike, I'm trying to do a comparison, between 3 arrays.

For example: I need to modulate a 16-QAM signal; this modulation use 4 bits, thats what I need to separate them, then I need to compare the 2 nibbles:

1001 0101
high low

and I have a predetermined matrix with all possible combinations, ex:

0000
0001
0010
0011
...


I want an efficient method for compare this rows with the variables high and low, thats what i think in a matrix, but i want to compare complete array, it means:

{ 0 ,1 ,0 ,0} == {0 ,1 ,0 ,0 }

not position by position, like:

for(....)
{
for(....)
{

My question is, is there a method for compare a matrix with a vector rows by rows? not position by position.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 3:03 am     Reply with quote

Maybe you're going about the task the wrong way.

I don't want to appear thick, but I still don't get it.

Can you outline in pseudo code the complete process.

Then either:-

I/We can see how to do what you're asking for as you see it.
OR
Suggest an alternative method which achieves the efficiency you're after.

Mike

EDIT

I keep getting stuck on the idea that {'0','0','1','0'} translates to 2 (or 4 depending on which way you read the bits).

So your high and low nibbles are then much easier to compare.
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 3:05 am     Reply with quote

Seriously, for is the way to go. There is nothing 'more efficient' about trying to compare the complete array, languages that offer features like this (C doesn't), do it 'behind the scenes', with simple 'for' loop tests....
However I'm not sure what you actually want to achieve. As a comment (for instance), your rows in the array you show are just binary numbers from 0 to 15. No comparison needed.....

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 4:37 am     Reply with quote

Thanks Ttelmah, I was beginning to believe I was the only one in the dark.

Mike
juanodiy



Joined: 07 Sep 2012
Posts: 17

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 10:22 am     Reply with quote

Ok, thanks to both, look I'm gonna show you my advance,

Code:

#include <18f452.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP

#include <math.h>
#include <lcd.c>
#include <stdlib.h>

#define RAND_MAX 256

int8 data=0,val,x,aux,i=0,m=0,y=0,p=0,flag=0,k=0,j=0;
char c[8],high[4],low[4];
short res=0,encontro=0;
float ai=0,aq=0.1;


char s1[16][4]={
{'0','0','0','0'},
{'0','0','0','1'},
{'0','0','1','0'},
{'0','0','1','1'},
{'0','1','0','0'},
{'0','1','0','1'},
{'0','1','1','0'},
{'0','1','1','1'},
{'1','0','0','0'},
{'1','0','0','1'},
{'1','0','1','0'},
{'1','0','1','1'},
{'1','1','0','0'},
{'1','1','0','1'},
{'1','1','1','0'},
{'1','1','1','1'},
};

void compare()
{
while(encontro!=1)
{
for(k=0;k<=15;k++)
{
   if(high[0]==s1[k][j] && high[1]==s1[k][j+1] && high[2]==s1[k][j+2] && high[3]==s1[k][j+3])
   {
      flag=k++;
      encontro=1;
   }
}
}
if(encontro==1)
{
lcd_putc("\f");
lcd_gotoxy(2,1);
printf(lcd_putc,"%u %u",flag,j);
switch(flag)   {
   case 1:
      ai=-0.821;
      aq=0.821;
      break;
   case 2:
      ai=-0.821;
      aq=0.22;
      break;
   case 3:
      ai=-0.821;
      aq=-0.22;
      break;
   case 4:
      ai=-0.821;
      aq=-0.821;
      break;
   case 5:
      ai=-0.22;
      aq=0.821;
      break;
   case 6:
      ai=-0.22;
      aq=0.22;
      break;
   case 7:
      ai=-0.22;
      aq=-0.22;
      break;
   case 8:
      ai=-0.22;
      aq=-0.821;
      break;
   case 9:
      ai=0.22;
      aq=0.821;
      break;
   case 10:
      ai=0.22;
      aq=0.22;
      break;
   case 11:
      ai=0.22;
      aq=-0.22;
      break;
   case 12:
      ai=0.22;
      aq=-0.821;
      break;
   case 13:
      ai=0.821;
      aq=0.821;
      break;
   case 14:
      ai=0.821;
      aq=0.22;
      break;
   case 15:
      ai=0.821;
      aq=-0.22;
      break;
   case 16:
      ai=0.821;
      aq=-0.821;
      break;
   default:
      lcd_gotoxy(2,2);
      printf(lcd_putc,"error");
      delay_ms(2000);
}
}
flag=0;
encontro=0;
lcd_gotoxy(1,2);
printf(lcd_putc,"listo");
lcd_gotoxy(7,2);
printf(lcd_putc,"%f %f",ai,aq);
}

void leer()
{
      read_adc(ADC_START_ONLY); //start the ADC reading
      //Now wait for the ADC to complete
      while (!adc_done()) ;
      val=read_adc(ADC_READ_ONLY); //read the value
}

void main()
{
   lcd_init();
   setup_adc(ADC_CLOCK_DIV_32);// a 32 div , 73 useg en el multiplexor a 8div, 60 us
   setup_adc_ports(RA0_RA1_RA3_ANALOG);
   set_adc_channel(0);
   lcd_putc("dato:");
   do {
      leer();
      //val=rand();
      delay_us(5); //ensure acquisition has completed
      lcd_putc("\f");
      lcd_gotoxy(1,1);
      printf(lcd_putc,"%u",val);
      for(x=0;x<=7;x++)
      {
      res=val%2;
      aux=val/2;
      val=aux;
      if(res==0){
      c[x]='0';
      }
      else
      {
      c[x]='1';
      }
      }
      for(i=0;i<=3;i++)
      {
      low[i]=c[i];
      }
      for(m=0;m<=3;m++)
      {
      high[m]=c[m+4];
      }
      lcd_gotoxy(1,2);
      printf(lcd_putc,"%s",low);
      lcd_gotoxy(6,2);
      for(y=0;y<=3;y++)
      {
      printf(lcd_putc,"%c",high[y]);
      }
      compare();
  } while(TRUE);
   
}



and it works, but not so efficiently , not so good, I think there is something that I'm doing wrong
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 3:39 pm     Reply with quote

I, for one, do not intend deciphering your code.

Please supply a SIMPLE explanation of the complete process.

What is your criterion for efficiency?

Is it speed, code size, simplicity, elegance, or what?

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Nov 07, 2012 3:56 pm     Reply with quote

Seriously, it is insane. flag at the end of the first part of the 'compare' routine, is just the numeric value you started with. The code looks to dismantle the binary value into individual bits, then look for patterns that are just the bit numbers, and give a result that was basically the original number. I'd say that everything in the main and compare, from the point the adc value is read, up to the switch, needs a maximum of one line. It shows a complete lack of understanding of what numbers 'are' and how they work...
oxo



Joined: 13 Nov 2012
Posts: 219
Location: France

View user's profile Send private message

PostPosted: Tue Nov 13, 2012 8:32 am     Reply with quote

Ttelmah wrote:
Seriously, it is insane. flag at the end of the first part of the 'compare' routine, is just the numeric value you started with. The code looks to dismantle the binary value into individual bits, then look for patterns that are just the bit numbers, and give a result that was basically the original number. I'd say that everything in the main and compare, from the point the adc value is read, up to the switch, needs a maximum of one line. It shows a complete lack of understanding of what numbers 'are' and how they work...


I agree. This sort of approach is found in those who don't understand the requirements before writing code.
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Nov 14, 2012 2:34 am     Reply with quote

There are now several of us telling you that you're going about it the wrong way.

I asked a while ago, please explain you're entire process from start to finish, in simple terms.

Then someone here will point you in a much better direction than the one you seem to have a fixation for.

BTW. Is this work, hobby, or school project?

Mike
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