|
|
View previous topic :: View next topic |
Author |
Message |
sebalitter
Joined: 05 May 2015 Posts: 47
|
uart stop TX when connect wifi |
Posted: Fri Jan 08, 2016 5:38 am |
|
|
Hello,
i am using a pic18f26j50 that communicates with a WIFI module( RN-171) by UART.
RN171 module cant receive commands in CMD mode.
detail.
PIC18F26J50
CCS 5.015
MPLAB X 3.35
Code: |
#include <18F26J50.h> // -> PCH
#device adc=16
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <pic18_usb.h> //Microchip PIC18Fxx5x Hardware layer for CCS's PIC USB driver
#include <usb_cdc.h> // DescripciĆ³n de funciones del USB.
#include <usb_desc_cdc.h> // Descriptores del dispositivo USB.
#FUSES NOWDT //No Watch Dog Timer
//#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOFCMEN
#FUSES NOIESO
#FUSES NOCPUDIV
//#FUSES HSPLL //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PLL3
#FUSES RTCOSC_INT
#use delay(crystal=12Mhz, clock=48Mhz)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=N ,stop=1, bits=8, stream = RN171) //// RS232 Configuration with RN171
#use rs232(baud=9600, xmit=PIN_C4, rcv=PIN_C5, parity=N, stop=1, bits=8, stream = PC) // RS232 Configuration with PC
#use standard_io(c)
#use fast_io(b)
#define CTS PIN_B2 //wake RN171 to receive data RX GPIO_12 clear to send
#define RTS PIN_B1 //wake RN171 to receive data RX GPIO_13 ready to send
#define BUFFER_SIZE 90
char buffer[BUFFER_SIZE];
char palabra[BUFFER_SIZE];
char delimitar[] = "- ||/|| -";
int next_in = 0;
int next_out = 0;
char *tokens[10];
int cmdON = 0;
int segundos=0 , segundos_ant = 0 ; // A running seconds counter
//int8 int_count = 0; // Number of interrupts left before a second has elapsed
int cant_tokens=0;
//Functions
void inicbuff(void){ // Inicia a \0 cbuff -------------------
int i;
for(i=0;i < BUFFER_SIZE;i++){ // Bucle que pone a 0 todos los
buffer[i]=0x00; // caracteres en el buffer
}
}
void inicpalabra(void){ // Inicia a \0 cbuff -------------------
int i;
for(i=0;i < BUFFER_SIZE;i++){ // Bucle que pone a 0 todos los
palabra[i]=""; // caracteres en palabra
}
}
#define bkbhit (next_in!=next_out)
BYTE bgetc()
{
BYTE c;
while(!bkbhit) ;
c = buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
int almacenarDatos(char *string, char *delim) //, char *tokens[])
{
int count=0;
char *stringp;
stringp = string;
char* token;
for (token = strtok(stringp, delim); token; token = strtok(NULL, delim))
{
tokens[count]= token;
//printf(usb_cdc_putc,"\n\r%s ", tokens[count]);
count++;
}
//for (i = 1; i < count-1; i++) // condicion final MIENTRAS QUE i sea menor a count
// printf(usb_cdc_putc,"\n\r%s ", tokens[i]);
return count;
}
void comando(char* comanda)
{
char comand[]="$$$";
char salida[]="exit";
if( strcmp(palabra,comand) == 0 )
{
printf(usb_cdc_putc,"\n\r :--------------Ingrese comando-----------------:\n\r");
delay_ms(260);
fprintf(RN171,"$$$"); //entra en modo comando
//puts("$$$",RN171); // manda la palabra del teclado al modulo
delay_ms(260);
cmdON =1;
//output_low(CTS);
while( cmdON==1 )
{
while(bkbhit)
{
putc( bgetc() );
}
//printf(usb_cdc_putc,"\n\r Buffer: %s\n\r",buffer);
inicpalabra();
//inicbuff();
get_string_usb(palabra,BUFFER_SIZE); //obtiene la palabra escrita en el teclado
delay_ms(100);
if(strcmp(palabra,salida) == 0)
{
fprintf(RN171,"exit\r"); //sale del modo comando
//puts(salida,RN171); // manda la palabra del teclado al modulo
output_high(CTS);
cmdON=0;
}else{
fprintf(RN171,"%s",palabra); // manda la palabra del teclado al modulo
//printf(usb_cdc_putc,"\n\r Buffer: %s\n\r",buffer);
}
delay_ms(100);
}
printf(usb_cdc_putc,"\n\r :--------------Salio del modo comando-----------------:\n\r");
}
}
void mostrarDatos( int cant) //char *dat[],
{
int i;
//printf(usb_cdc_putc,"\n\rcount %d, %s , %s , %s , %s", cant, tokens[0], tokens[1], tokens[2], tokens[3]);
for (i = 0; i < cant; i++) // condicion final MIENTRAS QUE i sea menor a count
printf(usb_cdc_putc,"\n\r %d/%d - %s ",i,cant, tokens[i]);
}
/*
ps:Pre escaler 256
tinst:4/frecuencia del cristal (4/4000000) PLL3 -> 12000000/3 = 4000000 Hz
carga:Valor a cargar en el timer 0
temp: tiempo deseado a temporizar 1 seg
*
* temp = tinst * 256 * (65536 - carga)
* 1seg = 4/4000000 * 256 * (65536 - carga)
*
*/
//interrupts
#int_timer0
void TIMER0_isr(void)
{
segundos++;
if(segundos > 59)
segundos=0;
}
#int_rda
void serial_isr() {
int t=0;
buffer[next_in]= getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out) {
next_in=t; // Buffer full !!
}
}
void main() {
//inputs 1, outputs 0
SET_TRIS_C( 0b11010111 ); // pone C como entradas y C4 y C6 como salidas (Sensor)
SET_TRIS_B(0x00 ); //Configuramos el puerto b como salidas
output_b(0x00);
usb_cdc_init(); // Configuramos al puerto virtual.
delay_ms(100);
usb_init(); // Inicializamos el stack USB.
usb_task(); //habilita periferico usb e interrupciones
usb_wait_for_enumeration(); //esperamos hasta que el PicUSB sea configurado por el host
//usb_init_cs();
//delay_us(100);
while(!usb_cdc_connected()) {} // espera a detectar una transmisiĆ³n de la PC
inicbuff(); // Borra buffer al inicio
inicpalabra();
setup_oscillator (OSC_PLL_ON); //IMPORTANTIIIISIMO SI NO NO FUNCIONA USB NI USART
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);
set_timer0(3906);
//interrupts
enable_interrupts(int_rda);
enable_interrupts(INT_TIMER0);
enable_interrupts(global);
cmdON=0;
output_high(CTS); //ready to send
printf(usb_cdc_putc,"\n\r :--------------Start-----------------:\n\r");
while(TRUE)
{
delay_ms(100);
while(bkbhit)
{
putc( bgetc() );
}
delay_us(100);
usb_task();
if( !bkbhit )
{
//delay_ms(200);
cant_tokens = almacenarDatos(buffer,delimitar); //,tokens);
//delay_us(100);
if (usb_enumerated()) //Returns a 1 if device is configured
{
if (usb_cdc_kbhit())
{
get_string_usb(palabra,BUFFER_SIZE); //obtiene la palabra escrita en el teclado
comando(palabra);
}
}
//inicbuff(); // Borra buffer al inicio
}
if(segundos != segundos_ant && cmdON==0)
{
mostrarDatos(cant_tokens);
segundos_ant=segundos;
}
}
}
|
Thanks
Last edited by sebalitter on Sat Sep 03, 2016 5:03 pm; edited 1 time in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Fri Jan 08, 2016 6:25 am |
|
|
quick answer..
Code: | fputs(palabra,WIFI); |
says to me.. take the STRING formatted data at palabra and send it to WIFI..
Odds are that you haven't delimited (ended) the data with a 0.
Others will know for sure but I suspect the PIC is 'looking' for the delimiter (the 0) at the end of the string 'palabra' and will wait until the cows come home for it.
Easy to fix, just load the last cell of the array with a 0.
Jay |
|
|
sebalitter
Joined: 05 May 2015 Posts: 47
|
|
Posted: Fri Jan 08, 2016 4:36 pm |
|
|
thanks for the kick answer.
i try it. still don't work.
Code: |
largo = strlen(palabra);
palabra[largo]='\0';
fputs(palabra,WIFI);
/*
i=0;
while(largo<BUFFER_SIZE && i<largo){
fputc(palabra[i],WIFI);
//delay_ms(50);
i++;
}
fputc('\0',WIFI);
*/
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Jan 08, 2016 5:35 pm |
|
|
RN-171 is a Microchip product. I'm not sure why you're asking about it
here. You can Google the Microchip forum and find many threads on it:
Google this:
Quote: | site:microchip.com/forums RN-171 no output |
There are hundreds of threads discussing problems with the RN-171. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9225 Location: Greensville,Ontario
|
|
Posted: Sat Jan 09, 2016 7:02 am |
|
|
I would configure a 'test serial port' to send the data going to the 'WIFI' device to a PC terminal program. This way you can verify that the data is correct and in the proper format for that WIFI device.
Simply take a spare PIC I/O pin, set for uses rs232(....) ,connect a USB<>TTL module, configure PC term pgm as required.
Most PCs today have 4 or 6 USB ports, so this should be possible.
Do NOT use the current USB setup as your 'test serial port'. You need a separate port for testing.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Sat Jan 09, 2016 9:37 am |
|
|
There are also some little syntax errors, that were pointed out to him in his previous thread. He has fixed some, but not other. So (for instance) fgetc, without a stream name.
Read the manual for each instruction, and make sure your code is using it right. |
|
|
|
|
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
|