+ Post New Thread
Results 1 to 3 of 3
  1. #1
    Member level 1
    Points: 508, Level: 4
    coolvasanth07's Avatar
    Join Date
    Sep 2012
    Posts
    37
    Helped
    0 / 0
    Points
    508
    Level
    4

    Convert port pin into uart(serial) pin

    Hi, i want to know how to convert portd pin into serial(uart) pin.....in my project i want to take tx & rx pins from portd pins...i am using pic16f877a + hitech c...plz help me,,,it's urgent

    >>thanks in advance

    •   Alt27th December 2012, 09:31

      advertising

        
       

  2. #2
    Advanced Member level 1
    Points: 3,016, Level: 12
    Jestin_cubetech's Avatar
    Join Date
    Jun 2012
    Posts
    499
    Helped
    74 / 74
    Points
    3,016
    Level
    12
    Blog Entries
    6

    Re: Convert port pin into uart(serial) pin

    hitech c software uart example code



    Code:
    /*
     *	Serial port driver (uses bit-banging)
     *	for 16Cxx series parts.
     *
     * 	IMPORTANT: Compile this file with FULL optimization
     *
     *	Copyright (C)1996 HI-TECH Software.
     *	Freely distributable.
     */
    #include	<htc.h>
    /*
     *	Tunable parameters
     */
    
    /*	Xtal frequency */
    #define	XTAL		4000000
    
    /*	Baud rate	*/
    #define	BRATE		9600
    
    /*	Transmit and Receive port bits */
    #define	TxData		RB6				/* Map TxData to pin */
    #define	RxData		RB7				/* Map RxData to pin */
    #define	INIT_PORT	TRISB = 1<<7	/* set up I/O direction */
    
    /*	Don't change anything else */
    #define SCALER			10000000
    #define ITIME			4*SCALER/XTAL	/* Instruction cycle time */
    #if BRATE > 1200
    	#define	DLY			3				/* cycles per null loop */
    	#define	TX_OHEAD	13				/* overhead cycles per loop */
    #else
    	#define	DLY			9				/* cycles per null loop */
    	#define TX_OHEAD	14
    #endif
    #define	RX_OHEAD		12				/* receiver overhead per loop */
    
    #define	DELAY(ohead)	(((SCALER/BRATE)-(ohead*ITIME))/(DLY*ITIME))
    
    void
    putch(char c)
    {
    	unsigned char	bitno;
    #if BRATE > 1200
    	unsigned char	dly;
    #else
    	unsigned int	dly;
    #endif
    
    	INIT_PORT;
    	TxData = 0;			/* start bit */
    	bitno = 12;
    	do {
    		dly = DELAY(TX_OHEAD);	/* wait one bit time */
    		do
    			/* waiting in delay loop */ ;
    		while(--dly);
    		if(c & 1)
    			TxData = 1;
    		if(!(c & 1))
    			TxData = 0;
    		c = (c >> 1) | 0x80;
    	} while(--bitno);
    NOP();
    }
    
    char
    getch(void)
    {
    	unsigned char	c, bitno;
    #if BRATE > 1200
    	unsigned char	dly;
    #else
    	unsigned int	dly;
    #endif
    
    	for(;;) {
    		while(RxData)
    			continue;		/* wait for start bit */
    		dly = DELAY(3)/2;
    		do
    							/* waiting in delay loop */ ;
    		while(--dly);
    		if(RxData)
    			continue;		/* twas just noise */
    		bitno = 8;
    		c = 0;
    		do {
    			dly = DELAY(RX_OHEAD);
    			do
    							/* waiting in delay loop */ ;
    			while(--dly);
    			c = (c >> 1) | (RxData << 7);
    		} while(--bitno);
    		return c;
    	}
    }
    
    char
    getche(void)
    {
    	char c;
    
    	putch(c = getch());
    	return c;
    }
    ----------------------------------------------
    One man's fault is other man's lesson...
    ----------------------------------------------



    •   Alt27th December 2012, 10:42

      advertising

        
       

  3. #3
    Member level 1
    Points: 508, Level: 4
    coolvasanth07's Avatar
    Join Date
    Sep 2012
    Posts
    37
    Helped
    0 / 0
    Points
    508
    Level
    4

    Re: Convert port pin into uart(serial) pin

    thanx sir thanx a lot,,,,i got it....



--[[ ]]--