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

Wiegand to Serial Driver

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

Wiegand to Serial Driver
PostPosted: Sat Aug 12, 2006 12:53 am     Reply with quote



I hope this simple code will help.

Code:

#byte PORTB   = 0xF81    // PortB special function register

#bit  RBPU    = 0xFF1.7  // PortB pull-ups enable bit (0-enable, 1-disable)
#bit  TMR3IF  = 0xFA1.1  // Timer3 Interrupt Flag bit

#bit  INTEDG0 = 0xFF1.6  // ext0 interrupt edge select bit
#bit  INT0IF  = 0xFF2.1  // ext0 interrupt flag bit
#bit  INTEDG1 = 0xFF1.5  // ext1 interrupt edge select bit
#bit  INT1IF  = 0xFF0.0  // ext1 interrupt flag bit

#bit  DATA0   = 0xF81.0  // data0 wiegand signal
#bit  DATA1   = 0xF81.1  // data1 wiegand signal

// PortB[0..7] weak pullups macro
#define ENABLE_PULLUPS()      RBPU=0    // enable weak pullups on PortB
#define DISABLE_PULLUPS()     RBPU=1    // disable weak pullups on PortB

#define TX_SIZE 64               // RS485 TX data size
byte TXbuffer[TX_SIZE];          // RS485 TX buffer
byte TXin  = 0;                  // RS485 TX data IN index
byte TXout = 0;                  // RS485 TX data OUT index

int1 gltmr3f    = 0;
int1 glprocessf = 0;
int1 gldata0f   = 0;
int1 gldata1f   = 0;

unsigned int8  siteCode    = 0;
unsigned int16 idCode      = 0;
unsigned int32 wiegandData = 0;


Code:

#int_ext    // Data0 external interrupt
void data0_isr()
{
   if (!gldata0f)
      wiegandData <<= 1;
   
   if (!gltmr3f){
      set_timer3(30536);
      clear_interrupt(int_timer3);
      enable_interrupts(int_timer3);
      gltmr3f = 1;
   }
}

#int_ext1   // Data1 external interrupt
void data1_isr()
{
   if (!gldata1f){
      wiegandData <<= 1;
      wiegandData |= 1;
   }
   
   if (!gltmr3f){
      set_timer3(30536);
      clear_interrupt(int_timer3);
      enable_interrupts(int_timer3);
      gltmr3f = 1;
   }
}

#int_timer3 // 3mSec. timeout
void timer3_isr()
{
   disable_interrupts(int_timer3);
   glprocessf = 1;
   gldata0f = 1;
   gldata1f = 1;
}

#int_tbe
void Serial_TX_isr()
{
   putc(TXbuffer[TXout]);           // put char to TXbuffer then send
   TXout=(TXout+1) % TX_SIZE;       // increment & limit to max buffer size
   if(TXin==TXout)                  // if last in data, stop RS485 TX interrupt
     disable_interrupts(int_tbe);   // disable transmit interrupt
}
Code:

void bputc(char c)                  // bufferred putc routine
{
   short restart;                   // restart flag
   int ni;                          // temp variable

   restart=TXin==TXout;             // set restart flag at start
   TXbuffer[TXin]=c;                // store char to TXbuffer
   ni=(TXin+1) % TX_SIZE;           // limit to max buffer size
   while(ni==TXout);
   TXin=ni;
   if(restart)                      // enable TX interrupt
     enable_interrupts(int_tbe);
}

int1 check_Parity(unsigned int8 index,unsigned int32 wgCode)
{
   int1 pResult   =0;
   unsigned int8 i=0;

   for (i=index; i<index>>16) & 0xFF;
               printf(bputc,"\r\nSite Code = %U ** ID Code = %LU \r\n",siteCode,idCode);
               wiegandData = 0;
            }
         }
         gldata0f = 0;
         gldata1f = 0;
         gltmr3f = 0;
      }
   }
}


Enjoy... =P
Newbaby



Joined: 12 Aug 2006
Posts: 1

View user's profile Send private message

Wiegand to Serial Driver
PostPosted: Sat Aug 12, 2006 2:49 am     Reply with quote

Hi ChingB,

Could you put an example code to show how to use your driver please
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

Re: Wiegand to Serial Driver
PostPosted: Sun Aug 13, 2006 2:21 am     Reply with quote

Newbaby wrote:
Hi ChingB,

Could you put an example code to show how to use your driver please


Below is the complete code listing:
Code:

#byte PORTB   = 0xF81    // PortB special function register

#bit  RBPU    = 0xFF1.7  // PortB pull-ups enable bit (0-enable, 1-disable)
#bit  TMR3IF  = 0xFA1.1  // Timer3 Interrupt Flag bit

#bit  INTEDG0 = 0xFF1.6  // ext0 interrupt edge select bit
#bit  INT0IF  = 0xFF2.1  // ext0 interrupt flag bit
#bit  INTEDG1 = 0xFF1.5  // ext1 interrupt edge select bit
#bit  INT1IF  = 0xFF0.0  // ext1 interrupt flag bit

#bit  DATA0   = 0xF81.0  // data0 wiegand signal
#bit  DATA1   = 0xF81.1  // data1 wiegand signal

// PortB[0..7] weak pullups macro
#define ENABLE_PULLUPS()      RBPU=0    // enable weak pullups on PortB
#define DISABLE_PULLUPS()     RBPU=1    // disable weak pullups on PortB

#define TX_SIZE 64               // RS485 TX data size
byte TXbuffer[TX_SIZE];          // RS485 TX buffer
byte TXin  = 0;                  // RS485 TX data IN index
byte TXout = 0;                  // RS485 TX data OUT index

int1 gltmr3f    = 0;
int1 glprocessf = 0;
int1 gldata0f   = 0;
int1 gldata1f   = 0;

unsigned int8  siteCode    = 0;
unsigned int16 idCode      = 0;
unsigned int32 wiegandData = 0;

#int_ext    // Data0 external interrupt
void data0_isr()
{
   if (!gldata0f)
      wiegandData <<= 1;
   
   if (!gltmr3f){
      set_timer3(30536);
      clear_interrupt(int_timer3);
      enable_interrupts(int_timer3);
      gltmr3f = 1;
   }
}

#int_ext1   // Data1 external interrupt
void data1_isr()
{
   if (!gldata1f){
      wiegandData <<= 1;
      wiegandData |= 1;
   }
   
   if (!gltmr3f){
      set_timer3(30536);
      clear_interrupt(int_timer3);
      enable_interrupts(int_timer3);
      gltmr3f = 1;
   }
}

#int_timer3 // 3mSec. timeout
void timer3_isr()
{
   disable_interrupts(int_timer3);
   glprocessf = 1;
   gldata0f = 1;
   gldata1f = 1;
}

#int_tbe
void Serial_TX_isr()
{
   putc(TXbuffer[TXout]);           // put char to TXbuffer then send
   TXout=(TXout+1) % TX_SIZE;       // increment & limit to max buffer size
   if(TXin==TXout)                  // if last in data, stop RS485 TX interrupt
     disable_interrupts(int_tbe);   // disable transmit interrupt
}

void bputc(char c)                  // bufferred putc routine
{
   short restart;                   // restart flag
   int ni;                          // temp variable

   restart=TXin==TXout;             // set restart flag at start
   TXbuffer[TXin]=c;                // store char to TXbuffer
   ni=(TXin+1) % TX_SIZE;           // limit to max buffer size
   while(ni==TXout);
   TXin=ni;
   if(restart)                      // enable TX interrupt
     enable_interrupts(int_tbe);
}



I got a problem of posting the entire code. I'll post it on a separate reply for the example on how I use it.

--
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

Re: Wiegand to Serial Driver
PostPosted: Sun Aug 13, 2006 2:40 am     Reply with quote

Newbaby wrote:
Hi ChingB,

Could you put an example code to show how to use your driver please


Code:

int1 check_Parity(unsigned int8 index,unsigned int32 wgCode)
{
   int1 pResult   =0;
   unsigned int8 i=0;

   for (i=index; i<(index+13); i++)
      pResult ^= bit_test(wgCode,i);

   return(pResult);     
}
chingB



Joined: 29 Dec 2003
Posts: 81

View user's profile Send private message

PostPosted: Sun Aug 13, 2006 2:42 am     Reply with quote

Below is the listing for the Main Program

Code:

void init_mcu() // MCU Initialization Routine
{
   delay_ms(4000); // delay for 4 seconds (stabilized MCU?)

   // B0 & B1 are conficured as input
   // the reset of PortB I/O are set to output
   set_tris_b(0x03); // set PortB I/O directions
   ENABLE_PULLUPS(); // enable weak pullups for PortB[0..7]

   INTEDG0 = 0;      // set to falling edge (External-0 Interrupt)
   INT0IF  = 0;      // clear ext0 interrupt flag
   INTEDG1 = 0;      // set to falling edge (External-2 Interrupt)
   INT1IF  = 0;      // clear ext2 interrupt flag
   TMR3IF  = 0;   // clear timer3 Interrupt flag

   setup_adc_ports(NO_ANALOGS); // setup PortA to digital I/O
   
   setup_timer_3(T3_INTERNAL | T3_DIV_BY_8); // 104.8576mSec. timer overflow

   enable_interrupts(int_ext);
   enable_interrupts(int_ext1);
   enable_interrupts(global);   // enable global interrupt
}

//------------------
// Main Program
//------------------
main()
{
   init_mcu();                 // initialize MCU


   while (TRUE)
   {
      if (glprocessf)
      {
         glprocessf = 0;
         //printf("\r\nWiegand Data = %LX\r\n",wiegandData);
         if (check_Parity(0,wiegandData) == 1)        // odd parity
         {
            if (check_Parity(13,wiegandData) == 0){   // even parity
               idCode = wiegandData & 0xFFFF;
               siteCode = (wiegandData>>16) & 0xFF;
               printf(bputc,"\r\nSite Code = %U ** ID Code = %LU \r\n",siteCode,idCode);
               wiegandData = 0;
            }
         }
         gldata0f = 0;
         gldata1f = 0;
         gltmr3f = 0;
      }
   }
}


=P
jrgob



Joined: 21 Jan 2010
Posts: 10
Location: Brazil

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

PostPosted: Fri Jul 01, 2011 8:36 am     Reply with quote

Hello ChingB...

I understand your software, the data is received for connverter wiegand protocol for serial ...

I have some questions:

1) It is a generic code, suitable for any pic?
2) The protocol format is receiving wiegand 26? or receive any wiegand format?

I need to make a protocol wiegand converter for RS232 ... I know if you can help me.
_________________
Thanks.

jrgob
cllunlu



Joined: 30 Jun 2014
Posts: 4

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

PostPosted: Mon Jun 30, 2014 1:59 am     Reply with quote

I have project that reading data with wiegand 26 protocol. I am using PIC18F46K22 and CCS C. Compiler version 5.0.13.

I have used this code. But I failed. I wanna ask, why we use timer? Using external interrupt for Data0 and Data1 is not enough?

Can you please help about it?

Thanks
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library 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