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

ADC and serial transmission help

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



Joined: 05 May 2011
Posts: 2

View user's profile Send private message

ADC and serial transmission help
PostPosted: Thu May 05, 2011 8:16 pm     Reply with quote

I am working on a project where i need to measure current and voltage values continuously and send it to zigbee.At receiver's end i am using one more zigbee and pic.In receiver's end i need to get the values continuously and display it on lcd.Pins b1,b2,b3 and b4 are connected to switches.When b1 is pressed voltage should be displayed,b2 current and b3 power.I am not able to do this continuously.I have to reset pic after executing once.How to do this continuously without resetting each time ?

In the transmitter side i need 10 bit adc to measure current.How to do that and transmit the 10 bit value through rs232?Can this be done in ccs?

I am using pic16f877a and ccs compiler.I am sending the transmitter and receiver side code

Transmitter side code

#include <16f877a.h>
#fuses HS,NOWDT
#use delay(clock=4000000)
#use RS232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#byte porta=0x05
#byte portc=0x07
#byte portb=0x06


void main()
{
unsigned int value;
unsigned int fvalue;
setup_port_a(ALL_ANALOG);
set_tris_a(0x03);
set_tris_c(0x80);
set_adc_channel(0);
delay_ms(10);
fvalue=read_adc();
putc(fvalue);

set_adc_channel(1);
delay_ms(10);
value=read_adc();
putc(value);

}


Receiver side code

#include<16f877a.h>
#use delay(clock=4000000)
#fuses HS,NOWDT,NOLVP
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7)
#byte porta=05
#byte portb=06
#byte portc=07
#byte portd=0x08
#byte porte=0x09
#bit rs=porte.0
#bit rw=porte.1
#bit en=porte.2
#byte lcd=portd
void lcd_init();
void lcd_putc(char data);
void lcd_display(char var);
void main()
{
unsigned int value;
unsigned int fvalue;

float v1,i1,power;
set_tris_a(0xff);
set_tris_b(0xff);
set_tris_c(0x80);
set_tris_d(0x00);
set_tris_e(0x00);
port_b_pullups(true);
lcd_init();
lcd_display(0x80);
printf(lcd_putc,"%s","SKIT college");

while(1)
{




if(!input(pin_b1) )
{

fvalue=getc();
v1=(5*((float)fvalue)/255);
lcd_display(0x80);
printf(lcd_putc,"%s","voltage ");
lcd_display(0xc0);
printf(lcd_putc,"%f",v1);

}

if(!input(pin_b2) )
{

value=getc();
i1=(5*((float)value)/255);
lcd_display(0x80);
printf(lcd_putc,"%s","current ");
lcd_display(0xc0);
printf(lcd_putc,"%f",i1);
}

if(!input(pin_b3) )
{
power=v1*i1*0.8;
lcd_display(0x80);
printf(lcd_putc,"%s","power ");
lcd_display(0xc0);
printf(lcd_putc,"%f",power);
}


}

}

void lcd_init()
{
int i=0;
char cmd[4]={0x38,0x0e,0x01,0x06};
for (i=0;i<4;i++)
{
lcd=cmd[i];
rs=0;
rw=0;
en=1;
delay_ms(25);
en=0;
}
}

void lcd_putc(char data)
{
lcd=data;
rs=1;
rw=0;
en=1;
delay_ms(25);
en=0;
}
void lcd_display(char var)
{
lcd=var;
rs=0;
rw=0;
en=1;
delay_ms(25);
en=0;
}
_________________
jayashree
ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Thu May 05, 2011 9:16 pm     Reply with quote

Hi,

To your specific question, you are probably overflowing the serial input buffer and causing your code to hang. Add the 'Errors' directive to the #use rs232 statement.

A couple of other things: You need to add the NOLVP fuse to the transmit code. You don't appear to be using any switch debouncing? Make your life easier by using 'Standard I/O' and get rid of all the TRIS statements. Make your life easier and use the Flex_LCD routines. Finally, clean up your code by getting rid of all the Port and Bit defines. At this point in your 'C' programming career, just use something like #define Switch1 Pin_B2 - your code will be much easier to read.

One last thing, use the CODE button for posting code in the forums.

John
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu May 05, 2011 9:41 pm     Reply with quote

Along with all the previously said help..
It looks like your transmitter only sends 2 readings then goes to sleep. If you want continuous data, you'll have to have some form of loop..
Also you should have a way to tell WHICH reading is being sent(volts or amps).
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Fri May 06, 2011 6:38 am     Reply with quote

10 bit A/D readings will be stored in a PIC as a 2 byte quantity. To send by RS232 you just send the 2 bytes. Look at the functions make8() and make16() to see how to pack and unpack bytes.
_________________
The search for better is endless. Instead simply find very good and get the job done.
jayashree



Joined: 05 May 2011
Posts: 2

View user's profile Send private message

PostPosted: Sat May 07, 2011 10:06 pm     Reply with quote

Thanks all for the help. I am still left with one problem. I am using ACS714 hall sensor to measure current values. The output of hall sensor is ac. So its rectified and given to channel 1. I am getting erratic values and I am not able to read 10 bit adc even after including #device adc=10.

Code:
 
//Transmitter
#include<16f877a.h>
#device ADC=10
#use delay(clock=4000000)
#fuses HS,NOWDT,NOLVP
#use rs232(baud=9600,xmit=pin_c6,rcv=pin_c7,errors)

#byte porta=05
#byte portb=06
#byte portc=07

void main()
{
unsigned int value;
unsigned int fvalue;

set_tris_a(0xff);
set_tris_b(0x00);
set_tris_c(0x80);

while(1)
{
    putc('V');
    setup_adc_ports(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAl);
    set_adc_channel(0);
    fvalue=read_adc();
    putc(fvalue);
    delay_ms(5);

    putc('C');
    setup_adc_ports(ALL_ANALOG);
    setup_adc(ADC_CLOCK_INTERNAL);
    set_adc_channel(1);
    value=read_adc();
    putc(value);
    delay_ms(5);
}
}

_________________
jayashree
temtronic



Joined: 01 Jul 2010
Posts: 9228
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun May 08, 2011 5:23 am     Reply with quote

Re-read SherpaDoug's reply !!!
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun May 08, 2011 6:08 am     Reply with quote

These declarations:

unsigned int value;
unsigned int fvalue;

declare 8 bit integers. There is no way for them to hold 10 bit values. You need to declare longer variables. And of course putc() is also only meant for 8 bits.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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