serial_1.c

来自「基于AM1单片机的一个RS232串口的通讯程序」· C语言 代码 · 共 184 行

C
184
字号
#include "101c49k.h"
#include "lcd.h"
#include "Serial_1.h"

/*#include <string.h>*/


char ReceiveData;							/* character data */
char input_buf[16+1];						/* receive character buffer */
char position_buf;							/* buffer position */

unsigned char LED_Status;					/* LED : On/Off */

unsigned char fDataReceive;					/* data receive flag */
unsigned char fTimeLED;						/* LED : Timer flag */
unsigned char fTimeLCD;						/* LCD : Timer flag */


void main(void)
{
	asm ("\tand\t0b10111111,PSW\n");		/* Interrupt disable */

	io_init();								/* initialize of CPU */

	/* initialize */
	position_buf = 0;
	input_buf[0] = 0;



	fDataReceive = FALSE;
	fTimeLED = FALSE;
	LED_Status = 0;

	asm ("\tor\t0b01110000,PSW\n");			/* Interrupt enable, Level 3 */

	lcd_init();								/* initialize of LCD */
    
	/* LCD Startup Messages */
	lcd_clear();
	
	lcd_puts("time",0,0);
/*	P1.OUT.BYTE = LED_DATA_1;*/
  
   
   
	while (1) {
		if( fDataReceive ){
		 disp_receive();
		 if( strcmp( "Hello", input_buf ) == 0 ){
			lcd_puts("Hi!             ", 1, 0);
		}
		 }
		if( fTimeLED )     disp_led();
	}
}

void disp_receive(void)
{
	fDataReceive = FALSE;							/* clear flag */

	if( ReceiveData == CHAR_CR ){
		lcd_puts("                ", 0, 0);			/* clear first line */
		if( position_buf == 0 ){					/* buffer empty */
	    lcd_puts("OK!             ", 1, 0);
	    }
		if( position_buf > 15 ){
			lcd_puts("                ", 1, 0);		/* clear second line */
		}
		if( strcmp( "Hello", input_buf ) == 0 ){
			lcd_puts("Hi!             ", 1, 0);
		}
		if( strcmp( "LED START", input_buf ) == 0 ){
			ICR.TBICR.BIT.IE = 0x01;				/* enable interrup */
		}
		if( strcmp( "LED STOP", input_buf ) == 0 ){
			ICR.TBICR.BIT.IE = 0x00;				/* disable interrupt */
			P1.OUT.BYTE = 0x00;						/* LED all off */
		}
		input_buf[0] = 0;
		position_buf = 0;							/* clear position */
	} else {
		if( position_buf < 16 ){
			input_buf[position_buf++] = ReceiveData;
			input_buf[position_buf] = 0;
			lcd_puts( input_buf, 1, 0);
			/*lcd_puts("                ", 1, 0);*/
		} else {
			lcd_puts(" Buffer Full ", 1, 0);
			input_buf[0] = 0;
		    position_buf = 0;							/* clear position */
		}
	}
      
}

void disp_led(void)
{
	fTimeLED = FALSE;

	LED_Status = ( LED_Status ? 0 : 1 );
	if( LED_Status ){
		P1.OUT.BYTE = LED_DATA_1;
	} else {
		P1.OUT.BYTE = LED_DATA_2;
	}
}

void io_init(void)
{
	CPUM.BYTE = 0x00;				/* CPU mode = NORMAL */
	CPUM.BIT.OSCSEL = 0x00;			/* divide ratio = 1 */
	CPUM.BIT.OSCDBL = 0x00;			/* internal system clock = NORMAL */
	OSCMD.BIT.SOSC2DS = 0x00;		/* slow oscillator = NORMAL */
	EXADV.BYTE = 0x00;				/* extend address output = INHIBIT */
	MEMCTR.BIT.IOW = 0x00;			/* special register wait = NONE */
	MEMCTR.BIT.EXMEM = 0x00;		/* memory extend = NONE */
	MEMCTR.BIT.EXWH = 0x01;			/* wait mode = FIXED */
	MEMCTR.BIT.IRWE = 0x00;			/* software interrupt = Disable */
	MEMCTR.BIT.EXW = 0x00;			/* fixed wait value = 0 */
	DLYCTR.BIT.DLYS = 0x00;			/* stability period = 2^14/fs */
	DLYCTR.BIT.BUZOE = 0x00;		/* Buzzer output = NONE */
	SBNKR.BIT.SBA = 0x00;			/* source address bank selection = Bank0 */
	DBNKR.BIT.DBA = 0x00;			/* destination address bank selection  = Bank0 */

/* LED OUT */
	P1.OUT.BYTE = 0x00;				/* P1 data output */
	P1.DIR.BYTE = 0xff;				/* P1 the other bit = OUTPUT */
	P1.PLU.BYTE = 0x00;				/* P1 pull up resistance = NONE */
	P1.OMD.BYTE = 0x00;				/* P1 timer output = NONE */
	P1.TCNT.BYTE = 0x00;			/* P1 real time output = NONE */
/*P7*/
       P7.OUT.BYTE = 0x00;				/* P7 data output */
	P7.DIR.BYTE = 0x06;				/* P7 2 and 3= OUTPUT,the other=input */
	P7.PLUD.BYTE = 0x00;				/* P7 pull up resistance = NONE */
/* LCD CONTROL */
	P5.OUT.BYTE = 0x00;				/* P5 data output */
	P5.DIR.BYTE = 0xff;				/* P5 all bit = OUTPUT */
	P5.PLU.BYTE = 0x00;				/* P5 pull up resistance = NONE */
	P6.OUT.BYTE = 0x00;				/* P6 data output */
	P6.DIR.BYTE = 0xff;				/* P6 all bit = OUTPUT */
	P6.PLU.BYTE = 0x00;				/* P6 pull up resistance = NONE */

/* LCD DATA */
	P8.OUT.BYTE = 0x00;				/* P8 data output */
	P8.DIR.BYTE = 0xff;				/* P8 all bit = OUTPUT */
	P8.PLU.BYTE = 0x00;				/* P8 pull up resistance = NONE */

/* TM1 timer for LCD (1ms/40us) */
	TM1.MD.BYTE = 0x01;				/* TM1 normal operation, stop, clock source = tm1psc */
	TM1.CK.BYTE = 0x06;				/* pre-scaler = fosc/128 */
	ICR.TM1ICR.BIT.LV = 0x01;		/* interrupt level = 1 */
	ICR.TM1ICR.BIT.IE = 0x01;		/* interrupt enable */

/* TM6 0.25sec TIMER (time base) */
	TM6.MD.BIT.CK0 = 0x01;			/* time base clock source = fx(32.768kHz) */
	TM6.MD.BIT.IR = 0x04;			/* time base interrupt period = fx*1/2^13(0.25sec) */
	TM6.TBCLR = 0x00;				/* clear time base */
	ICR.TBICR.BIT.LV = 0x02;		/* interrupt level = 2 */

/* SC0 */
	SC0.CKS.BYTE = 0x0e;			/* clock source = TM4 */
	SC0.ODC.BYTE = 0x00;			/* serial output terminal = push-pull */
	SC0.MD0.BYTE = 0x18;			/* send from LSB with start condition */
	SC0.MD1.BYTE = 0x3D;			/* input from SB0, clock is 1/8, full-duplex UART */
	SC0.MD2.BYTE = 0x88;			/* 8bit, 1stop, non-parity */
	P0.PLU.BIT.B0 = 0x00;			/* P00 pull up resistance = NONE */
	P0.PLU.BIT.B1 = 0x00;			/* P01 pull up resistance = NONE */
	P0.DIR.BIT.B0 = 0x01;			/* P00 = OUTPUT */
	ICR.SC0RICR.BIT.LV = 0x01;		/* interrupt level = 1 */
	ICR.SC0RICR.BIT.IE = 0x01;		/* enable interrupt */

/* TM4 for SC0 (1200bps)*/
    TM4.MD.BYTE = 0x01;
    TM4.CK.BYTE = 0x02;
    TM4.OC = 63;
	TM4.MD.BIT.EN = 0x01;			/* start operation */

/* OTHER */
	NFCTR.BYTE = 0x77;				/* noise filter 0/1 = ON, sampling = fosc/2^10 */
	PSCMD.BIT.PSCEN = 1;			/* pre-scaler = ON */
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?