can anyone send me a little program to receive data via RS232C for
the 80C32?. No matter using interruptions or not.
I have tried programming the serial interruption but a lot of
interruptions take place all the time even if I disconnect the RS232C
cable and no data is received. Does anybody know why?
Any help will be appreciated.Email me to mnu...@cistia.es if possible or
to mnu...@arrakis.es.
Thanks in advance, Miguel NЗЯez.
hi,
here is some code i use in almost all my projects using an 80c51 or any
of its derivitives. it uses the interrupt, and serial buffers organized
as circular queues. hope it helps you out, but if any guestions just
ask.
/********
Author: Robert E. Engle Jr.
Embedded Solutions
18408 Belvedere Road
Orlando, Florida 32820
Project: Demo
Description: RS-232 code fragments
Name: rs232.c
Revision: 1.0
Customer: None
Target: P80c652
Compiler: IAR
Date: March 6th, 1997
********/
/********
Define constants.
********/
#define XMIT_BIT 0x02
#define RECV_BIT 0x01
#define MAXBUFFER 0x80
#define BAUD_19200 0xFD
#define BAUD_9600 0xFD
#define BAUD_4800 0xFA
#define BAUD_2400 0xF4
#define BAUD_1200 0xE8
#define BAUD_600 0xD0
#define BAUD_300 0xA0
#define BAUD_150 0x40
#define RTC_RELOAD_H 0xc0
#define RTC_RELOAD_L 0x00
#define RTC_INTERVAL 20
#define SECOND 20
#define MINUTE 1200
#define FLUSH_RELOAD MINUTE
/********
Declare bit variables.
********/
bit xmit_idle;
/********
Declare variables.
********/
xdata char in_buffer[MAXBUFFER];
xdata char out_buffer[MAXBUFFER];
idata byte my_address;
idata byte baud_rate;
data byte out_count;
data byte out_head;
data byte out_tail;
data byte in_count;
data byte in_head;
data byte in_tail;
/********
Baud rate reload velues for timer one.
********/
const byte baud_table[] =
{
BAUD_9600,
BAUD_4800,
BAUD_2400,
BAUD_1200,
BAUD_600,
BAUD_300,
BAUD_150,
BAUD_19200
};
/********
Interrupt handler routines.
RS-232 Communication Interrupt handler routine.
********/
void interrupt S0_int (void)
{
char z;
if(S0CON & XMIT_BIT)
{
S0CON &= ~XMIT_BIT;
if(out_count == 0)
xmit_idle = TRUE;
else
{
S0BUF = out_buffer[out_tail];
out_tail++;
out_tail %= MAX_BUFFER;
--out_count;
}
}
if(S0CON & RECV_BIT)
{
z = S0BUF;
S0CON &= ~RECV_BIT;
if(in_count < MAX_BUFFER - 1)
{
in_buffer[in_head] = z;
in_head++;
in_head %= MAX_BUFFER;
++in_count;
}
}
}
/********
Serial port i/o routines.
Put character to the serial port using circular queue.
********/
int putchar(int c)
{
int x;
x = -1;
if(selected)
{
if(xmit_idle == TRUE)
{
xmit_idle = FALSE;
S0BUF = c;
x = c;
}
else
{
while(out_count == MAX_BUFFER - 1)
;
x = c;
out_buffer[out_head] = c;
out_head++;
out_head %= MAX_BUFFER;
++out_count;
}
}
}
/********
Fetch character from the recieve circular queue.
********/
int _getkey()
{
int c;
c = in_buffer[in_tail];
in_tail++;
in_tail %= MAX_BUFFER;
--in_count;
return(c);
}
/********
If recieve character available return it, else return End of File.
********/
int getchar(void)
{
int i;
i = EOF;
if(in_count)
i = _getkey();
return(i);
}
/********
Main system initialization routine.
********/
void initialize(void)
{
/* initialize rs232 comm functions */
my_address = read_XDATA(SW_REGISTER) & 0x1f;
baud_rate = read_XDATA(SW_REGISTER) & 0xe0;
baud_rate >>= 5;
if(baud_rate == 7)
PCON = 0x80;
TH1 = baud_table[baud_rate];
S0CON = S0CON_SM1 | S0CON_REN;
in_head = 0;
in_tail = 0;
in_count = 0;
out_head = 0;
out_tail = 0;
out_count = 0;
xmit_idle = TRUE;
}
/********
Main execution program loop.
********/
void main(void)
{
char in, c;
int x;
initialize(); /* initialize system */
while(FOREVER) /* main execution loop */
{
if(in_count) /* if any chars in queue */
parse(); /* process characters */
}
}
bob engle
embedded solutions
ren...@ix.netcom.com
Here is my file for my serial control. Basically, I disable the interrupts when
I am using the serial comms. This routine was originally published in Elektor
magazine and I have made a few small changes since. A few notes:
1) I use register 6 to send and receive the data,
2) There are 3 commands here, initialise, receive and send
3) The top of the program has to enable the serial receive flag.
4) This method uses the timer to set up the baud rate
5) My main program checks SCON to see if a bit is set before calling the receive
routine.
I would imagine that you use the interrupts to get you to go into your receive
routine. As soon as you get there, do you disable your interrupts?
----------------File Begins here----------------
; ----------------------------------------------------
; serial.asm Serial port control section
; ----------------------------------------------------
; -------------- Serial timer setup using 11.0592 MHz crystal ------------
.b19200 equ 256-3 ; 19200 baud:11.0592 MHz/(12*16*3)
.b14400 equ 256-4 ; 14400 baud:11.0592 MHz/(12*16*4)
.b9600 equ 256-6 ; 9600 baud: 11.0592 MHz/(12*16*6)
.b4800 equ 256-12 ; 4800 baud: 11.0592 MHz/(12*16*12)
.b2400 equ 256-24 ; 2400 baud: 11.0592 MHz/(12*16*24)
.b1200 equ 256-48 ; 1200 baud: 11.0592 MHz/(12*16*48)
; -------------- Serial timer setup using 12 MHz crystal ------------
.b4800a equ 256-13 ; 4800 baud: 12 MHz/(12*16*13) = 4807.69 bps
.b2400a equ 256-24 ; 2400 baud: 12 MHz/(12*16*26) = 2403.85 bps
.b1200a equ 256-48 ; 1200 baud: 12 MHz/(12*16*52) = 1201.92 bps
.cnt1 equ 050h ; in RAM: counter for CR/LF
.init_ser:
mov a,#b4800a ; > > > > > alter baud rate here < < < < <
mov pcon,#80h ; smod=1
mov tmod,#22h ; both counters as timer rclk=0, tcon also
mov th1,a ; ...
mov tl1,a ; preload value timer1 (baudrate generator)
setb tcon.6 ; start counter1
mov scon,#052h ; mode1, ren=1, t1=1, ri=0
ret
.transmit:
jnb scon.1,transmit ; wait until last char finally gone
clr scon.1 ; ti=0
mov sbuf,r6 ; start transmit
ret
.get_char: ; get character from serial port
jnb scon.0,get_char ; wait until one available
clr scon.0 ; signal: char fetched
mov r6,sbuf
ret ; ready
END
--
=*=*=*= NO ADVERTS =*=*=*=\ (And...@apmawds.demon.co.uk)
Andrew Mawdsley, Electronic \ NO email adverts please
production engineer, \ 'Reply-to' in header disabled
PAC International LTD. \ Pride prevents learning
Manchester, England. \ from mistakes
My views are not those of my employers http://www.pac.co.uk