Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

USART reception in c18

Status
Not open for further replies.

td micro

Member level 5
Joined
Jun 26, 2012
Messages
86
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,929
hi,
i tried one simple communication process USART and pic 18f in c18.but when i input something through key board, i m not getting any output. ie, reading 2 chars typed from a terminal and trying to print back these 2 chars. But I'm having problems when trying to print back. Here is the code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void main()
  {
 
  
char a[2];
int i;
 
  OSCCON =0b01100000;
TRISCbits.TRISC6 = 0;     // set TX (RC6) as output 
  TRISCbits.TRISC7 = 1;     // and RX (RC7) as input
 
 
  OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
                 USART_EIGHT_BIT & USART_SINGLE_RX & USART_BRGH_HIGH, 51);// baude rate 9.6
 
 
            putrsUSART( "\n hallooooooo " );
            Delay10KTCYx(200);
            //putrsUSART( "\r\nPlease Enter your Name\n" );
 
while (1){
        
    // write to the USART                
    putrsUSART("Type 2 chars: ");
    
  //  while(BusyUSART());    
    
    // wait for input
    while (!DataRdyUSART());
 
    // get 2 chars and put in inputstr
    getsUSART(a, 2);
    
    
 
    putrsUSART("\r\nYou typed: ");    
    putsUSART(a);
    putrsUSART("\r\n");
    
 }
 }


could you help me please.. How can i read the chars correctly?
and how string?

Thank you!
 
Last edited by a moderator:

Hi,

Code:
        TRISCbits.TRISC6 = 0;
	TRISCbits.TRISC7 = 1;
	


	/***TX Configuration************/
	TXSTA1bits.CSRC  = 0;      //
	TXSTA1bits.TX9   = 0;      //Selects 8-bit transmission
	TXSTA1bits.TXEN  = 1;      //Transmit is enabled
	TXSTA1bits.SYNC  = 0;      //Asynchronous mode
	TXSTA1bits.SENDB = 0;      //dont Send Sync Break on next transmission (cleared by hardware upon completion)
	TXSTA1bits.BRGH  = 0;      //High speed
	/***RX Configuration************/
	RCSTA1bits.SPEN  = 1;      //Serial port is enabled
	RCSTA1bits.RX9   = 0;      //Selects 8-bit reception
	RCSTA1bits.CREN  = 1;      //Enables the receiver
	RCSTA1bits.ADDEN = 0;      //Disables address detection, all bytes are received and ninth bit can be used as parity bit
	/***BAUDRATE Configuration************/
	BAUDCON1bits.ABDOVF = 0;   //No BRG rollover has occurred
	BAUDCON1bits.RXDTP  = 0;   //Receive data (RXx) is not inverted (active-high)
	BAUDCON1bits.TXCKP  = 0;   //Idle state for transmit (TXx) is a high level
	BAUDCON1bits.BRG16  = 0;   //8-bit Baud Rate Generator – SPBRGHx and SPBRGx
	BAUDCON1bits.WUE    = 0;   //RXx pin is not monitored or the rising edge detected
	BAUDCON1bits.ABDEN  = 0;   //Baufrate Measurement is Disable

	SPBRGH1 =  0;    //EUSART2 Baud Rate Generator Register High Byte
	SPBRG1	= 51;    //EUSART2 Baud Rate Generator Register Low Byte
					 //

try above code instead of

Code:
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_ON & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_SINGLE_RX & USART_BRGH_HIGH, 51);// baude rate 9.6


make necessary changes according to your device.
Regards,
Shreyas.

- - - Updated - - -

Also

try
printf("Type 2 chars: ");

if you have added USART.h
 

thanks.thank you very much for your reply..
in my program, i can print hallo on hyper terminal. problem is in reception. ie, in reading character or string. ie, when i type in keyboard, it is not displaying any thing..
while (!DataRdyUSART());
in this step output flow become stuck. it is not going out.so which step i have to use? please tell me...
 

remove while (!DataRdyUSART());
and try with above code with manual UART config.
 

thanks for quick reply.....
i tested..eventhough, i m not gettind character.. i m getting hallo on hyper.but i couldnt read anything from keyboard...
here is my code


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
void main()
  {
 
  
char a;
 
  OSCCON =0b01100000;
 
     TRISCbits.TRISC6 = 0;
    TRISCbits.TRISC7 = 1;
    
 
 
    /***TX Configuration************/
    TXSTAbits.CSRC  = 0;      //
    TXSTAbits.TX9   = 0;      //Selects 8-bit transmission
    TXSTAbits.TXEN  = 1;      //Transmit is enabled
    TXSTAbits.SYNC  = 0;      //Asynchronous mode
    TXSTAbits.SENDB = 0;      //dont Send Sync Break on next transmission (cleared by hardware upon completion)
    TXSTAbits.BRGH  = 1;      //High speed
    /***RX Configuration************/
    RCSTAbits.SPEN  = 1;      //Serial port is enabled
    RCSTAbits.RX9   = 0;      //Selects 8-bit reception
    RCSTAbits.CREN  = 1;      //Enables the receiver
    RCSTAbits.ADDEN = 0;      //Disables address detection, all bytes are received and ninth bit can be used as parity bit
    /***BAUDRATE Configuration************/
    BAUDCTLbits.ABDOVF = 0;   //No BRG rollover has occurred
    BAUDCTLbits.DTRXP  = 0;   //Receive data (RXx) is not inverted (active-high)
    BAUDCTLbits.CKTXP  = 0;   //Idle state for transmit (TXx) is a high level
    BAUDCTLbits.BRG16  = 0;   //8-bit Baud Rate Generator – SPBRGHx and SPBRGx
    BAUDCTLbits.WUE    = 0;   //RXx pin is not monitored or the rising edge detected
    BAUDCTLbits.ABDEN  = 0;   //Baufrate Measurement is Disable
 
    SPBRGH =  0;    //EUSART2 Baud Rate Generator Register High Byte
    SPBRG   = 51;    //EUSART2 Baud Rate Generator Register Low Byte
 
    printf("hallooo");
// write to the USART                
    putrsUSART("Type 2 chars: ");
    
  while(BusyUSART());    
    
    // wait for input
  // while (!DataRdyUSART());
 
 
   // get 2 chars and put in inputstr
   // getsUSART(a, 2);
    
    a=getcUSART(); 
 
    putrsUSART("\r\nYou typed: ");    
    putsUSART(a);
    putrsUSART("\r\n");
                    
 
 
while(1);
}

 
Last edited by a moderator:

Code:
if(PIR1bits.RC1IF == 1)
			{
				a = RCREG1;
				
			}	
TXREG1 =a;

use this code instead of
putrsUSART("Type 2 chars: ");
and try
 
i tried this
if(PIR1bits.RCIF == 1)
{
a = RCREG;

}
TXREG =a;
then also same thing happening.. character is not reading from keyboard..plz help me to solve this problem...
if i use "while (!DataRdyUSART());" then the flow stuck there. if i m not using that flow goes to while(1);
nothing is read
 

hello,


It is a good way to use C18 Libraries..
and interrupt mode for caracters receiving.

Here is a tested programme for a PUC18F46K22
You can receive a string (from Keyboard) when CR terminator is encountured.
My example use Q=10Mhz and 19200 bauds
you can easly adapt it.
Maybe you have to use OpenUSART instead of Open1USART ...

Code:
//03/07/2012
//TEST ok  en lecture

#include "p18f46k22.h"

#include "stdio.h"
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>   // pour fonctions UART HARDWARE
#include <adc.h>
#include <ctype.h>
#include <portb.h>
#include <timers.h>
#include <math.h>

// configuration bits set in code !
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF
#endif
#pragma config IESO=OFF,PWRTEN=ON,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,CCP3MX=PORTE0,T3CMX=PORTC0,P2BMX=PORTC0
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

// config vue sur Pickit3 : 25FF 3F1F BFFF FF85 C00F E00F 400F

#ifndef Byte
#define Byte unsigned char
#endif


#define DureeCycle 10 // * 1 diziemes de secondes => 1,0sec

#define CLS 12
#define BS 8
#define TAB 9
#define CR 13
#define LF 10
#define Bell 7
#define ON 0	// logique inverse avec led tiree au +5V
#define OFF 1

#define MAX_LEN 80


unsigned int  i,j,k;
unsigned int dummy;
unsigned int j1;
unsigned int M;
unsigned int BRG_REG;
volatile int i1;
volatile char  c1;
volatile int count;
long L;

char Texte[MAX_LEN]="18F46K22 test RS232 C18\r\n 19200bds Quartz 10Mhz \r\n\0";
char *txt=&Texte[0];
char F[9]="00000000";  // maxi 20MHz  "4294967296";
unsigned char Entree[17]="1234567890123456";
unsigned char *valtxt;
char buffer[MAX_LEN]="00000000000000000000000";	// buffer de reception
char received;
char *ptlec;		// pointeur de lecture
char *ptecr;		// pointeur d'ecriture
unsigned int CptCars;
unsigned int SaisieOk;


 struct chbits {   // 8 flags
                 unsigned FrameErr:1;
                 unsigned Over:1;
                 unsigned Elligible:1;
                 unsigned Togle:1;
                 unsigned Blc:1;
                 unsigned standby:1;
                 unsigned Fill:1;
                 unsigned Lcd:1;
               }Drapeaux ;

 union
{  unsigned char Fanion;
   struct chbits Drapeaux ;
} Flags;

#define OUT_RS232 Drapeaux.Lcd=0;
#define OUT_LCD Drapeaux.Lcd=1;

//--------- RS232 UART HARDWARE --------------

void Init_UART1(void);
int PutStr_RS(char *s);
void Put_RS(char * untel);
void CRLF();
void Write_Word(unsigned int M,char Sign);
void Tempo(long val);
void Remplis_N_blancs(Byte N);
void Init_Hardware(void) ;

void InterruptHandlerHigh (void);

//gestionnaire d'interruption
//------------------------------
// High priority interrupt vector
#pragma code InterruptVectorHigh = 0x08
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

// High priority interrupt routine
#pragma code
#pragma interrupt InterruptHandlerHigh
void InterruptHandlerHigh ()
{
  static char i ; // doit ętre statique pour conserver sa valeur entre les IT
  char C ;
//======== SERIAL======================
if(PIR1bits.RCIF) // si un car arrive
   {
   C =Read1USART(); // le lire => RAZ  RCIF
   if(RCSTAbits.FERR || RCSTAbits.OERR)
     {
      RCSTAbits.CREN = 0 ; RCSTAbits.CREN= 1 ;
     }
    while(Busy1USART()); // par sécurité
    if (PORTBbits.RB1==0) Write1USART(C); // echo
     if( C == BS)
        {
         if(i>0){
             i--;  }
        }
      else
      {
      if(C != CR && i<MAX_LEN)
      {
        buffer[i++]=C ; // stockage
        }else
        {
         buffer[i]='\0'; // fin de chaîne si CR
         i=0;
         received =1;
       }
      }
    }
} // fin de routine interrupts


void Init_UART1(void)
{
// USART_BRGH_HIGH,32
// init 38400Bd a 20Mhz
// init 19200Bd a 10Mhz avec IT en rx
// voir para 18.5 spec sheet DS41159B page 186
Open1USART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,32  
);
INTCONbits.PEIE = 0 ; // autorisation des IT des périphériques
INTCONbits.GIE = 0 ; // autorisation globale des IT
}

int PutStr_RS(char *s)
{
	int n;
	for (n = 0; *s; n++, s++)
	{
       	Put_RS(*s);
     	}
	return n;
}

void Put_RS(char * untel)
{
   	while(Busy1USART());
   	Write1USART(untel);
}

void CRLF()
{
  Put_RS(CR);  Put_RS(LF);
  }


void Tempo(long val)
{
while(val>0){val--;}
}

void main()
{
char a;
//OSCCON =0b01100000;
ANSELC=0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;

Init_UART1();

Put_RS(CLS);   // clear  Vbray terminal Screen
Tempo(150000L);

// init pour interruptions ----
  INTCONbits.GIE = 1;    // active global interrupt
  INTCONbits.GIEL=1;
  RCSTA1bits.CREN= 1 ;
  PIE1bits.RC1IE = 1;
  INTCONbits.PEIE = 1;   // autorisation des IT des périphériques
  
buffer[0]=0;
received=0;
;
k=PutStr_RS(txt);
printf("hallooo\r\n");
// write to the USART 
printf("Type some chars and CR to send them: ");

while(1)
{ 
 if (received==1) // test if reception occured
 {
  j=0;   
  printf("\r\nYou typed: ");    
  while (buffer[j]!=0)   
  {
    a=buffer[j]; 
    Put_RS(a);
    j++;
    Tempo(1000L); // you can remove it
  }
  received=0;
  buffer[0]=0;
 } 
Tempo(15000L); // you can remove it
}
}
/* results on Vbray terminal
18F46K22 test RS232 C18
 19200bds Quartz 10Mhz 
hallooo
Type some chars and CR to send them: 
You typed: 00001
You typed: 00010
You typed: 00100
You typed: 01000
You typed: 05000
You typed: 10000
You typed: 15000
You typed: 20000
You typed: 21000
You typed: S
You typed: C
You typed: T
You typed: D
You typed: R
You typed: X
You typed: How are you
*/
 

thanks.....
but this program is too large..... i coudnt follow it easily...so please please can you give some simple example to read from keyboard...please..ie, using getsUSART() and putsUSART ....
 

hello


You can test this Simplest example ..
it's only by pooling .. no use of interrupt
but Simple doesen't cover all cases,
so if you type car by car on keyboard, it's OK .. because low speed for car entry
but if you want to send a string of more than 3 car ..you get Overun error.

I repeat, best way is to use interrupt and buffer for string receiving.

What is your PIC18 reference ?

some pic have no Fifo ! so maybe probleme with 2 collapsed car instead of 3.



Code:
//https://www.edaboard.com/threads/258142/
//05/07/2012
// avec Pickit3 
// TEST OK if less than 3 cars collapsed 
// because of FIFO size=3 on 18F46K22 


#include "p18f46k22.h"

#include "stdio.h"
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>   // pour fonctions UART HARDWARE
#include <ctype.h>
#include <portb.h>


// configuration bits set in code !
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF
#endif
#pragma config IESO=OFF,PWRTEN=ON,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,CCP3MX=PORTE0,T3CMX=PORTC0,P2BMX=PORTC0
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

// config vue sur Pickit3 : 2200 3C18 9900 0084 C00F E00F 400F

#ifndef Byte
#define Byte unsigned char
#endif


#define CLS 12
#define TAB 9
#define CR 13
#define LF 10

#define Led_Rouge    PORTAbits.RA4
#define Allume 0
#define Eteint 1



unsigned int  i,j,k;
volatile int count;
long L;


//--------- RS232 UART HARDWARE --------------

void Init_UART1(void);
int PutStr_RS(char *s);
void Put_RS(char * untel);
void CRLF();
void Tempo(long val);




void Init_UART1(void)
{
// USART_BRGH_HIGH,32
// init 38400Bd a 20Mhz
// init 19200Bd a 10Mhz avec IT en rx
// voir para 18.5 spec sheet DS41159B page 186
Open1USART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,32  
);
INTCONbits.PEIE = 0 ; // autorisation des IT des périphériques
INTCONbits.GIE = 0 ; // autorisation globale des IT
}

int PutStr_RS(char *s)
{
	int n;
	for (n = 0; *s; n++, s++)
	{
       	Put_RS(*s);
     	}
	return n;
}

void Put_RS(char * untel)
{
   	while(Busy1USART());
   	Write1USART(untel);
}

void CRLF()
{
  Put_RS(CR);  Put_RS(LF);
  }


void Tempo(long val)
{
while(val>0){val--;}
}

void main()
{
char a;
unsigned char d;

//OSCCON =0b01100000;
ANSELA=0;
TRISA = 0b00001111 ; 
TRISAbits.TRISA4 = 0;   // led rouge
LATA = 0x00;

ANSELC=0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
Led_Rouge=Allume;

Init_UART1();

Put_RS(CLS);   // clear  Vbray terminal Screen
Tempo(100000L);

// init pour interruptions ----
INTCONbits.GIE = 0;    // active global interrupt
  INTCONbits.GIEL=0;
  RCSTA1bits.CREN= 1 ;
  PIE1bits.RC1IE = 0;
  INTCONbits.PEIE = 1;   // autorisation des IT des périphériques  


CRLF();
printf("hallooo\r\n");
// write to the USART 
printf("Type some chars and CR to send them: ");


while(1)
{ 
if(PIR1bits.RCIF)  // si un car arrive
   {
   a =Read1USART(); // le lire => RAZ  RCIF
   Led_Rouge=Allume; 
   printf("\r\nYou typed: ");    
   Put_RS(a);
  
   } 
// test if Overrun occured !
 if (RCSTA1bits.OERR==1) Led_Rouge=Allume; else Led_Rouge=Eteint;
}
}

/*

hallooo
Type some chars and CR to send them: 

You typed: C    2 cars  C + CR => ok
You typed: 

You typed: R   2 cars  R + CR => ok
You typed: 

You typed: X    2 cars  X + CR => ok
You typed: 

You typed: T    1 car  -->  OK
You typed: V    1 car  -->  ok
You typed: 0    number 0001 => Overrun => led allways ON
You typed: 0
You typed: 0
*/
 
thank you
actually i m new to this.i dont have any c18 reference.if u dont mind, plz give me some c18 reference links. it is necessary for me.
i got one gsm based project.so before starting that, i thought tru hyperterminal to read and write some strings..
in your pgm you are using pointers, ifndef, etc.. i thought read write pgm is very simple.by using DataRdyUSART(), can we read a character or string in few steps? any way i m going to study your pgm.after that i will do it in pic.

thank you so much....
 

on microchip site
look at this pdf document
MPLAB® C18 C COMPILER LIBRARIES
DS51297F © 2005 Microchip Technology In

The above example is recuced to the minimum size !
test of PIRbits.RCIF is the same way as DataReadyUsart()
but for reading string you must use a buffer !

gessUasrt(buffer,size) function , need to know the size of string before to get it .. it is not convenient
anyway here is another example for string

the result will be displayed only after typing 6 cars.
but if the terminal send more than 6 cars, OVERRUN problem.
see and study the solution with interrupt & buffer for your project.






Code:
// init part
char Texte[MAX_LEN]="18F46K22 test simple RS b  en MPLAB C18\r\n 05/07/2012 rev a\n\r19200bds Quartz 10Mhz \r\n\0";
char *txt=&Texte[0];



// main part
void main()
{

...

while(1)
{ 
txt=&Texte[0];  // init buffer  
Texte[0]=0;
if(PIR1bits.RCIF)  // si un car est arrive
   {
   gets1USART(txt,6); // attend la reception de 6 cars
   Led_Rouge=Allume; 
   printf("\r\nYou typed: "); 
   Texte[6]=0; // rajout terminateur de string   
   PutStr_RS(txt);
  
   } 
// test if Overrun occured !
 if (RCSTA1bits.OERR==1) Led_Rouge=Allume; else Led_Rouge=Eteint;
}
}
 

hi, thanks......
i run that 1st pgm,that you have given me to read sring.(in #8). but in that also same problm is happening.. from keyboard nothing is detecting.. when the flow reaches
void Tempo(long val)
{
while(val>0){val--;}
}

in that while loop,the flow is not exit.it is continuing in that while loop only.
im getting the output like

18F46K22 test RS232 C18
19200bds Quartz 10Mhz
hallooo
Type some chars and CR to send them:

- - - Updated - - -

like that i run that pgm given in #10, to read character..but for that also same thing happening. USART is not reading from keyboard....
the flow halt on the following last step

// test if Overrun occured !
if (RCSTAbits.OERR==1) Led_Rouge=Allume; else Led_Rouge=Eteint;

the same thing is happening. does this mean the mistake is in any of my settings... i dont know. plz help ne to sove this problem....
thanks
 

hi td micro,

it is nice and easy to use C18 library functions.

but if we want to find the problem and debug the code then we should do things manuallly e.g. user deined code.
It might be stucked any where in your library function,..

for receiving character through key board and display it back try following code dont use any other then this code.


Code:
unsigned char a;
	
	TRISCbits.TRISC6 = 0;
	TRISCbits.TRISC7 = 1;
	


	/***TX Configuration************/
	TXSTA1bits.CSRC  = 0;      //
	TXSTA1bits.TX9   = 0;      //Selects 8-bit transmission
	TXSTA1bits.TXEN  = 1;      //Transmit is enabled
	TXSTA1bits.SYNC  = 0;      //Asynchronous mode
	TXSTA1bits.SENDB = 0;      //dont Send Sync Break on next transmission (cleared by hardware upon completion)
	TXSTA1bits.BRGH  = 0;      //High speed
	/***RX Configuration************/
	RCSTA1bits.SPEN  = 1;      //Serial port is enabled
	RCSTA1bits.RX9   = 0;      //Selects 8-bit reception
	RCSTA1bits.CREN  = 1;      //Enables the receiver
	RCSTA1bits.ADDEN = 0;      //Disables address detection, all bytes are received and ninth bit can be used as parity bit
	/***BAUDRATE Configuration************/
	BAUDCON1bits.ABDOVF = 0;   //No BRG rollover has occurred
	BAUDCON1bits.RXDTP  = 0;   //Receive data (RXx) is not inverted (active-high)
	BAUDCON1bits.TXCKP  = 0;   //Idle state for transmit (TXx) is a high level
	BAUDCON1bits.BRG16  = 0;   //8-bit Baud Rate Generator – SPBRGHx and SPBRGx
	BAUDCON1bits.WUE    = 0;   //RXx pin is not monitored or the rising edge detected
	BAUDCON1bits.ABDEN  = 0;   //Baufrate Measurement is Disable

	SPBRGH1 =  0;    //EUSART2 Baud Rate Generator Register High Byte
	SPBRG1	= 51;    //EUSART2 Baud Rate Generator Register Low Byte

	while(1)
	{
		if(PIR1bits.RC1IF == 1)
			{
				a = RCREG1;	//receives character in a
				TXREG1 =a;	//echo transmits the same character
				
			}	
		
	}
 

hello

does this mean the mistake is in any of my settings...

the TEMPO value depend of your clock ! in my example i have Q=10Mhz (PLL off)
you can remove this Tempo if too long waiting ..

if you are blocked into
if (RCSTAbits.OERR==1) Led_Rouge=Allume; else Led_Rouge=Eteint;
remove it !
but there is no sense to be blocked if you put RA4 on or off !
so maybe the main problem could be environnement because :

- You never show us what type of PIC you are using PIC18 ??????
- What is your Configuration Bits
you can copy the configuration bits that appears on Pickit3 programm display
wich is the one putted into your PIC.

The examples i send to you, are not Simulate, but runing in real world!
but writen for a 18F46K22 .

did you test the terminal itself by removing the PIC from his support
and link RC6 and RC7 pins on the support...
to get echo of what you are typing.
 

we are using 18f45k20
i set the config bits like

Code:
# pragma config FOSC=INTIO67
# pragma config WDTEN = OFF
# pragma config LVP = OFF
 #pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF

my clk is 8mhz, and our baud rate is 9600

- - - Updated - - -

i write like this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <p18f45k20.h>
#include "stdio.h"
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>   // pour fonctions UART HARDWARE
#include <ctype.h>
#include <portb.h>
 
 
# pragma config FOSC=INTIO67
# pragma config WDTEN = OFF
# pragma config LVP = OFF
 #pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config MCLRE = OFF
#pragma config PBADEN = OFF
 
#ifndef Byte
#define Byte unsigned char
#endif
 
 
#define CLS 12
#define TAB 9
#define CR 13
#define LF 10
 
#define Led_Rouge    PORTAbits.RA4
#define Allume 0
#define Eteint 1
 
 
 
unsigned int  i,j,k;
volatile int count;
long L;
 
 
//--------- RS232 UART HARDWARE --------------
 
void Init_UART1(void);
int PutStr_RS(char *s);
void Put_RS(char * untel);
void CRLF();
void Tempo(long val);
 
 
 
 
void Init_UART1(void)
{
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_HIGH,51  
);
INTCONbits.PEIE = 0 ; // autorisation des IT des périphériques
INTCONbits.GIE = 0 ; // autorisation globale des IT
}
 
int PutStr_RS(char *s)
{
    int n;
    for (n = 0; *s; n++, s++)
    {
        Put_RS(*s);
        }
    return n;
}
 
void Put_RS(char * untel)
{
    while(BusyUSART());
    WriteUSART(untel);
}
 
void CRLF()
{
  Put_RS(CR);  Put_RS(LF);
  }
 
 
void Tempo(long val)
{
while(val>0){val--;}
}
 
void main()
{
char a;
unsigned char d;
OSCCON =0b01100000;
//ANSELA=0;
TRISA = 0b00001111 ; 
TRISAbits.TRISA4 = 0;   // led rouge
LATA = 0x00;
 
//ANSELC=0;
TRISCbits.TRISC6 = 0;
TRISCbits.TRISC7 = 1;
Led_Rouge=Allume;
 
Init_UART1();
 
Put_RS(CLS);   // clear  Vbray terminal Screen
Tempo(100000L);
 
// init pour interruptions ----
INTCONbits.GIE = 0;    // active global interrupt
  INTCONbits.GIEL=0;
  RCSTAbits.CREN= 1 ;
  PIE1bits.RCIE = 0;
  INTCONbits.PEIE = 1;   // autorisation des IT des périphériques  
 
 
CRLF();
printf("hallooo\r\n");
// write to the USART 
printf("Type some chars and CR to send them: ");
 
 
while(1)
{ 
if(PIR1bits.RCIF)  // si un car arrive
   {
   a =ReadUSART(); // le lire => RAZ  RCIF
   Led_Rouge=Allume; 
   printf("\r\nYou typed: ");    
   Put_RS(a);
  
   } 
// test if Overrun occured !
 if (RCSTAbits.OERR==1) Led_Rouge=Allume; else Led_Rouge=Eteint;
}
}



- - - Updated - - -




yes.. i short that rx tx pins and type on keyboard in to hyperterminal.then i got the echo

- - - Updated - - -



i tried shreyas_patel21's pgm also. but its also not receiving character...

Code:
if(PIR1bits.RC1IF == 1)

not entering to this line
 
Last edited by a moderator:

hello

Even the short links RC6 & RC7 is working
there is another doubt regarding you are using a
18F45K20 in nanoWatt technology , so power supply is 3.6V maxi !

18F46K22 is 3,0 up to 5Volts power supply
And i know i can get probleme if using 3,2V power supply with my DS275 RS232 converter .
witch can run only above 3,8V , not under !

what is your power supply ?

waht about the hardware interface to RS232 ?
did you adapt the correct levels ?
 

our power supply is 3.2v. we are giving through pickit3.there is no external supply..we are using max3232 and the pic demo board.there is nothing else.i just copied and paste your pgm without that much change for our PIC.that may be the problem.so i will check and mahe changes for my pic.

i have some doubts abt interrupts.. can we pass a string without using interrupt?
 

hello


can we pass a string without using interrupt?

example #12 doesn't use interrupt treatment , but test only flag interrupt.

Every 18F pic have there own particularity, pay attention about all Configuraton bits .
Test is CCPx , ANalog can interfere with RC6 bit config.

see if you need to disable comparators
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;


I tried the example of
shreyas_patel21
it didn't work also in my configuration.
no correct output .. i add test of transmit buffer befor to send the caractere
it' s why i use Put_RS(car); instead of direct register
After no receiving any caractere ...
So i added ANSELC=0 ;
and it's OK.

it's confirm to be carrefull with environnment and type of PIC18Fxxxx !

try again this , but with your Config bits

Code:
//https://www.edaboard.com/threads/258142/
// Re: USART reception in c18
//07/07/2012
// avec Pickit3
// reception et echo d'un car


#include "p18f46k22.h"

#include "stdio.h"
#include <stdlib.h>
#include <delays.h>
#include <string.h>
#include <usart.h>   // pour fonctions UART HARDWARE
#include <ctype.h>
#include <portb.h>


// configuration bits set in code !
#ifdef OSCILLATEUR_INTERNE
#pragma config FOSC = INTIO67, FCMEN = OFF, PLLCFG=OFF
#else
#pragma config FOSC = HSHP, PLLCFG=OFF, PRICLKEN=ON ,FCMEN=OFF
#endif
#pragma config IESO=OFF,PWRTEN=ON,BOREN=OFF,WDTEN=OFF,CCP2MX=PORTC1
#pragma config PBADEN=OFF,CCP3MX=PORTE0,T3CMX=PORTC0,P2BMX=PORTC0
#pragma config MCLRE=EXTMCLR,STVREN=OFF,LVP=ON,XINST=OFF,DEBUG=OFF
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF
#pragma config CPB = OFF, CPD = OFF
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF
#pragma config EBTRB = OFF

// config vue sur Pickit3 :
void Put_RS(char * untel)
{
   	while(Busy1USART());
   	Write1USART(untel);
}

// ---------------------------------------------
//https://www.edaboard.com/threads/258142/
//shreyas_patel21 

void main()
{
unsigned char a;

   ANSELC=0;
    
	TRISCbits.TRISC6 = 0;
	TRISCbits.TRISC7 = 1;



	/***TX Configuration************/
	TXSTA1bits.CSRC  = 0;      //
	TXSTA1bits.TX9   = 0;      //Selects 8-bit transmission
	TXSTA1bits.TXEN  = 1;      //Transmit is enabled
	TXSTA1bits.SYNC  = 0;      //Asynchronous mode
	TXSTA1bits.SENDB = 0;      //dont Send Sync Break on next transmission 
	TXSTA1bits.BRGH  = 0;      //High speed
	/***RX Configuration************/
	RCSTA1bits.SPEN  = 1;      //Serial port is enabled
	RCSTA1bits.RX9   = 0;      //Selects 8-bit reception
	RCSTA1bits.CREN  = 1;      //Enables the receiver
	RCSTA1bits.ADDEN = 0;      //Disables address detection
	/***BAUDRATE Configuration************/
	BAUDCON1bits.ABDOVF = 0;   //No BRG rollover has occurred
// 2 lines commented because error compil generation	
//	BAUDCON1bits.RXDTP  = 0;   //Receive data (RXx) is not inverted (active-high)
//	BAUDCON1bits.TXCKP  = 0;   //Idle state for transmit (TXx) is a high level
	BAUDCON1bits.BRG16  = 0;   //8-bit Baud Rate Generator – SPBRGHx and SPBRGx
	BAUDCON1bits.WUE    = 0;   //RXx pin is not monitored or the rising edge detected
	BAUDCON1bits.ABDEN  = 0;   //Baufrate Measurement is Disable

	SPBRGH1 =  0;    //EUSART1 Baud Rate Generator Register High Byte
	SPBRG1	= 16;    //EUSART1 Baud Rate Generator Register Low Byte    9600bauds Q=10Mhz
    Put_RS('H');
     Put_RS('E');
        Put_RS('L');
            Put_RS('L');
                Put_RS('O');
                    Put_RS(10);    
                        Put_RS(13);

    
	while(1)
	{
		if(PIR1bits.RC1IF == 1)
			{
    			Put_RS('*');	//yes! i get it
				a = RCREG1;	//receives character in a
				    Put_RS(a);	//echo transmits the same character

			}

	}
}
 

thanks.................
i got the out put.... now i can read characters from keyboars.....
thanks thanks a lot ..

but now i m getting the output like this
€HELLO
characters
here one extra " € " is coming. why that extra character is coming??

and what is the meaning of
Put_RS(13);
what is that 13 means ? i know 10 is for new line.
any way thank you for helping me...

- - - Updated - - -

now i tried to read string..
for that i use getsUSART(a,5);
putsUSART(a);
but the problem is if i enter 3 character, there will not be any response. for displaying, we have to enter 5 characters. more than 5 is also not evaluated...
so if we enter strings with unknown length, how can we displayed??
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top