| 
	
	|  |  |  
	
		| View previous topic :: View next topic |  
		| Author | Message |  
		| neverlog 
 
 
 Joined: 02 May 2010
 Posts: 36
 
 
 
			    
 
 | 
			
				| Please help! Can't manipulate array store in PIC |  
				|  Posted: Mon Jul 19, 2010 5:36 am |   |  
				| 
 |  
				| Dear All, 
 I would appreciate any help from you guys. I am stucked!
 
 Below is my code for PIC, my compiler is 4.104
 
 I am using 18F4520, I was wondering why I can't manipulate the array which I get from software UART.
 
  	  | Code: |  	  | #include <18F4520.h>
 #fuses hs,noprotect,nowdt,nolvp,BROWNOUT
 #device HIGH_INTS=TRUE
 #priority int_ext,int_rda
 #use delay(clock=20000000) //20mhz
 #use rs232(baud=19200, xmit=PIN_B1, rcv=PIN_B0, parity=N, bits=8, stream=reader, TIMEOUT=200, errors,SAMPLE_EARLY)
 #use rs232(baud=19200, xmit=PIN_C6, rcv=PIN_C7, parity=N, bits=8, errors,stream=pc, TIMEOUT=200,SAMPLE_EARLY)
 
 //EEPROM Storage for Card ID===================================
 #define EEPROM_SDA PIN_C4
 #define EEPROM_SCL PIN_C3
 
 #include <24256.c>
 #include <external_eeprom.c>
 
 #byte porta=5
 #byte portb=6
 #byte portc=7
 
 //Software UART RX Buffer================================================
 #define SU_BUFFER_SIZE 200
 BYTE SU_buffer[SU_BUFFER_SIZE];
 BYTE SU_next_in = 0;
 BYTE SU_next_out = 0;
 
 #int_ext high
 void SU_serial_isr() {
 int SU_t;
 
 SU_buffer[SU_next_in]=fgetc(reader);
 SU_t=SU_next_in;
 SU_next_in=(SU_next_in+1) % SU_BUFFER_SIZE;
 if(SU_next_in==SU_next_out)
 SU_next_in=SU_t;           // Buffer full !!
 }
 
 #define SU_bkbhit (SU_next_in!=SU_next_out)
 
 BYTE SU_bgetc() {
 BYTE SU_c;
 
 while(!SU_bkbhit) ;
 SU_c=SU_buffer[SU_next_out];
 SU_next_out=(SU_next_out+1) % SU_BUFFER_SIZE;
 return(SU_c);
 }
 
 
 //Inventory 16 Slot function==========================================
 void inventory(){
 
 fputc(0x40,reader);  //Header
 delay_ms(1);
 fputc(0x0C,reader);  //LEN
 delay_ms(1);
 fputc(0x00,reader);
 delay_ms(1);
 fputc(0x01,reader);  //Command
 delay_ms(1);
 fputc(0x06,reader);  //Flag For 16 slot=0x06; 1 slot=0x26
 delay_ms(1);
 fputc(0x00,reader);  //mask   length
 delay_ms(1);
 fputc(0x00,reader);  //AFI
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value1
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value2
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value3
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value4
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value5
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value6
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value7
 delay_ms(1);
 fputc(0x00,reader);  //Mask Value8
 delay_ms(1);
 fputc(0x0B,reader);  //BCC
 delay_ms(1);
 }
 
 //RF On
 void rf_on(){
 fputc(0x40,reader);
 delay_ms(1);
 fputc(0x02,reader);
 delay_ms(1);
 fputc(0x00,reader);
 delay_ms(1);
 fputc(0x41,reader);
 delay_ms(1);
 fputc(0x01,reader);
 delay_ms(1);
 fputc(0x42,reader);
 //delay_ms(50);
 }
 
 //main program================================================
 
 #pragma zero_ram     //clear all register memory
 
 void main() {
 unsigned int32 i;
 unsigned int32 len; //data length
 unsigned int32 j; //memory address
 unsigned int32 k; //Fix Number of id set
 unsigned int32 m;  //for use in combining bytes
 unsigned int32 a;  //to work in 16 slot
 char return_char[200];   //character return from DUR300
 char RF_char[10];
 char answer;   //User Command
 unsigned int32 combine1; //initialize UID first 32bits
 unsigned int32 combine2; //initialize UID second 32bits
 
 set_tris_a(0x00);
 set_tris_b(0x00000001);
 set_tris_c(0b10000000);
 output_low(pin_a0);     //Starting Buzzer and LED
 output_low(pin_a1);
 output_low(pin_B6);
 output_low(pin_B7);
 port_b_pullups(TRUE);
 ext_int_edge(H_to_L);
 enable_interrupts(global);
 
 enable_interrupts(int_ext);
 
 delay_ms(1000);
 
 fprintf(pc,"\r\n\Test 27 Slot_16 Running...Initializing ...\r\n");
 delay_ms(100);
 
 //RF ON Command=========================================
 rf_on();
 
 while(SU_bgetc()!=0x40); //clear all rubbish
 for (i=0;i<4;i++) {  //Reader will return 5 bytes
 rf_char[i]=SU_bgetc();
 fprintf(pc,"%02X", RF_char[i]);
 }
 
 //16 Slot Scanning==========================
 do{
 inventory();
 
 for (i=0;i<3;i++) {  //3 byte for header + length
 return_char[i]=SU_bgetc();
 fprintf(pc,"%02X", return_char[i]);
 }
 len=return_char[1];
 //Store All len data into array return_char[i]===============
 i=3;
 do{
 return_char[i]=SU_bgetc();
 fprintf(pc,"%02X", return_char[i]);
 i++;
 }while((i<(len+4)));
 
 //if got tag==(I wonder why the program skip process the code below)==
 if(len>18){
 
 j=4;
 
 while(j<(len+5)){
 combine1=0; //initialize UID first 32bits
 combine2=0; //initialize UID second 32bits
 
 if(return_char[j]==0x30){
 output_high(pin_B7);  // LED indicate Busy
 
 for((a=j+2);a<(j+6);a++){                //loop for first 32bit data
 
 combine1=(combine1<<8)|return_char[a];
 }
 for((a=i+6);a<(i+10);a++){
 
 combine2=(combine2<<8)|return_char[a];
 }
 j=j+10;
 fprintf(pc,"%LX %LX \r\n",combine1,combine2);
 }
 
 else j=j+1;
 
 }
 
 } else fprintf(pc,"No Tags!!\r\n ");
 
 } while (true);
 }
 | 
 
 Last edited by neverlog on Mon Jul 19, 2010 5:55 am; edited 4 times in total
 |  |  
		|  |  
		| neverlog 
 
 
 Joined: 02 May 2010
 Posts: 36
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 19, 2010 5:42 am |   |  
				| 
 |  
				| After I have send the Inventory() Command to the reader, it will return a string of bytes with size of _len_ which contain multiple tag ID which need to chop off by user(The code is under 16 slot scanning), I was wonder is there any limitation on the compiler or is it my programming problem. 
 When I stored all the bytes into the array, and I try to manipulate but no success. Can anyone please help me? Thank you very much.
 
 
 Note: the code which causing the problem is marked as comment :
 
 //if got tag==============================
 
 Last edited by neverlog on Mon Jul 19, 2010 5:47 am; edited 2 times in total
 |  |  
		|  |  
		| FvM 
 
 
 Joined: 27 Aug 2008
 Posts: 2337
 Location: Germany
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 19, 2010 5:43 am |   |  
				| 
 |  
				| Seems like you forgot to mention the actual problem with your code. 
 P.S.: I fear, it's not much clearer from your second post.
 |  |  
		|  |  
		| neverlog 
 
 
 Joined: 02 May 2010
 Posts: 36
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 19, 2010 5:59 am |   |  
				| 
 |  
				| Please guys, I really need your help. 
 Please let me know which part you don't understand. Thank you.
 
 The program din't process the code which mark as comment:
 
 
 //if got tag==(I wonder why the program skip process the code below)==
 |  |  
		|  |  
		| ckielstra 
 
 
 Joined: 18 Mar 2004
 Posts: 3680
 Location: The Netherlands
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 19, 2010 11:20 am |   |  
				| 
 |  
				| Well,...   len must be <= 18. 	  | Code: |  	  | //if got tag==(I wonder why the program skip process the code below)== if(len>18){
 | 
 I guess that's not the answer you were looking for but it is the best we can give you right now because you didn't show us the result of the header bytes printout.
 
 
 You can not always rely on the people in this forum to find your problem. We don't have your hardware so it is difficult to see the data going into your system and how this misbehaves.
 
 Debugging is just like any other workmanship: you need the right tools for the job. For debugging that is one of the following:
 1) Best is to debug on the real hardware with an In Circuit Debugger, this allows you to go through the code in single steps and examine all variables.  Several models exist, starting around US $25 (Microchip ICD2, ICD3, PicKit3, one of the CCS tools or a no brand clone).
 2) A cheaper option is to do the debugging in the free Microchip MPLAB included Simulator but then you have to simulate the external reader hardware.
 3) Cheapest but less flexible is the good old 'printf' debugging. In your program you insert printf commands at the locations where you think things re going wrong. Then when you see the program never reaches one execution point you narrow down here by adding even more printf commands.
 |  |  
		|  |  
		| neverlog 
 
 
 Joined: 02 May 2010
 Posts: 36
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 19, 2010 8:02 pm |   |  
				| 
 |  
				|  	  | ckielstra wrote: |  	  | Debugging is just like any other workmanship: you need the right tools for the job. For debugging that is one of the following:
 1) Best is to debug on the real hardware with an In Circuit Debugger, this allows you to go through the code in single steps and examine all variables.  Several models exist, starting around US $25 (Microchip ICD2, ICD3, PicKit3, one of the CCS tools or a no brand clone).
 2) A cheaper option is to do the debugging in the free Microchip MPLAB included Simulator but then you have to simulate the external reader hardware.
 3) Cheapest but less flexible is the good old 'printf' debugging. In your program you insert printf commands at the locations where you think things re going wrong. Then when you see the program never reaches one execution point you narrow down here by adding even more printf commands.
 | 
 
 Hi ckielstra,
 
 Thank you very much. I really appreciate your guide and advice.
 
 It did help a lot.
 
 Once again, thank you and have a nice day!
 
 Regards,
 neverlog
 |  |  
		|  |  
		|  |  
  
	| 
 
 | 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
 
 |