VoltVolt
Junior Member level 1
- Joined
- Sep 15, 2013
- Messages
- 15
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 124
PIC18F45K22 EUSART serial communication(transmit only) help...
hi everyone, just want to ask is there any problem from the below codes?
I just want to print out "helloword" by using serial communication
I'm using PIC18F45K22 and MPLABX IDE compiler...
The codes can be smoothly run but it cannot be print out...
Btw, i don't have interrupt, it is just a test code....
hi everyone, just want to ask is there any problem from the below codes?
I just want to print out "helloword" by using serial communication
I'm using PIC18F45K22 and MPLABX IDE compiler...
The codes can be smoothly run but it cannot be print out...
Btw, i don't have interrupt, it is just a test code....
Code:
#include <htc.h>
#include <string.h>
char sendBuffer[11];
char sendPosition=0;
void sendUartString(const char string[10]);
void init()
{
// Asynchronous Transmission Setup
// 1. Initialize the SPBRGHx:SPBRGx register pair and the BRGH and BRG16 bits to achieve the desired baud rate
SPBRGH2=0X01;
SPBRG2=0XA0;
TXSTA2bits.BRGH=1;
BAUDCON2bits.BRG16=1;
// 2. Set the RXx/DTx and TXx/CKx TRIS controls to ?1?.
TRISB = 0b11000000; //set TX2 and RX2 as output
// 3. Enable the asynchronous serial port by clearing the SYNC bit and setting the SPEN bit.
ANSELB = 0; //disable ANSEL
SYNC2 = 0; //asynchronous mode
SPEN2 = 1; //enable EUSART
// 4. If 9-bit transmission is desired, set the TX9 control bit. A set ninth data bit will indicate that the eight Least Significant data bits are an address when the receiver is set for address detection.
// 5. Set the CKTXP control bit if inverted transmit data polarity is desired.
// 6. Enable the transmission by setting the TXEN control bit. This will cause the TXxIF interrupt bit to be set.
TXEN2 = 1;
// 7. If interrupts are desired, set the TXxIE interrupt enable bit. An interrupt will occur immediately provided that the GIE/GIEH and PEIE/GIEL bits of the INTCON register are also set.
// 8. If 9-bit transmission is selected, the ninth bit should be loaded into the TX9D data bit.
// 9. Load 8-bit data into the TXREGx register. This will start the transmission.
}
void main()
{
init();
//now you want to send something, so first load a string into the sendBuffer
while(1)
{
//sendUartString("helloworld");
}
}
void sendUartString(const char string[10])
{
strcpy(sendBuffer,string); //please check the correct command,copy string[] to sendBuffer[]
sendPosition=0; //start sending first char
while((sendPosition!= strlen(sendBuffer)) && sendPosition<10 )
{
if(TX2IF==1)//if Transmit register is empty
{
TX2REG=sendBuffer[sendPosition]; //send one more char
sendPosition++;
}
}
}