|
|
View previous topic :: View next topic |
Author |
Message |
radiofm
Joined: 12 Jan 2006 Posts: 9 Location: Poland
|
question about command line communication via rs232 |
Posted: Sat Nov 18, 2006 3:03 pm |
|
|
Hello all
it's my first post
sorry for my poor english
my first project in CCS - C:
Radio FM tuner - i want to control my radio FM tuner via RS232 from command line
i am using TSA5522 PLL chip to control frequency and PIC16F877A
my first test:
Code: |
#include <16F877.h>
#use delay(clock=4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N)
#fuses HS,NOWDT,NOPROTECT,NOLVP,DEBUG
long freq,pll;
main ()
{
freq = 935; //manually i set fequency to 93.5 MHz
pll = freq+107; //now i create pll it's becouse we must add to freq 10.7 MHz to p.cz.+ 10.7 MHz
//but it's x 10 becouse i dont like ","
pll= pll/0.05; //now step = 50 KHz
i2c_start(); //start i2c
i2c_write(0xC2); //adress TSA5522 chip
i2c_write(pll/256/10); // DB1
i2c_write(pll/10); // DB2
i2c_write(0x80); //CB
i2c_write(0x00); //PB
i2c_stop(); //and radio play at 93.5 MHz
}
|
and it's working perfectly radio play at 93.5 MHz
ok - now i must wirte communnication with PIC via hyperterminal (rs232)
for example first i want to set via hyperterminal frequency when i am connect to pic
In my program i manualy set 935
probably i must use INTERRUPTS and gets() ?
thanks
Robert |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Nov 19, 2006 1:39 pm |
|
|
You can define a simple message protocol to send the FM frequency
to the PIC. The message will consist of ASCII characters.
Use one character to indictate the beginning of a message, such as '$'.
Then send three ASCII numbers, which are the FM frequency.
Finally, the end of the message is indicated by a carriage return (0x0D)
or a carriage return and Linefeed (0x0D 0x0A). Example:
"$935" (followed by 0x0D)
Then use the code in the CCS example file, Ex_Sisr.c, to create an
interrupt-driven receive buffer. In a while() loop in main(), call
the bkbhit() function to see if a character is in the buffer. Read
characters and thrown them away, until you get a '$' character.
This indicates the start of a message. Then read the next three
characters. Check if they are ASCII numbers. If not, then the
message is not valid, so go back to waiting for a '$' again.
When you get each ASCII number ('9' '3' '5'), put them into an array,
sequentially. When you have all three numbers, write a 0x00 byte
at the end. This makes it into a string. Now use the atol() function
to convert the numeric ASCII string into a long integer. Put the
result into the 'freq' variable. Then use the frequency in the rest of
your program.
--------------
Or, if you don't want to use interrupts, then a more simple method
would be to use the get_string() function. Get the string, then use
the atol() function to convert it to a long, and put the result in 'freq'.
The get_string() function is safer to use than gets(), because it allows
you to set a limit on the number of characters that can be entered,
so the user doesn't type beyond the end of the input array.
Look in this file for get_string():
c:\program files\picc\drivers\input.c |
|
|
radiofm
Joined: 12 Jan 2006 Posts: 9 Location: Poland
|
|
Posted: Sun Nov 19, 2006 4:08 pm |
|
|
hello
thanks for answer probably it will be better for me used a input.c driver but:
when i include input.c driver
Code: |
#include <16F877A.h>
#use delay(clock=4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N)
#include <input.c>
|
i recived error:
Code: |
Executing: "C:\Program files\Picc\CCSC.exe" "INPUT.C" +FM +DF +LN +T -A +M +Z +Y=9 +EA
*** Error 128 "C:\Program Files\PICC\Drivers\INPUT.C" Line 13(1,2): A #DEVICE required before this line
1 Errors, 0 Warnings.
Halting build on first failure as requested.
BUILD FAILED: Sun Nov 19 23:03:43 2006
|
i am using PCM 3.249 version.
(i am totally newbie in programming) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
radiofm
Joined: 12 Jan 2006 Posts: 9 Location: Poland
|
|
Posted: Mon Nov 20, 2006 3:11 am |
|
|
hello
now i create something like that:
Code: |
#include <16F877A.h>
#use delay(clock=4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, stream=RS232)
#include <input.c>
#include <string.h>
#include <stdlib.h>
#define STRING_SIZE 20
#define MAX_BUFFER 10
char string[MAX_BUFFER];
long pll,freq;
main ()
{
while(1)
{
printf("\r\nset freq: ");
// Wait for a key press
while(!kbhit());
get_string(string, MAX_BUFFER);
//printf("\r\n%s\r\n",string);
strcpy(string,freq);
freq = atol(string);
pll = freq+107;
pll= pll/0.05; //step = 50 KHz
i2c_start(); //start i2c
i2c_write(0xC2); //adres
i2c_write(pll/256/10); // DB1
i2c_write(pll/10); // DB2
i2c_write(0x80); //CB
i2c_write(0x00); //PB
i2c_stop();
}
} |
Code: |
BUILD SUCCEEDED: Mon Nov 20 10:20:32 2006
|
also i can connect with PIC via rs232
for example in hyperterminal:
set freq :
i write:
930 and presss enter
but radio don't play ... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 20, 2006 3:27 am |
|
|
Quote: | {
printf("\r\nset freq: ");
// Wait for a key press
while(!kbhit());
get_string(string, MAX_BUFFER);
//printf("\r\n%s\r\n",string);
strcpy(string,freq);
freq = atol(string);
pll = freq+107;
pll= pll/0.05; //step = 50 KHz
i2c_start(); //start i2c
i2c_write(0xC2); //adres
i2c_write(pll/256/10); // DB1
i2c_write(pll/10); // DB2
i2c_write(0x80); //CB
i2c_write(0x00); //PB
i2c_stop();
}
}
but don't working.... |
Look at the strcpy() line. It's not necessary. Also, the parameters
are not correct. It wouldn't work, even if it was needed. Delete it. |
|
|
radiofm
Joined: 12 Jan 2006 Posts: 9 Location: Poland
|
|
Posted: Mon Nov 20, 2006 4:22 am |
|
|
hello
thanks !!!!!
in this configurtion working !
Code: |
#include <16F877A.h>
#use delay(clock=4000000)
#use i2c(Master,sda=PIN_C4,scl=PIN_C3)
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N)
#include <input.c>
#include <stdlib.h>
#define STRING_SIZE 20
#define MAX_BUFFER 10
char string[MAX_BUFFER];
long pll,freq;
main ()
{
while(1)
{
printf("\r\nset freq: ");
while(!kbhit());
get_string(string, MAX_BUFFER);
freq = atol(string);
pll = freq+107;
pll= pll/0.05; //step = 50 KHz
i2c_start(); //start i2c
i2c_write(0xC2); //adres
i2c_write(pll/256/10); // DB1
i2c_write(pll/10); // DB2
i2c_write(0x80); //CB
i2c_write(0x00); //PB
i2c_stop();
}
}
|
but it's not all
i want create a few command to communicte with my radio for example:
when i type in hyperterminal :
setfreq930
i want to change freq to 93mhz
maybe command:
setlch10
set left chanel volume to 10 - i want to use a digital potentiometers ... etc..
my question is:
is it possible to add "setfreq" and when i add this programm know that must set frequency..
i am a technican and i work in radio station - i want to bulid remonte control fm tuner i using DIGI CONNECT MODULES rs232 to ethernet controllers
now i create to test other module to remonte temperature and voltage control
a simple programm i (found simple ccs ds1621 example in internet)
this send me information about temperature in (C) and about voltage(in mV - to 5012 mV) (it's measure a pic voltage)
Code: |
#include <16F877A.h>
#device adc=10
#fuses HS, NOWDT, NOPROTECT
#use delay(clock=8000000)
#use rs232(baud=19200, xmit=PIN_C1, rcv=PIN_C2, bits=8, parity=N, STREAM=COM_B) //future
#use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, bits=8, parity=N, STREAM=COM_A)
#use i2c(sda=PIN_C4,scl=PIN_C3)
void temp_config(byte data)
{
i2c_start();
i2c_write(0x90);
i2c_write(0xac);
i2c_write(data);
i2c_stop();
delay_ms(11);
}
void init_program()
{
i2c_start(0);
i2c_write(0x90);
i2c_write(0xee);
i2c_stop();
temp_config(8);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
set_adc_channel(0);
}
byte read_temp()
{
byte datah,datal;
long data;
i2c_start();
i2c_write(0x90);
i2c_write(0xaa);
i2c_start();
i2c_write(0x91);
datah=i2c_read();
datal=i2c_read(0);
i2c_stop();
data=datah;
datal=data;
return(datal);
}
main()
{
byte value;
int value2;
init_program();
do
{
value = read_temp();
// value2 = ((Read_ADC())*49)/10;
fprintf(COM_A,"\n\r\@TEMP1=%u",value);
delay_ms(1000);
fprintf(COM_A,"\n\r\@V1=%Lu",((Read_ADC())*49)/10);
delay_ms(1000);
}
while (TRUE);
}
|
i want to set fm tuner near FM transmitter and remonte test some parametrs and also i want to control this tuner set to other freq using DIGI CONNECT MODULES
exaple with temperature and voltage working perfectly - i can monitor tempertaure where my fm transmiiter is installed..
sorry for my bad english ;)
regards
Robert.
p.s. when i finish a hardware i i want to publish this for other people (schematics etc.) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 20, 2006 4:46 am |
|
|
You need to design a simple command protocol, in which each command
is a single ASCII letter. Then the numbers associated with the command
will come after the letter.
Code: |
Example of commands:
F: set frequency
L: set left channel volume
R: set right channel volume.
Example of use:
F935
L10
R15
|
After you get the command string (for example, "L10"), use the first
character in the buffer as the parameter for a switch-case statement.
Each command will be a different 'case' in the switch-case statement.
Code: | int16 command_value;
// Get the number which comes after the command letter.
command_value = atol(&buffer[1]);
// Take action based upon the command letter.
switch(buffer[0])
{
case 'F':
// Put code to here to set the frequency.
break;
case 'L':
// Put code here to set left volume.
break;
default:
} |
This is a very simple example. It's enough to get you started. |
|
|
|
|
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
|