abinesh raja
Newbie level 6
- Joined
- Mar 3, 2013
- Messages
- 14
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Location
- INDIA
- Activity points
- 1,374
#include<htc.h>
#include<pic18.h>
unsigned char e;
void tx(unsigned char c)
{
while(!TRMT1);
TXREG1='c';
}
void main()
{
TRISC6=0;
TRISC7=1;
BAUDCON1=0x40;
SPBRG1=25;
TXSTA1=0x06;
RCSTA1=0x80;
ANSELC=0;
GIE=1;
PEIE=1;
RC1IF=1;
TXEN1=1;
CREN1=1;
while(1)
{
while(!RC1IF)
{
e=RCREG1;
DelayMs(100);
tx(e);
}
}
}
#include<htc.h>
#include<pic18.h>
unsigned char e;
void tx(unsigned char c)
{
while(!TRMT1);
TXREG1='c';
}
void main()
{
TRISC6=0;
TRISC7=1;
BAUDCON1=0x40;
SPBRG1=25;
TXSTA1=0x06;
RCSTA1=0x80;
ANSELC=0;
GIE=1;
PEIE=1;
RC1IF=1;
TXEN1=1;
CREN1=1;
while(1)
{
while(!RC1IF);
e=RCREG1;
DelayMs(100);
tx(e);
}
}
TRMT is a read only status bit. It can't be cleared.After while(!TRMT1); you have to clear TRMT1 using TRMT1 = 0; otherwise it transmits only one character.
RCIF is autocleared when reading RCREG.You should also clear RC1IF inside the while(!RC1IF) loop.
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 #include <htc.h> //__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & WRT_ON & DEBUG_OFF); #define _XTAL_FREQ 16000000 //MHz //Function Prototyper void InitUART(void); void SendByte(unsigned char); unsigned char ReceiveByte(void); void SendString(const unsigned char*); void interrupt ISR(void) { if(RC1IF) //If UART Rx Interrupt { if(OERR) //if over run error, then reset the receiver { CREN = 0; CREN = 1; } SendByte(RCREG1); } } void InitUART(void) { TRISC6 = 0; //TX Pin TRISC7 = 1; //RX Pin TRISD6 = 0; TRISD7 = 1; TXSTA1 = 0b00100110; RCSTA1 = 0b10010000; BAUDCON1bits.BRG16 = 0; SPBRG1 = 0x67; TXSTA2 = 0b01100100; RCSTA2 = 0b00001001; BAUDCON2bits.BRG16 = 0; SPBRG2 = 0x67; INTCONbits.PEIE = 1; INTCONbits.GIEL = 1; INTCONbits.GIE = 1; INTCONbits.GIEH = 1; PIE1bits.RC1IE = 1; PIE1bits.TX1IE = 1; PIE3bits.RC2IE = 1; PIE3bits.TX2IE = 1; } void SendByte(unsigned char Byte) //Writes a character to the serial port { while(!TX1IF); //wait for previous transmission to finish TXREG1 = Byte; } unsigned char ReceiveByte(void) //Gets a character from the serial port { if(OERR) //if over run error, then reset the receiver { CREN = 0; CREN = 1; } while(!RC1IF); //wait for transmission to receive return RCREG1; } void SendString(const unsigned char* st) { while(*st) SendByte(*st++); } void main(void) { InitUART(); GIE = 1; //Enable global interrupts PEIE = 1; //Enable Peripheral Interrupts SendString("Hello World!"); while(1) { ; //Do nothing, as Received character is echoed back in the ISR //SendByte(ReceiveByte()); //Echo Back } }
void gps()
{
while(!RC1IF)
tx(RCREG1);
}
void gps()
{
while(!RC1IF)
tx(RCREG1);
}
void tx(unsigned char c)
{
while(!TRMT1);
TXREG1='c';
}
#include<htc.h>
#include<pic18.h>
void main()
{
TRISC6=1;
TRISC7=1;
BAUDCON1=0x40;
SPBRG1=25;
ANSELC=0;
TXSTA1=0x06;
RCSTA1=0x80;
CREN1=1;
TXEN1=1;
while(1)
{
while(!RC1IF);
tx(RCREG1);
}
}
void tx(unsigned char c)
{
while(!TRMT1);
TXREG1=c;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?