FRO AND RRO USING HEXADECIMAL SWITCH

Moderators: TomKerekes, dynomotion

Post Reply
AmitKumar171
Posts: 134
Joined: Tue Feb 20, 2018 7:35 am
Location: India

FRO AND RRO USING HEXADECIMAL SWITCH

Post by AmitKumar171 » Fri Sep 11, 2020 7:28 am

Hi Tom,

I am using KFLOP+KONNECT+KANALOG for connecting FRO i.e. the Hexadecimal switch which has 5 input pins and 16 position based on the 5 input pins.

I have connected the all 5 input pins for Hexadecimal switch to konnect pin number 1040,1041,1042,1043,1044. I want to set each position out of 16 position to use FRO 0 to 1.6.

How to proceed with the programs for checking 5 pins at each FRO change. ? how can i check the 5 pins for one input of FRO.

Waiting for your kind reply.
Thank You

AMIT KUMAR

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: FRO AND RRO USING HEXADECIMAL SWITCH

Post by TomKerekes » Fri Sep 11, 2020 6:51 pm

Hi Amit,

A hexadecimal switch should normally require 4 bits not 5.

Does the switch return gray code or binary?

If gray code you might do something like:

Code: Select all

#include "KMotionDef.h"

int grayToBinary(int num)
{
        unsigned int temp = num ^ (num>>4);
        temp ^= (temp>>2);
        temp ^= (temp>>1);
        return temp;
}

void main()
{
	float FRO=0.1f*grayToBinary(ReadBit(1040)*8+ReadBit(1041)*4+ReadBit(1042)*2+ReadBit(1043));
}
Regards,

Tom Kerekes
Dynomotion, Inc.

AmitKumar171
Posts: 134
Joined: Tue Feb 20, 2018 7:35 am
Location: India

Re: FRO AND RRO USING HEXADECIMAL SWITCH

Post by AmitKumar171 » Mon Sep 14, 2020 6:20 am

Hi tom ,

Thanks for your reply.
A hexadecimal switch should normally require 4 bits
Yes it requires 4 bits.
Does the switch return gray code or binary?
The switch returns binary codes.

Help me with Binary code program.

Waiting for your kind reply.
Thank You

AMIT KUMAR

User avatar
TomKerekes
Posts: 2677
Joined: Mon Dec 04, 2017 1:49 am

Re: FRO AND RRO USING HEXADECIMAL SWITCH

Post by TomKerekes » Mon Sep 14, 2020 4:56 pm

Hi Amit,

The problem with binary is sampling bits while they are changing can give indeterminate values. For example changing from 7 to 8 might temporarily read 0 or 15. I think the same debounce function can be used to identify when things have changed and are stable. See below:

Code: Select all

#include "KMotionDef.h"

int hlast=0,hlastsolid=-1,hcount=0;
int Debounce(int n, int *cnt, int *last, int *lastsolid);

void main()
{
	int hex;
	float FRO;
	
	for(;;)
	{
		WaitNextTimeSlice();

		// read hex value with potential glitches
		hex = ReadBit(1040)*8+ReadBit(1041)*4+ReadBit(1042)*2+ReadBit(1043);
		
		// "debounce" hex value
		if (Debounce(hex,&hcount,&hlast,&hlastsolid)!=-1) // changed and stable?
		{ 
			FRO = hlast * 0.1f;
			printf("FRO changed to %f\n",FRO);
		}
	}
}


// Debounce a bit
//
// return 1 one time when first debounced high 
// return 0 one time when first debounced low 
// return -1 otherwise 
#define DBTIME 300

int Debounce(int n, int *cnt, int *last, int *lastsolid)
{
	int v = -1;
	
	if (n == *last)  // same as last time?
	{
		if (*cnt == DBTIME-1)
		{
			if (n != *lastsolid)
			{
				v = *lastsolid = n;  // return debounced value
			}
		}
		if (*cnt < DBTIME)	(*cnt)++;
	}
	else
	{
		*cnt = 0;  // reset count
	}
	*last = n;
	return v;
}
Regards,

Tom Kerekes
Dynomotion, Inc.

AmitKumar171
Posts: 134
Joined: Tue Feb 20, 2018 7:35 am
Location: India

Re: FRO AND RRO USING HEXADECIMAL SWITCH

Post by AmitKumar171 » Tue Sep 15, 2020 12:49 pm

Hi tom,

THanks for your reply, will check that and update.
Thank You

AMIT KUMAR

Post Reply