CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

usb 18f26j50
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

usb 18f26j50
PostPosted: Mon Dec 14, 2015 6:21 pm     Reply with quote

Hello!

the module dont recognice commands in CMD mode.

The board is fine, it works with compiler XC8. (USB too)

detail.
PIC18F26J50
CCS 5.015
MPLAB X 3.15

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++;
    }
   
    return count;
}


//the problem is here!!
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);
                printf(usb_cdc_putc,"\n\r Buffer: %s\n\r",buffer);
                //puts(palabra,RN171);                  //  manda la palabra del teclado al modulo
            }
            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;
        }     
       
   }
}



And this is the schematic:

USB ports are PIN_C4, PIN_C5.



Thanks


Last edited by sebalitter on Sat Sep 03, 2016 5:09 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 14, 2015 6:43 pm     Reply with quote

sebalitter wrote:

#include <18F26j50.h>

#FUSES NOWDT
#FUSES NODEBUG
#FUSES NOXINST
#FUSES NOPROTECT
#FUSES NOFCMEN
#FUSES NOIESO
#FUSES NOCPUDIV
#FUSES HSPLL
#FUSES PLL2
#FUSES RTCOSC_INT

#use delay(clock=12000000) // 12Mhz Crystal is Used

The 18F26J50 data sheet says to use PLL3 with a 12 MHz crystal:
Quote:
bit 3-1 PLLDIV<2:0>: Oscillator Selection bits
Divider must be selected to provide a 4 MHz input into the 96 MHz PLL.
111 = No divide – oscillator used directly (4 MHz input)
110 = Oscillator divided by 2 (8 MHz input)
101 = Oscillator divided by 3 (12 MHz input)
100 = Oscillator divided by 4 (16 MHz input)
011 = Oscillator divided by 5 (20 MHz input)
010 = Oscillator divided by 6 (24 MHz input)
001 = Oscillator divided by 10 (40 MHz input)
000 = Oscillator divided by 12 (48 MHz input)


And the 2nd thing is, if you are using the PLL, and CPUDIV =1, then your
PIC will be running at 48 MHz. The compiler needs to know this. You
need to set the #use delay to 48 MHz.
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Mon Dec 14, 2015 7:00 pm     Reply with quote

Code:

 #FUSES PLL3
 
 #use delay(crystal=12Mhz, clock=48Mhz)



Is this correct?

still not working.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Tue Dec 15, 2015 2:21 am     Reply with quote

The crystal is now right, but you are not loading any USB code. Your printf, is using RS232, not usb.
Look at the USB serial examples.
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 15, 2015 7:05 pm     Reply with quote

Thanks, its working with a minor problem, this is the result

S¶./T¶./T./T¶./T¶./T¶./T¶./T./T¶./T¶./TT¶./T¶./T¶./T¶./T./T¶./TT¶./T¶./T¶./T¶./T¶./T¶./T¶./T¶./TT¶./T¶./T¶./T¶./T¶./T¶./T./T¶./T¶./TT¶./T¶./T

instead of "USB Working"

This is the code:


Code:


#include <18F26j50.h>

//#device ICD=TRUE
//#device ADC=10
#FUSES NOWDT
 #FUSES NODEBUG
 #FUSES NOXINST
 #FUSES NOPROTECT
 #FUSES NOFCMEN
 #FUSES NOIESO
 #FUSES NOCPUDIV
 #FUSES HSPLL
 #FUSES PLL3
 #FUSES RTCOSC_INT
 
// 12Mhz Crystal is Used
 #use delay(crystal=12Mhz, clock=48Mhz)
 #use rs232(baud=9600, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8,errors)  // RS232 Configuration


 #include<usb_desc_cdc.h>
 #include <usb_cdc.h>
 char x;

 void main(void)
 {
    int i, j, address, value;
    setup_oscillator(OSC_PLL_ON);
    delay_ms(100);
    enable_interrupts(GLOBAL);
    usb_init();
    delay_us(100);
    usb_cdc_init();
    delay_us(100);
    while(1)
    {
       usb_task();
       if (usb_enumerated())
       {
         output_high(pin_c2);
         delay_us(100);             
        if (usb_cdc_kbhit())
         {
             x = toupper(usb_cdc_getc());
             printf(usb_cdc_putc,"%c", x);
             if (x == 'S')
             {
                printf(usb_cdc_puts, "USB Working");
             }   
         }
       }
    }         
 }

ezflyr



Joined: 25 Oct 2010
Posts: 1019
Location: Tewksbury, MA

View user's profile Send private message

PostPosted: Wed Dec 16, 2015 9:13 am     Reply with quote

Hi,

Well, if that is a 'minor problem', what do you call a major one? Shocked

I'm curious which 'USB serial example' you looked at? I see that you've made some significant changes from the examples provided with the compiler! That's a bad idea! Keep the code as much the same as possible to get it working, and then make incremental changes to customize the examples to your needs!

Here is what I'd do to get your code working:

1. Remove '#include<usb_desc_cdc.h>'.
2. Put 'char x;' in Main - not critical, but this variable doesn't need to be 'global'.
3. Get rid of 'setup_oscillator(OSC_PLL_ON);' - this should not be needed.
4. Get rid of 'enable_interrupts(GLOBAL); ' - you have no interrupts, so this is a big No-No!
5. Get rid of 'usb_cdc_init();' - this should not be needed.
6. Get rid of unused variables - not critical, but your test program should be a simple and 'clean' as possible!
7. Change 'printf(usb_cdc_puts, "USB Working"); ' to 'printf(usb_cdc_putc, "USB Working");' - this may not be an issue, but the way 'putc' method does work!

If you look at the Windows Device Manager under 'Ports (Com and LPT)', what do you see? You should see something like this: 'CCS USB to UART (COMX)'. Do you?
_________________
John

If it's worth doing, it's worth doing in real hardware!
temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 16, 2015 11:21 am     Reply with quote

something else , more basic..

#8 which should be #0

go back and code a '1Hz flashing LED' program, show us and confirm that it is working properly. Having 'garbage' in any serial communication could be 'bad timing'.....
do this for a 'real' RS232 port as well as the USB port of the PIC as 'something' isn't right....


Jay
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Wed Dec 16, 2015 6:02 pm     Reply with quote

marvelous marvelous!

Thank
ezflyr, PCM programmer , Ttelmah, temtronic! I appreciate it.

temtronic i dont follow the point, 1Hz might not notice the led.
I have to investigate further because I do not understand the difference between USB and RS232. I thought it was a hardware issue.

SPI with Rx and TX is RS232?

This is the final code working.

Code:


#include <18F26j50.h>

 #FUSES NOWDT
 #FUSES NODEBUG
 #FUSES NOXINST
 #FUSES NOPROTECT
 #FUSES NOFCMEN
 #FUSES NOIESO
 #FUSES NOCPUDIV
 #FUSES HSPLL
 #FUSES PLL3
 #FUSES RTCOSC_INT
 
 #use delay(crystal=12Mhz, clock=48Mhz)
 #use rs232(baud=9600, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8,errors)  // RS232 Configuration

 #include <usb_cdc.h>

 void main(void)
 {
      char x;
    delay_ms(100);
    usb_init();
    delay_us(100);
    printf(usb_cdc_putc,"\r");
    while(1)
    {
       usb_task();
       if (usb_enumerated())
       {
         delay_us(100);             
        if (usb_cdc_kbhit())
         {
             x = toupper(usb_cdc_getc());
             if (x == 'S')
             {
                printf(usb_cdc_putc, "USB Working\n\r");
             }   
         }
       }
    }         
 }


temtronic



Joined: 01 Jul 2010
Posts: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 16, 2015 7:06 pm     Reply with quote

re: 1Hz LED program

It's just a small, simple test program to toggle an LED on an I/O pin at 1Hz.
Code:

while(1) {
delay_ms(500);
output_high(LED);
delay_ms(500);
output_low(LED);
}

Where LED has been defined as an I/O pin that has an LED and current limiting resistor attached to it.

The purpose is to confirm that the PIC is wired correctly and that fuse and clock selection is proper, and no major compiler issues. When running it will flash the LED at about a 1Hz rate.

Jay
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 18, 2015 11:15 pm     Reply with quote

Hello!
Emerged a mistake that i couldn't find answer in the forum.

Now, i want to send ascii comands to WIFI module(RN-171) and get the feedback in the tera term.
The problem is that i dont get the response from WIFI module.

ex_sisr.C modification with USB.

PC->PIC18F26j50->Uart->RN-171->uart->PIC18F26j50->PC again.

detail.
CCS 5.015
MPLAB X 3.15

Code:


#include <18F26j50.h>

#define LARGO 100
#define BUFFER_SIZE 32
#define bkbhit (next_in!=next_out)

//#device ICD=TRUE
//#device ADC=10
 #FUSES NOWDT
 #FUSES NODEBUG
 #FUSES NOXINST
 #FUSES NOPROTECT
 #FUSES NOFCMEN
 #FUSES NOIESO
 #FUSES NOCPUDIV
 #FUSES HSPLL
 #FUSES PLL3
 #FUSES RTCOSC_INT

 #use delay(crystal=12Mhz, clock=48Mhz)
 #use rs232(baud=9600, parity=N, xmit=PIN_C4, rcv=PIN_C5, bits=8,errors)  // USB
 #use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8,errors,RECEIVE_BUFFER=16 ,stream=WIFI)  // RN-171

 #include <usb_cdc.h>

BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;


#int_rda
void serial_isr() {
   int t;

   buffer[next_in]=getc();
   t=next_in;
   next_in=(next_in+1) % BUFFER_SIZE;
   if(next_in==next_out)
     next_in=t;           // Buffer full !!
}

BYTE bgetc() {
   BYTE c;

   while(!bkbhit) ;
   c=buffer[next_out];
   next_out=(next_out+1) % BUFFER_SIZE;
   return(c);
}

void main(void)
{
     char palabra[LARGO];
     
    delay_ms(100);
    usb_init();
    delay_us(100);
   
    enable_interrupts(int_rda);
    enable_interrupts(global);
   
    while(TRUE)
    {
       usb_task();
       if (usb_enumerated())
       { 
         delay_us(100);             
        if (usb_cdc_kbhit())
         {
            printf(usb_cdc_putc,"\r\n");
            get_string_usb(palabra,LARGO);
            printf(usb_cdc_putc,"\r\n");
       
            printf(usb_cdc_putc,"%s", palabra);
            fprintf(WIFI,"\r\n%s", palabra);
            delay_ms(100);
            while(bkbhit)
                printf(usb_cdc_putc,"%c", bgetc());
               
         }
       }
    }         
 }

sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 22, 2015 7:28 pm     Reply with quote

Dont understand if my problem is too hard(dont think so) or someone gave me the answer and i didn't catch it. Confused
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed Dec 23, 2015 1:47 am     Reply with quote

#USE RS232, has nothing to do with USB.

Your first #USE RS232 should not be there, and may well cause problems....

USB, IS NOT RS232.

The cdc code, makes a 'virtual' connection between the PIC and the USB port, that behaves at the PC end as if it were an RS232 port, and adds the code at the PIC end, so it can be talked to as if it 'were' an async serial connection. No baud rates involved (the _PC_ can send a baud rate command to the PIC, and if it wants the PIC can handle this and then change the baud rate on a physical port to correspond to this), but the actual USB connection can typically handle data well over 500Kbps....

Then you have conflicting code. You are telling #USE RS232 to generate buffering for you, with a 16byte buffer, and then adding buffer code yourself with a 32byte buffer. Wrong. You only want to use one or the other method. Two conflicting ways of handling serial buffering....

Now, you seem to be expecting the WiFi module to be automatically sending you data. Most don't. You have to send them commands to actually trigger the transmission (just like GSM modules etc., they all need to actually be setup....).

From the data sheet for your module:
"The RN171 is configured with a simple ASCII command
language. The “WiFly Command Reference,
Advanced Features and Applications User's Guide”
(DS50002230) contains a complete listing and
instructions of the ASCII command interface."

Then from this guide:
"Once you have performed the initial configuration,
the device automatically accesses a Wi-Fi network
and sends/receives serial data."

Note the 'Once you have performed the initial configuration".

You need to get the interface to the RN171 setup and working, before trying to interface it to the USB. I suggest you connect it to a PC (max232), and actually try using a terminal program to to it.

Next thing that will be wrong, is that by default with the current firmware, the module will not automatically connect to the Wlan. You need to switch it to command mode ($$$), then send the command "set wlan join 1", to actually tell the module to join the wlan.
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Wed Dec 23, 2015 1:59 pm     Reply with quote

Hi Ttelmah,

The PIC18F26J50 has two UART, one with usb interface (connected to PC and working) and the other to RN-171(wifi).

I am trying to send string commands to the pic with teraterm and the pic receive it in uart1-USB and retransmit to UART2-RN171, receive the feedback from the UART2-rn171 and retransmit it to UART1-USB to display it in teraterm.

teraterm->UART1usb->PIC18F26j50->UART2rn->RN-171->uart2rn->PIC18F26j50->uart1usb->teraterm

The first #USE RS232 is for UART2-rn171. I'm not sure if it is receiving well from uart2 or the problem is in the feedback.
The USB is the other UART1usb that is working fine.

I not sure if i am explaining well.

i write ascii commands with the keyboard, errands to the module and receive the feedback in the screen again.

Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed Dec 23, 2015 3:26 pm     Reply with quote

The second UART on this PIC is a _relocatable peripheral_. The syntax for this is:
Code:

#PIN_SELECT U2TX=PIN_xx //an RP pin that suits
#PIN_SELECT U2RX=PIN_yy //a second RP pin that suits
#USE RS232(UART2, BAUD=9600, ERRORS)

You cannot use the USB pins for this.

Currently you have a _software_ UART setup on the USB pins (which are _input only_ when used for normal I/O), clashing with your attempt to use these pins for USB.....

You need to step back and understand each step _and get each step working on it's own_, before trying to put code together.
sebalitter



Joined: 05 May 2015
Posts: 47

View user's profile Send private message Send e-mail

PostPosted: Sun Dec 27, 2015 2:16 pm     Reply with quote

I didnt find anything in ccs_c_manual.pdf.
The example of EX_STISR.C is not the example for me.

I understood the simulation or USB_cdc with RS232.
Could someone give me another example with usb_cdc.h(for conection with pc) and RS232(for wifi module) in the same code?


I did some changes but nothing happens.
The part of USB work fine. The problem is in the other part(wifi module).

if the comunication were working. The module sends comands like, "OK", "conected to IP..."
I but it`s not.




Code:


 #include <18F26J50.h>
 #include <string.h>
 #include <ctype.h>

 #FUSES NOWDT
 #FUSES NODEBUG
 #FUSES NOXINST
 #FUSES NOPROTECT
 #FUSES NOFCMEN
 #FUSES NOIESO
 #FUSES NOCPUDIV
 #FUSES HSPLL
 #FUSES PLL3
 #FUSES RTCOSC_INT

 #use delay(crystal=12Mhz, clock=48Mhz)
 

 #PIN_SELECT U2TX=PIN_C6
 #PIN_SELECT U2RX=PIN_C7
 #USE RS232(UART2,ERRORS,BAUD=9600,STREAM=WIFI,ERRORS)
 
 #include <usb_cdc.h>

 #define BUFFER_SIZE 32
 BYTE buffer[BUFFER_SIZE];
 BYTE next_in = 0;
 BYTE next_out = 0;

 #int_rda
 void serial_isr() {
    int t;

    buffer[next_in]=fgetc(WIFI);
    t=next_in;
    next_in=(next_in+1) % BUFFER_SIZE;
    if(next_in==next_out)
      next_in=t;           // Buffer full !!
 }

 #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);
 }
 
 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 main() {
     
    char palabra[BUFFER_SIZE];
    delay_ms(100);
    usb_init();
    delay_us(100);
    inicbuff(); // Borra buffer al inicio
   
    enable_interrupts(int_rda);
    enable_interrupts(global);
    int i=0;
 
    delay_ms(50);
   
    while(TRUE)
    {
        delay_ms(50);
       
        while(bkbhit) {   
            printf(usb_cdc_putc,"something: %c" ,fgetc(WIFI));
            delay_ms(50);
        }
        delay_ms(50);
        usb_task();
        if (usb_enumerated())
        {


            if (usb_cdc_kbhit())
            {
           
                printf(usb_cdc_putc,"\r\n");      //limpia la pantalla
                get_string_usb(palabra,BUFFER_SIZE);     //obtiene la palabra escrita en el teclado
                printf(usb_cdc_putc,"\r\n");
               
                i=0;
                while(palabra[i]!=0x00){
                    fputc(palabra[i],WIFI);
                    delay_ms(50);
                    i++;
                }
            }
        }
       
    }

 }




Last edited by sebalitter on Tue Dec 29, 2015 10:03 am; edited 2 times in total
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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