I was trying to control r305 finger print sensor with a java code running on raspberry pi. r305 doesn't respond ,I couldn't understand what's going wrong.So, I would like to go through some working codes.
Posts: 6,843
Threads: 4
Joined: Mar 2015
/*
*AttendenceSystem.c
*
* Created: 21/09/2015 10:02:30 PM
* Author: Otoman
*/
#define F_CPU 16000000UL // set the CPU clock
#define BAUD 57600 // define baud
#define BAUDRATE ((F_CPU)/16/BAUD-1) // set baud rate value for UBRR
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
char buffer[10];
PortInit();
FlashLED();
Strobe();
Clear_Display();
Unpack_To_PortC(char data);
Short_Delay();
unsigned char USART_Receive();
void USART_TransmitFlag();
void USART_Transmit(unsigned char data);
void searchFinger(int bufferID, int startPage, int pageNum);
void USART_Init(unsigned int bRate);
void SetModuleAdder();
void SetParaSecurityLevel();
void SetParaBaudRateControl();
void verifyPassword();
void SetDataPackageLength();
void collect_finger_1();
void collect_finger_2();
void turnONPortCom();
void turnOFFPortCom();
void genTemplateFile();
void genCharacterFile();
void main(void)
{
PortInit();
LCDinit();
USART_Init(BAUDRATE);
sei();
LCD_Display("Welcome to Attendance System");
delay_ms(100);
Clear_Display();
while(1)
{
PORTB = PORTB & 0b11111110; // Lower PB0
PORTB = PORTB | 0b00000010; // Raise PB1
delay_ms(10);
if ((PINB & 0b00000100)==0){
LCD_Display("Switch 1");
FlashLED(); // Flash LED once
delay_ms(100);
Clear_Display();
}
if ((PINB & 0b00001000)==0){
LCD_Display("Switch 2");
FlashLED(); // Flash LED twice
FlashLED();
delay_ms(50);
Clear_Display();
}
PORTB = PORTB & 0b11111101; // Lower PB1
PORTB = PORTB | 0b00000001; // Raise PB0
delay_ms(10);
if((PINB & 0b00000100)==0){
LCD_Display("Switch 3");
delay_ms(10);
Clear_Display();
//Set ports
PORTD = PORTD & 0b11000010; //set S0 and S1 to Low bit
PORTD = PORTD | 0b00000010; //raise output PD1 and input PD0
turnONPortCom(); //Turn on com port
verifyPassword(); //Verify password
}
if ((PINB & 0b00001000)==0){
LCD_Display("Switch 4");
FlashLED(); // Flash LED four times
FlashLED();
FlashLED();
FlashLED();
delay_ms(50);
Clear_Display();
}
}
}
//*****************************************************************
//****************Initializing USART******************
void USART_Init(unsigned int bRate)
{
UBRRH = (bRate>>8); // shift the register right by 8 bits
UBRRL = bRate; // set baud rate
UCSRB = (1<<TXEN)|(1<<RXEN); // enable receiver and transmitter
UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); // set frame format: 8bit data, 2stop bit
}
//*****************************************************************
//****************Transmitting Frames with 5 to 8 Data Bits**********************
void USART_Transmit(unsigned char data )
{
while (!( UCSRA & (1<<UDRE))) /* Wait for empty transmit buffer */
;
UDR = data; /* Load data in the register */
while(!(UCSRA & (1<<TXC))); // wait until the data is transmitted from buffer
LCD_Display(itoa(data, buffer, 10));
delay_ms(10);
Clear_Display();
}
//*****************************************************************
//****************Receiving Frames with 5 to 8 Data Bits**********************
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while (!(UCSRA & (1<<RXC)));
/* Get and return received data from buffer */
return UDR;
}
//*****************************************************************
//************************ MACROS AND FUNCTIONS ********************
void delay_ms (int d){
int i = 0;
while(i<d)
{
_delay_ms(2);
i++;
}
}
//********************************************************************
//************************** LCD INITIALIZATION **********************
void LCDinit(void)
{
DDRC = 0xFF; // Port C is output
PORTC = 0x00; // E = 0
delay_ms(5); // Wait for LCD
PORTC = PORTC & 0x0F; // mask away high nibble from Port D
PORTC = PORTC | 0x20; // enable 4-bit data transfer
Strobe();
Unpack_To_PortC(0x28); // enable 4-bit data transfer
Unpack_To_PortC(0x0C); // display on, no cursor
Unpack_To_PortC(0x06); // cursor shifts right
Clear_Display();
}
//******************************************************************
//*************************** LCD_Display ******************************
void LCD_Display(char *strg){
char character; // Exits loop when character = NULL
PORTC = PORTC | 0b00000100; // E = 1
PORTC = PORTC & 0b11111101; // RW = 0,
PORTC = PORTC | 0b00000001; // RS = 1 (Data)
while (character = *strg)
{
Unpack_To_PortC(character); // write data to LCD
*strg++;
}
}
//************************** Strobe *********************************
Strobe(){
PORTC = PORTC | 0b00000100; // E = 1
Short_Delay;
PORTC = PORTC & 0b11111011; // E = 0
delay_ms(1);
}
//*******************************************************************
//************************ Clear_Display ****************************
Clear_Display(){
PORTC = PORTC | 0b00000100; // E = 1
PORTC = PORTC & 0b11111100; // RW = 0, RS = 0 (Cmd)
Short_Delay(); // Wait for LCD
Unpack_To_PortC(0x01); // clear LCD
Unpack_To_PortC(0x02); // cursor home
}
//*************************** Short_Delay ****************************
Short_Delay(){
char i;
for (i=0; i<2; i++){};
}
//*******************************************************************
//************************ Unpack_To_PortC **************************
Unpack_To_PortC(char data){
char temp;
temp = data;
PORTC = PORTC & 0x0F; // mask away high nibble from Port D
data = data & 0xF0; // mask away low nibble from data
PORTC = PORTC | data; // data low nibble to Port D high nibble
Strobe();
PORTC = PORTC & 0x0F; // mask away high nibble from Port D
temp = temp <<4; // move data low nibble to high nibble
PORTC = PORTC | temp; // data low nibble to Port D high nibble
Strobe();
}
//**********************************************************************
//**************************** Flash LED ************************/
FlashLED()
{
PORTA = PORTA | 0b00000001;
delay_ms(250);
PORTA = PORTA & 0b11111110; // Flash LED once
delay_ms(250);
}
//**************************** Port_Init *************************/
PortInit()
{
DDRD = DDRD | 0b00001110; // PD1, PD2, and PD3 are outputs and PD0 is input
PORTD = PORTD & 0b00000000; // PD0 are 0
DDRB = DDRB | 0b00000011; // PortB0 and PB1 are outputs
PORTB = PORTB & 0b00000000; // PortB0 and PB1 are 0
DDRA = DDRA | 0b00000111; // PortA0 is an output
PORTA = PORTA & 0b00000000; // PortA0 is 0
}
//**********************************************************************
void turnONPortCom(){
USART_Transmit(0xEF);
USART_Transmit(0x01);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0x01);
USART_Transmit(0x00);
USART_Transmit(0x04);
USART_Transmit(0x17);
USART_Transmit(0x01);
USART_Transmit(0x00);
USART_Transmit(0x1D);
LCD_Display("Start");
delay_ms(1);
Clear_Display();
int count=0;
unsigned char UDR_Value, UDR_ValueFinal;
while(count<10){
UDR_Value = USART_Receive();
UDR_ValueFinal = UDR_Value;
if(count==9){
if(UDR_ValueFinal ==0x00){
PORTA|=(1<<0);
LCD_Display("Port operation complete");
delay_ms(200);
Clear_Display();
}
else if(UDR_ValueFinal==0x01){
PORTA|=(1<<0);
LCD_Display("Error when receiving package");
delay_ms(20);
Clear_Display();
}
else if(UDR_ValueFinal==0x0D){
PORTA|=(1<<0);
LCD_Display("Fail to operate the com port");
delay_ms(20);
Clear_Display();
}
else{
PORTA|=(1<<0);
LCD_Display("No returned parameter");
delay_ms(20);
Clear_Display();
}
}
count++;
}
}
void turnOFFPortCom(){
USART_Transmit(0xEF);
USART_Transmit(0x01);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0x01);
USART_Transmit(0x00);
USART_Transmit(0x04);
USART_Transmit(0x17);
USART_Transmit(0x00);
USART_Transmit(0x00);
USART_Transmit(0x1C);
char UDR_Value, UDR_ValueFinal;
int i;
for(i=0; i<10; i++){
UDR_Value = USART_Receive();
if(i==9){
UDR_ValueFinal = UDR_Value;
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value= USART_Receive();
if(UDR_Value ==0x00){
LCD_Display("Port was turned Off");
delay_ms(200);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
else if(UDR_Value==0x01){
LCD_Display("Error when turning off the port");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
else if(UDR_Value==0x0D){
LCD_Display("Fail to operate the com port");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
else{
PORTA|=(1<<0);
LCD_Display("No returned parameter");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
}
}
}
void verifyPassword(){
USART_Transmit(0xEF);
USART_Transmit(0x01);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0x01);
USART_Transmit(0x00);
USART_Transmit(0x07);
USART_Transmit(0x13);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0xFF);
USART_Transmit(0x04);
USART_Transmit(0x17);
char UDR_Value, UDR_ValueFinal;
int i;
for(i=0; i<10; i++){
LCD_Display("Start");
delay_ms(50);
Clear_Display();
UDR_Value = USART_Receive();
itoa(UDR_Value, buffer, 10);
LCD_Display(buffer);
delay_ms(200);
Clear_Display();
if(i==9){
UDR_ValueFinal = UDR_Value;
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value = USART_Receive();
UDR_Value= USART_Receive();
if(UDR_ValueFinal ==0x00){
PORTA|=(1<<2);
LCD_Display("Correct Password");
delay_ms(200);
Clear_Display();
PORTA= PORTA & 0xb00000100;
}
else if(UDR_ValueFinal==0x01){
PORTA|=(1<<2);
LCD_Display("Error when receiving package");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
else if(UDR_ValueFinal==0x13){
PORTA|=(1<<2);
LCD_Display("Wrong password");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
else{
PORTA|=(1<<2);
LCD_Display("No returned parameter");
delay_ms(20);
Clear_Display();
PORTA= PORTA & 0xb00000000;
}
}
}
}