Problème UART PIC24FJ
Répondre à la discussion
Affichage des résultats 1 à 2 sur 2

Problème UART PIC24FJ



  1. #1
    kronanberg

    Problème UART PIC24FJ


    ------

    Bonjour,

    Voila ça fait plusieurs jours que je teste ma liaison UART entre mon PIC24FJ256GB110 et mon PC et je n'arrive toujours pas à envoyer des données du PC vers le PIC.
    Pourtant j'arrive bien à transmettre des caractères du PIC vers le PC. J'ai donc repris un bout de code que j'ai trouvé sur ce forum pour tester la réception de l'UART du PIC mais j'ai toujours rien.

    Le programme permet juste d'attendre qu'un caractère soit reçu (gérer par interruption) et d'envoyer le caractère 'B' au Pc si il a bien reçu.

    Voici le code :
    Code:
    #include <p24FJ256GB110.h>
    
    
    //****************************************************************************************
    //	STEP 1:
    //	Set up the Configuration Bits in the file using the _CONFIGx macros
    //  Macros and bit configuration labels are defined in the p24FJ256GB110.h file
    //  Details about bit configuration registers in p24FJ256GB110 datasheet, section 23
    //	(JTAG=Disabled, WATCHDOG=Disabled, EMUC/EMUD share PGC1/PGD1, PriOSC=HS, IOLOCK=1 Way)
    //***************************************************************************************/
    
    _CONFIG1(JTAGEN_OFF & ICS_PGx1 & FWDTEN_OFF)	// ###  configure _CONFIG1
    _CONFIG2(FNOSC_PRI & POSCMOD_HS & IOL1WAY_ON)	// ###  configure _CONFIG2
    
    
    /*** Function Prototypes **************************************************************/
    void ioMap(void);			// Assign RPn pin(s) to input/output functions for this lab
    void unlockIO(void);		// Unlock sequence for RPn configuration
    void lockIO(void);			// Lock sequence for RPn configuration
    void UARTInit(void);		// Initiallize UART for 9600/N/8/1/HW Flow Control
    void IOInit(void);			// Initialize switch and LED I/O
    
    
    /*** main() Function********************************************************************/
    int main(void)
    {
    	ioMap();				// Configure the remappable I/O pins
    	UARTInit();      		// Initialize UART2
    	IOInit();				// Initialize Switch and LED I/O
    	
    	IEC1bits.U2RXIE = 1;	// Enable Uart2 RX Interrupts
    	IFS1bits.U2RXIF = 0;
    	
    	while(1)
    	{
    		
    	}
    }
    
    /*********************************************************************************** 
     * Function: ioMap()
     *
     * Preconditions: None.
     *
     * Overview: This executes the necessary process to map the RPn pins to UART 2 for the
     *				PIC24FJ256GB110
     *
     * Input: None.
     *
     * Output: None.
     *
     *****************************************************************************/
    void ioMap()
    {
    	//*************************************************
    	//	STEP 2:     Unlock the Reprogrammable Pin Mechanism
    	//*************************************************/
    	unlockIO();                         	// ###
    
    	//*************************************************
    	//	STEP 3:     Map RP6 pin to input function U2RX
    	//*************************************************/
    	RPINR19bits.U2RXR = 6;         	    	// ###
    	
    	//*************************************************
    	//	STEP 5:     Map Output Function U2TX to RP7
    	//*************************************************/
    	RPOR3bits.RP7R = 5;                 	// ###
    
    	//*************************************************
    	//	STEP 7:     Lock the Reprogrammable Pin Mechanism
    	//*************************************************/
    		lockIO();                       	// ###
    }
     
    /*********************************************************************************** 
     * Function: lockIO
     *
     * Preconditions: None.
     *
     * Overview: This executes the necessary process to set the IOLOCK bit to lock
     * I/O mapping from being modified.
     *
     * Input: None.
     *
     * Output: None.
     *
     *****************************************************************************/
    void lockIO(){
    
    asm volatile ("mov #OSCCON,w1 \n"
    				"mov #0x46, w2 \n"
    				"mov #0x57, w3 \n"
    				"mov.b w2,[w1] \n"
    				"mov.b w3,[w1] \n"
    				"bset OSCCON, #6":::"w1","w2","w3");
    }
    
    /*****************************************************************************
     * Function: unlockIO
     *
     * Preconditions: None.
     *
     * Overview: This executes the necessary process to clear the IOLOCK bit to 
     * allow I/O mapping to be modified.
     *
     * Input: None.
     *
     * Output: None.
     *
     *****************************************************************************/
    void unlockIO()
    {
    asm volatile ("mov #OSCCON,w1 \n"
    				"mov #0x46, w2 \n"
    				"mov #0x57, w3 \n"
    				"mov.b w2,[w1] \n"
    				"mov.b w3,[w1] \n"
    				"bclr OSCCON, #6":::"w1","w2","w3");
    }
    
    /*********************************************************************************
    *
    *	UARTinit() - Initialize UART2 for 9600/8/N/1/HW Flow control
    *
    **********************************************************************************/
    void UARTInit()
    {
    	//TRISBbits.TRISB6 = 1;	// RB6 connected to UART RX = input function
    	//TRISBbits.TRISB7 = 0;	// RB7 connected to UART TX = output function
    
    	TRISB = 0x0040;
    
    	//********************************************************************************
    	//  Load the count to get 9600 baudrate
    	//	BRG = Fcy/(16*BaudRate)-1  where Fcy = Fosc/2 = 4MHz
    	//********************************************************************************/
    	U2BRG = 0x19; 
    
    	//********************************************************************************
    	//  Flow Control Mode, No Loop-Back
    	// 	No Auto-Baud, No Rx-Inversion
    	//	Low BaudRate, 1 Stop-bit
    	//	8-bit, no parity 
    	//********************************************************************************/
    	U2MODE = 0x8000;
    
    	//********************************************************************************
    	//  Tx Interrupt to interrupt when at least one location is free in Tx buffer
    	//	No Tx Inversion, Disable Sync Break
    	//	Enable Transmit, Disable Address Detect
    	//	Interrupt on every receive. 
    	//********************************************************************************/
    	U2STA = 0x0400;
    
    	IFS1bits.U2RXIF = 0;		// Clear U2RX_Int flag
    }
    
    /*********************************************************************************
    *
    *	IOInit() - Initialize Switch and LED I/O for the project
    *
    **********************************************************************************/
    void IOInit()
    {
    	LATDbits.LATD4 = 0;	
    	TRISDbits.TRISD4 = 0;	// RD4 as output for LED 
    }
    
    /*********************************************************************************
    *
    *	_U2RXInterrupt - Echo character back to Hyperterminal or another terminal SW
    *
    **********************************************************************************/
    // shadow attribute is used here to reduce interrupt save and restore overhead
    volatile char C;
    void __attribute__((interrupt, shadow, no_auto_psv)) _U2RXInterrupt()
    {
    	IFS1bits.U2RXIF = 0;
    	
    
    	while(!U2STAbits.TRMT);
    
    	C = U2RXREG;
    		U2TXREG = 0x42;			//envoie du caractère B si reception
    	if ( PORTDbits.RD4 == 1)	PORTDbits.RD4 = 0;
    	else PORTDbits.RD4 = 1;
    	
    }
    J'ai aussi testé avec l'UART1 et c la même chose. Le PIC n'arrive pas à faire la réception. J'ai beau changé le bit à tester pour la réception en utilisant par exemple le bit URXDA du registre U2STA (sans interruption du coup) mais c'est sans résultat !!!

    Si quelqu'un à une autre proposition ??
    Ou alors est ce que c'est possible que la reception de l'UART de mon PIC soit défectueux ??
    Comme j'ai soudé ce composant CMS à la main, il a peut être trop chauffer à un moment...

    -----

  2. #2
    kronanberg

    Smile Re : Problème UART PIC24FJ

    Bon ben finalement je viens de résoudre le pb !!
    J'ai changé les broches et ça marche !! ma broche RB6 est soit mal comfigurer ou alors elle ne peut pas fonctionner en Reception !!!

    Code:
    #include <p24FJ256GB110.h>
    #include "uart.h"
    #include "iomapping.h"
    
    
    //*************************************************
    //	STEP 1:     Configure l'oscillateur interne 8 MHz
    //*************************************************/
    _CONFIG2(FNOSC_FRC & IOL1WAY_ON)
    
    unsigned char reception;
    
    main()
    {
    	//*************************************************
    	//	STEP 2:     Unlock the Reprogrammable Pin Mechanism
    	//*************************************************/
    	unlockIO();		
    	
    	//*************************************************
    	//	STEP 3:     Map RP25 pin to input function U1RX
    	//				Map RP20 pin to output function U1TX
    	//*************************************************/	
    	ioMap();
    
    	//*************************************************
    	//	STEP 4:     Lock the Reprogrammable Pin Mechanism
    	//*************************************************/
    	lockIO();
    
    	//*************************************************
    	//	STEP 5:    Initialise l'UART1
    	//*************************************************/
        UART1Init();
    
    	IEC0bits.U1RXIE = 1;	// Enable Uart1 RX Interrupts
    
    	while(1)
    	{
    	}
    }
    
    /*********************************************************************************
    *
    *	_U1RXInterrupt - renvoie le caractere envoyer par l'hyperterminal
    *
    **********************************************************************************/
    
    void __attribute__((interrupt, shadow, no_auto_psv)) _U1RXInterrupt()
    {
    	IFS0bits.U1RXIF = 0;
    
    	while(!U1STAbits.TRMT);
    
    	reception = U1RXREG;
    	UART1PutByte(reception);
    }
    void UART1Init()
    {
    	TRISDbits.TRISD4 = 1;	// RD4 connected to UART RX = input function
    	TRISDbits.TRISD5 = 0;	// RD5 connected to UART TX = output function
    
    	U1BRG = 25;				// configuration pour une horloge de 8Mhz/2 et une transmission de 9600 bauds 
    	U1MODE = 0x8000; 		// UARTEN = 1	--	 BRGH = 0
    	U1STA =  0x0400; 		// UTXEN = 1
    
    	IFS0bits.U1RXIF=0;		// reset RX flag
    }
    
    
    void UART1PutByte(unsigned char byte)
    {
    	while(U1STAbits.UTXBF == 1);   /* Attend que le buffer soit vide */
    	U1TXREG = byte;
    	return;
    }
    
    void ioMap()
    {
        //*************************************************
    	// Configure Input Functions **********************
    	//*************************************************
    	RPINR18bits.U1RXR = 25;     	// Assign UART1RX To Pin RP25 
        
    	
    	//*************************************************
    	// Configure Output Functions *********************
    	//*************************************************
    	RPOR10bits.RP20R = 3;      		// Assign UART1TX To Pin RP20
        
    }

Discussions similaires

  1. Souci de programmation PIC24FJ
    Par kronanberg dans le forum Électronique
    Réponses: 0
    Dernier message: 04/05/2009, 15h15
  2. Problème UART AVR
    Par inviteee2a87e5 dans le forum Électronique
    Réponses: 2
    Dernier message: 07/01/2009, 10h27
  3. Problème interuption UART PIC16F877
    Par invite55fe29e6 dans le forum Électronique
    Réponses: 4
    Dernier message: 28/09/2008, 19h49
  4. probleme uart pic24
    Par djwolf dans le forum Électronique
    Réponses: 3
    Dernier message: 03/06/2008, 00h07
  5. Uart
    Par invitedba2660f dans le forum Électronique
    Réponses: 1
    Dernier message: 17/04/2007, 01h03
Découvrez nos comparatifs produits sur l'informatique et les technologies.