Can someone check my mikroC code ? It looks easy but not work yet. :-(

Status
Not open for further replies.

boyguitar1

Member level 1
Joined
Nov 8, 2010
Messages
33
Helped
12
Reputation
24
Reaction score
12
Trophy points
1,288
Activity points
1,486
Hello...Sir

Could you please help check my code as below ?
I use PIC16F737 (28pins)
I just want to read ADC by 4 channels (RA0,RA1,RA2,RA3)

RA0 for 1 ~ 1.5 vdc
RA1 for 2 ~ 2.5 vdc
RA2 for 3 ~ 3.5 vdc
RA3 for 4 ~ 4.5 vdc

if RA0 can read at 1 ~ 1.5 vdc then LED1 on [PORTB.F0]
if RA1 can read at 2 ~ 2.5 vdc then LED2 on [PORTB.F1]
if RA2 can read at 3 ~ 2.5 vdc then LED3 on [PORTB.F2]
if RA3 can read at 4 ~ 2.5 vdc then LED4 on [PORTB.F3]

2 day ago..it still does not work.

--------------------------------------------------------------

Please help advice me too...:-(
Thank you in advance...

Code:
// PIC16F737
// Clock 4 MHZ

long  analog_val;
long  analog_val2;
long  analog_val3;
long  analog_val4;
int i=0;


void main()
{
  TRISB  = 0B00000000;   // Set PORTC as output
  ADCON1 = 0x87;         // Configure analog
  TRISA  = 0xFF;        // Set PORTA as Input
  PORTB  = 0B00000000;   // Clear PORTB

  while (1)
{


            analog_val = Adc_Read(0);     //
            Delay_ms(10);
            analog_val = ((analog_val*2000)/2046);

            if (analog_val >= 180 && analog_val <= 380){portb.f0=1;}
            else { portb.f0 =0;}
               
          /******************************************************************/
               
            analog_val2 = Adc_Read(1);
            Delay_ms(10);
            analog_val2 = ((analog_val2*2000)/2046);
            
            if (analog_val2 >= 380 && analog_val2 <=580){portb.f1=1;}
            else { portb.f1 =0;}
         /******************************************************************/
         
            analog_val3 = Adc_Read(2);
            Delay_ms(10);
            analog_val3 = ((analog_val3*2000)/2046);

            if(analog_val3 >= 580 && analog_val3 <= 780){portb.f2=1;}
            else { portb.f2 =0;}
            /******************************************************************/
            
            analog_val4 = Adc_Read(3);
            Delay_ms(10);
            analog_val4 = ((analog_val4*2000)/2046);

            if(analog_val4 >= 780 && analog_val4 < 1023) {portb.f3=1;}
            else { portb.f3 =0;}
} 
}
 

Did you put #include<p16f737.h> ?
 

hey..try this..

Code:
long  analog_val;
long  analog_val2;
long  analog_val3;
long  analog_val4;
int i=0;


void main()
{
  TRISB  = 0B00000000;   // Set PORTC as output
  ADCON1 = 0x87;         // Configure analog
  TRISA  = 0xFF;        // Set PORTA as Input
  PORTB  = 0B00000000;   // Clear PORTB

  while (1)
{


            analog_val = Adc_Read(0);     //
            Delay_ms(10);

            if (analog_val >= 307 && analog_val3 <= 512){portb.f0=1;}
            else { portb.f0 =0;}

          /******************************************************************/

            analog_val2 = Adc_Read(1);
            Delay_ms(10);

            if (analog_val2 >= 512 && analog_val3 <= 716){portb.f1=1;}
            else { portb.f1 =0;}
         /******************************************************************/

            analog_val3 = Adc_Read(2);
            Delay_ms(10);

            if(analog_val3 >= 716 && analog_val3 <= 920){portb.f2=1;}
            else { portb.f2 =0;}
            /******************************************************************/

            analog_val4 = Adc_Read(3);
            Delay_ms(10);

            if(analog_val4 >= 920 && analog_val4 < 1023) {portb.f3=1;}
            else { portb.f3 =0;}
}
}


---------- Post added at 18:26 ---------- Previous post was at 18:09 ----------

if it dosnt work, try this, i thinkthis is what u want..

Code:
long  analog_val;
long  analog_val2;
long  analog_val3;
long  analog_val4;
int i=0;


void main()
{
  TRISB  = 0B00000000;   // Set PORTC as output
  ADCON1 = 0x87;         // Configure analog
  TRISA  = 0xFF;        // Set PORTA as Input
  PORTB  = 0B00000000;   // Clear PORTB

  while (1)
{


            analog_val = Adc_Read(0);     //
            Delay_ms(10);

            if (analog_val >=205  && analog_val3 <= 307){portb.f0=1;}
            else { portb.f0 =0;}

          /******************************************************************/

            analog_val2 = Adc_Read(1);
            Delay_ms(10);

            if (analog_val2 ==409  && analog_val3 <= 512){portb.f1=1;}
            else { portb.f1 =0;}
         /******************************************************************/

            analog_val3 = Adc_Read(2);
            Delay_ms(10);

            if(analog_val3 >=615  && analog_val3 <= 716){portb.f2=1;}
            else { portb.f2 =0;}
            /******************************************************************/

            analog_val4 = Adc_Read(3);
            Delay_ms(10);

            if(analog_val4 >=820  && analog_val4 < 920) {portb.f3=1;}
            else { portb.f3 =0;}
}
}
 

Did you put #include<p16f737.h> ?

mikroC doesn't need that. It includes everything on its own when you select 16f737 as your target PIC.

I'm taking a look at the code.

---------- Post added at 18:42 ---------- Previous post was at 18:31 ----------

Hi,
Try this:
Code:
// PIC16F737
// Clock 4 MHZ

unsigned int  analog_val;
unsigned char analog_value;
int i=0;


void main()
{
  TRISB  = 0B00000000;   // Set PORTC as output
  ADCON1 = 0x87;         // Configure analog
  TRISA  = 0xFF;        // Set PORTA as Input
  PORTB  = 0B00000000;   // Clear PORTB

  while (1)
{


            analog_val = Adc_Read(0);     //
            Delay_ms(10);

            if (analog_val >= 204 && analog_val <= 307){portb.f0=1;}
            else { portb.f0 =0;}

          /******************************************************************/

            analog_val = Adc_Read(1);

            if (analog_val2 >= 409 && analog_val2 <=512){portb.f1=1;}
            else { portb.f1 =0;}
         /******************************************************************/

            analog_val = Adc_Read(2);

            if(analog_val3 >= 614 && analog_val3 <= 717){portb.f2=1;}
            else { portb.f2 =0;}
            /******************************************************************/

            analog_val = Adc_Read(3);

            if(analog_val4 >= 819 && analog_val4 < 921) {portb.f3=1;}
            else { portb.f3 =0;}
}
}

//RA0 for 1 ~ 1.5 vdc  204-307
//RA1 for 2 ~ 2.5 vdc  409-512
//RA2 for 3 ~ 3.5 vdc  614-717
//RA3 for 4 ~ 4.5 vdc  819-921
//
//if RA0 can read at 1 ~ 1.5 vdc then LED1 on [PORTB.F0]
//if RA1 can read at 2 ~ 2.5 vdc then LED2 on [PORTB.F1]
//if RA2 can read at 3 ~ 2.5 vdc then LED3 on [PORTB.F2]
//if RA3 can read at 4 ~ 2.5 vdc then LED4 on [PORTB.F3]

I think this should work.

Hope this helps.
Tahmid.
 

hi john blue, thank for replied but in mikroC no this function.

Hi lockman,Hi Tahmid
Thank you for the code... today I will try it again n any problem will feedback here.


BGT
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…