|
|
View previous topic :: View next topic |
Author |
Message |
hamham Guest
|
ADC read error when data is receieved from RS232 wiht GETS() |
Posted: Mon Jan 29, 2007 6:02 am |
|
|
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
|
|
Posted: Mon Jan 29, 2007 1:37 pm |
|
|
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
|
|
Posted: Mon Jan 29, 2007 4:22 pm |
|
|
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
|
|
Posted: Tue Jan 30, 2007 10:11 am |
|
|
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
|
|
Posted: Tue Jan 30, 2007 10:13 am |
|
|
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
|
|
Posted: Tue Jan 30, 2007 10:31 am |
|
|
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
|
|
Posted: Tue Jan 30, 2007 3:19 pm |
|
|
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
|
|
Posted: Tue Jan 30, 2007 3:49 pm |
|
|
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
|
|
Posted: Tue Jan 30, 2007 4:04 pm |
|
|
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
|
|
Posted: Wed Jan 31, 2007 1:04 am |
|
|
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
|
|
Posted: Wed Jan 31, 2007 2:43 am |
|
|
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. |
|
|
|
|
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
|