Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
Giang Ho
06/24/09 19:47
Read: 199 times
Sydney
Australia


 
#166433 - Re: use a cast
Responding to: Maarten Brock's previous message
Hi Maarten,

Maarten said:
I suspect that your compiler sends the small (<256) number 1 as a char to the printf function which interprets it as an unsigned int. So it reads 0x01FF of which only 0x01 was really sent to it. Try casting the constant to unsigned int.

I have declared unsigned int variable and assigned to a constant value but the data received on the HyperTerminal is not reliable:

Code:
//Declaration:
xdata unsigned int data1=1;
xdata unsigned int data2=1550;
xdata unsigned int data3=15550;

//Output to UART:
printf("DATA 1:%un",data1); //Output result to UART
printf("DATA 2:%un",data2); //Output result to UART
printf("DATA 3:%un",data3); //Output result to UART

Output on HyperTerminal:

DATA 1:53925
DATA 2:53926
DATA 3:30127
;--------------------------------------
I have also tested by using "printf" to send string and SBUF to send data, but nothing come up on the terminal:

e.g.

void serial_init(void)
{ //Configure UART
T3CON = 0x83; //configure timer 3...
T3FD = 0x12; //...for 9600 baud
SCON = 0x50;
TI=1; //Initialise to send
}

printf("DATA 1:");

SBUF=data1;
while(! TI) {} // Wait for transmission completion
TI =0; // Clear the interrupt
;--------------------------------------
Do you have any idea why it went wrong?




List of 44 messages in thread
TopicAuthorDate
CMP-10 thermal printer & 8051      Giang Ho      06/13/09 08:47      
   Printer settings      Hal Albach      06/13/09 10:27      
   Some hints/advice..      Valentin Angelovski      06/13/09 11:11      
   I sent commands like "BEL", or "ESC RS"      Andy Neil      06/13/09 11:14      
      ASCII table      Per Westermark      06/13/09 13:58      
      HyperTerminal & ASCII code      Giang Ho      06/13/09 18:56      
         Would it be realistic....      Michael Karas      06/13/09 21:45      
         Not text,      Per Westermark      06/14/09 01:54      
            Not text,      Giang Ho      06/14/09 03:08      
               Spend more time with the ASCII table.      Per Westermark      06/14/09 03:35      
                  Still fail to test the printer      Giang Ho      06/18/09 07:37      
                     The printer is working      Giang Ho      06/18/09 09:04      
                     ASCII char on HyperTerminal      Mahesh Joshi      06/20/09 07:11      
                        Re: ASCII char on HyperTerminal      Giang Ho      06/21/09 09:16      
                     ASCII char on HyperTerminal      Mahesh Joshi      06/20/09 07:11      
                        Is that guaranteed?      Andy Neil      06/20/09 13:15      
                           I use it that way      Mahesh Joshi      06/22/09 01:28      
   XOn/XOff set on MCU side      Giang Ho      06/18/09 22:08      
      You have to do it yourself      Andy Neil      06/19/09 01:36      
         Re:You have to do it yourself      Giang Ho      06/21/09 08:57      
            Intern? [edited]      Andy Neil      06/24/09 03:00      
   HyperTerminal and microcontroller - please help!      Giang Ho      06/23/09 10:36      
      HyperTerminal and microcontroller - please help!      Giang Ho      06/23/09 19:07      
         May be I need to use cross over serial cable      Andy Neil      06/24/09 03:03      
   Use printf to output to UART, but receive incorrect data      Giang Ho      06/24/09 10:09      
      Re: Use printf to output to UART, but receive incorrect dat      Giang Ho      06/24/09 10:13      
         printf only can output value from 256 to 32767 correctly      Giang Ho      06/25/09 07:42      
            Hasn't Maarten already explained this?      Andy Neil      06/25/09 07:54      
               Re: Hasn't Maarten already explained this?      Giang Ho      06/25/09 08:31      
                  Show it!      Andy Neil      06/25/09 09:08      
                     Re: Show it      Giang Ho      06/25/09 19:48      
      use a cast      Maarten Brock      06/24/09 16:11      
         or RTFM      Andy Neil      06/24/09 17:06      
            Re: RTFM      Giang Ho      06/25/09 19:55      
            I found the solution. Thanks      Giang Ho      06/26/09 08:46      
               Direct Memory?        Andy Neil      06/26/09 09:03      
                  Re: Direct memory      Giang Ho      06/26/09 09:21      
                     Definitely a BAD Idea!      Andy Neil      06/26/09 09:43      
                        Re:Definitely a BAD Idea!      Giang Ho      06/26/09 10:01      
                           Could you please suggest?      Andy Neil      06/26/09 10:25      
                              Re: Do not call from ISR      Giang Ho      06/26/09 18:19      
                                 I didn't say that      Andy Neil      06/27/09 03:02      
                                    Thanks!      Giang Ho      06/27/09 09:10      
         Re: use a cast      Giang Ho      06/24/09 19:47      

Back to Subject List