From: kbongers Date: Tue, 3 Jul 2001 04:43:36 +0000 (+0000) Subject: add a simple serial program X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=0d5e90f0ae15c3ec0179ae9184b8398ded392bbf;p=fw%2Fsdcc add a simple serial program git-svn-id: https://sdcc.svn.sourceforge.net/svnroot/sdcc/trunk/sdcc@1006 4a8a32a2-be11-0410-ad9d-d568d2c75423 --- diff --git a/device/examples/mcs51/simple/go b/device/examples/mcs51/simple/go new file mode 100755 index 00000000..7b41b17a --- /dev/null +++ b/device/examples/mcs51/simple/go @@ -0,0 +1,3 @@ +#!/bin/bash +sdcc hi.c + diff --git a/device/examples/mcs51/simple/go.bat b/device/examples/mcs51/simple/go.bat new file mode 100644 index 00000000..c5b12f69 --- /dev/null +++ b/device/examples/mcs51/simple/go.bat @@ -0,0 +1,6 @@ +; this is a windows batch file to compile hi.c + +set SDCCINC=c:\usr\local\sdcc\device\include +set SDCCLIBS=c:\usr\local\sdcc\device\lib\small + +sdcc -I $(SDCCINC) hi.c sdcclib.lib -L $(SDCCINC) diff --git a/device/examples/mcs51/simple/hi.c b/device/examples/mcs51/simple/hi.c new file mode 100644 index 00000000..d58911c4 --- /dev/null +++ b/device/examples/mcs51/simple/hi.c @@ -0,0 +1,164 @@ +/*------------------------------------------------------------------------ + hi.c - This is a simple program designed to operate on basic MCS51 + hardware at 11.0592Mhz. It sets up the baudrate to 9600 and sends + a "Hi\n" "There\n" message every few seconds. The timer interrupt + is used. + + Its intended to be a simple example for SDCC, a good first + program to compile and play with. + + The simulator can be used to run this program: + s51 -Sout=testout.txt hi.ihx (run, stop, quit) + + 6-28-01 Written by Karl Bongers(karl@turbobit.com) +|------------------------------------------------------------------------*/ +#include <8052.h> + +typedef unsigned char byte; +typedef unsigned int word; +typedef unsigned long l_word; + +//---- most of the following declares are simply to demostrate some +// of SDCC's variable storage declaration syntax + +// volatile keyword is needed for variables shared by interrupt routine +// and normal application thread, otherwise things get optimized out. +volatile data byte timer; +volatile data byte hi_flag; + +data byte a_data_byte; // normal < 128 bytes of 8031 internal memory. +idata byte a_idata_byte; // in +128 byte internal memory of 8032 +xdata byte a_xdata_byte; // in external memory. +xdata at 0x8000 byte mem_mapped_hardware; // example at usage + +bit my_bit; // mcs51 bit variable, stored in single bit of register space + +sfr at 0xd8 WDCON; // special function register declaration +sbit LED_SYS = 0xb5; // P3.5 is led, example use of sbit keyword + +code char my_message[] = {"GNU rocks"}; // placed in code space + +void timer0_irq_proc(void) interrupt 1 using 2; + +/*------------------------------------------------------------------------ + timer0_int - Timer0 interrupt. Notice we are using register bank 2 + for this interrupt. +|------------------------------------------------------------------------*/ +void timer0_irq_proc(void) interrupt 1 using 2 +{ + if (timer != 0) + { + --timer; + } + else + { + hi_flag = 1; + timer = 250; + } + + TR0 = 0; /* Stop Timer 0 counting */ + TH0 = (~(5000)) << 8; + TL0 = (~(5000)) & 8; + TR0 = 1; /* Start counting again */ +} + +#if 0 +/*------------------------------------------------------------------------ + uart0_int - Interrupt 4 is for the UART, notice we use register bank 1 + for the interrupt routine. +|------------------------------------------------------------------------*/ +void uart0_int(void) interrupt 4 using 1 +{ + if (RI) + { + c = SBUF; + RI = 0; + } +} +#endif + +/*------------------------------------------------------------------------ + tx_char - transmit(tx) a char out the serial uart. +|------------------------------------------------------------------------*/ +void tx_char(char c) +{ + while (!TI) + ; + TI = 0; + SBUF = c; +} + +/*------------------------------------------------------------------------ + tx_str - transmit(tx) a string out the serial uart. +|------------------------------------------------------------------------*/ +void tx_str(char *str) +{ + + while (*str) + tx_char(*str++); +} + +/*------------------------------------------------------------------------ + stop - a break point in Daniel D's s51 can be set at 65535 memory + location to stop the simulation. This routine also shows how to + embed assembly. +|------------------------------------------------------------------------*/ +void stop(void) +{ + _asm; + mov dptr, #65535; + movx a, @dptr; + nop; + _endasm; +} + +/*------------------------------------------------------------------------ + main - Simple test program to send out something to the serial port. +|------------------------------------------------------------------------*/ +void main(void) +{ + PCON = 0x80; /* power control byte, set SMOD bit for serial port */ + SCON = 0x50; /* serial control byte, mode 1, RI active */ + TMOD = 0x21; /* timer control mode, byte operation */ + TCON = 0; /* timer control register, byte operation */ + + TH0 = (~(5000)) << 8; /* the initial time is not important */ + TL0 = (~(5000)) & 8; + + TH1 = 0xFA; /* serial reload value, 9,600 baud at 11.0952Mhz */ + TR1 = 1; /* start serial timer */ + + TR0 = 1; /* start timer0 */ + ET0 = 1; /* Enable Timer 0 overflow interrupt IE.1 */ + EA = 1; /* Enable Interrupts */ + + TI = 0; /* clear this out */ + SBUF = '.'; /* send an initial '.' out serial port */ + hi_flag = 1; + //ES = 1; /* Enable serial interrupts IE.4 */ + + tx_str(my_message); + + for (;;) + { + if (hi_flag) + { + tx_str("Hi\n"); + tx_str("There\n"); + hi_flag = 0; + } + +stop(); + +#ifdef TEST_IDLE_MODE + // this was a simple test of the low power sleep mode of a + // dallas DS5000 cmos part, to see how much power requirements + // dropped in sleep mode. + + // into idle mode until next interrupt. Draws only 3ma. + PCON = 0x81; +#endif + } + +} + diff --git a/device/examples/mcs51/simple/test b/device/examples/mcs51/simple/test new file mode 100755 index 00000000..40cf480d --- /dev/null +++ b/device/examples/mcs51/simple/test @@ -0,0 +1,10 @@ +#!/bin/bash + +# Try out the simulator. Route serial port output to a text file. +# After simulator is started, you need to type: +# run +# stop +# quit + +s51 -Sout=testout.txt hi.ihx +