![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| Hi, I am trying to do a small program to get the USART to work. I want to write the letter "A" out on the RS232 port. I have downloaded a sample hex file and loaded into the PIC to test the RS-232 comms to my pc. All is working except my version of code. I am getting funny asterix symbols out the comms port. Have I left something out of the code. I have set up the registers as per the datasheet and I am only transmitting at the moment. Code: #include <p18cxxx.h>
void init_usart(void) // Initialise the USART
{
TXSTA=0x24; // Asynch,8bits,tx enb
RCSTA=0x90;
SPBRG=0x12; // BaudRate=9600 @ 20Mhz as Table
RCSTAbits.SPEN=1; // Enable Serial Port
}
void write_usart(void) //Write to the USART
{
while(TXSTAbits.TRMT==1)
TXREG='A';
}
void main (void)
{
TRISC=0x80; //Set RC7=Input, RC0:6 as Output
init_usart(); // Initialise the USART
write_usart();
} Mike | |
| |
| | (permalink) |
| Code: SPBRG = 0x12; Are you sure about that value? Shouldn't it be 0x20? | |
| |
| | (permalink) |
| My bad.... I forgot to take out the 0x The correct one is Code: SPBRG=12; // BaudRate=9600 @ 20Mhz as Table16-5 | |
| |
| | (permalink) |
| Oh, the BRGH bit is set, I missed that, sorry! We must refer to table 16-5, right. The correct value is 129 (0x81) for 9600 baud, isn't it? | |
| |
| | (permalink) |
| ok...i need glasses.... I was looking at the 96Kbps and not 9.6Kbps. (thanks for that!!) I have changed the SPBRG value to 129 however now i am getting nothing on the RS232, not even the symbols. im not to sure about my "while" loop... I'm new to C. Code: #include <p18cxxx.h>
void init_usart(void) // Initialise the USART
{
TXSTA=0b00100100; // Asynch,8bits,tx enb
RCSTA=0b10010000;
SPBRG=129; // BaudRate=9600 @ 20Mhz as Table
RCSTAbits.SPEN=1; // Enable Serial Port
}
void write_usart(void) //Write to the USART
{
while(TXSTAbits.TRMT==1)
TXREG='A';
}
void main (void)
{
TRISC=0x80; //Set RC7=Input, RC0:6 as Output
init_usart(); // Initialise the USART
write_usart();
} | |
| |
| | (permalink) |
| Code: void write_usart(void)
{
TXREG='A'; // copy 'A' into the buffer
while(PIR1bits.TXIF==0); // wait until all bits are transmitted
} | |
| |
| | (permalink) |
| Aha!!! Thats it... its working now. I'm getting an infinite amount of A's on the screen. How would I send a complete word "Hello" to the usart and then stop the usart from sending? Could I use an array? THanks for your help..!!! | |
| |
| | (permalink) | ||
| Quote:
Quote:
| |||
| |
| | (permalink) |
| Thanks. I shall look into the array's.... | |
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| Title | Starter | Forum | Replies | Latest |
| Drive 33 Servos With One PIC + USART | wschroeder | Micro Controllers | 45 | 25th October 2008 10:10 AM |
| RF receive using PIC USART | col_implant | Electronic Projects Design/Ideas/Reviews | 11 | 15th November 2007 05:06 PM |
| JDM programmer problem | krvavizmaj | Micro Controllers | 3 | 2nd October 2007 09:36 AM |
| URGENT!! 16F877 & USART & Interrupt & Recieve Problem | DaMieN | Micro Controllers | 0 | 18th September 2007 11:53 AM |
| RF tranceiver problem | darvaish | General Electronics Chat | 2 | 3rd April 2005 08:44 AM |