使用USART2连接两个PIC18F入门工具包

时间:2015-03-05 14:31:32

标签: c mplab pic18 xc8

我想使用RS232连接两个PIC18F入门套件(使用PIC18F46J50)。由于USART1已经连接到读卡器(集成),我需要使用USART2(我需要将RX2 / TX2重新映射到RP19 / RP20引脚-RD2 / RD3)。

我已经找到了一些SENDING的演示代码:

#define _XTAL_FREQ 8000000 //The speed of your internal(or)external oscillator
 #include <p18cxxx.h>
 #include <usart.h>
int i = 0;

 // CONFIG1L
#pragma config WDTEN = OFF          // Watchdog Timer (Disabled - Controlled by SWDTEN bit)
#pragma config PLLDIV =3            // PLL Prescaler Selection bits - Divide by 3 (12 MHz oscillator input)
#pragma config STVREN = ON          // Stack Overflow/Underflow Reset  (Enabled)
#pragma config XINST = OFF          // Extended instruction set disabled

// CONFIG1H
#pragma config CPUDIV = OSC1        // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF            // Code Protect (Program memory is not code-protected)

// CONFIG2L
#pragma config OSC = HSPLL          //HS oscillator, PLL enabled, HSPLL used by USB
#pragma config T1DIG = ON           // T1OSCEN Enforcement (Secondary Oscillator clock source may be selected)
#pragma config LPT1OSC = OFF        // Low-Power Timer1 Oscillator (High-power operation)
#pragma config FCMEN = OFF          //Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF           //Two-Speed Start-up disabled

// CONFIG2H
#pragma config WDTPS = 32768        // Watchdog Postscaler (1:32768)

// CONFIG3L
#pragma config DSWDTOSC = INTOSCREF // DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = T1OSCREF    // RTCC Clock Select (RTCC uses T1OSC/T1CKI)
#pragma config DSBOREN = OFF        // Zero-Power BOR disabled in Deep Sleep
#pragma config DSWDTEN = OFF        // Deep Sleep Watchdog Timer (Disabled)
#pragma config DSWDTPS = 8192       //1:8,192 (8.5 seconds)

// CONFIG3H
#pragma config IOL1WAY =OFF         //IOLOCK bit can be set and cleared
#pragma config MSSP7B_EN = MSK7     // MSSP address masking (7 Bit address masking mode)

// CONFIG4L
#pragma config WPFP = PAGE_1        // Write/Erase Protect Page Start/End Location (Write Protect Program Flash Page 0)
#pragma config WPEND = PAGE_0       //Start protection at page 0
#pragma config WPCFG = OFF          //Write/Erase last page protect Disabled

// CONFIG4H
#pragma config WPDIS = OFF             //WPFP[5:0], WPEND, and WPCFG bits ignored

 #define USE_AND_MASKS

unsigned char Txdata[] = "MICROCHIP_USART";
void Delay1Second(void);

 void main (void)
 {
     unsigned char spbrg=0,baudconfig=0,i=0;

     // REMAPE ID PORT

    PPSCON = 0x00;              // unlock peripheral Pin select register
    RPOR19 = 0x05;              // assign USART2 TX to RP19/RD2
    RPINR16 = 0x14;             // assign USART2 RX to RP20/RD3
    PPSCON = 0x01;              // lock peripheral Pin select register

    TRISDbits.TRISD2 = 0;       // TX2 output
    TRISDbits.TRISD3 = 1;       // RX2 input
 //------USART Setup ----

     Close2USART();  //turn off usart if was previously on

     spbrg = 51;

     Open2USART(USART_TX_INT_OFF &
                USART_RX_INT_OFF &
                USART_ASYNCH_MODE &
                USART_EIGHT_BIT &
                USART_CONT_RX &
                USART_BRGH_HIGH, spbrg);

     baudconfig = BAUD_8_BIT_RATE & BAUD_AUTO_OFF;
     baud2USART (baudconfig);

     PORTB = 0x03;

 while(1){

//------USART Transmission ----
    while(Busy2USART());             //Check if Usart is busy or not
   puts2USART((char *)Txdata);                //transmit the string
    Delay1Second();
   Close2USART();
}                         
 }

 void Delay1Second()
{
    for(i=0;i<100;i++)
    {
         __delay_ms(10);
    }
}

我使用两条线(在D2和D3引脚上)连接两块板,我想从board1向board2发送一个字符串。我想我需要使用中断来读取,但我还没有找到任何演示代码。另外,我不知道配置引脚是否正常。 有人可以告诉我如何接收发送的文本到第2号?

1 个答案:

答案 0 :(得分:0)

我会假设您正确接线。

您不需要中断即可接收。它可能很有用,但不是必需的。要接收你的字符串,你只需要这样做:

gets2USART( str, 10 );

如果您的字符串长度为10个字符(包括空终止符)。

如果要接收任意长度的字符串,可以编写自己的gets2USART来读取,直到空终止符。它实际上非常简单,这里有gets2USART的源代码可以帮助您:

void gets2USART(char *buffer, unsigned char len)
{
  char i;    // Length counter
  unsigned char data;

  for(i=0;i<len;i++)  // Only retrieve len characters
  {
    while(!DataRdy2USART());// Wait for data to be received

    data = getc2USART();    // Get a character from the USART
                       // and save in the string
    *buffer = data;
    buffer++;              // Increment the string pointer
  }
}

此外,这里是puts2USART的源代码:

void puts2USART( char *data)
{
  do
  {  // Transmit a byte
    while(Busy2USART());
    putc2USART(*data);
  } while( *data++ );
}

您可以看到已经存在while(Busy2USART());,因此您无需添加一个{{1}}。这是合乎逻辑的,因为UART是字节方式的,因此它在发送一个字节时很忙,并且该函数需要检查每个字节。

此外,您在使用后关闭了UART,但实际上是循环重做发送。由于您刚关闭了UART,因此无法正常工作。

相关问题