Tuks
unread,Nov 1, 2011, 7:03:01 PM11/1/11Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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*/
============