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

reading information serially

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



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

reading information serially
PostPosted: Wed Mar 15, 2006 7:57 am     Reply with quote

I want to read information from a smart card
What i want to know is how would i implement a pin on the 16f74 to receive information that is being sent to it serially
I already have a circuit set up so that this pin (which ever one i choose to use) will be used for input only
I will be reading in binary from the smart card

Do i have to set up several variables to capture each binary bit, or can i just let it run and have all the binary in the one variable?

is there a way of saving the information to the internal eprom as it arrives on the pin for transmission to a computer via comm1 later?

Im sure someone here has some experience in this sort of serial communication and thanks in advance for any information that is provided, if you need to know more about what im doing or if the information i have provided is of no use or is to vague please let me know

cheers
ninebarmaximus



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

reading information serially
PostPosted: Wed Mar 15, 2006 11:03 am     Reply with quote

I was thinking of something like this please tell me if it is feasible
Code:

char reset_instring[112];//estimate that responce will be 14 bytes

void A_Reset()
{
output_high(pin_xx)     //RST is set to high mode

While (input(pin_xx));
        for (i=0;i<112;I++)
        {
               reset_instring[i]=input(pin_xx);//input taken from pin chosen to be received info from sim card
         }


the main reason i think this might be dodgy is that information is being sent on each clock pulse and this code i think will take more than one clock pulse to input each binary digit
does anyone know of a better method of doing this??

Cheers
Ttelmah
Guest







PostPosted: Wed Mar 15, 2006 11:34 am     Reply with quote

Internal EEPROM. No way. Takes typically 4mSec to write a byte...
You say '16F74'. This doesn't have internal EEPROM.
A 112 byte array on the 16F74 won't work. It has a maximum contiguous 'block' in the RAM, of just 96 bytes.
One 'clock pulse' of what clock?.
If the clock is even close at the processor instruction rate, forget doing it in a PIC. You will need to build a hardware memory controller in discrete logic, or a FPGA, and store the data into this. The PIC will take at least half a dozen instruction times, just to read a bit, and loop. With a counter, and test, add another four or so instructions. With an array access, probably another 8 instructions. Say close to twenty instruction times at the best.

Best Wishes
ninebarmaximus



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

reading information serially
PostPosted: Wed Mar 15, 2006 12:08 pm     Reply with quote

Fair comment i hadnt really thought about how the clock cycles were going to affect each instruction,

oh and its 112 bits of information 14 bytes that i would be expecting from the answer to reset

thanks for the info it will give me plenty to cogitate on
ckielstra



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

View user's profile Send private message

PostPosted: Wed Mar 15, 2006 12:50 pm     Reply with quote

I don't know the protocol used by SMART cards, but isn't it possible to use the UART or SPI module?

Isn't a SIM card used in mobile telephones a special edition of SMART cards? If yes, using the search function of this forum should give you some useful pointers.
ninebarmaximus



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

reading information serially
PostPosted: Wed Mar 15, 2006 2:50 pm     Reply with quote

Well since the first post i have realised, i really should have realised earlier, that the external clock supplying both pic and sim card is running at 4.9152MHz and at this speed the sim card will transmit/receive at 9600, so i have since set up the USART to master mode, transmit enable, synchrounous, low speed and the baud rate generator set to 9600.

and have receive pin set to serial port enable 8 bit continous receive, dont really know yet if these are the correct values yet but im gettin there, the thing i need to know now is mainly the origional question how do i set up variables to grab information that is being sent back from the sim

cheers again
ninebarmaximus



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

PostPosted: Wed Mar 15, 2006 2:59 pm     Reply with quote

this is the code i am using,, not finished but its a start getting a strange compiler error it says that i have an undefined identifier putchar at the point shown below, do i need an include file for that i would have thought that the compiler would know of one of the main operans such as putchar
Code:

#include "C:\finbar\Sim.h"
#include <stdio.h>
char answer;
int timeout;
char instring;
long clock_count;
long i;
long j;
char reset_instring[112];//estimate that responce will be 14 bytes

//*******************************************************************************************
void set_up_contacts()
{
output_low(pin_B4);    //RST is in state L
delay_us(10);
output_high(pin_B7);    //VCC is set to high
delay_us(10);
output_high(pin_B3);    //VPP is raised to idle
delay_us(10);
output_high(pin_B1);    //I/O transmit to card set to off---(receive from card enabled)---
delay_us(10);
output_low(PIN_B0);     //CLK is allowed to transmit to card
}

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


void A_Reset()
{
output_high(pin_B4);     //RST is set to high mode
                       
While (input(pin_C7))
{  clock_count++;
   if ((input(pin_C7)) && clock_count >40000)
   {
      goto start;
   }
}

for (i=0;i<112;i++)
{
reset_instring[i]=input(pin_C7);//input taken from pin chosen to be received info from sim card
//if needed can put a delay in here to get everypulse
//need to transfer all the information in array to a series
//of byte size strings to send to PC via RS232

}
for (j=0;j<112;j++)
{
   putc(reset_instring[j]);<---HERE IS THE ERROR LINE SPECIFIED<---
}
}

void set_usart_speed()
{
#asm
bsf      STATUS,RP0     //memory bank 1
movlw    0x7F           // 9600
movwf    b'10110000'    //master,8bit,transmit enabled,sync,low speed
movwf    TXSTA

bcf      STATUS,RP0     //memory bank 0
movlw    b'10010000'    //serial port enable,8bit,continous receive
movwf    RCSTA
bcf      STATUS,RP0     //memory bank 0

clrf     TXREG          //clear TXREG
bcf      PIR1,TXIF
#endasm
}



void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
//at this point all the contacts for the sim card should be disabled to avoid damaging the card


#use rs232(baud=9600, parity=n, bits=8, xmit=pin_a0, rcv=pin_a1, errors)
#use fixed_io(c_outputs=PIN_C6, c_inputs=PIN_C7)
set_usart_speed();



//****************************************************************************
//****************************************************************************
//must turn off all contacts to the card before they can be turned on in correct order
output_low(pin_B4);    //RST is in state L
output_low(pin_B7);    //VCC is set to high
output_low(pin_B3);    //VPP is raised to idle
output_low(pin_B1);    //I/O transmit to card set to off---(receive from card enabled)---
output_high(PIN_A0);     //CLK disabled
//*****************************************************************************
//*****************************************************************************
//this code is to see if RS232 is operating correctly
start:
answer = 'g';

timeout = 1;

   while(!kbhit()&&(++timeout<500)) //5ms apparently
      delay_cycles(1);
   if(kbhit())
   answer=getch();
   else
   goto start;

   if (answer != 'g')   //fairly redundant since it has to have an answer to get this far
      putc(answer);

   if (answer == 'c')
      set_up_contacts();

   if (answer == 'r')
      A_Reset();

   goto start;

}
[/b]
ckielstra



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

View user's profile Send private message

PostPosted: Thu Mar 16, 2006 2:47 am     Reply with quote

Move the #use rs232 and #use fixed_io lines to the start of your program, or at least before the first use of any putc and I/O statements, and _never_ place these lines inside a function but at the global level.

Which processor are you using? I don't see a line like
Code:
#include <16F74.h>
Also the #fuses are not shown. Are these in sim.h?

You still have the 112 bytes aray, this won't fit in a PIC16F74. Also note that the putc function will transmit bytes, not bits. Make it a 14 byte array.
ninebarmaximus



Joined: 27 Feb 2006
Posts: 20

View user's profile Send private message

PostPosted: Thu Mar 16, 2006 9:49 am     Reply with quote

In the asm code im using above i get an error when i try to switch banks!! do i just leave it as it is but remove the bank switching? does the compiler know which banks do what and do it automatically?

Also im using hte 16f74 its contained in the sim.h file
Code:
#include <16C74.h>
#device adc=8
#fuses NOWDT,RC, PUT, NOPROTECT
#use delay(clock=4915200)

Ttelmah
Guest







PostPosted: Thu Mar 16, 2006 3:25 pm     Reply with quote

Yes.
The internal assembler, is not standard ASM. If you address a variable, and it is in a different bank, it automatically handles the switching for you. You can use the keyword "#ASM asis", and then the assembler will not bank switch for you. This is in the latter manuals (under #ASM).

Best Wishes
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