PIC16f877 and mikroC (data acquisition)

Beta Testing discussion on mikroC PRO for PIC.
Post Reply
Author
Message
x saber x
Posts: 3
Joined: 17 Jul 2014 18:29

PIC16f877 and mikroC (data acquisition)

#1 Post by x saber x » 17 Jul 2014 19:14

hi,
to begin, i want to say that my level in english is average, so forgive me for the mistakes..
i am a beginner in C programming and this is the first time i work with mikroC.
so i want to write a code and i need your help ..
the code should do the following :

first : PORTB.f0=1

when i send the letter 'c'
i must receive the data that is on the port 'RA0'

when i send again the letter 'c'
i must receive the data that is on the port 'RA1'

when i send again the letter 'c'
i must receive the data that is on the port 'RA2'

when i send again the letter 'c'
i must receive the data that is on the port 'RA3'

when i send again the letter 'c'
i must receive the data that is on the port 'RA5'

when i send again the letter 'c'
i must receive the data that is on the port 'RE0'

PORTB.f0=0

when i send the letter 'c'
i must receive the data that is on the port 'RA0'

when i send again the letter 'c'
i must receive the data that is on the port 'RA1'

when i send again the letter 'c'
i must receive the data that is on the port 'RA2'

when i send again the letter 'c'
i must receive the data that is on the port 'RA3'

when i send again the letter 'c'
i must receive the data that is on the port 'RA5'

when i send again the letter 'c'
i must receive the data that is on the port 'RE0'

now the code should return to the beginning and expected that i send the letter 'c'
---

I actually have this code

Code: Select all

float rtdv, rtdr, temp,  y;
int msd, isd, i;
char temperature[6];
char uart_rd;

calcule()
{
rtdv=rtdv *5/1024;
rtdv=rtdv/5;
rtdr=rtdv*1000.0/(5.0-rtdv);
y=0.15274-(rtdr-100.0)*0.0002310;
if (y>=0)y = sqrt(y);
temp = (y-0.39083)/(-0.0001155);
bytetostr(temp,temperature);

delay_ms(1);
}

affichage() 
{
UART1_Write_Text(temperature);
strcpy(temperature,"");
}

void main()
{

TRISA = 0xFF;  
TRISE = 0xFF;
TRISB = 0x00;  

PORTB = 1;      

UART1_Init(9600);

while(1)                             
{       portb.f1=1;
        if (UART1_data_ready())
         {
                  uart_rd = UART1_read();
                  if(uart_rd=='c')           
                  {
                        PORTB.f0=1;        

                        for (i=0; i<6 ;i++)
                        {
                         delay_ms(5);          
                         rtdv=adc_read(i);
                         UART1_Write(13);        
                         calcule();              
                         affichage();         
                         }

                        PORTB.f0=~portb.f0;      
                        
                        for (i=0; i<6; i++)
                        {
                         delay_ms(5);
                         rtdv=adc_read(i);
                         UART1_Write(13);        
                         calcule();             
                         affichage();       
                         }
                 }
         }
}
}
but it does not satisfy my need..
in this code, when i sent the letter 'c' i receive all data contained on ports..

could you help me modify this code or write one that works according to algorthme above?

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: PIC16f877 and mikroC (data acquisition)

#2 Post by Muphy » 11 Sep 2014 13:31

The reason this is happening is that you are reading all the AD ports in your loop. If you want to read each port everytime you press the C button then something like this would do it. Crude but it works

Code: Select all


float rtdv, rtdr, temp,  y;
int msd, isd, i;
char temperature[6];
char uart_rd;
int test_port;

calcule()
{
rtdv=rtdv *5/1024;
rtdv=rtdv/5;
rtdr=rtdv*1000.0/(5.0-rtdv);
y=0.15274-(rtdr-100.0)*0.0002310;
if (y>=0)y = sqrt(y);
temp = (y-0.39083)/(-0.0001155);
bytetostr(temp,temperature);

delay_ms(1);
}

affichage() 
{
UART1_Write_Text(temperature);
strcpy(temperature,"");
}

void main()
{

TRISA = 0xFF;  
TRISE = 0xFF;
TRISB = 0x00;  

PORTB = 1;      
test_port =1;
UART1_Init(9600);

while(1)                             
{       portb.f1=1;
        if (UART1_data_ready())
         {
                  uart_rd = UART1_read();
                  if(uart_rd=='c')           
                  {
                        PORTB.f0=1;        

                         delay_ms(5);          
                         rtdv=adc_read(test_port);
                         UART1_Write(13);        
                         calcule();              
                         affichage();         
                   

                         PORTB.f0=~portb.f0;      
                        
                         delay_ms(5);
                         rtdv=adc_read(test_port);
                         UART1_Write(13);        
                         calcule();             
                         affichage();       
                         test_port++;
                         if(test_port>6)
                             test_port=1;

                 }
         }
}
}

Note that defining an int for test_port is overkill, I only do it for illustration purposes :-)
HTH

Mark

Post Reply

Return to “mikroC PRO for PIC Beta Testing”