Tengo un PIC16 (hoja de datos aquí ) para la que funciona la transmisión de UART, excepto justo después de un sueño de vigilancia, donde los primeros caracteres están dañados.
He publicado mi código reducido a continuación. ¿Qué podría causar la corrupción de los primeros caracteres de transmisión de UART justo después de un sueño de vigilancia?
// Baud rates
#define BAUD_115200 0
#define BAUD_9600 1
// Watchdog sleep times
#define WATCHDOG_SLEEP_4S 0b01100
/* Note: The watchdog operating mode configuration is done in configuration word 1 */
void watchdog_configure(char interval) {
// Configure the watchdog interval (e.g. 10010 for 256 seconds)
WDTCONbits.WDTPS = interval & 0b11111;
}
void watchdog_sleep(void) {
WDTCONbits.SWDTEN = 0b1;
SLEEP();
WDTCONbits.SWDTEN = 0b0;
}
void UART_setBaudRate(const unsigned char type) {
// See table 25-5
if(type == BAUD_115200) {
SPBRG = 68;
} else {
SPBRGH = (832 >> 8);
SPBRGL = 832 & 0b11111111;
}
}
/* Configure the UART for full-duplex asynchronous transmission */
void UART_initialise(void) {
UART_setBaudRate(BAUD_115200);
// Configure the RX/DT pin as an input
TRISCbits.TRISC4 = 0b1;
// Enable the asynchronous transceiver
TXSTAbits.TXEN = 0b1;
TXSTAbits.SYNC = 0b0;
RCSTAbits.SPEN = 0b1;
RCSTAbits.CREN = 0b1;
TXSTAbits.BRGH = 0b1;
// Use 16 bits for the baud rate
BAUDCONbits.BRG16 = 0b1;
}
void UART_write(const unsigned char character) {
// Wait while the transmit shift register empties
while(!PIR1bits.TXIF) {}
// Indicate the character to be written to the Transmit Shift Register
TXREG = character;
// Add a one cycle delay to ensure that the TXIF flag is valid
_delay(1);
}
void UART_writeString(const unsigned char *string) {
unsigned char i = 0;
while(*(string + i) != '// Baud rates
#define BAUD_115200 0
#define BAUD_9600 1
// Watchdog sleep times
#define WATCHDOG_SLEEP_4S 0b01100
/* Note: The watchdog operating mode configuration is done in configuration word 1 */
void watchdog_configure(char interval) {
// Configure the watchdog interval (e.g. 10010 for 256 seconds)
WDTCONbits.WDTPS = interval & 0b11111;
}
void watchdog_sleep(void) {
WDTCONbits.SWDTEN = 0b1;
SLEEP();
WDTCONbits.SWDTEN = 0b0;
}
void UART_setBaudRate(const unsigned char type) {
// See table 25-5
if(type == BAUD_115200) {
SPBRG = 68;
} else {
SPBRGH = (832 >> 8);
SPBRGL = 832 & 0b11111111;
}
}
/* Configure the UART for full-duplex asynchronous transmission */
void UART_initialise(void) {
UART_setBaudRate(BAUD_115200);
// Configure the RX/DT pin as an input
TRISCbits.TRISC4 = 0b1;
// Enable the asynchronous transceiver
TXSTAbits.TXEN = 0b1;
TXSTAbits.SYNC = 0b0;
RCSTAbits.SPEN = 0b1;
RCSTAbits.CREN = 0b1;
TXSTAbits.BRGH = 0b1;
// Use 16 bits for the baud rate
BAUDCONbits.BRG16 = 0b1;
}
void UART_write(const unsigned char character) {
// Wait while the transmit shift register empties
while(!PIR1bits.TXIF) {}
// Indicate the character to be written to the Transmit Shift Register
TXREG = character;
// Add a one cycle delay to ensure that the TXIF flag is valid
_delay(1);
}
void UART_writeString(const unsigned char *string) {
unsigned char i = 0;
while(*(string + i) != '%pre%') {
UART_write(*(string + i));
i += 1;
}
}
void main(void) {
// Perform initialisations
watchdog_configure(WATCHDOG_SLEEP_4S);
UART_initialise();
watchdog_sleep();
UART_writeString("UART TESTING");
}
') {
UART_write(*(string + i));
i += 1;
}
}
void main(void) {
// Perform initialisations
watchdog_configure(WATCHDOG_SLEEP_4S);
UART_initialise();
watchdog_sleep();
UART_writeString("UART TESTING");
}