User: alciro    User
 Original    Translate to:  Deutsch  English  Français  中文  
 

Microcontroladores 8051

7.3. Communications serial sampling (polling)

Example of serial communications using the sampling method (Polling):

 /************************************************* *********
 Example of a program for data transmission by
 RS-232 serial port, operating sampling (polling).
 For Keil C51 C compiler and microcontrollers
 8051.

 Description: A program that given a character a, b, c.. in 
 lowercase or uppercase set to 0 or 1 bits
 P1 port.

 Author: Rafael Aranda
 Date: 11/20/2009
 Version: 1.0
************************************************** ********/

# Include <reg52.h> / / Include the generic domains of 8052

/ / Set the position P1 bit, 8051 microcontroller
SBIT Led_0 = P1 ^ 0; / / Assign the symbol Led_0 bit P1.0
SBIT LED_1 = P1 ^ 1; / / Assign the symbol LED_1 bit P1.1
SBIT LED_2 = P1 ^ 2 / / Assign the symbol LED_2 bit P1.2
SBIT Led_3 = P1 ^ 3; / / Assign the symbol Led_3 bit P1.3
SBIT Led_4 = P1 ^ 4; / / Assign the symbol Led_4 bit P1.4
SBIT Led_5 = P1 ^ 5; / / Assign the symbol Led_5 bit P1.5
SBIT Led_6 = P1 ^ 6; / / Assign the symbol Led_6 bit P1.6
SBIT Led_7 = P1 ^ 7; / / Assign the symbol Led_7 bit P1.7

/ / Declaration of the prototype functions
InitSerial void (void) / / Start the serial port
RecDat void (void) / / Function Receive data

/ / Global variable declaration


//------------------------------------------------ ----------
/ / Main Program
//------------------------------------------------ ----------
void main (void) {
        
	/ / Force initial preparation
	P1 = 0, / / Set all bits of P1 to 0
	InitSerial (); / / Begin communications
	
	/ / Body of the program in an infinite loop
	while (1) {
		/ / Displays the RI until the arrival of a new Byte
		if (RI) {
			RecDat ();
		}
	}
}

//------------------------------------------------ ----------
/ / Function Receive data
//------------------------------------------------ ----------
RecDat void (void) {
	unsigned char dat;
	
	RI = 0, / / Clear receive flag
	dat = SBUF; / / Retrieve the byte receive buffer
	SBUF = dat; / / Echo the data received
	
	/ / Analyze the data received and depending on the nature
	/ / Set to 0 or 1 the corresponding bit
	switch (dat) {
		/ / Zero port 1 bits
		case 'a':
			Led_0 = 0, / / Bit P1.0 to 0
			break;
		case 'b':
			LED_1 = 0, / / Bit P1.1 to 0
			break;
		case 'c':
			LED_2 = 0, / / Bit P1.2 to 0
			break;
		case 'd':
			Led_3 = 0, / / Bit P1.3 to 0
			break;
		case 'e':
			Led_4 = 0, / / Bit P1.4 to 0
			break;
		case 'f':
			Led_5 = 0, / / Bit P1.5 to 0
			break;
		case 'g':
			Led_6 = 0, / / Bit P1.6 to 0
			break;
		case 'h':
			Led_7 = 0, / / Bit P1.7 to 0
			break;
		/ / Make a port 1 bits
		case 'A':
			Led_0 = 1, / / Bit P1.0 to 1
			break;
		case 'B':
			LED_1 = 1, / / Bit P1.1 to 1
			break;
		case 'C':
			LED_2 = 1, / / Bit P1.2 to 1
			break;
		case 'D':
			Led_3 = 1, / / Bit P1.3 to 1
			break;
		case 'E':
			Led_4 = 1, / / Bit P1.4 to 1
			break;
		case 'F':
			Led_5 = 1, / / Bit P1.5 to 1
			break;
		case 'G':
			Led_6 = 1, / / Bit P1.6 to 1
			break;
		case 'H':
			Led_7 = 1, / / Bit P1.7 to 1
			break;
	}

}

//------------------------------------------------ ----------
/ / Start the serial port
//------------------------------------------------ ----------
InitSerial void (void) {
	TMOD | = 0x20 / / Timer 1 in mode 2 (auto reload)
	TL1 = 0xFD / / 9600 baud rate
	TH1 = 0xFD;
	SCON = 0x50, / / Setup UART mode 2
	TR1 = 1, / / Start Timer1 and UART

} 

Loading
copyright © 2007-2024  www.alciro.org  All rights reserved.         
Share |