[Beginer] usart question

[Beginer] usart question

Postby Entropy » Fri Sep 04, 2015 7:18 am

Made a simple program to learn how USART works, all is good so far just for one thing

http://i.imgur.com/g6NXJEd.jpg

At the end is a 0 or something, what is that? is my program? its the protocol? Also the terminal see it, first hello its ok, then is got that à in front of every hello
Note, in header.c is just the configuration bits.
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Sat Sep 05, 2015 1:01 am

At the end is "0A", which is LF, then "0D", which is CR. That is the "\n\r" in your string.
The "E0" character shouldn't be there, but I think I know why it is.

Note, in header.c is just the configuration bits.

I do NOT think it is a good idea to hide the configuration bits in a header file.
You must only include them into a single C file, so there's no value in having them in a header, and in this case, I think the problem is in the configuration bits, but you haven't shown them.
I suspect you have left the Watchdog Timer enabled, but your loop is not resetting it, so the WDT is expiring and resetting your PIC while you are in those delay loops.

As a test, it might have been better to move the "while(BusyUSART())" test to inside the loop, and omitted the delays altogether. Then you would have seen the reset happen in the middle of the string, and got a hint what was happening.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Sat Sep 05, 2015 4:59 am

Done and is working fine: http://i.imgur.com/9QYyUHz.jpg
WDT was ON, probably this explain also why from time to time my pic resets itself and start over
The __delays where not for my pic program, they where for that little UART Terminal, whithout delays the terminal gets flooded and in few seconds go Non Responding

Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <p18f2550.h>
#include <xc.h>
#include <usart.h>

#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
#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)
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
#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)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
#define _XTAL_FREQ 20000000

void main(void){
   
    ADCON1=0b00001111;
   
    OpenUSART (USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH &
            USART_ADDEN_OFF, 130);
    while(BusyUSART());
   
    while(1){
      putsUSART("hello\n\r");
      while(BusyUSART());
    // __delay_ms(30);__delay_ms(30);__delay_ms(30);
    }
   
}
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Sat Sep 05, 2015 5:19 am

Replace all these:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <p18f2550.h>
#include <xc.h>
#include <usart.h>

with just:
Code: Select all
#include <xc.h>

That automatically brings in the correct processor file, and any required plib includes.
You rarely require stdio.h, or stdlib.h in an embedded project.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby ric » Sat Sep 05, 2015 6:31 am

You also have LVP turned on.
Unless you really know what that is for, and require it, turn it off.
(Right now it is forcing RB5 to be an input, and will reset your PIC if and when it floats high if it is floating.)

There is still a stray character in between your strings. It has changed from E0 to 80, but it is still there.
You should follow that up to try to determine the cause.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Sat Sep 05, 2015 9:51 am

ric wrote:You also have LVP turned on.
Unless you really know what that is for, and require it, turn it off.
(Right now it is forcing RB5 to be an input, and will reset your PIC if and when it floats high if it is floating.)

There is still a stray character in between your strings. It has changed from E0 to 80, but it is still there.
You should follow that up to try to determine the cause.


I dont, LPV is OFF atm and that thing is still there, just now is changing, see image: http://i.imgur.com/TRdsIs6.jpg (ignore crashing of the terminal)
also with different terminal http://i.imgur.com/0yIbfY7.jpg in this pic I remove\n\r just to be sure

My logic analizer show is a 0b01111111 there. Could it be something with putsUSART? aparently is the only thing who can do that in my program. Another info: the Rx(18) Tx(17) pins are free, no pull up or pull down

Code: Select all
#include <xc.h>
//#include <usart.h>

#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
#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)
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
#define _XTAL_FREQ 20000000

void main(void){
   
    ADCON1=0b00001111;
   
    OpenUSART (USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH &
            USART_ADDEN_OFF, 130);
    while(BusyUSART());
   
    while(1){
      putsUSART("hello");
      while(BusyUSART());
     //__delay_ms(30);__delay_ms(30);__delay_ms(30);
    }
   
}
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Sat Sep 05, 2015 10:24 am

This reminds me why I dislike using the library functions.
It's usually quicker to roll your own than to learn all the quirks of the provided functions.
In this case, the library putsUSART function sends a NULL terminated string, INCLUDING THE NULL !
I would guess that your terminal program is having trouble handling the NULL character.

Here is the code for puts1USART (which is what putsUSART is handled by)
Code: Select all
#include <p18cxxx.h>
#include <usart.h>

/**********************************************************************
*    Function Name:  puts1USART                                        *
*    Return Value:   void                                             *
*    Parameters:     data: pointer to string of data                  *
*    Description:    This routine transmits a string of characters    *
*                    to the USART1 including the null.                 *
**********************************************************************/
#if defined (AUSART_V2) || defined (EAUSART_V6)|| defined (EAUSART_V7) ||\
    defined (EAUSART_V8) || defined (EAUSART_V9) || defined (EAUSART_V10) ||\
   defined (EAUSART_V11) || defined (EAUSART_V11_1) || defined (EAUSART_V12)
void puts1USART( char *data)
{
  do
  {  // Transmit a byte
    while(Busy1USART());
    putc1USART(*data);
  } while( *data++ );
}
#endif

Try making your own version as follows:
Code: Select all
void myputs( char *data)
{
  while (*data)    //loop until we hit a NULL
  {  // Transmit a byte
    while(Busy1USART());    //wait until USART not busy
    putc1USART(*data++);
  };
}

That is actually a pretty inefficient routine, because Busy1USART() waits until the complete previous character has been sent.
It is a lot more efficient to just do it all yourself, as follows:
Code: Select all
void myputs( char *data)
{
  while (*data)    //loop until we hit a NULL
  {  // Transmit a byte
    while(PIR1.TXIF == 0);    //wait until USART TX buffer is not full
    TXREG = *data++;    //send next character, then bump pointer
  };
}
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Sat Sep 05, 2015 1:37 pm

Finaly is working, thank you man :mrgreen: . Mean time I search the forums to see whats the deal with that NULL. It seems C/C++ at the end of the strings puts a NULL to know where the string is ending (I did not know that). For some reason PIR1.TXIF == 0 is not recognized by my x8, is working fine just with TXIF == 0.
In the end... I guess we need better terminals who can deal with strings null

This is the final cutdown form, just in case someone else have this problem

Code: Select all
#include <usart.h>
#include <xc.h>

#pragma config ...whatever pic needs

#define _XTAL_FREQ 20000000

void myputs( char *data)
{
  while (*data)    //loop until we hit a NULL
  {                // Transmit a byte
    while(PIR1bits.TXIF == 0);    //wait until USART TX buffer is not full
    TXREG = *data++;    //send next character, then bump pointer
  };
}

void main(void){
   
    ADCON1=0b00001111;
   
    OpenUSART (USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH &
            USART_ADDEN_OFF, 130);
            while(BusyUSART());
   
    while(1){
        myputs("hello\r\n");
      //putsUSART("hello");
      while(BusyUSART());
    }
   
}
Last edited by Entropy on Mon Sep 07, 2015 6:13 am, edited 1 time in total.
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Re: [Beginer] usart question

Postby ric » Sat Sep 05, 2015 2:34 pm

Entropy wrote:... It seems C/C++ at the end of the strings puts a NULL to know where the string is ending (I did not know that).

Yes, that has been part of C from the beginning. Strings are always terminated with a NULL.
(Unlike BASIC, which stores a length seperately)

For some reason PIR1.TXIF == 0 is not recognized by my x8, is working fine just with TXIF == 0.

Oops, my mistake, it should have been
PIR1bits.TXIF
but as you found, just TXIF works too in processors that only have a single USART.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: [Beginer] usart question

Postby Entropy » Thu Oct 01, 2015 8:08 pm

me again :mrgreen:

example getsUSART(message, 10); what is the procedure when the length of the string is not known? In this example it waits until I send him 10 characters/numbers.
this is my code atm:
Code: Select all
//18F2550 USART to Morse Code
//The duration of a dash is three times the duration of a dot.
//Each dot or dash is followed by a short silence, equal to the dot duration.
//The letters of a word are separated by a space equal to three dots (one dash)
//The words are separated by a space equal to seven dots.

#include <stdio.h>
#include <stdlib.h>
#include <pic18f2550.h>
#include <xc.h>
#include <string.h>
#include <usart.h>

#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator (HS))
#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)
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (Minimum setting)
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
#pragma config CCP2MX = ON      // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) is not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM is not code-protected)
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) are not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) is not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM is not write-protected)
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) is not protected from table reads executed in other blocks)


#define _XTAL_FREQ 20000000

unsigned int stringLength;
unsigned char i;

char message[] = "";

void myputs( char *data)
{
  while (*data)    //loop until we hit a NULL
  {                // Transmit a byte
    while(PIR1bits.TXIF == 0);    //wait until USART TX buffer is not full
    TXREG = *data++;    //send next character, then bump pointer
  };
}

void spunct (void){
    __delay_ms(30);__delay_ms(30);__delay_ms(30);__delay_ms(10);
    __delay_ms(30);__delay_ms(30);__delay_ms(30);__delay_ms(10);
    }

void slinie (void){spunct();spunct();spunct();}

void punct (void){
    LATBbits.LB4=1;
    spunct();
    LATBbits.LB4=0;
    spunct();
}

void linie (void){LATBbits.LB4=1;
    slinie();
    LATBbits.LB4=0;
    spunct();
}

void spatiu (void){spunct();spunct();spunct();spunct();spunct();spunct();spunct();}

void A (void){punct();linie();slinie();}
void B (void){linie();punct();punct();punct();slinie();}
void C (void){linie();punct();linie();punct();slinie();}
void D (void){linie();punct();punct();slinie();}
void E (void){punct();slinie();}
void F (void){punct();punct();linie();punct();slinie();}
void G (void){linie();linie();punct();slinie();}
void H (void){punct();punct();punct();punct();slinie();}
void I (void){punct();punct();slinie();}
void J (void){punct();linie();linie();linie();slinie();}
void K (void){linie();punct();linie();slinie();}
void L (void){punct();linie();punct();punct();slinie();}
void M (void){linie();linie();slinie();}
void N (void){linie();punct();slinie();}
void O (void){linie();linie();linie();slinie();}
void P (void){punct();linie();linie();punct();slinie();}
void Q (void){linie();linie();punct();linie();slinie();}
void R (void){punct();linie();punct();slinie();}
void T (void){linie();slinie();}
void S (void){punct();punct();punct();slinie();}
void U (void){punct();punct();linie();}
void V (void){punct();punct();punct();linie();slinie();}
void W (void){punct();linie();linie();slinie();}
void X (void){linie();punct();punct();linie();slinie();}
void Y (void){linie();punct();linie();linie();slinie();}
void Z (void){linie();linie();punct();punct();slinie();}
void n1 (void){punct();linie();linie();linie();linie();slinie();}
void n2 (void){punct();punct();linie();linie();linie();slinie();}
void n3 (void){punct();punct();punct();linie();linie();slinie();}
void n4 (void){punct();punct();punct();punct();linie();slinie();}
void n5 (void){punct();punct();punct();punct();punct();slinie();}
void n6 (void){linie();punct();punct();punct();punct();slinie();}
void n7 (void){linie();linie();punct();punct();punct();slinie();}
void n8 (void){linie();linie();linie();punct();punct();slinie();}
void n9 (void){linie();linie();linie();linie();punct();slinie();}
void n0 (void){linie();linie();linie();linie();linie();slinie();}

void main (void){
    ADCON1=0b00001111;
    TRISBbits.TRISB4=0;
    LATBbits.LB4=0; // LED off
    i=0;
    OpenUSART (USART_TX_INT_OFF &
            USART_RX_INT_OFF &
            USART_ASYNCH_MODE &
            USART_EIGHT_BIT &
            USART_CONT_RX &
            USART_BRGH_HIGH &
            USART_ADDEN_OFF, 130);
    while(BusyUSART());
   
    while(1){
       
        getsUSART(message, 10);
        while(BusyUSART());
       
        if (message!="") {
        myputs("busy\n\r");
        while(BusyUSART());
       
        stringLength = strlen(message);
        for(i = 0; i < stringLength; ++i){
            if (message[i]=='a') A();
            if (message[i]=='b') B();
            if (message[i]=='c') C();
            if (message[i]=='d') D();
            if (message[i]=='e') E();
            if (message[i]=='f') F();
            if (message[i]=='g') G();
            if (message[i]=='h') H();
            if (message[i]=='i') I();
            if (message[i]=='j') J();
            if (message[i]=='k') K();
            if (message[i]=='l') L();
            if (message[i]=='m') M();
            if (message[i]=='n') N();
            if (message[i]=='o') O();
            if (message[i]=='p') P();
            if (message[i]=='q') Q();
            if (message[i]=='r') R();
            if (message[i]=='s') S();
            if (message[i]=='t') T();
            if (message[i]=='u') U();
            if (message[i]=='v') V();
            if (message[i]=='w') W();
            if (message[i]=='x') X();
            if (message[i]=='y') Y();
            if (message[i]=='z') Z();
            if (message[i]=='1') n1();
            if (message[i]=='2') n2();
            if (message[i]=='3') n3();
            if (message[i]=='4') n4();
            if (message[i]=='5') n5();
            if (message[i]=='6') n6();
            if (message[i]=='7') n7();
            if (message[i]=='8') n8();
            if (message[i]=='9') n9();
            if (message[i]=='0') n0();
            else {}
        }
        //Ready for another transmission from USART
        strcpy(message,""); //string to NULL
        myputs("notbuzy\n\r");
        while(BusyUSART());
       
    }
    }
}


whole lot of code... problem with morse code is that it takes time to blink every character, plus is a difference of time if I transmit ten 0(zero) or ten E's https://upload.wikimedia.org/wikipedia/ ... e_Code.svg

What I can do is or program a separate com terminal who looks at the COM and when he recive "notbuzy" then send a message to be made morsecode
or... simply close the pic USART until that for is done. But with all this the first problem remains getsUSART(message, 10);

Edit: this program is in construction, please ignore variables who I did not use and so on
Entropy
 
Posts: 24
Joined: Fri Sep 04, 2015 7:04 am
Location: Timisoara, Romania

Next

Return to SCI/USART/EUSART

Who is online

Users browsing this forum: No registered users and 9 guests

cron