Problems with Button() routine and timing

General discussion on mikroPascal.
Post Reply
Author
Message
PC Pete
Posts: 68
Joined: 10 Nov 2005 09:19
Location: Melbourne, Australia
Contact:

Problems with Button() routine and timing

#1 Post by PC Pete » 28 Dec 2008 09:04

I'm in the process of writing code for a fairly complex project, but I'm just doing basic functionality tests at this stage. But I'm having problems with the Button() utility routine, and the delay library routines.. At least, I think I am!

The MCU is a PIC16F877A, MP version 6.0.0.2. The PIC is clocked at 20MHz on a 5V supply. It's properly bypassed, stable, etc.

[EDIT] I was using the Button() utility routine, but now I've found that it doesn't matter, the routine just doesn't plain work.[/EDIT]

So in an endless loop that includes a simple bit output on to portD (this port is connected to 2 4-bit R/2R ladders for simple D2A control of a motormind controller in analog mode, and yes I know that PWM would be better, but that's not an option!). But regardless of whether the button code is in the loop or not, the loop doesn't output the correct values to the port!

My main routine is exactly this:

Code: Select all

 
procedure CheckButton;
begin
  if Button(PORTB,7,1,0)
  then ClearBit(PORTA,0)
  else SetBit(PORTA,0);
end;
 
begin
  TRISA := $1E;//A.0 is output bit, others are input
  PORTA := $01;
  TRISD := $00;// D is all output
  PORTD := $88;// halfway value for D2A ladder
  ClearBit(OPTION_REG,7);// want weak pullups
  TRISB := $F8;// B0..B2 are output, all others are input
  PORTB := $01; 
  TRISC := $FF;// don't really care
  TRISE := $03;       // line followers
  while True do
  begin
    SetBit(PORTB,0);
    ClearBit(PORTB,1);
    Delay_ms(500);
    SetBit(PORTB,1);
    ClearBit(PORTB,0);
    Delay_ms(500);
    for ii := 0 to 15 do
    begin
      PORTD := ii shl 4 + ii;
      Delay_ms(100);
    end;
  end;
end.
What happens when I run this code is that the PORTD values are switched between $88 and $99 (i.e. only MSB and LSB toggle on both nybbles) and $00. There is no intermediate port activity of any kind - I've 'scoped the pins and there's nothing, not even a glitch..

There is another problem as well. The delays on the board are nothing like what the programmed values are!

The first delay (500mS) lasts less than 90mS physically. The second delay (500mS) is around 900mS. I want to use a 100mS delay for the port D pins, but the delay appears to be about 6mS, unless I change it to 700mS, in which case it's about 50mS. The delays are quite variable, but they are consistent - the first 500mS delay is ALWAYS 80-90% shorter than the second 500mS delay, and so on.

I've set the project options so the clock speed is at 20MHz, but the delays are nothing close to those values. I know that there will always be a bit of give and take between what I want and what I get from a timing-dependent routine and it's not easy to be so accurate unless I count cycles, but 90% difference means I'm doing something very wrong, and I don't know what!

On the plus side, the button code worked great for this test! Woo Hoo! (I'll take a win wherever I can :))

If anyone can help me figure out what I'm doing wrong, I would very much appreciate it!
Data is not Information. Information is not Knowledge. Knowledge is not Wisdom.

Dany
Posts: 3854
Joined: 18 Jun 2008 11:43
Location: Nieuwpoort, Belgium
Contact:

#2 Post by Dany » 28 Dec 2008 10:00

Hi, I do not think it will solve your problem(s), but one never knows: the mP version you use (MP version 6.0.0.2) is rather "old". :oops:
About PortD: are some pins multiplexed with some other PIC functions? (I do not know the 877A). Make anyhow sure all pins of PortD are used for digital output.
Kind regards, Dany.
Forget your perfect offering. There is a crack in everything, that's how the light gets in... (L. Cohen)
Remember when we were young? We shone like the sun. (David Gilmour)

PC Pete
Posts: 68
Joined: 10 Nov 2005 09:19
Location: Melbourne, Australia
Contact:

#3 Post by PC Pete » 28 Dec 2008 12:39

Thanks, Dany, that's a great suggestion! I'll check out the default values for the port pins in the config registers first. It may be that other ports are similarly setup but I don't know them (yet)...
I did think that just setting the TRISx registers would do the trick. Bugger.
Why didn't I think of that?
Data is not Information. Information is not Knowledge. Knowledge is not Wisdom.

arcom
Posts: 271
Joined: 07 Dec 2007 17:00
Location: Rijeka/Croatia
Contact:

#4 Post by arcom » 28 Dec 2008 15:05

877A has analog inputs on PORTA and PORTE and they are on by default. To turn them off and put all I/O pins into digital mode add this:

Code: Select all

ADCON0 = 0 ' turn off ADC
ADCON1 = 7 ' all ports digital
CMCON = 7 ' disable comparators
Default POR value for comparator register is 7 but you you never know :)

PC Pete
Posts: 68
Joined: 10 Nov 2005 09:19
Location: Melbourne, Australia
Contact:

#5 Post by PC Pete » 28 Dec 2008 23:02

I actually want port a's analogue inputs, I just haven't got that far in the code development yet. But I forgot about the comparators...

I was going to test that suggestion (and some other ideas) out, but now I've found that MP won't compile the project any more. It's coming up with silly errors about unterminated strings on the program declaration! I have no idea why the hell it's doing that all of a sudden. It compiled last night, and I left the project open, but this morning when I tried to recompile the same code (in the same IDE window!), it freaks out and breaks. I think it's time to upgrade MP!

On the '877A, PORTD's only other function is an 8-bit parallel readable/writable register (controlled externally by PORTE). So there's nothing there that would seem to cause problems (there's no possibility of UART/SPI/PWM/etc, they're all on PORTC).

I considered that with the age of the MP version I'm using, maybe the Button() or Delay_mS() routines are causing side-effects, but I've taken out all that code and turned all the ports to digital outputs (I can do that because at this early stage, I haven't wired up any of the IO for the bumper switches, GPS module, sonar, compass, etc, which is a bonus!).

But even running flat out, I get no activity of any kind on the D1,D2,D5, or D6 pins.

I'm starting to wonder if this PIC is dead or dying... I'm using the same device I was having trouble getting the GLCD display to work. Maybe it's not my code or design?

I was going to change the MCU to a 18F4620 once I got proof of concept, but I think I'll go out and buy a couple of '4620s and use them instead.

I'll post back later. But meanwhile if anyone else has any ideas, please feel free to make suggestions!

PC Pete
Posts: 68
Joined: 10 Nov 2005 09:19
Location: Melbourne, Australia
Contact:

#6 Post by PC Pete » 29 Dec 2008 04:03

The PIC was dead. Long live the new PIC.

No matter how I compiled and tested, nothing was going out on D2/3 or D5/6. I tried everything I could think of, including going to an 8MHz crystal and then when that didn't work, a 400kHz RC oscillator. Nothing happened.

I plugged in my spare '877A, programmed it, and WHAM! It runs like a dream. For anyone interested, here's the CRO grab of the R/2R DtoA output (from incrementing one nybble of PORTD) without delays:
Image

I've never come across a partially dead PIC before. I've killed my fair share of Z80s, 6809s, ARMs, and even a few PICs, with mis-routing ground and power, accidental HV spikes on analogue inputs, etc, but they've always popped their clogs completely (what we used to call SP : smoke possible ;)), but not a partly defunct MCU.

I used to do insurance investigations and hardware escalations for a computer company, so I know how simple it is for a simple ESD flashover or voltage surge to kill most of the chip die, especially at the etch geometries Microchip uses!

Still, I I learn something new every day!

Please disregard my moaning and groaning about unterminated lines. When I actually looked at the actual error message, instead of the line the compiler highlighted, I saw the unterminated line that was causing the problem (that's what I get for pasting C code into MP without cleaning it up first).

I feel more stupid than I can say.

Thanks again to everyone for your suggestions!

John Ferrell
Posts: 111
Joined: 19 Sep 2006 21:21
Location: Julian NC, US
Contact:

#7 Post by John Ferrell » 29 Dec 2008 17:25

I have had weird things happen that have been corrected by uninstalling/reinstall of the compiler. I consider this a "Windows Feature". Fortunately, it is quick and easy!

I have not verified it, but somewhere I read that 30 ma is the sum of currents that one should expect from most PICs.

I have personally destroyed an 18F452 with a demo version of the Microchip C compiler by trying to write to a declared string constant. I know that should not have done that but that is the way it hapened.

I fall back to Blink Leds as a quick hardware check very often.

BTW, once you turn on the Analog function for Port A there seems to be no way to use a Port A bit as output.
John Ferrell W8CCW

PC Pete
Posts: 68
Joined: 10 Nov 2005 09:19
Location: Melbourne, Australia
Contact:

#8 Post by PC Pete » 29 Dec 2008 23:16

I haven't found that problem with port a's outputs, John (at least not on an 877 or 877A). After checking that I could actually do things with port a output bit 0, I configured it for 2 digital outputs and 6 inputs (PCFG:1001), and I can do conversions and change the output bit fine. Of course, I haven't actually tried changing the output bit(s) while a conversion is in progress (which is theoretically possible, given the real-time nature of this particular design). The electrical and ADC specs don't mention that possibility, not even in any application notes I've got.

The DC specs for the 877x are roughly speaking a total VDD current of 300mA, regardless of how many output bits are sourcing or sinking current. There is also a "block" limit of 200mA for combined port A/B/E, and the same limit on ports C/D. I assume the ratings would be similar across the range. Of course, that doesn't take into consideration timers, PWM, ADC, and core current consumption, so I guess it will be quite a bit less than the "theoretical" maximum.

Thankfully, I'm not sinking or sourcing anywhere near those currents, so I don't have to count milliamps! (My math is lousy :oops:).

John Ferrell
Posts: 111
Joined: 19 Sep 2006 21:21
Location: Julian NC, US
Contact:

#9 Post by John Ferrell » 30 Dec 2008 00:42

The mistake on the current is all mine...I failed to check it out before I said it. I must have simply remembering wrong and plead old age.

As to mixing analog and digital on PortA, I tried pretty hard on that score. I wanted to stay on one Port so that I only needed one 10-pin port connector to attach my project to the EP5 board. I will look into it again!
I was using the 18F4520 chip as I recall.
John Ferrell W8CCW

Post Reply

Return to “mikroPascal General”