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 support@ccsinfo.com

RDM6300 replicating reads
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
erpgc82



Joined: 02 May 2020
Posts: 62

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

RDM6300 replicating reads
PostPosted: Sun Jul 04, 2021 2:18 pm     Reply with quote

Hi friends, can someone help me?
When I believe I've learned everything, complex problems arise. :\

I am using the RFID reader RDM6300, a very low cost and easy to use SERIAL reader, I believed it to be easy...

It Reads and replicates the reading of the same card many times.

I've tried dozens of possibilities and all, no matter how well it works, at some point the code replicates, and even the decimal calculation is incorrect.

Edit.: Most of the time it reads, reads and converts correctly! The code is rarely read replicated, including incorrectly calculating the conversion, but after clearing by the clear_card() function; the next code then reads correctly. my logic, IT MUST NOT BE GOOD.



See the code below:

Code:


#include <18f46k22.h>

#case // important to use variables with the same name!

#use delay(internal=64Mhz)

#fuses INTRC_IO
#fuses PUT
#fuses BROWNOUT
#fuses NOLVP
#fuses NODEBUG
#fuses NOMCLR
#fuses CCP3E0
#fuses NOXINST
#fuses NOPBADEN
#fuses NOIESO
#fuses NOFCMEN

#use rs232(baud=9600, parity=N, xmit=PIN_D6, rcv=PIN_D7, bits=8, stop=1, ERRORS, stream=READER)
#use rs232(baud=9600, parity=N, xmit=PIN_C6, rcv=PIN_C7, bits=8, stop=1, ERRORS, stream=PC)

#bit tmr0if = 0xff2.2

#use standard_io (a)
#use standard_io (b)
#use standard_io (c)
#use standard_io (d)
#use standard_io (e)

#include "display_8bits18f46k22.c"

#define bkbhit      (next_in!=next_out)
#define ckbhit      (next_inPC!=next_outPC)

#define BUFFER_SIZE     64
#define BUFFER_SIZEPC 64

////////////////////////// Reader //////////////////////////////////
BYTE  next_in                 = 0;
BYTE  next_out               = 0;
BYTE  buffer                    [BUFFER_SIZE];   
int8  barcode_buffer            [10];
int8  rfidcod_buffer            [15];   
//////////////////////// PC/Software ///////////////////////////////
BYTE  next_inPC               = 0;
BYTE  next_outPC             = 0;
BYTE  buffer_PC                 [BUFFER_SIZEPC];

int1  timer_Td                = FALSE;
int1  rfid8_16                  = FALSE;
int1  barcode                   = FALSE;
int1  card_reader             = FALSE;

int   decRFID8              = 0;
int   tcode                 = 0;
int16 decRFID16             = 0;

void clear_card();
void convertHexDec();
void reader_CODE();

/*********************************************************************/
#INT_RDA
void serial_isr1()
{
    int p;
   
    buffer_PC[next_inPC]=fgetc(PC);
    p=next_inPC;
    next_inPC=(next_inPC+1)%BUFFER_SIZEPC;   
    if(next_inPC==next_outPC) next_inPC=p;
}

/*********************************************************************/
#INT_RDA2
void serial_isr()
{
    int t;
   
    buffer[next_in]=fgetc(READER);
    t=next_in;
    next_in=(next_in+1)%BUFFER_SIZE;   
    if(next_in==next_out) next_in=t;   
}

/*********************************************************************/

BYTE bgetc()
{
    BYTE c;
    if(card_reader)
    {
        while(!bkbhit);
        c=buffer[next_out];
        next_out=(next_out+1)%BUFFER_SIZE;
    }
    if((c==0x03)&&(!barcode)) card_reader=FALSE;
    if (barcode) return(c);   
}     

/*********************************************************************/
BYTE cgetc()
{
    BYTE d;
    while(!ckbhit);
    d=buffer_PC[next_outPC];
    next_outPC=(next_outPC+1)%BUFFER_SIZEPC;
   
    return(d);
   
}

/*********************************************************************/

void main()
{
    setup_oscillator(OSC_64MHZ|OSC_PLL_OFF);
    int8 p;
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_8);
    set_timer0(15536);
    enable_interrupts(GLOBAL);
    enable_interrupts(INT_RDA);
    enable_interrupts(INT_RDA2);
    // setup_timer_2(T2_DIV_BY_4,255,1);
    // setup_ccp3(CCP_PWM|CCP_PULSE_STEERING_A);
    display_ini();

    while(TRUE)
    {
        if(timer_Td)
        {
            if(tmr0if)
            {
                tmr0if=0;   
                //set_timer0(11);
                set_timer0(15536);
                tcode++;
            }
        }
        if((!card_reader )&&(!barcode)&&(tcode==80)) clear_card();

        if((bkbhit)&&(!barcode)&&(card_reader))
        {
            p=bgetc();                   
           
            if(((p==0x03)||(p==0x02)||(p=='0')||(p=='1')||(p=='2')||(p=='3')||(p=='4')||(p=='5')||(p=='6')||(p=='7')||(p=='8')||(p=='9'))&&((!barcode)&&(card_reader)))
            {
               
               
                rfidcod_buffer[0]  = p;        // STX  // STX 0x02 START
                rfidcod_buffer[1]  = bgetc();  // 1º  7
                rfidcod_buffer[2]  = bgetc();  // 2º  1
                rfidcod_buffer[3]  = bgetc();  // 3º  0 // RS  0x30
                rfidcod_buffer[4]  = bgetc();  // 4º  0 // RS  0x30
                rfidcod_buffer[5]  = bgetc();  // 5º  7 // Decimal 122
                rfidcod_buffer[6]  = bgetc();  // 6º  A // Decimal 122
                rfidcod_buffer[7]  = bgetc();  // 7º  3 // Decimal 14100
                rfidcod_buffer[8]  = bgetc();  // 8º  7 // Decimal 14100
                rfidcod_buffer[9]  = bgetc();  // 9º  1 // Decimal 14100
                rfidcod_buffer[10] = bgetc();  // 10º 4 // Decimal 14100
                rfidcod_buffer[11] = bgetc();  // 11º 2
                rfidcod_buffer[12] = bgetc();  // 12º 8
                rfidcod_buffer[13] = bgetc();  // ETX  // ETX 0x03 STOP
                rfidcod_buffer[11]=0;          //
                convertHexDec();
               
                if(rfid8_16) fprintf(PC,"%03u%05Lu#\n\r",decRFID8,decRFID16);
                else fprintf(PC,"%08Lu#\n\r",decRFID16);
               
                reader_CODE();
            }
        }
    }
}

/*********************************************************************/

void convertHexDec()
{   
    BYTE  nibleA, nibleB, nible1, nible2, nible3, nible4;
   
    if ((!card_reader)&&(!barcode))
    {
        nibleA = rfidcod_buffer[5];
        if(nibleA=='A') nibleA=10;
        if(nibleA=='B') nibleA=11;
        if(nibleA=='C') nibleA=12;
        if(nibleA=='D') nibleA=13;
        if(nibleA=='E') nibleA=14;
        if(nibleA=='F') nibleA=15;
        if(nibleA=='0') nibleA=0;
        if(nibleA=='1') nibleA=1;
        if(nibleA=='2') nibleA=2;
        if(nibleA=='3') nibleA=3;
        if(nibleA=='4') nibleA=4;
        if(nibleA=='5') nibleA=5;
        if(nibleA=='6') nibleA=6;
        if(nibleA=='7') nibleA=7;
        if(nibleA=='8') nibleA=8;
        if(nibleA=='9') nibleA=9;

        nibleB = rfidcod_buffer[6];
        if(nibleB=='A') nibleB=10;
        if(nibleB=='B') nibleB=11;
        if(nibleB=='C') nibleB=12;
        if(nibleB=='D') nibleB=13;
        if(nibleB=='E') nibleB=14;
        if(nibleB=='F') nibleB=15;
        if(nibleB=='0') nibleB=0;
        if(nibleB=='1') nibleB=1;
        if(nibleB=='2') nibleB=2;
        if(nibleB=='3') nibleB=3;
        if(nibleB=='4') nibleB=4;
        if(nibleB=='5') nibleB=5;
        if(nibleB=='6') nibleB=6;
        if(nibleB=='7') nibleB=7;
        if(nibleB=='8') nibleB=8;
        if(nibleB=='9') nibleB=9;

        nible1 = rfidcod_buffer[7];
        if(nible1=='A') nible1=10;
        if(nible1=='B') nible1=11;
        if(nible1=='C') nible1=12;
        if(nible1=='D') nible1=13;
        if(nible1=='E') nible1=14;
        if(nible1=='F') nible1=15;
        if(nible1=='0') nible1=0;
        if(nible1=='1') nible1=1;
        if(nible1=='2') nible1=2;
        if(nible1=='3') nible1=3;
        if(nible1=='4') nible1=4;
        if(nible1=='5') nible1=5;
        if(nible1=='6') nible1=6;
        if(nible1=='7') nible1=7;
        if(nible1=='8') nible1=8;
        if(nible1=='9') nible1=9;

        nible2 = rfidcod_buffer[8];
        if(nible2=='A') nible2=10;
        if(nible2=='B') nible2=11;
        if(nible2=='C') nible2=12;
        if(nible2=='D') nible2=13;
        if(nible2=='E') nible2=14;
        if(nible2=='F') nible2=15;
        if(nible2=='0') nible2=0;
        if(nible2=='1') nible2=1;
        if(nible2=='2') nible2=2;
        if(nible2=='3') nible2=3;
        if(nible2=='4') nible2=4;
        if(nible2=='5') nible2=5;
        if(nible2=='6') nible2=6;
        if(nible2=='7') nible2=7;
        if(nible2=='8') nible2=8;
        if(nible2=='9') nible2=9;

        nible3 = rfidcod_buffer[9];
        if(nible3=='A') nible3=10;
        if(nible3=='B') nible3=11;
        if(nible3=='C') nible3=12;
        if(nible3=='D') nible3=13;
        if(nible3=='E') nible3=14;
        if(nible3=='F') nible3=15;
        if(nible3=='0') nible3=0;
        if(nible3=='1') nible3=1;
        if(nible3=='2') nible3=2;
        if(nible3=='3') nible3=3;
        if(nible3=='4') nible3=4;
        if(nible3=='5') nible3=5;
        if(nible3=='6') nible3=6;
        if(nible3=='7') nible3=7;
        if(nible3=='8') nible3=8;
        if(nible3=='9') nible3=9;

        nible4 = rfidcod_buffer[10];
        if(nible4=='A') nible4=10;
        if(nible4=='B') nible4=11;
        if(nible4=='C') nible4=12;
        if(nible4=='D') nible4=13;
        if(nible4=='E') nible4=14;
        if(nible4=='F') nible4=15;
        if(nible4=='0') nible4=0;
        if(nible4=='1') nible4=1;
        if(nible4=='2') nible4=2;
        if(nible4=='3') nible4=3;
        if(nible4=='4') nible4=4;
        if(nible4=='5') nible4=5;
        if(nible4=='6') nible4=6;
        if(nible4=='7') nible4=7;
        if(nible4=='8') nible4=8;
        if(nible4=='9') nible4=9;

        decRFID16 = /*(int16)*/ ((4096 * nible1) + (256 * nible2) + (16 * nible3) + (1 * nible4));
       
        if (rfid8_16) decRFID8  = ((16 * nibleA) + (1 * nibleB));
    }
}

/*********************************************************************/

void reader_CODE()
{
   
    timer_Td=TRUE;
   
    display_pos_xy(9,2);
   
    if(barcode) printf(write_display,"00%c%c%c%c%c%c",barcode_buffer[0],barcode_buffer[1],barcode_buffer[2],barcode_buffer[3],barcode_buffer[4],barcode_buffer[5]);
    else
    {
        if(rfid8_16) printf(write_display,"%03u%05Lu",decRFID8,decRFID16);
        else printf(write_display,"%08Lu",decRFID16);
    }
}

/*********************************************************************/

void clear_card()
{
    if ((!card_reader)&&(!barcode))
    {
        memset(buffer,0,BUFFER_SIZE+64);
        card_reader=TRUE;
    }
}   


_________________
Gradually you will go far with persistence, will and determination!


Last edited by erpgc82 on Sun Jul 04, 2021 9:20 pm; edited 4 times in total
temtronic



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

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 3:07 pm     Reply with quote

I had a quick scan of your code....

serious....
I did NOT see an ISR for the UART assigned to the PC port( RDA) BUT you've enabled the INT_RDA. That will, sooner or later 'get you' ! Either disable the interrupt or actually code an ISR. Course I am getting new glasses tomorrow, so maybe it's really there ??

also...

not serious...
you call the stream for the RFID reader 'READY'. Not a good choice of words, perhaps RFRDR or RFID_RDR ? Something more descriptive of what it's for.READY looks more like a positive response to something, gets old guys like me confused ....

Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 21477

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 4:24 pm     Reply with quote

Your code doesn't compile. I got 21 errors. Fix that.
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Sun Jul 04, 2021 4:28 pm     Reply with quote

Hello temtronic, thanks for your attention!

Edited, see changes
I found it unnecessary to put the interrupt "#INT_RDA" in sending to "PC", as the problem is related only to reading the RFID code.

The same problem that occurs is shown on Display or sent to PC.

Just my logic to prevent the reading from repeating is not being effective after a week of trying and dozens of attempts!

Thanks anyway, I'll keep trying...
_________________
Gradually you will go far with persistence, will and determination!


Last edited by erpgc82 on Sun Jul 04, 2021 4:38 pm; edited 1 time in total
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Sun Jul 04, 2021 4:33 pm     Reply with quote

PCM programmer wrote:
Your code doesn't compile. I got 21 errors. Fix that.


Hello PCM Programmer, impossible, here it is working normally, the problem is only in the logic of avoiding the code to replicate.

Isn't it for lack of the #case directive?

I was very careful to post here all the functions and variable declarations necessary for understanding.

I just forgot to comment out "\\" enable_interrupts(INT_RDA);

edited, see the changes
_________________
Gradually you will go far with persistence, will and determination!
dyeatman



Joined: 06 Sep 2003
Posts: 1836
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 5:07 pm     Reply with quote

Yes, some of it is #case related

Where is the routine: display_8bits18f46k22.c ?

Without complete compilable code how can anyone test your program?
_________________
Google and Forum Search are some of your best tools!!!!
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Sun Jul 04, 2021 5:19 pm     Reply with quote

dyeatman wrote:
Yes, some of it is #case related

Where is the routine: display_8bits18f46k22.c ?

Without complete compilable code how can anyone test your program?


Hi dyeatman, as I said before, I posted with the intention of looking at it visually, so I didn't post about PC RDA interrupt.
That's why I didn't put all the functions I'm doing.

I'm still learning, I'm "playing" with readers, LCD, LEDs and I came across this problem, which makes RFID reading with the RDM6300 nothing functional!

See the file code below:

#include "display_8bits18f46k22.c"
Code:

#define rs PIN_D4 
#define en PIN_D5 
#define data output_b

void display_cmd(int cmd)   
{
    data(cmd);     
    output_high(en);
    output_low(en);
}

void display_send_byte(short int level, int data)
{
    output_bit(rs,level);
    delay_us(100);       
    display_cmd(data);   
 }

void display_pos_xy(int x, int y)
{
    int address;
    if (y!=1)   
        address=0xc0;
    else       
        address=0x80;
   
    address+= x-1;
                   
    display_send_byte(0,address);
}

void write_display(char c)   
{
    switch(c)   
    {
        case '\f' : display_send_byte(0,1);   
                                             
                    delay_us(1600);           
                    break;                   
        case '\n' :                           
        case '\r' : display_pos_xy(1,2);     
                    break;                   
        case '\b' : display_send_byte(0,0x10);
                    break;                   
          default : display_send_byte(1,c);   
                                             
                                             
                                             
    }
}

void display_ini()   
{
    output_low(rs);   
    delay_ms(16);     
   
    display_cmd(0x30);
    delay_us(4500);   
   
    display_cmd(0x30);
    delay_us(110);   
   
    display_cmd(0x30);
    delay_us(45);     
   
    display_cmd(0x38);
   
    display_cmd(0x01);
    delay_us(1600);   
   
    display_cmd(0x0c);
   
    display_cmd(0x06);
   
   
   
}

_________________
Gradually you will go far with persistence, will and determination!
dyeatman



Joined: 06 Sep 2003
Posts: 1836
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 6:14 pm     Reply with quote

Havng a complete compilable program allows the group to look at all the code
and alert you to what all we see. In some cases we also have the hardware
and can fully test it. For example the hex/decimal conversion can be
accomplished in a much "cleaner" way and we can show you ways to do that
in addition to the duplication issue.
_________________
Google and Forum Search are some of your best tools!!!!
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Sun Jul 04, 2021 6:26 pm     Reply with quote

dyeatman wrote:
Havng a complete compilable program allows the group to look at all the code
and alert you to what all we see. In some cases we also have the hardware
and can fully test it. For example the hex/decimal conversion can be
accomplished in a much "cleaner" way and we can show you ways to do that
in addition to the duplication issue.



hi dyeatman, i just found the atoi() function that maybe could take a character and convert it to an integer, but it didn't work. Tried the int() option didn't work either!

I couldn't find a function that would convert 0xFF to 255, so I did it as "manual" as possible... but it worked.

From what I've learned so far, I should wait for a certain character, in this case 0x03 which would be the end of the code read or 0x02 which would be the beginning, and thus make a decision, the decision I made was to stop a 1-bit variable that I allowed another read.

I did this both in the function that handles the interrupt, and inside the while, and without success!

Sad


I'll keep trying, thanks!
_________________
Gradually you will go far with persistence, will and determination!
PCM programmer



Joined: 06 Sep 2003
Posts: 21477

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 6:40 pm     Reply with quote

I'm still getting "Functions may not be nested" errors with your program.

If you want help, you need to post a compilable program
dyeatman



Joined: 06 Sep 2003
Posts: 1836
Location: Norman, OK

View user's profile Send private message

PostPosted: Sun Jul 04, 2021 6:54 pm     Reply with quote

PCM programmer is correct. You are still missing a close parentheses
among a few other things for it to compile and all those need to be fixed.

In regard to your question:
I would wait for the start character (0x2) then loop to receive the
remainder of the characters coming from the reader with a routine similar
to the following (using x as the loop counter):

p=bgetc();

if (p >= '0' && p <= '9')
rfidcod_buffer[x] = (p - 48);
else
if (p >= 'A' && p <= 'F')
rfidcod_buffer[x] = (p - 55);
else
rfidcod_buffer[x] = p;

This approach would clean up your code considerably. And there are a few
more ways to do this.
_________________
Google and Forum Search are some of your best tools!!!!
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Sun Jul 04, 2021 9:19 pm     Reply with quote

dyeatman wrote:
PCM programmer is correct. You are still missing a close parentheses
among a few other things for it to compile and all those need to be fixed.

In regard to your question:
I would wait for the start character (0x2) then loop to receive the
remainder of the characters coming from the reader with a routine similar
to the following (using x as the loop counter):

p=bgetc();

if (p >= '0' && p <= '9')
rfidcod_buffer[x] = (p - 48);
else
if (p >= 'A' && p <= 'F')
rfidcod_buffer[x] = (p - 55);
else
rfidcod_buffer[x] = p;

This approach would clean up your code considerably. And there are a few
more ways to do this.




hi dyeatman, i edited it again!

Now the code, small/clean, compiles this far for me!
There is enough to display on the display or send to PC.
Thanks for your tip, I'll try here a way to start reading, count the amount of characters and clean up.
_________________
Gradually you will go far with persistence, will and determination!
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Mon Jul 05, 2021 12:50 am     Reply with quote

Several little comments.

First use comments. You have some, but not enough to actually explain
what you are really doing. Comments are useful _to you_ when you come
back to code that you wrote ages ago. They then allow you to remember
what you were up to. Seriously, if I was marking this, you would get an
instant fail, for the lack of adequate comments.... Sad

Now there are some little faults. For example:
Code:

    setup_oscillator(OSC_64MHZ|OSC_PLL_OFF);


The OSC_PLL_OFF here actually does nothing. A good thing really, since
64MHz, _requires_ the PLL to be on. Get rid of this.

The 'glaring' thing though is this:
Code:

            p=bgetc();                   
           
            if(((p==0x03)||(p==0x02)||(p=='0')||(p=='1')||(p=='2')||(p=='3')||(p=='4')||(p=='5')||(p=='6')||(p=='7')||(p=='8')||(p=='9'))&&((!barcode)&&(card_reader)))
            {

The start of the packet from the chip is marked with 02. None of the
other characters are wanted. If the system has got out of sync, having
all these other characters accepted, will result in garbage being read.
Code:

            p=bgetc();                   
           
            if((p==0x2))&&((!barcode)&&(card_reader)))
            {

Is the correct code to identify the start of the packet.

As has already been pointed out your hex conversion code is appalling:
Code:

int8 hexval(int8 chr)
{
    if (chr>='0' && chr<='9')
        return chr-'0'; //value is digit minus 48
    if (chr>='A' && chr<='F')
        return chr-('A'+10); //value here is digit minus 55
    return 0;//invalid character
}

void convertHexDec(void)
{
    unsigned int16 result=0;
    unsigned int8 result8=0;
    int digit;
    if ((!card_reader)&&(!barcode))
    {     
        for (digit=5;digit<7;digit++) //first two nibbles
        {
            result8*=16;
            result8+=hexval(rfidcod_buffer[digit]);
        }
        for (digit=7;digit<11;digit++) //now the next four nibbles
        {
            result16*=16;
            result16+=hexval(rfidcod_buffer[digit]);
        }
        //for each digit, convert the hex ASCII character to a numeric
        //value, add to the result. Repeat for each digit, multiplying
        //the total by 16 as we go.

        decRFID16 = result16;
       
        if (rfid8_16)
             decRFID8  = result8;
    }
}

Typed this untested, so I may well have an error, but the point is you
don't have to repeat code six times, instead use a subroutine. Also the
actual conversion from Hex ASCII to decimal is just a matter of a simple
subtraction, depending on whether the value is 0 to 9, or A to F.

Now as a long term 'comment', consider not using the % in the buffer
handling routines, or if you do add a remark to your definitions:
Code:

#define BUFFER_SIZE     64
#define BUFFER_SIZEPC 64 //Beware these sizes _must_ be binary
//values 2,4,8,16 etc..


The % operator, the compiler is 'smart', and replaces with &(val-1), to
avoid a warning that you are using a division in the subroutine. However
it can't do this for non binary values.
The alternative code (avoiding this limitation) is:
Code:

/*********************************************************************/
#INT_RDA
void serial_isr1()
{
    int p;
   
    buffer_PC[next_inPC]=fgetc(PC);
    p=next_inPC;
    if (++next_inPC)==BUFFER_SIZEPC)
        nextinPC=0;   
    if(next_inPC==next_outPC) next_inPC=p;
}

This merrily copes with both binary and non binary buffer sizes.
erpgc82



Joined: 02 May 2020
Posts: 62

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

PostPosted: Tue Jul 06, 2021 9:02 pm     Reply with quote

Ttelmah wrote:
Several little comments.

First use comments. You have some, but not enough to actually explain
what you are really doing. Comments are useful _to you_ when you come
back to code that you wrote ages ago. They then allow you to remember
what you were up to. Seriously, if I was marking this, you would get an
instant fail, for the lack of adequate comments.... Sad

Now there are some little faults. For example:
Code:

    setup_oscillator(OSC_64MHZ|OSC_PLL_OFF);


The OSC_PLL_OFF here actually does nothing. A good thing really, since
64MHz, _requires_ the PLL to be on. Get rid of this.

The 'glaring' thing though is this:
Code:

            p=bgetc();                   
           
            if(((p==0x03)||(p==0x02)||(p=='0')||(p=='1')||(p=='2')||(p=='3')||(p=='4')||(p=='5')||(p=='6')||(p=='7')||(p=='8')||(p=='9'))&&((!barcode)&&(card_reader)))
            {

The start of the packet from the chip is marked with 02. None of the
other characters are wanted. If the system has got out of sync, having
all these other characters accepted, will result in garbage being read.
Code:

            p=bgetc();                   
           
            if((p==0x2))&&((!barcode)&&(card_reader)))
            {

Is the correct code to identify the start of the packet.

As has already been pointed out your hex conversion code is appalling:
Code:

int8 hexval(int8 chr)
{
    if (chr>='0' && chr<='9')
        return chr-'0'; //value is digit minus 48
    if (chr>='A' && chr<='F')
        return chr-('A'+10); //value here is digit minus 55
    return 0;//invalid character
}

void convertHexDec(void)
{
    unsigned int16 result=0;
    unsigned int8 result8=0;
    int digit;
    if ((!card_reader)&&(!barcode))
    {     
        for (digit=5;digit<7;digit++) //first two nibbles
        {
            result8*=16;
            result8+=hexval(rfidcod_buffer[digit]);
        }
        for (digit=7;digit<11;digit++) //now the next four nibbles
        {
            result16*=16;
            result16+=hexval(rfidcod_buffer[digit]);
        }
        //for each digit, convert the hex ASCII character to a numeric
        //value, add to the result. Repeat for each digit, multiplying
        //the total by 16 as we go.

        decRFID16 = result16;
       
        if (rfid8_16)
             decRFID8  = result8;
    }
}

Typed this untested, so I may well have an error, but the point is you
don't have to repeat code six times, instead use a subroutine. Also the
actual conversion from Hex ASCII to decimal is just a matter of a simple
subtraction, depending on whether the value is 0 to 9, or A to F.

Now as a long term 'comment', consider not using the % in the buffer
handling routines, or if you do add a remark to your definitions:
Code:

#define BUFFER_SIZE     64
#define BUFFER_SIZEPC 64 //Beware these sizes _must_ be binary
//values 2,4,8,16 etc..


The % operator, the compiler is 'smart', and replaces with &(val-1), to
avoid a warning that you are using a division in the subroutine. However
it can't do this for non binary values.
The alternative code (avoiding this limitation) is:
Code:

/*********************************************************************/
#INT_RDA
void serial_isr1()
{
    int p;
   
    buffer_PC[next_inPC]=fgetc(PC);
    p=next_inPC;
    if (++next_inPC)==BUFFER_SIZEPC)
        nextinPC=0;   
    if(next_inPC==next_outPC) next_inPC=p;
}

This merrily copes with both binary and non binary buffer sizes.



Hi Ttelmah, I accept your criticisms, they are constructive, thank you!

I started studying C programming with CCS in May 2020, my first contact with programming.

With simple programming, several "things" have already worked out and I was very happy. I don't know commercially, but testing here worked correctly. I still have a lot to learn.

That code of yours for Hexadecimal Decimal conversion didn't work. Can the CCS assess which character is smaller or larger than the other? I figured it was just whole numbers.

In my simple way, I have a great result. After the interrupt, inside the bgetc() function I do 2 checks.

Code:

BYTE bgetc()
{
    BYTE c;
    if(card_reader)
    {
        while(!bkbhit);
        c=buffer[next_out];
        next_out=(next_out+1)%BUFFER_SIZE;
    }
    if((c==0x02)&&(!barcode)) countRFID++;
    if((c==0x03)&&(!barcode)) card_reader=FALSE;
    return(c);
}


This is the code provided by the RDM6300 reader (0x02)0900408F8147(0x03)

So I ordered a count variable to be incremented, every time I have the value 0x02, and I ordered the reader to be disabled if I have the value 0x03.

After that I call the Hexadecimal to Decimal conversion function, when I get to the function I check if countRFID is >=2 if it is, I delete "clean" everything related to reading the card, example x=0.

Finally, before sending it to the display or to the PC, I check if the 16-bit variable resulting from the conversion is != of ​​0. In other words, as the first time it reads correctly it is different from 0, the normal operation will be done, if it repeats the reading, and it is 0, it will not send anything to the Display or to the PC.
_________________
Gradually you will go far with persistence, will and determination!
Ttelmah



Joined: 11 Mar 2010
Posts: 17318

View user's profile Send private message

PostPosted: Wed Jul 07, 2021 12:43 am     Reply with quote

OK. Typed the code in. Two mistakes. I'd left out the 16 on a variable name,
and added 10 not subtracted it:
Code:

int8 hexval(int8 chr)
{
    if (chr>='0' && chr<='9')
        return chr-'0'; //value is digit minus 48
    if (chr>='A' && chr<='F')
        return chr-('A'-10); //value here is digit minus 55
    return 0;//invalid character
}

void convertHexDec(void)
{
    unsigned int16 result16=0;
    unsigned int8 result8=0;
    int digit;
    if ((!card_reader)&&(!barcode))
    {     
        for (digit=5;digit<7;digit++) //first two nibbles
        {
            result8*=16;
            result8+=hexval(rfidcod_buffer[digit]);
        }
        for (digit=7;digit<11;digit++) //now the next four nibbles
        {
            result16*=16;
            result16+=hexval(rfidcod_buffer[digit]);
        }
        //for each digit, convert the hex ASCII character to a numeric
        //value, add to the result. Repeat for each digit, multiplying
        //the total by 16 as we go.

        decRFID16 = result16;
       
        if (rfid8_16)
             decRFID8  = result8;
    }
}

This is tested and works.

Sounds as if you move forwards. Smile
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