• AVR Freaks

Helpful ReplyLockedXC8 USART getsUSART stops after a couple reads

Author
k3nt
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2013/02/09 23:53:40
  • Location: 0
  • Status: offline
2013/02/27 17:33:02 (permalink)
0

XC8 USART getsUSART stops after a couple reads

Hello All,
 
I'm using a PIC18F46K22.
 
I'm attempting to read a string on the pic which is being sent by a cmucam4(programmable camera module for basic image processing - http://www.cmucam.org/projects/cmucam4/ )  via uart.  I'm then sending that string out on the other usart to my computer for debugging purposes. I can send commands to the cmucam4 and retreive single responses just fine and see them relayed back to my terminal on my computer. The problem arises when I send a command that tells the cmucam4 to begin sending a particular message out multiple times a second. The pic receives about 3 lines and then stops. I have noticed when I reprogram the pic after this has happened I see it dump a string out to my computer. I imagine this was waiting in the buffer and never got out.. Any ideas would be greatly appreciated.
Thanks!
 
Just to note, I use my own puts functions vs the plib ones because the cmucam4 will only accept '\r' terminated strings, not '\0'. 
 
I apologize for the lack of formatting
 
THE PROBLEM ARISES IN THE WHILE LOOP
 

// PIC18F46K22 Configuration Bit Settings
#include <xc.h>#include <stdio.h>#include <stdint.h>#include <plib/usart.h>#define _XTAL_FREQ 16000000
// CONFIG1H#pragma config FOSC = INTIO67   // Oscillator Selection bits (Internal oscillator block)#pragma config PLLCFG = OFF     // 4X PLL Enable (Oscillator used directly)#pragma config PRICLKEN = ON    // Primary clock enable bit (Primary clock is always enabled)#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L#pragma config PWRTEN = OFF     // Power-up Timer Enable bit (Power up timer disabled)#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))#pragma config BORV = 190       // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
// CONFIG2H#pragma config WDTEN = OFF       // Watchdog Timer Enable bits (WDT is always enabled. SWDTEN bit has no effect)#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H#pragma config CCP2MX = PORTC1  // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)#pragma config CCP3MX = PORTB5  // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)#pragma config HFOFST = ON      // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)#pragma config T3CMX = PORTC0   // Timer3 Clock input mux bit (T3CKI is on RC0)#pragma config P2BMX = PORTD2   // ECCP2 B output mux bit (P2B is on RD2)#pragma config MCLRE = EXTMCLR  // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
// CONFIG4L#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L#pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)#pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)#pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)#pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L#pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)#pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)#pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)#pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) 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)
// CONFIG7L#pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)#pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)#pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)#pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)


#define LED1 RA1#define LED2 RA2#define LED3 RA3
#define ON 1
void led1_on();void led2_on();void led3_on();
void toggle_led1();void toggle_led2();void toggle_led3();
void puts1(char *data);void puts2(char *data);void putrs1(const char *data);void putrs2(const char *data);
uint8_t sPORTA = 0;
void toggle_led1() {    PORTAbits.RA1 = !PORTAbits.RA1;}
void toggle_led2() {    PORTAbits.RA2 = !PORTAbits.RA2;}
void toggle_led3() {    PORTAbits.RA3 = !PORTAbits.RA3;}
void led1_on() {    sPORTA |= (1 << 1);    PORTA = sPORTA;}
void led1_off() {    sPORTA &= ~(1 << 1);    PORTA = sPORTA;}
void led2_on() {    sPORTA |= (1 << 2);    PORTA = sPORTA;}
void led2_off() {    sPORTA &= ~(1 << 2);    PORTA = sPORTA;}
void led3_on() {    sPORTA |= (1 << 3);    PORTA = sPORTA;}
void led3_off() {    sPORTA &= ~(1 << 3);    PORTA = sPORTA;}
void puts1(char *data) {    do { // Transmit a byte        while (Busy1USART());        putc1USART(*data);    } while (*(++data));}
void puts2(char *data) {    do { // Transmit a byte        while (Busy2USART());        putc2USART(*data);    } while (*(++data));}
void puts2slow(char *data) {    do { // Transmit a byte        while (Busy2USART());        putc2USART(*data);        __delay_ms(40);    } while (*(++data));}
void putrs1(const char *data) {    do { // Transmit a byte        while (Busy1USART());        putc1USART(*data);    } while (*(++data));}
void putrs2(const char *data) {    do { // Transmit a byte        while (Busy2USART());        putc2USART(*data);    } while (*(++data));}
char cmu_ready_str[16];
char track_color_input[40];
char inputstr[6];
char inputcmd[20];
const char auto_gain_off[6] = "AG 0\r";
const char auto_wb_off[6] = "AW 0\r";
const char set_track_param[30] = "ST 255 255 255 255 255 255 \r";
const char track_color[5] = "TC \r";
char c;
/* *  */int main(int argc, char** argv) {
    PORTA = 0;
    TRISA = 0b11110001;    TRISD = 0b11111111;    TRISC = 0b11111111;
    OSCCON = 0b01110111;    ANSELD = 0;    ANSELC = 0;
    Open1USART(USART_TX_INT_ON &            USART_RX_INT_ON &            USART_ASYNCH_MODE &            USART_EIGHT_BIT &            USART_CONT_RX &            USART_BRGH_HIGH,            51);


    Open2USART(USART_TX_INT_ON &            USART_RX_INT_ON &            USART_ASYNCH_MODE &            USART_EIGHT_BIT &            USART_CONT_RX &            USART_BRGH_HIGH,            51);

    while (!DataRdy1USART());
    gets1USART(cmu_ready_str, 16);
    puts2(cmu_ready_str);
    __delay_ms(40);
    putrs1(auto_gain_off); //TURN AUTO GAIN OFF
    gets1USART(inputstr, 5);
    puts2(inputstr); //DEBUG OUT
    __delay_ms(40);

    putrs1(auto_wb_off); //TURN AUTO WHITE BALANCE OFF
    gets1USART(inputstr, 5);
    puts2(inputstr); //DEBUG OUT
    __delay_ms(40);
    putrs1(set_track_param); //SET TRACKING PARAMETERS
    gets1USART(inputstr, 5);
    puts2(inputstr); //DEBUG OUT
    __delay_ms(40);
    putrs1(track_color); //Track Color
    gets1USART(track_color_input, 22);
    puts2(track_color_input); //DEBUG OUT
    while (1) {
        gets1USART(track_color_input, 16);
        puts2(track_color_input); //DEBUG OUT

    }
    Close1USART();    Close2USART();}

#1
NKurzman
A Guy on the Net
  • Total Posts : 18856
  • Reward points : 0
  • Joined: 2008/01/16 19:33:48
  • Location: 0
  • Status: online
Re:XC8 USART getsUSART stops after a couple reads 2013/02/27 20:50:54 (permalink) ☄ Helpful
+2 (1)
Are you checking the OERR flag?  if you do not pull the data from the buffer fast enough it will disable the receiver.
#2
k3nt
New Member
  • Total Posts : 9
  • Reward points : 0
  • Joined: 2013/02/09 23:53:40
  • Location: 0
  • Status: offline
Re:XC8 USART getsUSART stops after a couple reads 2013/02/28 12:27:36 (permalink)
0
Thank you very much NKurzman.
 
The XC8 plib/usart asm getc1USART(); does not check the OERR flag.
 
I wrote my own getc with an OERR check and all is good!
 
Cheers
#3
Jump to:
© 2020 APG vNext Commercial Version 4.5