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

ADC read error when data is receieved from RS232 wiht GETS()

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







ADC read error when data is receieved from RS232 wiht GETS()
PostPosted: Mon Jan 29, 2007 6:02 am     Reply with quote

Dear Sir,
This program is received a data from PC (pc sent 3 chr int and there are an ascii13 chr end of this data)
ADC read a value (10 bit) and sent it to pc
this sytem work perfect when without received data; when I start to chrs from pc, ADC value start to incorrect sometimes (if adc is 8bit inccorrect value 0, if adc is 10 bit incorrect value 768,
I mean when I start pic,
I see a value on pc
for example
(adc 8 bit and potantiometer half is connected A0)
89
89
87
87
89
88
all value is correct
when I start to data from pc to Pic
data is Pc screen;
87
88
89
87
88
89
0 // (error)
87
88
0 //(error)
88
0 // (error)

What is the reason of this error?



#include <18F452.h>
#use delay (clock=40000000)
#include <stdio.h>
#include <stdlib.h>
#include <lcd.c>
#include <math.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=57600, xmit=PIN_C6,rcv=PIN_C7,stream=PC)


int32 timersondeger=0,CARPIM=0;
int32 xxx=0,sayici,sayici2=0;
short kontrol=0;
char acich[3];
int16 throttle=0,aci=450,devir=0,sondeg=0,kalan=0,tam=0,thhata;
int8 i=0,k=0,x=0,mn;





#INT_RDA
void rs232()
{

gets(acich);

}



void setup()
{
set_tris_a(0b001111);
setup_adc_ports(RA0_RA1_ANALOG_RA3_REF);
setup_adc(ADC_CLOCK_INTERNAL );

set_adc_channel(0);

setup_psp(PSP_DISABLED);
setup_spi(FALSE);
ext_int_edge( 0, L_TO_H);
ext_int_edge( 1, L_TO_H);
enable_interrupts(global);
enable_interrupts(int_rda);
lcd_init();

}

void main()
{
setup();

while(true)
{

lcd_putc("\f") ;
lcd_putc("aci:"); printf(LCD_PUTC, "%ld",aci);
lcd_putc("\n");
lcd_putc("kel:"); printf(LCD_PUTC, "%lU",throttle);
lcd_putc(" d:"); printf(LCD_PUTC, "%lu",devir);
aci=atol(acich);

delay_ms(1);
thhata=throttle;
throttle=Read_ADC();

throttle++;


printf("%4lud",devir);
aci=atol(acich);
printf("%04luy",throttle);




}
}
Ttelmah
Guest







PostPosted: Mon Jan 29, 2007 1:37 pm     Reply with quote

First, don't use gets, in an interrupt handler.
When the interrupt is called, potentially just _one_ character is waiting. gets, waits for an entire string (up to the terminating character, and using 'gets', means everthing in the main, will be stopped till the whole string arrives. Just get one character in the interrupt, and have your own code to build the string and terminate it.
Second comment, be careful about lengths. You only allow three characters of storage for your input string. String storage must be enough to hold the terminating character, as well as the string itself. I'd guess that sometimes this string is overrunning, and destroying data held afterwards. When you write your own input function, make it stop when the buffer is full...
Learn to use the code buttons.
Why do you have two setups forexternal interrupts that are not used?.

Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Jan 29, 2007 4:22 pm     Reply with quote

Quote:
#use delay (clock=40000000)
You are running at 40MHz? If yes, than your fuse settings are wrong. 40MHz requires the EC or EC_IO fuse and an external 40MHz driver or the H4 fuse setting with a 10MHz crystal.
hamham
Guest







PostPosted: Tue Jan 30, 2007 10:11 am     Reply with quote

Ttelmah wrote:
First, don't use gets, in an interrupt handler.
When the interrupt is called, potentially just _one_ character is waiting. gets, waits for an entire string (up to the terminating character, and using 'gets', means everthing in the main, will be stopped till the whole string arrives. Just get one character in the interrupt, and have your own code to build the string and terminate it.
Second comment, be careful about lengths. You only allow three characters of storage for your input string. String storage must be enough to hold the terminating character, as well as the string itself. I'd guess that sometimes this string is overrunning, and destroying data held afterwards. When you write your own input function, make it stop when the buffer is full...
Learn to use the code buttons.
Why do you have two setups forexternal interrupts that are not used?.


Best Wishes
I erased the not important codes, thank you for your information I try to use getch command.
HAMHAM
Guest







PostPosted: Tue Jan 30, 2007 10:13 am     Reply with quote

ckielstra wrote:
Quote:
#use delay (clock=40000000)
You are running at 40MHz? If yes, than your fuse settings are wrong. 40MHz requires the EC or EC_IO fuse and an external 40MHz driver or the H4 fuse setting with a 10MHz crystal.

I cant understant What is the EC or EC_IO please explanain them
Ttelmah
Guest







PostPosted: Tue Jan 30, 2007 10:31 am     Reply with quote

The internal oscillator, is not rated to work beyond 25MHz. To work at 40MHz, requires you to either use a 10MHz crystal, and HSPLL fuse, or feed an external clock at 40Mhz into the chip, and select EC, or EC_IO, which are designed to accept such an input.

Best Wishes
Guest








PostPosted: Tue Jan 30, 2007 3:19 pm     Reply with quote

Ttelmah wrote:
The internal oscillator, is not rated to work beyond 25MHz. To work at 40MHz, requires you to either use a 10MHz crystal, and HSPLL fuse, or feed an external clock at 40Mhz into the chip, and select EC, or EC_IO, which are designed to accept such an input.

Best Wishes

but my circuit is running well with hs and 40 mhz osc. how is it running ? and can I see a problem in the future with my configuration (hs and 40 mhz osc)
Guest








PostPosted: Tue Jan 30, 2007 3:49 pm     Reply with quote

Ttelmah wrote:
First, don't use gets, in an interrupt handler.
When the interrupt is called, potentially just _one_ character is waiting. gets, waits for an entire string (up to the terminating character, and using 'gets', means everthing in the main, will be stopped till the whole string arrives. Just get one character in the interrupt, and have your own code to build the string and terminate it.
Second comment, be careful about lengths. You only allow three characters of storage for your input string. String storage must be enough to hold the terminating character, as well as the string itself. I'd guess that sometimes this string is overrunning, and destroying data held afterwards. When you write your own input function, make it stop when the buffer is full...
Learn to use the code buttons.
Why do you have two setups forexternal interrupts that are not used?.

Best Wishes


#fuses EC_IO,NOWDT,NOPROTECT,NOLVP, BROWNOUT, PUT
#use rs232(baud=57600, xmit=PIN_C6,rcv=PIN_C7,stream=PC,ERRORS)
#INT_RDA
void rs232()
{
a=getc();
if (a==97)
{
acii[0]=acich[0]; acii[1]=acich[1]; acii[2]=acich[2]//an error occured in the lcd
}

for (i=0;i<3;i++)
{
acich[i-1]=acich[i];
}
acich[2]=a;
}
//in the main about these commands

lcd_putc("aci:"); printf(LCD_PUTC, "%S",acii);
throttle=Read_ADC();

I change the interrupt with getch but
I want to get 3 character from rs232,
I am shifting the array value and When I see the "a" character (ascii:97) I want to sent first 3 byte in the array (acich 0,1,2) but when I use the following commands
"acii[0]=acich[0]; acii[1]=acich[1]; acii[2]=acich[2]"
I saw different character on the LCD. IT is unbelievable because I am not sending an other value to lcd.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Jan 30, 2007 4:04 pm     Reply with quote

Please, when posting code use the 'code' button. This will help to preserve the formatting and makes for easier reading.

What does your clock circuit look like? Is it a crystal or a clock generator? EC_IO is only to be used with a clock driver circuit, not with a crystal.

Code:
for (i=0;i<3;i++)
{
  acich[i-1]=acich[i];
}
Bug: When i==0 then achich[i-1] will overwrite other data in RAM... leading to unexpected results.
Guest








PostPosted: Wed Jan 31, 2007 1:04 am     Reply with quote

ckielstra wrote:
Please, when posting code use the 'code' button. This will help to preserve the formatting and makes for easier reading.

What does your clock circuit look like? Is it a crystal or a clock generator? EC_IO is only to be used with a clock driver circuit, not with a crystal.

Code:
for (i=0;i<3;i++)
{
  acich[i-1]=acich[i];
}
Bug: When i==0 then achich[i-1] will overwrite other data in RAM... leading to unexpected results.

I am using a part which has 4 leg. I dont know exactly if it is clock generator or cyristal.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jan 31, 2007 2:43 am     Reply with quote

Anonymous wrote:
I am using a part which has 4 leg. I dont know exactly if it is clock generator or cyristal.

http://www.ccsinfo.com/forum/viewtopic.php?t=29630
Than... if your PIC works with the EC_IO fuse it must be a generator.
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