Как отправить строку в PIC18 через UART? - PullRequest
1 голос
/ 02 июня 2019

Я пытаюсь отправить и получить строку на свой компьютер через UART.Мой MCU - PIC18F65K40.Программист PicKit 3. Проблема в том, что я получаю некоторые значения мусора при программировании устройства, но после этого произошло замечание.Почти тот же код работает на PIC18F25K50 без проблем.На мой взгляд, проблема в значении часов или в настройке скорости передачи.Я пытаюсь установить внутренние часы на 8 МГц, и я использую 9600 бод.

биты конфигурации:

#define CONBITS_H

#include <xc.h>

#define _XTAL_FREQ 8000000UL

// PIC18F65K40 Configuration Bit Settings

// 'C' source line config statements


// CONFIG1L
#pragma config FEXTOSC = HS     // External Oscillator mode Selection bits (HS (crystal oscillator) above 8 MHz; PFM set to high power)
#pragma config RSTOSC = HFINTOSC_64MHZ// Power-up default value for COSC bits (HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1)

// CONFIG1H
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled)
#pragma config CSWEN = ON       // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)

// CONFIG2L
#pragma config MCLRE = EXTMCLR  // Master Clear Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RG5 pin function is MCLR )
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (Power up timer disabled)
#pragma config LPBOREN = OFF    // Low-power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)

// CONFIG2H
#pragma config BORV = VBOR_2P45 // Brown Out Reset Voltage selection bits (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCD = OFF        // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = ON     // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config DEBUG = OFF      // Debugger Enable bit (Background debugger disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)

// CONFIG3L
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = ON        // WDT operating mode (WDT enabled regardless of sleep)

// CONFIG3H
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4L
#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)

// CONFIG4H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config SCANE = ON       // Scanner Enable bit (Scanner module is available for use, SCANMD bit can control the module)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)

// CONFIG5L
#pragma config CP = OFF         // UserNVM Program Memory Code Protection bit (UserNVM code protection disabled)
#pragma config CPD = OFF        // DataNVM Memory Code Protection bit (DataNVM code protection disabled)

// CONFIG5H

// CONFIG6L
#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG6H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.



#endif
#include "pic18f65k40.h"
#include "conbits.h"

void interrupt UART3_receive_int(void);
void interrupt low_priority UART3_transmit_int(void);

void UART3_init(void)
{
    //UART3 init - PC communication

    TRISEbits.TRISE0 = 0;
    TRISEbits.TRISE1 = 1; // input?    

    RC3STAbits.SPEN = 1; 
    RC3STAbits.CREN = 1;
    TX3STAbits.TXEN = 1;
    TX3STAbits.SYNC = 0;
    TX3STAbits.BRGH = 1; //????
    //framing error ??
    //overrun error ??

    BAUD3CONbits.SCKP = 0;
    BAUD3CONbits.BRG16 = 1;
    SP3BRGL = 0xCF; // baud rate 9600

    //PIE4bits.RC3IE = 1;
    PIE4bits.TX3IE = 1;

    IPR4bits.TX3IP = 0;
    //IPR4bits.RC3IP = 1;
}

void UART_Send(unsigned char data[])
{
    int i = 0;
    while(data[i] != '\0')
    {
        if(PIR4bits.TX3IF == 1)
        {
            TX3REG = data[i];
            i++;
        }
    }
    __delay_ms(5);
}

void K1_active(void)
{
    LATDbits.LATD1 = 1;
    //delay
    __delay_ms(2000);
    LATDbits.LATD1 = 0;
    __delay_ms(2000);
}

//relay 2,3,4,5,6 functions
//active led function / on and blink - choose functionality for LEDs
//error led function / on and blink

void main(void) 
{
    //Internal Clock set

    OSCCON1bits.NOSC = 0x06;
    OSCCON1bits.NDIV = 0;

    OSCFRQbits.HFFRQ = 0x03;

    OSCENbits.HFOEN = 1;
    while(OSCSTATbits.HFOR != 1);

    INTCONbits.GIE = 1;
    INTCONbits.PEIE = 1;




    //Relays
    TRISDbits.TRISD1 = 0; //K1
    TRISDbits.TRISD2 = 0; //K2
    TRISHbits.TRISH2 = 0; //K3
    TRISHbits.TRISH3 = 0; //K4
    TRISEbits.TRISE7 = 0; //K5
    TRISDbits.TRISD0 = 0; //K6

    //LEDs
    TRISEbits.TRISE5 = 0; //Error LED
    TRISEbits.TRISE6 = 0; //Active LED

    TRISAbits.TRISA2 = 0;

    UART3_init();

    //temp
    LATDbits.LATD1 = 0; //K1
    LATDbits.LATD2 = 0; //K2
    LATHbits.LATH2 = 0; //K3
    LATHbits.LATH3 = 0; //K4
    LATEbits.LATE7 = 0; //K5
    LATDbits.LATD0 = 0; //K6

    //TX3REG =65;

    while(1)
    {
        //K1_active();
        UART_Send("aaaaaa\n");


        //__delay_ms(1000);        
    }
}```

1 Ответ

1 голос
/ 04 июня 2019

Согласно ошибкам вашего устройства, в кремнии ревизии A3 есть ошибка, которая влияет на регистр NVMREG. Это может повлиять на UART и объяснить, почему ваш код работал на семейство K50.

Чтобы проверить, является ли ваша PIC версией A3, вам нужно подключить свой программатор и нажать Refresh Debug Tool Status Icon:

enter image description here

Версия кремния будет напечатана на выходе консоли отладчика.

Если у вас есть ревизия A3, вы можете исправить эту проблему, перейдя в Свойства проекта -> Линкер XC8 -> Дополнительные параметры и написав +NVMREG в поле для исправлений:

enter image description here

Об этой ошибке сообщалось на нескольких форумах по микрочипам, а также здесь .

...