# Programming- save serial data in an int variable

#### Pravin Gosavi

##### Member
Hello. I am using PIC microcontroller. I have created an interrupt subroutine. I want to get data from a port pin (say Data_Pin). Everytime interrupt occurs I want to update bits of a 16 bit variable one bit at a time starting from 1st bit to 16th bit depending on the state of the Data_Pin. In short I want to get data serialy using a port pin (not by other methods like UART).
I am stuck at how to "write" individual bits of the "int variable".

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
So... You want to sample a pin over course of time... Each sample needs to be shifted 16 times into an integer number.. the transmitted over Usart.

Assembler? C? this is such an easy task... If you vistit Nigels web site and look at software I2C or SPI it is virtually the same thing.

#### dknguyen

##### Well-Known Member
Shift operation. Be careful of automatic integer promotion though. It might screw up your results.

#### Pravin Gosavi

##### Member
I use MicroC PRO for PIC.

I could not completely understand the tutorials on Nigel's Website. Here's my code that needs some modification. I know it's easy but I'm bit confused for now. Any suggestions?

unsigned char nth_bit = 0;
unsigned int dat[16];

void interrupt()
{
if (INTF_bit == 1)
{
dat[nth_bit] = data_pin;
if(nth_bit < 16) nth_bit++;
}
INTF_bit = 0;
}

And the main loop is like-

void main()
{
if (latch_pin == 1)
{
nth_bit = 0;
INPUT_DATA = dat;
}
}

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
dat[nth_bit] = data_pin;
Depending on the quality of the copiler, this may not do what you expect..

The best way is to shift it in

If its one 16 bit variable:
C:
unsigned char nth_bit = 0;
unsigned int dat;

void interrupt()
{
if (INTF_bit == 1)
{
dat = data_pin;
dat<<=1;
nth_bit++;
}
INTF_bit = 0;
}
And change the main loop to:
C:
void main()
{
if (nth_bit == 16)
{
nth_bit = 0;
INPUT_DATA = dat;
}
}
I didn't test the code, it's an example..... I do not have MikroC for little pics....

Last edited: