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

18F26K22 PORT A

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



Joined: 27 Apr 2008
Posts: 168

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

18F26K22 PORT A
PostPosted: Thu Oct 27, 2022 8:24 am     Reply with quote

Hi!

I am having trouble to read PORT_A with an optical sensor. It is a simple pulse counter, but I need to detect only when the PIN_A1 goes high.

The strange thing is that, sometimes, when the transition is from high to low, I also have a pulse detected.

As this PIC has a lot of peripherals attached to the ports, I really don’t know what is going on here.
This is my configuration:
Code:
 
SETUP_ADC_PORTS(NO_ANALOGS);
SETUP_COMPARATOR(NC_NC_NC_NC);
SETUP_COUNTERS(RTCC_INTERNAL,RTCC_DIV_16);
SETUP_TIMER_1(T1_INTERNAL|T1_DIV_BY_8);      
SETUP_TIMER_2(T2_DISABLED,0xFF,16);
SETUP_TIMER_3(T3_DISABLED|T3_DIV_BY_8);
SETUP_TIMER_4(T4_DISABLED,0xFF,16);
SETUP_TIMER_5(T5_DISABLED|T5_DIV_BY_8);
SETUP_TIMER_6(T6_DISABLED,0xFF,16);
SET_TRIS_A(0b11011111); 
SET_TRIS_B(0b11000111);
SET_TRIS_C(0b11010111);   
SET_TRIS_E(0b11111111);
LCD_INIT();
RTC_TEST_OSC();
ENABLE_INTERRUPTS(INT_TIMER1);
ENABLE_INTERRUPTS(INT_EXT);   
ENABLE_INTERRUPTS(GLOBAL);


And this is my loop:

Code:
 
while(true) //NORMAL WORKING STATE
 {
  //DIR -> PIN_A0
  //CLK -> PIN_A1      
  if(INPUT(PIN_A1)&&LOCK_ENCODER)
     {
      LOCK_ENCODER=0;
      COUNT++;
      }
  if(!INPUT(PIN_A1)&&!LOCK_ENCODER)LOCK_ENCODER=1;
 }


Sometimes what seems to be easy becomes very hard.

Any tip?

Regards;
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Oct 27, 2022 10:30 am     Reply with quote

What is the Vdd voltage of the PIC, and what are the voltage levels on Pin A1 ?

Do either of these function access Port A ?
LCD_INIT();
RTC_TEST_OSC();
rudy



Joined: 27 Apr 2008
Posts: 168

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

PostPosted: Thu Oct 27, 2022 10:52 am     Reply with quote

Thank you PCM programmer!

All 5 volts! And nobody else use this port.

I start to think that this issue is related with the cable length, around 5 meters between the sensor and the PIC.

The cable capacitance may be doing this! (SUSPICIOUS)



Regards;
temtronic



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

View user's profile Send private message

PostPosted: Thu Oct 27, 2022 11:08 am     Reply with quote

'optical sensor' ?????
You should post the make/model/ link to it.....

distance ( 5 meters +- 15 feet ) shouldn't be a problem
but....
may need shielded cable, good +5 to sensor, proper pullup on data line

hmm..speed ? 'optical sensor' may be 'slow' compared to your loop speed

also
what 'triggers' the sensor ? is the sensor have good square edges during transitions ?
rudy



Joined: 27 Apr 2008
Posts: 168

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

PostPosted: Thu Oct 27, 2022 11:22 am     Reply with quote

It is a regular type, model TCST110

The cable is not shielded, and the speed is not fast.

I will hook my oscilloscope and see better the signal. It is very weird, because when I poll the sensor with a certain low speed, this issue always occurs, but when I speed up a little, the problem disappears.

I am suspicious the cable, because when I was developing the sensor mechanics, the cable between the PIC (development board) and the sensor was about 30 cm, and never this issue happens before.

Also, I will try to replace the cable (it has 4 legs (+5, GND, DIR and CLK)) for a LAN twisted pair, and check if this effect vanishes.

Let’s see!

Regards;
rudy



Joined: 27 Apr 2008
Posts: 168

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

PostPosted: Thu Oct 27, 2022 11:57 am     Reply with quote

Confirmed!

IT IS NOT THE CABLE!

Rsrsrsrsrs (smiling)

Lost in paradise again!

Now move to oscilloscope. Don't know what to do anymore!

Regards!
temtronic



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

View user's profile Send private message

PostPosted: Thu Oct 27, 2022 2:03 pm     Reply with quote

ok, it's what I call a 'flag' sensor... block the IR beam with a flat black bar...
So...need to know what resistors did you use for the LED and the transistor for the optocoupler ?
rudy



Joined: 27 Apr 2008
Posts: 168

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

PostPosted: Thu Oct 27, 2022 3:32 pm     Reply with quote

temtronic wrote:
ok, it's what I call a 'flag' sensor... block the IR beam with a flat black bar...
So...need to know what resistors did you use for the LED and the transistor for the optocoupler ?


Well, the LED and Transistor are 1k pulled up. I didn't have time today, but I will go deep tomorrow.

The difference between the development and the final PCB, that is assembled today, is that I added two others NPN BC-548 transistors in the PCB, just to isolate the sensor signal.

The issue for sure is being generated in this area. Sure about that! because when was in development breadboard, these extra transistors weren't there.

Also, at the collector of these transistors, I added two LEDs, just to see the sensor working.

Don't know if I made myself clear on that.

Regards;
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Thu Oct 27, 2022 9:51 pm     Reply with quote

I can't see any way that two NPN transistors can provide isolation.
Post the circuit used.
Suspicion is that something in this circuit is preventing the signal actually
getting to the signal levels required, or some capacitive effect (could
explain the 'speed' behaviour).
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 28, 2022 5:57 am     Reply with quote

Had a quick read of the sensor sheet, using 1K for the diode means about 3.5 ma, not the 20 ma they use. It could be the diode isn't putting out a 'lot' of IR energy.... I'd try 330r or 470r
then...
Using the 1K on the transistor side, is 10x bigger than what they use. Again it could be just 'out of spec, a bit'. My gut says 330r should be better

I prefer to put the resistor between the emitter to ground. This gives a positive logic configuration. If the flag IS there, you get a 'one'. TI thermal terminals did this for the 'carriage home' sensors (mid '70s era ). Kinda made sense to me.

This is a classic 'white breadboard' problem. Try a few combinations and see with scope or DVM which parts gives the desired result.
I don't know but IF that pin on the PIC can be configured as an 'ST' input, you'll get better response (cleaner '1-0-1' signals.
Also put a 10-100mfd cap on the +5 AT the sensor.
rudy



Joined: 27 Apr 2008
Posts: 168

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

PostPosted: Fri Oct 28, 2022 7:12 am     Reply with quote

Good day!

Did everything at my hand, found no explanation about this issue. What I did to solve this? Follow one of your valuable tips.

I have some pins not used, C0 and C1, both ST I/O. Just changed to use them. PROBLEM VANISHES.

Thank you all for helping me again..

Best regards;
temtronic



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

View user's profile Send private message

PostPosted: Fri Oct 28, 2022 8:18 am     Reply with quote

NICE !!
Schmitt (sp?) have well defined highs and lows plus 'hysterisis' so the 'ones' and 'zeros' are accurate,repeatable and give 'clean' edges.
You're lucky you have extra pins.....it's easy for a project to grow and either you run out of pins or memory...
Ttelmah



Joined: 11 Mar 2010
Posts: 19549

View user's profile Send private message

PostPosted: Mon Oct 31, 2022 3:19 am     Reply with quote

It does suggest, especially since he was seeing an 'edge', that the problem
is actually some pickup in the long cable. Switching to having some voltage
margin by using the Schmitt inputs is 'better', but long term it might be
worth considering if there is anything that could be added to help?.
Simply having a small capacitor across the input, could help get rid of
HF noise. Also where are the resistors placed?. Having the pull-up/down
resistors at the opposite end of the cable to the sensor, implies that the
cable is carrying current, rather than otherwise being a floating cable.
Does help.
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