source: Deliverables/D4.1/GCC/SerialWriteRead.c @ 557

Last change on this file since 557 was 204, checked in by mulligan, 9 years ago

Added file that performs both serial input and output on the 8051.

File size: 959 bytes
Line 
1#include "mcs51reg.h"
2
3int factorial(int i)
4{
5  if(i == 0 | i == 1)
6    return 1;
7  else
8    return i * factorial(i - 1);
9}
10
11void prepare_serial(void)
12{
13  SCON = SCON | 0x40;  // Change serial port mode to mode 1
14  TMOD = TMOD | 0x20;  // Configure timer 1 to mode 2 (8 bit autoreload)
15  TH1  = 0xFD;         // Baud rate to 19,200 with ~11mhz clock
16  PCON = PCON | 0x80;  // PCON.7 set to double baud rate (needed?)
17}
18
19void write_serial(int d)
20{
21  TI   = 0x00;    // Clear the transmission bit
22  SBUF = d;       // Write data to the serial buffer
23  while(!TI) { }  // Wait until the data has been transmitted
24}
25
26int read_serial(void)
27{
28  while(!RI) { }  // Wait until receiver bit is set (we have data on the serial line).
29  return SBUF;    // If so, we need to return SBUF, which contains the data.
30}
31
32int main(void)
33{
34  int x = 0;
35  prepare_serial();
36  write_serial(10);
37  write_serial(20);
38  write_serial(factorial(3));
39  x = read_serial();
40  return 5;
41}
Note: See TracBrowser for help on using the repository browser.