View previous topic :: View next topic |
Author |
Message |
arys
Joined: 09 Mar 2007 Posts: 31
|
PIC's variable changing through PC |
Posted: Tue Jan 15, 2008 3:09 am |
|
|
Hi, is it possible to do that? I never try it but it's in my mind. If can, how? or may be there are links for that. Actually I want to change through VB. |
|
|
D-Kens
Joined: 09 May 2005 Posts: 35 Location: Toulouse (France)
|
|
Posted: Tue Jan 15, 2008 3:44 am |
|
|
Assuming that you use port COM of your computer to connect to the RS232 of your PIC, you just have to scan for incoming characters (using int_rda or just kbhit() function), extract the desired value for the delay and copy it in a global variable or in EEPROM or wherever you want. Then you can use this value for anything in your programme... |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Tue Jan 15, 2008 9:14 am |
|
|
Be careful tho, as it says in the help file
Quote: | time - a variable 0-255 or a constant 0-65535 |
if you make it a variable you only can go 0-255 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Jan 15, 2008 11:31 am |
|
|
treitmey wrote: | Be careful tho, as it says in the help file
Quote: | time - a variable 0-255 or a constant 0-65535 |
if you make it a variable you only can go 0-255 | The V4 compiler improved this and allows the variable to be int16 (0 - 65535) as well. |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
generate ~16khz pulses ??? |
Posted: Thu Feb 14, 2008 2:58 am |
|
|
Hi, thanks for the ideas. I've tried out. but its not work. when I send any 0-255 from VB it generates ~16kHz. then it doesnt want to change the second value (from VB). seems like int_rda doesnt work. here the codes
Code: |
int8 x;
int8 j;
int8 y;
#int_rda
void rda_isr(void)
{
char c;
char string[10];
c = getc(); // Get character from PC
putc(c); // Send it back to the pc
strcpy(string,"c");
x=atoi(String);
}
void main()
{ x=145; //initial value
while(1)
{ enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
for(y=0; y<=x; y++);{
output_b(0b00000001);
delay_us(10);
}
for(j=0; j<=x; j++);{
output_b(0b00000000);
delay_us(10);
}
}
}
| . I want to try get_string() function but I dont know how.
|
|
|
D-Kens
Joined: 09 May 2005 Posts: 35 Location: Toulouse (France)
|
|
Posted: Thu Feb 14, 2008 3:04 am |
|
|
No matter what character you send, you copy the string "c" to your string variable, and not the character stored in c ! |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
see output/result |
Posted: Tue Feb 19, 2008 3:39 am |
|
|
thanks D-Kens. I add few function. seems like it work.
Code: | c_ptr=&c;
strcpy(string,c_ptr);
x=atoi(string); |
but how can I see the result of x, since I only have CCS and MPLAB. |
|
|
D-Kens
Joined: 09 May 2005 Posts: 35 Location: Toulouse (France)
|
|
Posted: Tue Feb 19, 2008 4:05 am |
|
|
Don't you have the Serial Port Monitor in the "Tools" of your compiler, or Hyperterminal on your computer ? You can ask your main() to write to the console :
Code: | printf("x=%d\n\r", x) ; |
For test purpose, you should also add a delay of a few seconds, else you'll flood the screen with lines and lines of "x=..." ! |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Wed Feb 20, 2008 4:00 am |
|
|
many thanks. I try to use siow but nothing happen. what i did was, open siow --> open file **.c --> send hex number.
How to use siow correctly? |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Feb 20, 2008 7:18 am |
|
|
I have a major problem with your code.
Quote: |
c_ptr=&c;
strcpy(string,c_ptr);
x=atoi(string);
|
strcpy relies on NULL terminated strings. c is a single char in memory, there is no way of knowinf what value comes after it in memory. you seem to have been very lucky in that a null terminating character must come quite soon after the initial char otherwise the string array would overrun and you will corrupt memory.
you can either use strncpy(string, &c, 1) to copy just the 1 char or
string[0] = c;
string[1] = '\0';
and then use atoi(string).
The other option is to just do x = c - '0'; to retrieve the single digit.
A better method for what you are trying to do is:-
Code: |
char buffer[10];
int index = 0;
#int_rda
void rda_isr(void)
{
char c;
c = getc();
if (c == '\r') {
buffer[index] = '\0';
x = atoi(buffer);
index = 0;
} else
buffer[index++] = c;
}
|
Note there are no checks to see if index becomes 10 or greater which would cause a buffer overrun. |
|
|
casandralam
Joined: 26 Jan 2008 Posts: 15
|
can receive integer from 0-255?working? |
Posted: Sun Mar 02, 2008 7:09 am |
|
|
Wayne, do your code support for integer from 0-255?
Thanks.. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Mar 03, 2008 2:38 am |
|
|
The atoi function converts an ascii string of "0" to "255" to an integer so "0" becomes 0 and "255" becomes 255.
Integers in CCS are 8 bit so my code should work for a string value of 0 to 255. |
|
|
casandralam
Joined: 26 Jan 2008 Posts: 15
|
where is the output shown? |
Posted: Tue Mar 04, 2008 3:10 am |
|
|
I'm a newbie.Please forgive me if you feel my question is stupid..
Which pin of the pic is the output?can i set myself for example port b0 as our output?
Then,
how can we observe the output?using lcd or 7 segment display?or any program can do for it?can you suggest me some method to do do?
Thanks you very much |
|
|
casandralam
Joined: 26 Jan 2008 Posts: 15
|
|
Posted: Tue Mar 04, 2008 3:16 am |
|
|
Why arys put x=145 as initial value in his main program?what does it means? |
|
|
arys
Joined: 09 Mar 2007 Posts: 31
|
|
Posted: Tue Mar 04, 2008 3:31 am |
|
|
yes, it's initial value. then i hope i can change the x value through pc.
Which pin of the pic is the output?can i set myself for example port b0 as our output?
--> actually we can each pin either become input or output. you can find it out in the CCS manual how to set IO.
how can we observe the output?
-->i'm usually use ossciloscope or you can put the LED at the output. |
|
|
|