Forum: Mikrocontroller und Digitale Elektronik PIC24 + Explorer 16 + UART


von Norbert (Gast)


Angehängte Dateien:

Lesenswert?

Hallo zusammen,

ich habe mir das Microchip Explorer 16 Development Board zusammen mit 
einem ECAN Daughterboard gekauft. Als PIM verwende ich den 
PIC24HJ256GP610A. Microchip stellt Beispielcode zur Verfügung:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1406&dDocName=en024858&part=dm240001&redirects=explorer16

Der CAN-Teil meines Codes klappt, aber ich habe Probleme UART richtig 
zum Laufen zu bekommen. Ich habe einen USB/RS232-Adapter zum PC 
angeschlossen, erhalte aber nicht die richtigen Nachrichten. 
Einstellungen sind 19200 Baud, 8 Datenbits, 1 Stoppbit, kein CTS. Hier 
Ausschnitte aus meinem Code:

1
#define BAUDRATE2    19200
2
3
// UART IOs
4
#ifdef  PPS_UART2_TX_TRIS
5
#define UART2_TX_TRIS   PPS_UART2_TX_TRIS
6
#define UART2_RX_TRIS   PPS_UART2_RX_TRIS
7
#else
8
#define UART2_TX_TRIS   TRISFbits.TRISF5
9
#define UART2_RX_TRIS   TRISFbits.TRISF4
10
#endif
11
12
13
#define BAUDRATEREG2 SYSCLK/32/BAUDRATE2-1
14
15
#if BAUDRATEREG2 > 255
16
#error Cannot set up UART2 for the SYSCLK and BAUDRATE.\
17
 Correct values in main.h and uart2.h files.
18
#endif
19
20
#define BAUDRATE_MISTAKE 1000*(BAUDRATE2-SYSCLK/32/(BAUDRATEREG2+1))/BAUDRATE2
21
#if (BAUDRATE_MISTAKE > 2)||(BAUDRATE_MISTAKE < -2)
22
#error UART2 baudrate mistake is too big  for the SYSCLK\
23
 and BAUDRATE2. Correct values in uart2.c file.
24
#endif
25
26
27
28
void oscConfig(void)
29
{
30
    /*  Configure Oscillator to operate the device at 40Mhz
31
    Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
32
    Fosc= 8M*40/(2*2)=80Mhz for 8M input clock */
33
34
    CLKDIVbits.PLLPOST=0;    /* N1=2 */
35
    CLKDIVbits.PLLPRE=0;    /* N2=2 */
36
    OSCTUN=0;          /* Tune FRC oscillator, if FRC is used */
37
38
    /* Disable Watch Dog Timer */
39
    RCONbits.SWDTEN=0;
40
41
    /* Clock switch to incorporate PLL*/
42
    __builtin_write_OSCCONH(0x03);    // Initiate Clock Switch to Primary
43
    // Oscillator with PLL (NOSC=0b011)
44
    __builtin_write_OSCCONL(0x01);    // Start clock switching
45
    while (OSCCONbits.COSC != 0b011);  // Wait for Clock switch to occur
46
47
    /* Wait for PLL to lock */
48
    while(OSCCONbits.LOCK!=1) {};
49
}
50
51
52
void UART2Init()
53
{
54
    // Set directions of UART IOs
55
  UART2_TX_TRIS = 0;
56
  UART2_RX_TRIS = 1;
57
  U2BRG = 129;//BAUDRATEREG2;
58
  U2MODE = 0;
59
  U2STA = 0;
60
  U2MODEbits.UARTEN = 1;
61
  U2STAbits.UTXEN = 1;
62
    // reset RX flag
63
   IFS1bits.U2RXIF = 0;
64
}
65
66
void  UART2PutChar(char Ch)
67
{
68
    // wait for empty buffer
69
    while(U2STAbits.UTXBF == 1);
70
      U2TXREG = Ch;
71
}
72
73
int main(void)
74
{
75
    /* Configure Oscillator Clock Source   */
76
    oscConfig();
77
78
    // Setup the UART
79
    UART2Init();
80
81
    /* Loop infinitely */
82
    while (1)
83
   {
84
            UART2PutChar('a');
85
   }
86
}

Danke!

von Frank K. (fchk)


Lesenswert?

WO setzt Du PLLDIV (M)?

fchk

von Norbert (Gast)


Lesenswert?

Frank K. schrieb:
> WO setzt Du PLLDIV (M)?

Das setze ich nirgendwo. Im Handbuch steht:
If PLLPOST<1:0> = 0, then N2 = 2. This provides a Fosc of 160/2 = 80 
MHz. The resultant device operating speed is 80/2 = 40 MIPS.


Ich habe übrigens einen Fehler im ersten Post. In meinen Tests hieß es 
natürlich
1
 U2BRG = BAUDRATEREG2;
statt
1
 U2BRG = 129;//BAUDRATEREG2;

von Chris B. (dekatz)


Lesenswert?

......
#define BAUDRATEREG2 SYSCLK/32/BAUDRATE2-1
......

Wo ist SYSCLK definiert und welcher Wert steht dort ??

von Frank K. (fchk)


Lesenswert?

Norbert schrieb:
> Frank K. schrieb:
>> WO setzt Du PLLDIV (M)?
>
> Das setze ich nirgendwo. Im Handbuch steht:
> If PLLPOST<1:0> = 0, then N2 = 2. This provides a Fosc of 160/2 = 80
> MHz. The resultant device operating speed is 80/2 = 40 MIPS.

Nach einem Power-On steht dort 50 drin. (DS70592D-page 129) Passt das 
für Dich?

Was für einen Quarz hast Du?

fchk

von Norbert (Gast)


Lesenswert?

// External oscillator frequency
#define SYSCLK          8000000

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.