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.
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!