PDA

View Full Version : Bit Banging input to output on PIC16F876A



Bronurstomp
- 26th May 2008, 16:54
Hi folks.
Some init stuff: I'm using a PIC16F876A.

DEFINE OSC 20 ' use 20mhz xtal.
DEFINE HPWM1_TMR 1 'Use timer 1 for both channels
DEFINE HPWM2_TMR 1
DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit

I want to output a bit on PORTB.2. Ok np.
Now I want to logically AND it to the input pulse on PORTA.2 and send it to another pin, say PORTB.6. The input pin PORTA.2 is electrically connected to HPWM pin (output) of this same chip. I want to basically multiplex the PWM output to other output pins via the logical AND. I've generated the HPWM before calling this subroutine.

If (stuff happens)
HPWM 1, percntpwm, 3000 REM ----- 3KHZ output for example. How fast can this be?

Gosub Bitbang

This is the subroutine I'm thinking of using. It seems pretty straight forward, but can it be done like this?

Bitbang:
PORTB.1 = 0 ; These outputs
PORTB.2 = 1 ; are also needed
PORTB.3 = 1
PORTB.4 = 0

PORTB.6 = PORTA.2 AND PORTB.2 ; Perform AND then route the result to the output

RETURN

I'm away from my hardware for a few months on business so I can't test this idea.
The real question is: What is the highest HPWM frequency I can accurately reproduce on the PORTB.6 output assuming a 20MHZ Crystal?
Thanks in advance!

Melanie
- 26th May 2008, 17:25
So why not go a stage further...

Chose a PIC that not only has Hardware PWM, but also a Hardware Comparator (eg 16F628) with dedicated output. Cross Connect the HPWM to one Comparator Input. The 2nd Comparator Input is cross connected to the PIC pin of your choice. This pin now ANDs (modulates) the Comparator (PWM Carrier) output. Not much need to bit-bang anything... and almost no software overhead...

Life is simple (or at least it can be)...

mister_e
- 26th May 2008, 19:00
The real question is: What is the highest HPWM frequency I can accurately reproduce on the PORTB.6 output assuming a 20MHZ Crystal?
Thanks in advance!

Well, i really depend of the accuracy you can/want to deal with. PICMultiCalc says you can produce up to 2.5 MHz @ 20MHz.. but <4 bit resolution... only 8 duty steps :eek:

To do so, you'll need to configure the CCP registers manually.

Bruce
- 26th May 2008, 20:35
Melanie,

I would love to see an example of how this works. It would be really nice for sending IR serial
data modulated at 38-40kHz.

Melanie
- 26th May 2008, 21:27
Give me a couple of days Bruce to find some spare time and throw an example together - I'll post it here... watch this space...

mister_e
- 26th May 2008, 21:34
7 days a week, 24 hours a day, you only work 120 hours a week... so you still have spare time huh? :D

I see what's you're cooking. I've used something like that in the past... before i lost everything on my hard drive :eek:

I remember that i also modified SEROUT routine... and it also remember me something that Darrel already did here... i'll search for it now.

Bruce
- 26th May 2008, 22:02
I've tried what Melanie mentioned, but could never get it to work properly when changing
Vref with another I/O-pin.

mister_e
- 27th May 2008, 19:22
Probably where...
... and almost no software overhead... came from.

2 diodes, 1 resistor build a cheap AND gate... without code overhead.

<hr>
EDIT: smells like the comparator input change, then within the ISR you perform the AND function.

Dave
- 28th May 2008, 12:50
Bronurstomp, Why even involve hardware as the ccp module inside the pic can do the work for you. Just set the period and pwm of the ccp module at 38khz. and set the ccpl value to 50 % or 0%. I have been doing this for the last 5 years in the development of the tire pressure lf transmissions at 125 khz...

Dave Purola,
N8NTA

Bronurstomp
- 29th May 2008, 02:38
I guess I wasn't very clear about what I want to know. I'm holding back some info because this circuit may be very valuable.
Presently, I have the 2 HPWM outputs going to a quad 2-input AND gate with the other 4 digital outputs used for "steering" the 2 PWMs to 2 places each. They are the forward and reverse speed control of two motor control H-Bridges. PWM ANDED.JPG
This circuit works fine. I want to eliminate the 4 AND gates, thus the chip and caps from my PCB. See Un ANDED PIC.JPG
The question is about the PICBASIC PRO syntax.
PORTB.6 = PORTA.2 AND PORTB.2
Will this syntax work assuming the PIC is producing 2 varying duty cycle PWMs at 3KHZ, hard wire connected to general purpose digital inputs?
PORTA.2 tied to HPWM1, and PORTA.3 tied to HPWM2 (Pin 12-4 and Pin 13-5)
I get no errors when compiling, but I have no way to test the hardware.

Thanks!

mister_e
- 29th May 2008, 04:41
This could work, but you'll need to do it all the time in a loop, hence why the comparator or maybe interrupt on change idea came from... and you will need to use Bitwise AND

PORTx=PORTx & PORTx

I haven't investigate the CCP idea, but sure there's something valuable in it. Have a look at Microchip CCP Tricks n Tips document.
http://ww1.microchip.com/downloads/en/DeviceDoc/41214a.pdf

Bronurstomp
- 29th May 2008, 17:51
Now that's what I'm screamin... Thanks Mister E. This is all running in an IF/THEN loop.
So to recap... My syntax should look like this:

PORTB.6 = PORTA.2 & PORTB.2 ?

And judging from what I read; At 20MHZ, this thing should keep up with a PWM frequency of 3000 with no problem? That makes sense considering the PWM is hardware driven.

Mister E = my hero.

Thanks for your help too Mel.
And everyone else for contributing.
I hope this thread helps someone else that might want to multiplex the two PWM outputs to general purpose outputs.

Bron...

Bronurstomp
- 26th June 2008, 19:50
Now that I'm pretty sure I have the software settled... Has anyone been able to simulate a PIC16F876A with MPLAB's Simulator? What's the trick? I think I have everything set up right, it doesn't complain, but when I run the compile option it opens the PBP editor and then nothing else happens... Anyone?

Bron

Ok I did a IDE Compile in PBP(Microcode Studio +) and the simulator started running after I exited manualy from PBP

For some reason it's stuck in this loop in PBPPIC14.LIB

CLRWDT? macro
ifndef NO_CLRWDT
clrwdt
endif
endm
hserinloop CLRWDT? ; Keep Watchdog timer clear
btfss PIR1, RCIF ; Check for char
goto hserinloop ; Loop till char
NOLIST
HSERINX_USED = 1
endif
Return

Is it looking for the watchdog timer?
I'll keep playing with the simulator but this isn't my code, it looks like junk PBP added from the PBPPIC14.LIB?