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

Programming PIC16F87X as 10 bit ADC and reading it serially
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

Programming PIC16F87X as 10 bit ADC and reading it serially
PostPosted: Thu Mar 04, 2010 2:18 am     Reply with quote

Hello everyone....

I want to programme pic16f877 as 10 bit adc and read it serially such that it can be interfaced with a Mobile or Modem to send the data. The program should send AT commands for SMS then the Value V shown in the program below. Someone please suggest modification. It is urgent..

Quote:
#include<16F877.h>
#device ADC=10
#fuses HS,NOWDT
#use delay(clock=10000000)
#use re232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)
#include<stdio.h>
#include<math.h>
#include<STDLIB.H>

int16 value,value1,h,V,M;
void main(){
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(1);

while(1){
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);
delay_ms( 3000000);

value = read_adc();

h=0.0078201*value;
if(h<=4)
{
M=2*acos((4-h)/4);
V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
}
else
{
M=2*acos((h-4)/4);
V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
}
printf("%f/n/r",(float)V);
}}

_________________
zaky
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Thu Mar 04, 2010 3:10 am     Reply with quote

Someone please help me.. it is urgent....
_________________
zaky
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Thu Mar 04, 2010 4:31 am     Reply with quote

Reply please..........
_________________
zaky
Ttelmah
Guest







PostPosted: Thu Mar 04, 2010 4:55 am     Reply with quote

Comments in the code:
Code:

   while(1) {
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      delay_ms( 3000000);
      //If these delays worked, it'd be 8.3 hours, before you got here
      //For debugging, stick the delays after the transmission, so you can
      //see what happens.....
      //However, RTFM. What is the maximum value you can use in a
      //delay?......
      value = read_adc();

      h=0.0078201*value; //h=0 to 8 _integer_
      if(h<=4) {
         M=2*acos((4-h)/4);
         //If incoming value < 512
         //Just five possible values for 'M'. 1.5707, 1.318, 1.047, 0.7227, 0
         //Is this what you really want/mean?....
         V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
         //Again only five possible output values fo the whole 512 inputs

     }
     else {
        M=2*acos((h-4)/4);
        V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
     }
     printf("%f/n/r",(float)V);
  }
}

It'd be much simplr, if you posted what you actually expect your arithmetic to actually do?.

You seem to be forgetting at least one thing - h -is an integer, 0 to 8. with only nine possible output values for the arithmetic, it'd be much simpler just to have these stored and output them from a lookup table....

Read up in the data sheet on the recommended ADC clock. ADC_CLOCK_INTERNAL, is _not_ what should be used....

Best Wishes
Guest








PostPosted: Thu Mar 04, 2010 6:57 am     Reply with quote

Code:
#include<16F877.h>
#device ADC=10
#fuses HS,NOWDT
#use delay(clock=10000000)
#use re232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

#include<stdio.h>
#include<math.h>
#include<STDLIB.H>

float value,value1,h,V,M;

void main(){
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(1);

while(1){
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);
   delay_ms( 300000);

   value = read_adc();

   h=0.0078201*value;
   if(h<=4)
     {
      M=2*acos((4-h)/4);
      V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
     }
   else
     {
      M=2*acos((h-4)/4);
      V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
     }
   printf("%f/n/r",(float)V);
   }

}

I think it should be okay now with float. I used Turbo C to apply the same formula and it was working fine. These are to find out the volume of a oil in a cylinder.

My main problem is how edit this program to send AT commands followed by value V through SMS using a mobile ?
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Thu Mar 04, 2010 7:05 am     Reply with quote

Sorry

I posted the above comment without login...

Also check the commands for serial communication whether they are alright..

Zaky
_________________
zaky
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Fri Mar 05, 2010 2:19 am     Reply with quote

Hi everyone

I am getting the following error messages when i run it. Someone please confirm is it the right way to send AT commands followed by the adc result on a mobile phone.
Code:

#include<16F877.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,PUT
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

#include<stdio.h>
#include<math.h>
#include<STDLIB.H>

//#include "C:\Program files\PICC\Devices\16f877.h"   
#Byte PortA=0x05
#Byte PortB=0x06
#use fast_io(A)
#use fast_io(B)
#Define Nop #Asm Nop #EndAsm

int16 value;
float h,V,M;

void main(){
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(1);

while(1){
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);

   value = read_adc();

   h=0.0078201*value;
   if(h<=4)
     {
      M=2*acos((4-h)/4);
      V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
     }
   else
     {
      M=2*acos((h-4)/4);
      V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
     }
     
     send_SMS();

void send_SMS(void)
{
  printf("AT\r\n");
  delay_ms(5000);
  printf("at+cmgf=1\r\n");
  delay_ms(5000);
  printf("at+cmgs=\"+4478XXXXXXXX\"\r\n");
  delay_ms(5000);
  printf("%f/n/r",(float)V);
  putc(0x1A);
  delay_ms(5000);
}

void main()
{   
   SET_TRIS_A( 0b10000110 );
   SET_TRIS_B( 0b00000101 );   delay_ms(1);

   send_SMS();
 
     while(1)
     {
         // Toggle led
         PORTA ^= 1;
         // Simple delay
         delay_ms(500);
     }
}
}

After compiling it I am getting the following error messages:
Quote:

Error 12 Undefined identifier Send_SMS()
Error 117 Improper use of a function identifier in the line ""printf("AT\r\n");
Error31 line70 identifier is already used in this scope.

_________________
zaky
Ttelmah
Guest







PostPosted: Fri Mar 05, 2010 3:20 am     Reply with quote

You have two 'main' functions defined. Won't work.
The first doesn't have a closing bracket, so the whole bracketting 'goes to pot....
Basic syntax errors.

Best Wishes
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Fri Mar 05, 2010 4:33 am     Reply with quote

Thanks Ttelmah
Code:

#include<16F877.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,PUT
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

#include<stdio.h>
#include<math.h>
#include<STDLIB.H>

//#include "C:\Program files\PICC\Devices\16f877.h"   
#Byte PortA=0x05
#Byte PortB=0x06
#use fast_io(A)
#use fast_io(B)
#Define Nop #Asm Nop #EndAsm

int16 value;
float h,V,M;

void main(){
setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(1);

while(1){
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);
   delay_ms( 30000);

   value = read_adc();

   h=0.0078201*value;
   if(h<=4)
     {
      M=2*acos((4-h)/4);
      V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
     }
   else
     {
      M=2*acos((h-4)/4);
      V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
     }
     
     send_SMS();

void send_SMS(void)
{
  printf("AT\r\n");
  delay_ms(5000);
  printf("at+cmgf=1\r\n");
  delay_ms(5000);
  printf("at+cmgs=\"+4478XXXXXXXX\"\r\n");
  delay_ms(5000);
  printf("%f/n/r",(float)V);
  putc(0x1A);
  delay_ms(5000);
}

void main()
{   
   SET_TRIS_A( 0b10000110 );
   SET_TRIS_B( 0b00000101 );   delay_ms(1);

   send_SMS();
 
     while(1)
     {
         // Toggle led
         PORTA ^= 1;
         // Simple delay
         delay_ms(500);
     }
}
}

Now I am getting the error message
"Improper use of function identifier" at line printf("AT\r\n");
and undefined identifier at line "send_SMS();"
_________________
zaky
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Fri Mar 05, 2010 6:22 am     Reply with quote

Again you have TWO main() functions. This will not work, then you have some statements outside the {} brackets.

Then in send_sms function you are using variables, that are not declared there...
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Fri Mar 05, 2010 7:28 am     Reply with quote

Someone please remove the errors... I am really stuck...
_________________
zaky
bungee-



Joined: 27 Jun 2007
Posts: 206

View user's profile Send private message

PostPosted: Fri Mar 05, 2010 10:44 am     Reply with quote

We certainly could do that, but errors there are so trivial and by doing so you would not learn anything.

But I'll give you a hint structure ....

Code:

#FUSES

int some_global_variable;

void some_function1()
{
   do_something();
}

void other_function(int pass_variable)
{
   printf(" %X \r\n",pass_variable);
}

void main()
{
        int Local_variable;

   do_setup();
   
   while(1)
   {
      do_some_operations();
      some_function1();

      other_function(35);
   }
}


Observe where I declared functions and where I placed function main(). Global variable can be used by all functions, where Locab_variable can be used only in function main.
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Sun Mar 07, 2010 11:58 pm     Reply with quote

Code:

#include<16F877.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NOBROWNOUT,PUT
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=

#include<stdio.h>
#include<math.h>
#include<STDLIB.H>

//#include "C:\Program files\PICC\Devices\16f877.h"
#Byte PortA=0x05
#Byte PortB=0x06
#use fast_io(A)
#use fast_io(B)
#Define Nop #Asm Nop #EndAsm

int16 value;
float h,V,M;

void send_SMS(void)
{
printf("AT\r\n");
delay_ms(5000);
printf("at+cmgf=1\r\n");
delay_ms(5000);
printf("at+cmgs=\"+923469400983\"\r\n");
delay_ms(5000);
printf("%f/n/r",(float)V);
putc(0x1A);
delay_ms(5000);
}


void main(){


setup_adc( ADC_CLOCK_INTERNAL );
setup_adc_ports( ALL_ANALOG );
set_adc_channel(1);

SET_TRIS_A( 0b10000110 );
SET_TRIS_B( 0b00000101 ); delay_ms(1);


while(1){
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);
delay_ms( 30000);

value = read_adc();

h=0.0078201*value;
if(h<=4)
{
M=2*acos((4-h)/4);
V=10*(16*M/2-16*sin(M/2)+h*4*sin(M/2));
}
else
{
M=2*acos((h-4)/4);
V=10*(16*3.14159*(1-M/(2*3.14159))+h*4*sin(M/2)-16*sin(M/2));
}

send_SMS();

}
}

Now I am getting the error message Line94(0,1) Out of ROM, a segment or the program is too large sin.
_________________
zaky
yousafzai82



Joined: 04 Mar 2010
Posts: 13
Location: Pakistna

View user's profile Send private message AIM Address Yahoo Messenger ICQ Number

PostPosted: Mon Mar 08, 2010 2:35 am     Reply with quote

Someone tell me how to use #separate functions or use some other means to remove the above error....
_________________
zaky
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Mon Mar 08, 2010 4:27 am     Reply with quote

I compiled your program and it didn't show any error.
Why it is showing an error at your side?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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