EmbeddedRelated.com
Forums

Annoying Scanf

Started by linked82 November 7, 2007
Hi all,

I'm still unable to use scanf because I can't find any useful info
about how to make it work. I've implemented the getchar() and
putchar() on my own code, but it still hangs when enters the scanf()
or gets() functions. I know that there are low level get functions,
but how to configure them? I can't understand why putchar() works fine
with printf() but getchar don't. Here's may code.
int putchar(int ch)
{
while (!(IFG2 & UTXIFG1));
TXBUF1 = ch;
return ch;
}

//char _low_level_get(void) /* Read one char from I/O */
//int getchar(void)
{
_BIS_SR(LPM0_bits);
return RXBUF1;
}

#pragma vector=UART1RX_VECTOR
__interrupt void usart1_rx (void)
{
_BIC_SR_IRQ(CPUOFF);
}

Thanks In advance. Any help will be useful no that I'm in a hurry.

Best regards.

Beginning Microcontrollers with the MSP430

--- In m..., "Leon" wrote:
>
> ----- Original Message -----
> From: "linked82"
> To:
> Sent: Wednesday, November 07, 2007 1:29 PM
> Subject: [msp430] Annoying Scanf
> > Hi all,
> >
> > I'm still unable to use scanf because I can't find any useful info
> > about how to make it work. I've implemented the getchar() and
> > putchar() on my own code, but it still hangs when enters the scanf()
> > or gets() functions. I know that there are low level get functions,
> > but how to configure them? I can't understand why putchar() works fine
> > with printf() but getchar don't. Here's may code.
>
> What compiler?
>

IAR 3.40A
linked82 wrote:
> > > > > Hi all,
> > > > >
> > > > > I'm still unable to use scanf because I can't find any useful info
> > > > > about how to make it work. I've implemented the getchar() and
> > > > > putchar() on my own code, but it still hangs when enters the
> scanf()
> > > > > or gets() functions. I know that there are low level get
> functions,
> > > > > but how to configure them? I can't understand why putchar()
> works fine
> > > > > with printf() but getchar don't. Here's may code.
> > > >
> > > > What compiler?
> > > >
> > >
> > > IAR 3.40A
> >
> > Do you use CLib or DLib?
> >
> CLIB

Ok, the the function should be named __getchar, just like microbit
already wrote.

(For DLib, the low-level input and output functions are named __read and
__write.)

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.
linked82 wrote:
> --- In m... , "Leon"
> wrote:
> >
> > ----- Original Message -----
> > From: "linked82"
> > To: >
> > Sent: Wednesday, November 07, 2007 1:29 PM
> > Subject: [msp430] Annoying Scanf
> >
> >
> > > Hi all,
> > >
> > > I'm still unable to use scanf because I can't find any useful info
> > > about how to make it work. I've implemented the getchar() and
> > > putchar() on my own code, but it still hangs when enters the scanf()
> > > or gets() functions. I know that there are low level get functions,
> > > but how to configure them? I can't understand why putchar() works fine
> > > with printf() but getchar don't. Here's may code.
> >
> > What compiler?
> > IAR 3.40A

Do you use CLib or DLib?

-- Anders
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.
----- Original Message -----
From: "linked82"
To:
Sent: Wednesday, November 07, 2007 1:29 PM
Subject: [msp430] Annoying Scanf
> Hi all,
>
> I'm still unable to use scanf because I can't find any useful info
> about how to make it work. I've implemented the getchar() and
> putchar() on my own code, but it still hangs when enters the scanf()
> or gets() functions. I know that there are low level get functions,
> but how to configure them? I can't understand why putchar() works fine
> with printf() but getchar don't. Here's may code.

What compiler?

Leon
--
Leon Heller
Amateur radio call-sign G1HSM
Yaesu FT-817ND and FT-857D transceivers
Suzuki SV1000S motorcycle
l...@btinternet.com
http://webspace.webring.com/people/jl/leon_heller/
--- In m..., "microbit_virgin" wrote:
>
> If you don't use ints but polling, the least you need to do is to
wait until a char is available.
> Here's a simple example of polled serial :
>
> int __putchar (int ch)
> {
> while (!(IFG2 & UTXIFG1));
> U1TXBUF = ch;
> return ch;
> }
>
> int __getchar (void)
> {
> while (!(IFG2 & URXIFG1));
> return (U1RXBUF);
> }
>
> Also note that if you don't stick to the prototype of the LIB I/O,
your func(s) won't be called.
> Make sure getchar() returns an int.
>

I'm sorry for saying that this still doesn't works. When I call
gets(), I've seen that it is polling getchar for incoming characters,
but it loops and doesn't ends, even if I send CR, LF, EOL or whatever.
I'm going mad.
> I'm sorry for saying that this still doesn't works. When I call
> gets(), I've seen that it is polling getchar for incoming characters,
> but it loops and doesn't ends, even if I send CR, LF, EOL or whatever.
> I'm going mad.

Did you enable interrupts properly?

JJS
Hmmm. OK, these things can get frustrating at times.
You must have a problem with either :
- initialisation of USART1
- hardware issue

If it doesn't work with a simple poll there's obviously no point in putting in interrupts.
Can you probe/trace on RXD pin & check that your serial (logic) data indeed arrives at MSP430 ?
Set your Terminal for slow speed, like 1200 bps or so. When you press a key you should see the RXD
pin pulsing to low for a few mS - make sure that's OK.
If all OK, check initialisation, some simple test code :

Say, using SMCLK at 8 MHz, 9,600 bps :

#define BAUD (9600)

void init_UART1 (void) {
UCTL_1 |= SWRST;
UCTL_1 |= CHAR; // UART1=Async,8 bit,1 stop,
// no parity, enable SWRST=0,
UTCTL_1 = SSEL1+SSEL0; // SSEL=SMCLK
URCTL_1 = URXEIE; // all RXd chars set URXIFG1
U1BR0 = (unsigned char)(8000000UL/BAUD);
U1BR1 = 8000000UL/BAUD/256;
UMCTL_1 = 0; // Baudrate set
ME2 |= URXE1+UTXE1; // enable UART1 RX + TX
UCTL_1 &= ~SWRST;
P3SEL |= (BIT6 + BIT7); // select RXD & TXD alt pin
P3DIR |= BIT6; // TXD1 out
}
HTH
Best Regards,
Kris

-----Original Message-----
From: m... [mailto:m...] On Behalf Of linked82
Sent: Thursday, 8 November 2007 2:16 AM
To: m...
Subject: [msp430] Re: Annoying Scanf

I'm sorry for saying that this still doesn't works. When I call
gets(), I've seen that it is polling getchar for incoming characters,
but it loops and doesn't ends, even if I send CR, LF, EOL or whatever.
I'm going mad.
> If it doesn't work with a simple poll there's obviously no point in
putting in interrupts.
> Can you probe/trace on RXD pin & check that your serial (logic) data
indeed arrives at MSP430 ?
> Set your Terminal for slow speed, like 1200 bps or so. When you
press a key you should see the RXD
> pin pulsing to low for a few mS - make sure that's OK.
> If all OK, check initialisation, some simple test code :
>
> Say, using SMCLK at 8 MHz, 9,600 bps :
>
> #define BAUD (9600)
>
> void init_UART1 (void) {
> UCTL_1 |= SWRST;
> UCTL_1 |= CHAR; // UART1=Async,8
bit,1 stop,
> // no parity, enable
SWRST=0,
> UTCTL_1 = SSEL1+SSEL0; // SSEL=SMCLK
> URCTL_1 = URXEIE; // all RXd chars set
URXIFG1
> U1BR0 = (unsigned char)(8000000UL/BAUD);
> U1BR1 = 8000000UL/BAUD/256;
> UMCTL_1 = 0; // Baudrate set
> ME2 |= URXE1+UTXE1; // enable UART1 RX + TX
> UCTL_1 &= ~SWRST;
> P3SEL |= (BIT6 + BIT7); // select RXD & TXD alt pin
> P3DIR |= BIT6; // TXD1 out
> }

The hardware works OK. If I call the __getchar() or getchar() function
it works Ok. If I use gets() the softtware hangs waiting for some kind
of character that never arrives. I've tested various terminal programs.
> > > > Hi all,
> > > >
> > > > I'm still unable to use scanf because I can't find any useful info
> > > > about how to make it work. I've implemented the getchar() and
> > > > putchar() on my own code, but it still hangs when enters the
scanf()
> > > > or gets() functions. I know that there are low level get
functions,
> > > > but how to configure them? I can't understand why putchar()
works fine
> > > > with printf() but getchar don't. Here's may code.
> > >
> > > What compiler?
> > >
> >
> > IAR 3.40A
>
> Do you use CLib or DLib?
>
CLIB