PDA

View Full Version : serial in parallel out using PIC



daxki
- 4th April 2007, 00:03
Hi there!
This program I intend to convert serial to parallel. Say, serial input is 101, I want it convert to parallel by lighting 3 LEDs. I use 16F628 with RB0 the clock input (60hz square wave) and RB1 as data input which is a 1ms pulse right after the zero crossing of clock. When I simulate this, 4 LEDs are just blinking at the same time very fast instead of blinking 101 as I would expect. What could be wrong? Thanks...

INCLUDE "modedefs.bas"

@ DEVICE MCLR_OFF, INTRC_OSC, WDT_OFF, LVP_OFF, PWRT_ON, PROTECT_OFF


CMCON = 7 'Comparators off
VRCON = 0 ' Vref off
TRISB.0 = 1
TRISB.1=1
TRISA =%00000
DIPIN var byte
wvar var byte
CLPIN VAR bit
here:
PORTA=0

SHIFTIN DIPIN,CLPIN,MSBPRE,[wvar\6]

PORTA=wvar
pauseus 500
PORTA=0
goto here
end

skimask
- 4th April 2007, 00:47
INCLUDE "modedefs.bas"

@ DEVICE MCLR_OFF, INTRC_OSC, WDT_OFF, LVP_OFF, PWRT_ON, PROTECT_OFF

CMCON = 7 : VRCON = 0 : TRISB.0 = 1 : TRISB.1=1 : TRISA = 0 : DIPIN var byte : wvar var byte : CLPIN VAR bit

here:






PORTA=0








SHIFTIN DIPIN,CLPIN,MSBPRE,[wvar\6] : PORTA=wvar : pauseus 500 : PORTA=0 : goto here
end

You keep shutting off everything on PortA every time thru the loop...

Josuetas
- 4th April 2007, 02:49
500 us shouldn´t let you see much of it, do you remember how we humans can hardly see 1/60 seconds events (light bulb), well 500 us is much less than that, maybe 500 ms is easier to watch.

Try Pause 500 instead uf pauseus 500.

Of course you could also avoid turning the leds of as the previous post said and then the leds would only change when data is recieved.

daxki
- 5th April 2007, 00:35
Hi there!
I edited portion of the program and incorporate both of your comments as shown:

here:
'PORTA=0

SHIFTIN DIPIN,CLPIN,MSBPRE,[wvar\6]

PORTA=wvar
'pauseUS 500
'PORTA=0
goto here

What happened is all LEDS on RA0 to RA3 are all lighted continuously, no change whatsoever.
I expect that LEDs will be lighted as data comes in. For example, if data in is 1011, then I expect RA0 and RA1 to be high, RA2 to be low and RA3 to be high. And this should happen at the same time AFTER the last bit of serial input is received.

What could be wrong?

skimask
- 5th April 2007, 01:06
How can you be sure that the data you are receiving into wvar is really what you want in the first place? Maybe, after the shiftin, wvar is actually all 1's.
Don't automatically assume everything is working according to plan...'cause usually it doesn't...

Also, you said in post #1, that RB0 is the 60hz clock input. Shiftin generates it's own clock pulse for output to another device. So, Shiftin might not be what you're looking for.

Try this:

INCLUDE "modedefs.bas"

@ DEVICE MCLR_OFF, INTRC_OSC, WDT_OFF, LVP_OFF, PWRT_ON, PROTECT_OFF

CMCON = 7 : VRCON = 0 : TRISB.0 = 1 : TRISB.1 = 1 : TRISA = 0 : porta = 0
DIPIN var byte : wvar var byte : CLPIN VAR bit
x var byte

'blink leds 5 times for test
for x = 1 to 5
porta.0 = 1 : porta.1 = 1 : porta.2 = 1 : pause 500
porta.0 = 0 : porta.1 = 0 : porta.2 = 0 : pause 500
next x
pause 5000 'pause 5 seconds before starting main program

here:

SHIFTIN DIPIN,CLPIN,MSBPRE,[wvar\6]

'blink a.0 with hundreds digit of wvar
for x = 1 to ( wvar dig 2 ) 'hundreds digit of wvar
porta.0 = 1 : pause 500 : porta.0 = 0 : pause 500
next x

'blink a.1 with tens digit of wvar
for x = 1 to ( wvar dig 1 ) 'tens digit of wvar
porta.1 = 1 : pause 500 : porta.1 = 0 : pause 500
next x

'blink a.2 with ones digit of wvar
for x = 1 to ( wvar dig 0 ) 'ones digit of wvar
porta.2 = 1 : pause 500 : porta.2 = 0 : pause 500
next x

goto here
end