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 program wont move forward

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

USB program wont move forward
PostPosted: Thu Jul 31, 2008 12:10 pm     Reply with quote

hi, i'm having issues with my USB CDC code. I'm using the example but when combined with my code the program wont move forward unless i hit the keyboard.

the code is as follows:

Code:


#define __USB_PIC_PERIF__ 1

#if !defined(__PCH__)
 #error USB CDC Library requires PIC18
#endif


 #DEFINE LED1  PIN_A5
 #include <18F4550.h>
 #include <string.h>
 #fuses XT,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL1,CPUDIV1,VREGEN
 #use delay(clock=4000000)


#use rs232(baud=4800,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=GSM,bits=8,ERRORS)





#include <usb_cdc.h>

#define LED2 PIN_B4
#define LED3 PIN_B5
#DEFINE BUTTON PIN_A4
#define LED_ON output_low
#define LED_OFF output_high


#byte RCREG = 0x1A
#define BUFFER_SIZE 80
char inStr[BUFFER_SIZE];

char char_rcvd;
char done = 1;
int set_flag = 0;

//int cursor=0x80;
int i=0;
int sentence= 0;

void out_LCD(int i);
void int_LCD(void);
void cursor_position(int position);
void out_LCD_string(char *string);
void clear(void);
char intro[]="Altus Fortis inc.";
char intro2[]="KabTrak Beta";
char intro3[]="** Ver. 1.05 **";
char initial[]="Initializing...";
char ready[]="Ready";
void altus_fortis_init(void);

//char number[11]="5 ";
//byte num[] ="5104682725";
char temp[70] = "  ";


#int_rda
void serial_isr() {

char_rcvd = fgetc(GSM);
if( char_rcvd == '$'){
      set_flag = 1;
}

      if(set_flag ==1 && i <= 62){
      inStr[i]= char_rcvd;
      i++;
      }

}


void usb_debug_task(void) {
   static int8 last_connected;
   static int8 last_enumerated;
   int8 new_connected;
   int8 new_enumerated;
   static int8 last_cdc;
   int8 new_cdc;

   new_connected=usb_attached();
   new_enumerated=usb_enumerated();
   new_cdc=usb_cdc_connected();

   if (new_enumerated)
      LED_ON(LED1);
   else
      LED_OFF(LED1);

   if (new_cdc)
      LED_ON(LED2);
   else
     LED_OFF(LED2);

   if (usb_cdc_carrier.dte_present)
         LED_ON(LED3);
   else
      LED_OFF(LED3);

   if (new_connected && !last_connected)
      printf("USB connected, waiting for enumaration...\r\n\n");
   if (!new_connected && last_connected)
      printf("USB disconnected, waiting for connection...\r\n\n");
   if (new_enumerated && !last_enumerated)
      printf("USB enumerated by PC/HOST\r\n\n");
   if (!new_enumerated && last_enumerated)
      printf("USB unenumerated by PC/HOST, waiting for enumeration...\r\n\n");
   if (new_cdc && !last_cdc) {
      printf("Serial program initiated on USB<->UART COM Port\r\n\n");
      printf(usb_cdc_putc, "\r\n\nCCS CDC (Virtual RS232) Example\r\n\n");
   }

   last_connected=new_connected;
   last_enumerated=new_enumerated;
   last_cdc=new_cdc;
}








void main(void) {
char c;
int gsm_set = 0;

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_comparator(NC_NC_NC_NC);
   enable_interrupts(global);
   disable_interrupts(int_rda);
   setup_vref(FALSE);
   setup_low_volt_detect(FALSE);
   setup_oscillator(False);



output_b(0x00);
output_low(PIN_C2);
delay_ms(500);
output_high(PIN_C2);
delay_ms(2000);
output_low(PIN_C2);
output_low(PIN_C2);
int_lcd();
delay_ms(400);
altus_fortis_init();
delay_ms(4000);
clear();
out_lcd_string(initial);
delay_ms(30000);
clear();
cursor_position(0x94);
out_lcd_string(ready);
delay_ms(1000);
output_b(0x00);
cursor_position(0x80);


   LED_OFF(LED1);
   LED_OFF(LED2);
   LED_OFF(LED3);

   while (TRUE) {
  // #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
      usb_task();
      usb_debug_task();


      if (kbhit()) {
         c=getc();
         if (c=='\n') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         if (c=='\r') {usb_cdc_putc('\r'); usb_cdc_putc('\n');}
         else {usb_cdc_putc(c);}
         output_b(0x02);
      }
      if (usb_cdc_kbhit()) {
         c=usb_cdc_getc();
         if (c=='\n') {putc('\r'); putc('\n');}
         if (c=='\r') {putc('\r'); putc('\n');}
         else {putc(c);}

         output_b(0x01);
                          if(gsm_set == 0){
                                 output_b(0xff);
                                 fprintf(GSM, "AT+CMGF=1\r\n");
                                 //fprintf(GSM, "\r\n");
                                 delay_ms(3500);
                                 //delay_ms(500);
                                 fprintf(GSM, "AT+CMGR=3\r\n");
                                 delay_ms(1);
                                 enable_interrupts(int_rda);
                               
                                 gsm_set = 1;
                                 }

                           if(i >= 62){
                                 clear();
                                 disable_interrupts(int_rda);
                                 set_flag = 0;
                                 gsm_set = 0;
                                 inStr[i]=0;
                                 cursor_position(0x80);
                                 out_lcd_string(inStr);
                                 printf(usb_cdc_putc, "\r\n %s in if 2\r\n", inStr);
                                 sentence = 0;
                                 i=0;
                              }

            printf(usb_cdc_putc,"\r\n go back around \r\n");



      }

   }

}


the program does what i need as long as i keep hitting the keyboard. I'm trying to get it to move forward without hitting the keyboard. I'm wondering if it's my interrupt that is stopping the program. I'm really stumped i've gone all around trying to keep getting it to work. Any help would be appreciated.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Jul 31, 2008 7:55 pm     Reply with quote

Not sure if this is your problem but the getc() in your main loop will do a blocking wait if no data is available.
The kbhit() should prevent this blocking behaviour but there is a problem with int_rda() reading the data from the same port in an interrupt.
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

PostPosted: Fri Aug 01, 2008 9:23 am     Reply with quote

i'll try to remove the getc(). However, if the problem is with reading from the same port, how do i go around this? would a serial->usb solution not work in my case then?
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

PostPosted: Thu Sep 04, 2008 2:18 pm     Reply with quote

i was on a break for a while.

if there is a problem with my int_rda picking up from the same port, should i configure my serial port for my USB to another port?
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

PostPosted: Thu Sep 04, 2008 2:22 pm     Reply with quote

I'm also getting USB re-entrancy warnings. I'm not entirely sure how to get rid of those. When i do get rid of them, the program wont work. I may be getting rid of it wrongly
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Sep 04, 2008 4:46 pm     Reply with quote

Your intentions with the RS232 input are appearing mysterious.
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

PostPosted: Fri Sep 05, 2008 1:06 pm     Reply with quote

oh i'm sorry. I'm using the rs232 to receive information from a GSM unit. whenever i send out a command, the rs232 will receive serial data from the GSM.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Fri Sep 05, 2008 3:24 pm     Reply with quote

Yes, I see this so far. But the handling is strange to my opinion. Enabling the interrupt reception while the background kbhit() action is still in effect, as far as I understand. Creates a kind of race condition with unpredictable behaviour, I fear.
madtoilet



Joined: 02 Apr 2008
Posts: 36

View user's profile Send private message

PostPosted: Mon Sep 08, 2008 2:38 pm     Reply with quote

yes! it is a strange way of handling, i re did the whole code and fixed it. The whole approach that i was using was very wrong. I found a much simpler way to do it that actually makes sense. Thanks for all the help!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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