Ich begann mit der Arbeit am Mikrocontroller PIC24FJ128GA202. Versuchte Beispielprogramme wie LED blinken und schalten, Controller funktioniert gut mit der Codierung, dann begann ich mit der Arbeit an UART, konfigurierte alle Parameter gemäß den Datenblattnormen. Aber mein PIC funktioniert nicht mit meinem Code. Ich habe meinen Controller mit dem PC verbunden, um die übertragenen Zeichen zu überprüfen. Ich habe meinen Code unten gepostet. Ich erhalte kein Zeichen auf Hyperterminal... Manchmal habe ich Müllwerte erhalten.
#include "p24FJ128GA202.h" // Pic24FJ128GA202 header file
#define led PORTAbits.RA2
void init_processor(void);
void init_processor(void)
{
/* oscillator configuration is pending*/
OSCCON = 0x0000;
CLKDIV = 0x0000; //do not divide
OSCTUN = 0;
RPINR18bits.U1RXR = 7; //UART1 receive set to RB7
RPOR4bits.RP8R = 3; //UART1 transmit set to RB8
__builtin_write_OSCCONL(OSCCON & 0xbf); //clear the bit 6 of OSCCONL to
//unlock Pin Re-map
RPOR4bits.RP8R = 3;
//__builtin_write_OSCCONL(OSCCON | 0x40); //set the bit 6 of OSCCONL to
//lock Pin Re-map
// configure digital pins
ANSA = 0x0000;
ANSB = 0x0000;
// assign IO values for ports
PORTA = 0x0000;
TRISA = 0x0000;
PORTB = 0x0000;
TRISB = 0x0000; // 7 as receive and 8 as transmitter
// Init UART1
U1MODE = 0x0000;
U1STA = 0x0400; //Enable Transmission, Clear all flags
U1BRG = 25;
U1MODEbits.UARTEN = 1; // And turn the peripheral on
}
void uart1tx(char in_c)
{
while(U1STAbits.UTXBF != 0);
U1TXREG = in_c;
}
void uart1str(char *s)
{
while(*s!='\0')
{
uart1tx(*s);
s++;
}
}
char uart1rx()
{
char c;
while(U1STAbits.URXDA == 0);
c = U1RXREG;
return c;
}
int main()
{
int i,j;
led = 1;
init_processor();
while(1)
{
uart1tx('A');
led = ~led;
for(j=0;j<250;j++)
{
for(i=0;i<1275;i++);
}
}
return 0;
}
Sie müssen das PPS entsperren, bevor Sie Änderungen vornehmen können. Insbesondere die
__builtin_write_OSCCONL(OSCCON & 0xbf);
muss nach oben verschoben werden
RPINR18bits.U1RXR = 7;
Außerdem möchten Sie wahrscheinlich MC-Makros verwenden, um dies zu tun. Hier ist ein Beispiel für eine funktionierende UART-Init für ähnliche Mikro -> https://github.com/felis/cdb/blob/master/fw/minimal/bsp.c#L185
Junior
kabZX
Spehro Pefhany
Pradeep Kumark