|
From: Peter <pl...@ac...> - 2005-06-18 09:21:57
|
On Fri, 17 Jun 2005, J S wrote:
> My basic goal is to send a "test" message to a serial
> LCD display that requires 9600 baud rate and a 8 bit
> data and 1 bit stop transmission with no parity. I
> wrote this up to initialize the serial communication:
>
> SCON=0x50;
> TMOD=0x20;
> TH1=0xFD;
> TR1=1;
> T1=1;
>
> This combination seems to be ok, as I can send one
> letter to the display by:
>
> SBUF='T';
Two solutions:
No interrupts:
// set TI at program start
TI=1;
// write characters
for(i = 32; i < 96; ++i) { // span ascii set
while(!TI); // wait until TI is 1
TI=0; // clear it
SBUF=i; // send a character;
}
With interrupts:
volatile bit Txinuse = 0; // whether Txptr is busy
char *Txptr; // pointer to characters to be sent
// s must be static for the duration of the transmission!
// i.e. while Txinuse is 1, then it can be reused
void putsa(unsigned char *s)
{
// note: the next line returns immediately on the 1st call, but blocks
// if the previous string did not go out completely
while(Txinuse); // wait until all chars from a previous putsa call go out
Txptr = s; // Txptr is used by the ISR
if(*Txptr) { //
SBUF = *Txptr++;
Txinuse = 1;
}
}
// serial interrupt, shared with RI
void serial_int (void) interrupt 4
{
if (TI) { // transmit buffer empty ?
TI = 0; // clear flag
if(Txinuse) { // if putsa signalled there is data to send
if(*Txptr) // while more characters
SBUF = *Txptr++; // send current char and point past it
else
Txinuse = 0; // else clear Txptr pointer in use
}
}
}
// usage:
void main(void) {
SCON=0x50; // init, as you wrote
TMOD=0x20;
TH1=0xFD;
TR1=1;
TI=1; // == SCON | 0x02
P0=0xFF;
P0.0=0; // look for a H->L edge on P0.0
putsa("Hello, World!\n"); // returns immediately
P0.1=0; // P0.1 is L quickly after P0.0
putsa("Goodbye, World\n"); // blocks until above is sent
P0.2=0; // P0.2 takes a while to go on (about
// 14char*104usec*10bits ~= 14.6 msec)
while(1); // loop forever
}
> ?ASlink-Warning-Undefined Global '_putchar' referenced
> by module 'printf_tiny'
Read the fine manual. You have to define putchar. SDCC has no idea what
is attached to the circuit, you have to write a function that does the
actual output. You could output to leds, a lcd, or serial. A synchronous
putchar suitable for serial from above would be:
// in init code, after SCON=... etc
// you HAVE to set TI at the start of the program
TI=1;
// send a character via serial line, suitable for printf() etc
int putchar(int ch)
{
while(!TI); // wait until TI is set (it MUST be set in the init
// code for the first character to go out!!!
TI=0; // clear
SBUF=ch; // send it out
return((unsigned char)ch); // also return it as per standard C
}
good luck,
Peter
|