如何通过 UART 发送 PIC18 中的字符串?
How to send string in PIC18 via UART?
我正在尝试通过 UART 向我的 PC 发送和接收字符串。我的单片机是 PIC18F65K40。程序员是 PicKit 3。问题是我在对设备进行编程时收到一些垃圾值,但在那之后发生了注意。几乎相同的代码在 PIC18F25K50 上运行没有问题。在我看来,问题出在时钟值或波特率设置上。
我正在尝试将内部时钟设置为 8MHz,并且我使用的是 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] != '[=11=]')
{
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);
}
}```
我正在尝试通过 UART 向我的 PC 发送和接收字符串。我的单片机是 PIC18F65K40。程序员是 PicKit 3。问题是我在对设备进行编程时收到一些垃圾值,但在那之后发生了注意。几乎相同的代码在 PIC18F25K50 上运行没有问题。在我看来,问题出在时钟值或波特率设置上。 我正在尝试将内部时钟设置为 8MHz,并且我使用的是 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] != '[=11=]')
{
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);
}
}```