|
|
View previous topic :: View next topic |
Author |
Message |
syide
Joined: 10 Nov 2008 Posts: 16 Location: Malaysia
|
Is this the way to make connection between Modem<=>18f |
Posted: Mon Feb 23, 2009 12:57 am |
|
|
Hello,
Anyone please tell me whether this code will work.
I try to connect Siemen Hc25 terminal with PIC.
PIC work as client to send data to server in PC.
Siemen HC25 did have TCP/IP stack AT cammand. Here is the code
Code: | #include <18F4620.h>
#fuses H4, NOFCMEN, NOXINST, NOIESO, NOPBADEN, MCLR, NOWDT, NOPROTECT, NODEBUG, NOSTVREN
#use delay(clock=4000000)
#define STACK_USE_PPP 1
#define STACK_USE_MAC 0
#define STACK_USE_ICMP 1 //for ping
#define STACK_USE_TCP 1 //To send TCP packets (talk to HTTP clients)
#use rs232(baud=9600, xmit=PIN_C6,rcv=PIN_C7, STREAM=USER)
#include "tcpip/stacktsk.c" //include Microchip's TCP/IP stack
#define MY_HTTP_SOCKET 80
int8 HTTPSocket=INVALID_SOCKET;
void HTTPPut(char c) {
TCPPut(HTTPSocket, c);
}
void HTTPTask(void) {
static enum {HTTP_ST_CONNECT=0, HTTP_ST_WAIT_CONNECT=1, HTTP_ST_GET=2,
HTTP_ST_PUT=3, HTTP_ST_DISCONNECT=4, HTTP_ST_FORCE_DISCONNECT=5} state=0;
static TICKTYPE timeout_counter;
TICKTYPE current;
static char lc, lc2;
char c;
char content[250];
int8 a0, a1;
current=TickGet();
if (!MACIsLinked()) {
if (HTTPSocket!=INVALID_SOCKET) {
state=HTTP_ST_FORCE_DISCONNECT;
}
}
if (HTTPSocket==INVALID_SOCKET)
state=HTTP_ST_CONNECT;
switch(state) {
case HTTP_ST_CONNECT:
if (MACIsLinked()) {
HTTPSocket=TCPListen(MY_HTTP_SOCKET);
if (HTTPSocket!=INVALID_SOCKET) {
state=HTTP_ST_WAIT_CONNECT;
}
}
break;
case HTTP_ST_WAIT_CONNECT:
if (TCPIsConnected(HTTPSocket)) {
state=HTTP_ST_GET;
timeout_counter=current;
}
break;
case HTTP_ST_GET:
if (!TCPIsConnected(HTTPSocket)) {
state=HTTP_ST_FORCE_DISCONNECT;
break;
}
if (TCPIsGetReady(HTTPSocket)) {
while (TCPGet(HTTPSocket, &c)) {
if ( (c=='\n') && (lc2=='\n') ) {
state=HTTP_ST_PUT;
timeout_counter=current;
TCPDiscard(HTTPSocket);
break;
}
lc2=lc;
lc=c;
}
}
else if (TickGetDiff(current, timeout_counter) > ((int16)30 * TICKS_PER_SECOND))
state=HTTP_ST_DISCONNECT;
break;
case HTTP_ST_PUT:
if (!TCPIsConnected(HTTPSocket)) {
state=HTTP_ST_FORCE_DISCONNECT;
break;
}
if (TCPIsPutReady(HTTPSocket)) {
a0=read_adc();
set_adc_channel(1);
delay_us(20);
a1=read_adc();
set_adc_channel(0);
sprintf(content, "<HTML><BODY><H1>HELLO</H1><P>AN0 = 0x%X<BR>AN1 = 0x%X</BODY></HTML>", a0, a1);
printf(HTTPPut, "HTTP/1.1 200 OK\r\n");
printf(HTTPPut, "Content-Type: text/html\r\n");
printf(HTTPPut, "Content-Length: %u\r\n",strlen(content));
printf(HTTPPut, "\r\n");
printf(HTTPPut, "%s", content);
TCPFlush(HTTPSocket);
state=HTTP_ST_GET;
timeout_counter=current;
}
else if (TickGetDiff(current, timeout_counter) > ((int16)30 * TICKS_PER_SECOND))
state=HTTP_ST_DISCONNECT;
break;
case HTTP_ST_DISCONNECT:
if (TCPIsPutReady(HTTPSocket))
state=HTTP_ST_FORCE_DISCONNECT;
else
break;
case HTTP_ST_FORCE_DISCONNECT:
TCPDisconnect(HTTPSocket);
HTTPSocket=INVALID_SOCKET;
state=HTTP_ST_CONNECT;
break;
}
}
char ppp_username[32];
char ppp_password[32];
char ppp_phonenumber[20];
void ISPInit(void) {
sprintf(ppp_username, " ");
sprintf(ppp_password, " ");
sprintf(ppp_phonenumber, "*99***1#");
}
void main(void) {
MODEM_RESP resp;
ISPInit();
//setup_adc_ports(ANALOG_AN0_TO_AN1);
//set_tris_a(3); //set A0 and A1 to input for ADC
//set_tris_b(0);
//setup_adc(ADC_CLOCK_INTERNAL);
StackInit();
while(TRUE) {
if (!ppp_is_connected() && !ppp_is_connecting()) {
printf("\fDialing");
resp=ppp_connect(ppp_username, ppp_password, ppp_phonenumber);
if (resp==MODEM_BUSY) {
printf("\fBusy Signal");
delay_ms(2000);
}
else if (resp==MODEM_NO_DIALTONE) {
printf("\fNo Dialtone");
delay_ms(2000);
}
else if (resp!=MODEM_CONNECTED) {
printf("\fDial Error");
delay_ms(2000);
}
else {
printf("\f%LUbps", connected_baudrate);
printf("\nNegotiating PPP");
}
}
StackTask();
HTTPTask();
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Feb 23, 2009 1:52 pm |
|
|
Quote: | #include <18F4620.h>
#fuses H4, NOFCMEN, NOXINST, NOIESO, NOPBADEN, MCLR, NOWDT, NOPROTECT, NODEBUG, NOSTVREN
#use delay(clock=4000000) |
I didn't look closely at your program, but there is one immediate error.
You're using the H4 fuse, but your clock speed is set to 4 MHz.
The H4 fuse multiplies the crystal frequency by 4. So if you have a
10 MHz crystal, you get 40 MHz oscillator speed. If you really have a 4
MHz crystal, then change the fuse to XT instead of H4.
Quote: |
a0=read_adc();
set_adc_channel(1);
delay_us(20);
a1=read_adc();
set_adc_channel(0); |
You should set the channel before you read the adc. Not after.
This is the correct order for the lines of code:
Code: | set_adc_channel(0);
delay_us(20);
result = read_adc(); |
|
|
|
|
|
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
|