I am using a Texas Instruments CC1110, which is based on the 8051 microcontroller. I'm programming it using the IAR Embedded Workbench.
I have written a function named sendChar
that sends a 8-bit char over the UART interface.
Running the following code:
sendChar('H');
sendChar('e');
sendChar('l');
sendChar('l');
sendChar('o');
sendChar(' ');
sendChar('W');
sendChar('o');
sendChar('r');
sendChar('l');
sendChar('d');
Results into the expected behavior of Hello world
being sent over the UART channel. But then defining the sendString
function as:
void Serial::sendString(char *str) {
unsigned int i;
for(i = 0; i < strlen(str); i++) this->sendChar(str[i]);
}
and running the following code:
sendString("Hello world");
results in 0xFF
being sent more than 100 times.
I have noticed that sending the strlen
output with the sendChar
function reveals that the wrong string length is being generated. For example, if I run the following code:
char str[100] = "Hello world";
sendChar(strlen(str));
I receive 101
instead of 11
as one would expect.
What am I doing wrong?
Edit
If I run the following code:
char str[100] = "Hello world";
sr.sendChar(str[1]);
sr.sendString(str);
one would expect to receive eHellow world
, but instead I receive 101 dots .
(0xFF).
Edit 2
Complete code:
#include <ioCC1110.h>
#include <ioCCxx10_bitdef.h>
#include <math.h>
#include <string>
void sendChar(char c);
void sendString(char *str);
void main(void) {
CLKCON = 0; // Crystal oscillator, no pre-scaler
U0CSR |= U0CSR_MODE; // USART0 in UART mode
P0SEL |= BIT3 | BIT2; //P0.2 and P0.3 as peripherials
// Baud rate = 9600 (9597)
U0GCR |= 8;
U0BAUD = 131;
// Enable Rx and Tx interrupts
IEN2 |= IEN2_UTX0IE;
URX0IE = 1;
while(P0_4); // Wait for P0.4
char str[100] = "Hello world";
sr.sendString(str);
while(1); // Wait so main() never ends
}
void sendString(char *str) {
while (*str) sendChar(*str++);
}
void sendChar(char c) {
U0DBUF = c & 0xFF;
while(!UTX0IF);
UTX0IF = 0;
}
Edit 3
I ran a simple main:
int main( void ) {
char str[] = "Hello world";
CLKCON = 0; // Crystal oscillator, no pre-scaler
P2DIR = 1; // P2.0 as output
P2 = 0;
char l = str[2];
Serial sr;
while(P0_4);
sr.sendString(str);
while(1);
}
and debugged it. Here is a screenshot of the value of the str
array.
Its full of dots, as I reported previously.
Any idea on what's causing that?