Need some help for PIC16F684 and PC Parallel port communication.

79 views
Skip to first unread message

Tuks

unread,
Nov 1, 2011, 7:03:01 PM11/1/11
to pickit-devel
We have written following code for communication between PIC and
Parallel port on the PC.
I want parallel port to send a number to PIC and PIC should increment
it by one and send it back.
STROBE signal from parallel port is used for synchronization.
RC0, RC1, RC2 and RC3 are used as data pins and RA1 is used for
STROBE.

Issue:
When parallel port reads the data from PIC, it always reads 0x0B or
0x0F or some garbage value.
Can some one please have a look at the code below and help us to
understand where are doing wrong and what it is?

================
PIC 16F684 side C Code:

#define _LEGACY_HEADERS

#include <htc.h>
#include <pic.h>

__CONFIG(WDTDIS & PWRTEN & MCLRDIS & UNPROTECT & UNPROTECT & BORDIS &
IESODIS & FCMDIS & INTIO );

#define HIGH 1
#define LOW 0

int main ()
{
CMCON0 = 7;
CMCON1 = 0;
ANSEL = 0;

int value = 0;
TRISA = 0b00000010; /* Port A1 = Strobe set for input */

while (1) {
TRISC = 0b00001111; /* Port C pins are set for input */

while (!RA1);

value = (PORTC & 0x0F);
value = value + 1;

TRISC = 0b000000;
PORTC = (value & 0x0F);

while (RA1);
}

}

=============

Parallel Port Side C Code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/io.h>

#define STROBEHIGH_DIRECTIONINPUT 0x21
#define STROBEHIGH_DIRECTIONOUTPUT 0x01
#define STROBELOW_DIRECTIONINPUT 0x20
#define STROBELOW_DIRECTIONOUTPUT 0x00

#define BASEPORT 0x378

unsigned long DATAPORT = BASEPORT + 0;
unsigned long STATUSPORT = BASEPORT + 1;
unsigned long CONTROLPORT = BASEPORT + 2;

int main ()
{
unsigned int value;
char valueToSend;

if (ioperm (BASEPORT, 3, 1) < 0) {
perror ("ioperm");
exit (EXIT_FAILURE);
}

else {
printf ("\nGet Port.\n");
while (1) {

outb (STROBEHIGH_DIRECTIONOUTPUT, CONTROLPORT);
outb (0x00, DATAPORT);

printf ("\nEnter the value: ");
scanf ("%d", &value);

outb ((value & 0x000F), DATAPORT);
outb (STROBELOW_DIRECTIONOUTPUT, CONTROLPORT);

sleep(2);

outb (STROBELOW_DIRECTIONINPUT, CONTROLPORT);

printf ("\nThe value read from PIC: 0x%X\n", (inb (DATAPORT) &
0x0F));
outb (STROBEHIGH_DIRECTIONOUTPUT, CONTROLPORT);
} /* (1) elihw */

} /* esle */
} /* niam*/
============

Xiaofan Chen

unread,
Nov 3, 2011, 9:22:30 PM11/3/11
to pickit...@googlegroups.com
On Wed, Nov 2, 2011 at 7:03 AM, Tuks <khandeka...@gmail.com> wrote:
> We have written following code for communication between PIC and
> Parallel port on the PC.

Hmm, this list is not meant for technical support for Microchip
MCUs, you may want to try Microchip forum.
http://www.microchip.com/forums/Default.aspx?

BTW, this list seems to be more or less dead. PICkit 2 now works
fine under Windows, Linux and Mac OS X with pk2cmd, or with the
latest MPLAB X. So probably there are no longer much interests
in this list, unless Microchip opens PICKit 3 codes. In that case,
probably there will be some more interests again.


--
Xiaofan

Jeff Post

unread,
Nov 3, 2011, 11:31:26 PM11/3/11
to pickit...@googlegroups.com
On Thursday 03 November 2011 18:22, Xiaofan Chen wrote:
>
> BTW, this list seems to be more or less dead. PICkit 2 now works
> fine under Windows, Linux and Mac OS X with pk2cmd, or with the
> latest MPLAB X. So probably there are no longer much interests
> in this list, unless Microchip opens PICKit 3 codes. In that case,
> probably there will be some more interests again.
>
Probably? I'd say assuredly. How ya doin' Xiaofan? I've been busier than a
chicken in a fox den, but I expect to have a bit more free time at the end of
the year.

Jeff

Reply all
Reply to author
Forward
0 new messages