spacer.png, 0 kB

Torna indietro   Roboitalia.com - Il primo portale in Italia sulla robotica amatoriale > Robotica di base > Informatica > P. in C, C++

Rispondi
 
Strumenti discussione Modalità  visualizzazioe
  #1  
Vecchio 01-11-2009, 09.18.48
amuro78 amuro78 non è collegato
Nuovo robottaro
 
Data registrazione: 27-02-2009
Residenza: Catania
Età : 45
Messaggi: 8
Potenza reputazione: 0
amuro78 E' per ora ancora un mistero
Predefinito Problema comunicazione RS232

Ciao Ragazzi da un paio di mesi ho iniziato a lavorare con dsPic33F, ora però sono bloccato in quanto vorrei far comunicare il dsPic con il pc tramite porta seriale ma non ne vuole sapere. Iniziamo col dire che il segnale sul pin di uscita del max232 c'è in quanto impostando un bit al secondo ho notato che sul led (non ho a disposizione un oscilloscopio) che ho appositamente messo per vedere se ci fosse trasmissione ho il char che volevo mandare al pc. Riporto di seguito il codice usato (ho usato MPLAB C30 come compilatore C).

#include "p33FJ12MC202.h"
#include "delay.h"
#define FCY 4000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
unsigned int i;

int settingSerial();


_FOSCSEL(FNOSC_FRC);
// Oscillatore Esterno <10 MHz
_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT);
//Oscillatore Interno FRC
//_FOSC(FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_NONE);
// Clock Switching is enabled and Fail Safe Clock Monitor is disabled
// OSC2 Pin Function: OSC2 is Clock Output
// Primary Oscillator Mode: XT Crystanl


_FWDT(FWDTEN_OFF); // Watchdog Timer Enabled/disabled by user software
// (LPRC can be disabled by clearing SWDTEN bit in RCON register
_FPOR(FPWRT_PWR1); // Turn off the power-up timers.
_FGS(GCP_OFF); // Disable Code Protection


int main(void){

// Configure Oscillator to operate the device at 40Mhz
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 8M*40/(2*2)=80Mhz for 8M input clock
PLLFBD=38; // M=40
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
OSCTUN=0; // Tune FRC oscillator, if FRC is used

// Disable Watch Dog Timer
RCONbits.SWDTEN=0;

// Clock switch to incorporate PLL
//Oscillatore Esterno
__builtin_write_OSCCONH(0x03);
//Oscillatore Interno
//__builtin_write_OSCCONH(0x01); // Initiate Clock Switch to Primary
// Oscillator with PLL (NOSC=0b011)
__builtin_write_OSCCONL(0x01); // Start clock switching
//Oscillatore Esterno
while (OSCCONbits.COSC != 0b011);
//Oscillatore Interno
// while (OSCCONbits.COSC != 0b001); // Wait for Clock switch to occur

while(OSCCONbits.LOCK != 1) {};



int v;
v=settingSerial();

if(v!=1){
return 0;
}
PORTB=0x01;


Delay(Delay_1S_Cnt);
while(1){
U1TXREG ='U';
Delay(Delay_1S_Cnt);
}
return 0;
}

int settingSerial(){
TRISB=0;

//************************************************** ***********
// Unlock Registers
//************************************************** ***********
__builtin_write_OSCCONL(OSCCON & ~(1<<6));
//************************************************** **********
// Configure Input Functions
// (See Table 30-1)
//************************************************** ***********
//*******************************
// Assign U1Rx To Pin RP10
//***************************
RPINR18bits.U1RXR = 10;
//***************************
// Assign U1CTS To Pin RP1
//***************************
//RPINR18bits.U1CTSR = 11;
//************************************************** ***********
// Configure Output Functions
// (See Table 30-2)
//************************************************** ***********
//***************************
// Assign U1Tx To Pin RP11
//***************************
RPOR5bits.RP11R = 3;
//***************************
// Assign U1RTS To Pin RP3
//***************************
//RPOR1bits.RP3R = 4;
//************************************************** ***********
// Lock Registers
//************************************************** ***********
__builtin_write_OSCCONL(OSCCON | (1<<6));

// configure U1MODE
U1MODEbits.UARTEN = 0; // Bit15 TX, RX DISABLED, ENABLE at end of func
//U1MODEbits.notimplemented; // Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IR translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U2MODEbits.notimplemented; // Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.URXINV = 0; // Bit4 IdleState = 1 (for dsPIC)
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit

// Load a value into Baud Rate Generator. Example is for 9600.
// See section 19.3.1 of datasheet.
// U2BRG = (Fcy/(16*BaudRate))-1
// U2BRG = (37M/(16*9600))-1
// U2BRG = 240
U1BRG = BRGVAL; // 40Mhz osc, 9600 Baud

// Load all values in for U1STA SFR
U1STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
U1STAbits.UTXINV = 0; //Bit14 N/A, IRDA config
U1STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
//U2STAbits.notimplemented = 0; //Bit12
U1STAbits.UTXBRK = 0; //Bit11 Disabled
U1STAbits.UTXEN = 0; //Bit10 TX pins controlled by periph
U1STAbits.UTXBF = 0; //Bit9 *Read Only Bit*
U1STAbits.TRMT = 0; //Bit8 *Read Only bit*
U1STAbits.URXISEL = 0; //Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; //Bit5 Address Detect Disabled
U1STAbits.RIDLE = 0; //Bit4 *Read Only Bit*
U1STAbits.PERR = 0; //Bit3 *Read Only Bit*
U1STAbits.FERR = 0; //Bit2 *Read Only Bit*
U1STAbits.OERR = 0; //Bit1 *Read Only Bit*
U1STAbits.URXDA = 0; //Bit0 *Read Only Bit*



U1MODEbits.UARTEN = 1; // And turn the peripheral on

U1STAbits.UTXEN = 1;
// I think I have the thing working now.

return 1;


}

Dalle prove fatte ho notato come il codice non superi la parte di settaggio del PLL. Infatti per poter trasmettere ho tolto while(OSCCONbits.LOCK != 1) {}; e while (OSCCONbits.COSC != 0b011); perchè se no il programma mi rimaneva bloccato la.
Spero ragazzi vi venga qualche idea perchè a me sono finite tutte. Magari nella mia newbaggine ho fatto qualche mardornale errore.

Grazie anticipatamente per qualsiasi idea
Rispondi citando
  #2  
Vecchio 01-11-2009, 12.18.49
L'avatar di guiott
guiott guiott non è collegato
Robottaro sostenitore
 
Data registrazione: 23-04-2004
Residenza: Roma
Età : 67
Messaggi: 1,418
Potenza reputazione: 350
guiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua famaguiott La sua reputazione è oltre la sua fama
Invia un messaggio via AIM a guiott Invia un messaggio via MSN a guiott Invia un messaggio via Yahoo a guiott Send a message via Skype™ to guiott
Predefinito

quello del blocco del programma era un problema noto, molto discusso sul forum Microchip, ma solo in fase di simulazione. Con l'MPLAB SIM non c'era verso di farlo andare. Io avevo infatti risolto così:

#define SIM 1

#ifndef SIM
while (OSCCONbits.COSC != 0b011); // Wait for Clock switch to occur
while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock
#endif

commentando la define lo usavo nel normale funzionamento. Con #define SIM 1 attivo facevo il debug in SIM.

Poi, aggiornando aggiornando MPLAB e C30, un giorno mi sono dimenticato di "discommentare" la define e tutto ha funzionato anche in SIM. Non ho più cercato sul forum ma probabilmente il problema è stato risolto.

Per quanto riguarda la comunicazione, se ti può consolare, io ci passai un'intera domenica per farla funzionare in mezzo a quella marea di registri non tutti spiegati molto bene, anche perché mi ero impuntato a capirli tutti la demo sul manuale non mi aiutò granché, anzi...

Non ho analizzato bit a bit il tuo codice, ho fatto una rapidissima comparazione rispetto alla mia configurazione e, per ora, ho trovato solo una differenza sul flow control. Ti allego la mia config, questa funziona.
Io uso il DMA per la parte di trasmissione, se ti va di leggere un po' trovi l'intero programma sul mio sito

Ho perso anche un po' di tempo sulla formuletta per calcolare BRG.
All'inizio avevo fatto un casting esplicito a long della define BAUDRATE (la trovi ancora così sulla versione nel sito) altrimenti mi troncava certi valori e non calcolava bene il valore.
Ora ho messo un quarzo a 10MHz invece che a 7.3728 (con il quale i conti per la seriale venivano precisissimi). Con quello a 10 ho usato addirittura due float in modo da poter fare (come vedi nell'esempio) l'approssimazione del calcolo all'intero più vicino, in questo modo la precisione del calcolo rimane sempre migliore dell'1%.

Spero di esserti stato un po' di aiuto.

void UsartSetting(void)
{
/*---------------------------------------------------------------------------*/
/* USART1 [6] */
/*---------------------------------------------------------------------------*/
float BaudRate;
float BRG;
/* Baud Rate = Fcy / ( 4 * (UxBRG + 1) ) with BRGH = 1
value for the U1BRG register rounded to closest integer (+0.5)
*/
BaudRate = 57600; // desired baud rate
BRG = (FCY/(4*(BaudRate)))-0.5;

/*................................................. ..............DMA UART TX */
// Associate DMA Channel 6 with UART Tx
DMA6REQ = 0x000c; // Select UART1 Transmitter
DMA6PAD = (volatile unsigned int) &U1TXREG;

DMA6CONbits.SIZE = 1; // Transfer bytes
DMA6CONbits.DIR = 1; // Transfer data from RAM to UART
DMA6CONbits.HALF = 0; // Interrupt when all of the data has been moved
DMA6CONbits.AMODE = 0; // Register Indirect with Post-Increment
DMA6CONbits.MODE = 1; // One-Shot mode

//DMA6CNT = 31; // # of DMA requests

// Associate one buffer with Channel 6 for one-shot operation
DMA6STA = __builtin_dmaoffset(UartTxBuff);

// Enable DMA Interrupts
IFS4bits.DMA6IF = 0; // Clear DMA Interrupt Flag
IEC4bits.DMA6IE = 1; // Enable DMA interrupt
/*................................................. ..............DMA UART TX */

/*................................................. ....................USART */
U1MODEbits.STSEL = 0; // 1-stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
U1MODEbits.ABAUD = 0; // Autobaud Disabled
U1MODEbits.RTSMD = 1; // No flow control
U1MODEbits.BRGH = 1; // Hi speed
U1BRG = BRG; // BAUD Rate Setting

// Configure UART for DMA transfers
U1STAbits.UTXISEL0 = 0; // Interrupt after one Tx character is transmitted
U1STAbits.UTXISEL1 = 0;
U1STAbits.URXISEL = 0; // Interrupt after one RX character is received

// Enable UART Rx and Tx
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART Tx
IEC4bits.U1EIE = 0;
_U1RXIF = 0; // Reset RX interrupt flag
_U1RXIE = 1; // Enable RX interrupt

UartRxStatus = 0;
ChkSum=0;

/*................................................. ....................USART */
}
__________________
Guido
------
www.guiott.com
Rispondi citando
  #3  
Vecchio 01-11-2009, 12.44.15
amuro78 amuro78 non è collegato
Nuovo robottaro
 
Data registrazione: 27-02-2009
Residenza: Catania
Età : 45
Messaggi: 8
Potenza reputazione: 0
amuro78 E' per ora ancora un mistero
Predefinito

Ciao Guido grazie per l'aiuto nel primo pomeriggio testerò il codice che mi hai passato una cosa sola però. Da me il similutore non si bloccava si bloccava nei test reali infatti mettendo un PORTB=1 dopo il ciclo di lock del pll il led associato a quella porta non si accendeva invece togliendo il ciclo si.
Rispondi citando
  #4  
Vecchio 09-11-2009, 06.51.21
amuro78 amuro78 non è collegato
Nuovo robottaro
 
Data registrazione: 27-02-2009
Residenza: Catania
Età : 45
Messaggi: 8
Potenza reputazione: 0
amuro78 E' per ora ancora un mistero
Predefinito

Ciao scusa il ritardo della risposta ma sono stato fuori la scorsa settimana ho provato ad usare il dma ma continua a non funzionare, sto perdendo le speranze di vedere un solo carattere sullo schermo del mio pc.....
Rispondi citando
  #5  
Vecchio 19-02-2010, 09.13.21
L'avatar di calo
calo calo non è collegato
Moderator
 
Data registrazione: 14-06-2006
Residenza: EmiliaSiciliaRomagna
Messaggi: 802
Potenza reputazione: 98
calo E' un faro della comunità
Predefinito

ti passo la mia configurazione dell'uart per un dspic33FJ16GS502. Il dspic comunica con un attuatore (uno stepper) che monta un micro della Freescale. Non ho avuto problemi, anche se fin'ora ho usato solo il TX. La configurazione è un adattamento di quanto trovato sul reference manual e sugli esempi della explorer16.
Ho sfruttato l'interrup perchè le stringhe da trasmettere erano più lunghe di 5 caratteri, che sono la dimensione massima del buffering possibile sul dspic.
Vado a 38400 come baud con l'oscillatore interno.

void initUart(void)
{
//***** configure U1MODE ************************************************** **
U1MODEbits.UARTEN = 0; // Bit15 Disable uart, enable at end of function
//U1MODEbits.notimpl; // Bit14
U1MODEbits.USIDL = 0; // Bit13 Continue in Idle
U1MODEbits.IREN = 0; // Bit12 No IRDA translation
U1MODEbits.RTSMD = 0; // Bit11 Simplex Mode
//U1MODEbits.notimpl; // Bit10
U1MODEbits.UEN = 0; // Bits8,9 TX,RX enabled, CTS,RTS not
U1MODEbits.WAKE = 0; // Bit7 No Wake up (since we don't sleep here)
U1MODEbits.LPBACK = 0; // Bit6 No Loop Back
U1MODEbits.ABAUD = 0; // Bit5 No Autobaud (would require sending '55')
U1MODEbits.URXINV = 0; // Bit4 IdleState = 1 (for dsPIC)
U1MODEbits.BRGH = 0; // Bit3 16 clocks per bit period (low speed mode)
U1MODEbits.PDSEL = 0; // Bits1,2 8bit, No Parity
U1MODEbits.STSEL = 0; // Bit0 One Stop Bit

//***** configure Baud Rate Generator SFR ***********************************
/* Baud Rate desiderato 9600.
BRGVAL = (Fcy/(16*BaudRate))-1
BRGVAL è una define */
U1BRG = BRGVAL;

//***** configure U1STA ************************************************** ***
U1STAbits.UTXISEL1 = 0; // Bit15 (continua al del Bit13) Istante di generazione dell'interrupt
U1STAbits.UTXINV = 0; // Bit14 legato al bit U1MODEbits.IREN; Idle state is ‘1’
U1STAbits.UTXISEL0 = 1; // Bit13 when the last character is shifted out (all the transmit operations are completed)
//U1STAbits.notimpl; // Bit12
U1STAbits.UTXBRK = 0; // Bit11 Sync Break transmission disabled
U1STAbits.UTXEN = 0; // Bit10 Disable uart Tx, enable at end of function
//U1STAbits.UTXBF; // Bit9 (read-only) Transmit Buffer Full Status bit
//U1STAbits.TRMT; // Bit8 (read-only)Transmit Shift Register Empty bit
U1STAbits.URXISEL = 0; // Bits6,7 Int. on character recieved
U1STAbits.ADDEN = 0; // Bit5 Address Detect Disabled
//U1STAbits.RIDLE; // Bit4 (read-only) Receiver Idle bit
//U1STAbits.PERR; // Bit3 (read-only) Parity Error Status bit
//U1STAbits.FERR; // Bit2 (read-only) Framing Error Status bit
//U1STAbits.OERR; // Bit1 (read-only) Receive Buffer Overrun Error Status bit
//U1STAbits.URXDA; // Bit0 (read-only) Receive Buffer Data Available bit

//***** clear interrupt, enable interrupt and priority, enable peripheral ****************
IFS0bits.U1TXIF = 0; // Clear the Transmit Interrupt Flag
IEC0bits.U1TXIE = 1; // Enable Transmit Interrupts
IFS0bits.U1RXIF = 0; // Clear the Recieve Interrupt Flag
IEC0bits.U1RXIE = 1; // Enable Recieve Interrupts
//IPCx = ; // Interrupt Priority level (DA IMPOSTARE)
U1MODEbits.UARTEN = 1; // Enable uart
U1STAbits.UTXEN = 1; // Enable uart Tx
}
Rispondi citando
  #6  
Vecchio 26-02-2010, 09.09.40
amuro78 amuro78 non è collegato
Nuovo robottaro
 
Data registrazione: 27-02-2009
Residenza: Catania
Età : 45
Messaggi: 8
Potenza reputazione: 0
amuro78 E' per ora ancora un mistero
Predefinito

Ciao proverò questa configurazione questo weekend con i dovuti cambiamenti per usare i pin su cui ho connesso la seriale.

Michele
Rispondi citando
Rispondi


Utenti attualmente attivi che stanno leggendo questa discussione: 1 (0 utenti e 1 ospiti)
 
Strumenti discussione
Modalità  visualizzazioe

Regole di scrittura
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code è Attivato
Le smilies sono Attivato
[IMG] è Attivato
Il codice HTML è Disattivato

Vai al forum

Discussioni simili
Discussione Autore discussione Forum Risposte Ultimo messaggio
Comunicazione RS232 con CONTROLLO DI PARITA' maverik46 Elettronica 2 12-11-2011 20.41.10
problemi di comunicazione PIC16F887-->RS232 luigi91 E. Digitale 5 30-01-2010 09.02.44
Comunicazione seriale rs232 e rs485 Kenophy Elettronica 0 13-02-2009 12.13.05
Comunicazione wireless tra 2 PIC (RS232) Chip8110 E. Digitale 6 11-01-2009 11.10.07
Comunicazione Rs232/USB BQuadra Elettronica 12 20-08-2008 23.42.21


Tutti gli orari sono GMT. Adesso sono le: 03.47.43.


Basato su: vBulletin Versione 3.8.8
Copyright ©2000 - 2023, Jelsoft Enterprises Ltd.
Realizzazione siti web Cobaltica Foligno
spacer.png, 0 kB