Question

I am trying to print message on serial terminal from p89v664 using following code,

#include<P89V66x.H>
#include<stdio.h>
char putchar(char c) {

if (c == '\n')  {   
    while (!TI);   
    TI = 0;   
    S0BUF = 0x0d;   
}   
TI = 0;
S0BUF = c;    
while (!TI);
return c;

}

int printf(char*str) {

unsigned int cnt = 0;
while(*str != '\0')
{
    putchar(*str);
    cnt++;
    str++;
}
}

void delay(unsigned int i) {
int d = 100;
for(;i!=0;i--) {
    for(;d!=0;d--);
    d = 100;
}
}

int main(void) {
/**Serial init*/
S0CON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
TH1   = 0xF6;                   /* TH1:  reload value for 9600 baud         */
TR1   = 1;                    /* TR1:  timer 1 run                        */
TI = 1;
while(1) {
    printf("Hello\n");
    delay(300);
    printf("Hello World\n");
    delay(10000);
}
}

above program works fine till the time printf function definition in this program is not commented. If printf function in above program is commented to use printf from standard library then junk characters are printed on serial console. (i used putty). I used Keil uVision V4.14.4.0 compiler.

Is there anything missing? I dont understand what is wrong with this program.

Was it helpful?

Solution

After some experiments i found that problem was with keil uVision4 evaluation version. I compiled this code using sdcc and ran it and it worked. May be keil evaluation version's limitation was creating problem. Thank very much you Mellowcandle for all replies.

Edit:

#include <P89V66x.H>
#include<stdio.h>

void putchar(char c) {

TI = 0;
S0BUF = c;  
if (c == '\n')  {   
    while (!TI);   
    TI = 0;   
    S0BUF = 0x0d;   
}
while (!TI);
}
int main(void) {

/**Serial init*/
unsigned short int c = 65334;
S0CON  = 0x50;                /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD |= 0x20;                 /* TMOD: timer 1, mode 2, 8-bit reload      */
/**For 11.0592 crystal
value should TH = -3 or
TH1 = FD*/
TH1   = 0xF6;                 /* TH1:  reload value for 9600 baud  for 
                              18 Mhz cyrstal */
TR1   = 1;                    /* TR1:  timer 1 run                        */

while(1) {
    printf("Hello %u\n", c);
    delay(300);
    printf("Hello World %u\n" ,c);
    delay(10000);
}
}

command used to compile this code is,

sdcc {filename}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top