1. Trang chủ
  2. » Kỹ Thuật - Công Nghệ

VI XỬ LÝ 8051 chap8 c programming

50 1 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề The 8051 Microcontroller Chapter 8 C Programming for 8051
Tác giả Lê Chí Thông
Người hướng dẫn TS. Lê Chí Thông
Trường học Ho Chi Minh City University of Technology
Chuyên ngành C Programming
Thể loại Bài tập lớn
Năm xuất bản 2012
Thành phố Ho Chi Minh City
Định dạng
Số trang 50
Dung lượng 2,82 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

• Chung-Ping Young, 8051 Programming in C pdf• Ibrahim Kamal, C programming for 8051 using KEIL IDE, keil-ide/ Blinky Program Schematic... C Programming for the 8051• C programming is le

Trang 1

Ho Chi Minh City University of Technology

Trang 2

• Chung-Ping Young, 8051 Programming in C (pdf)

• Ibrahim Kamal, C programming for 8051 using KEIL IDE,

keil-ide/

Blinky Program

(Schematic)

Trang 3

Blinky Program//Documentatons

/**************************

Blinky program Date: 05-Apr-2012 Version: 1.0

// Main Program

main (void) { while (1) { LED1 = ON; // Turn on LED1 Delayms (500); // Delay 500 ms LED1 = OFF; // Turn off LED1 Delayms (500); // Delay 500 ms }

}

source

From the C program

to the machine language

Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/

Trang 4

C Programming for the 8051

• C programming is less time consuming , but has

larger hex file size

• The reasons for writing programs in C

– It is easier and less time consuming to write in C than Assembly

– C is easier to modify and update – You can use code available in function libraries – C code is portable to other microcontroller with little

Subprograms (User defined functions)

Main Program (Main function)

Structure of a C program

Trang 5

//Main function

void main(void) {

int sum = 0; //Local Declarations int x;

float y;

//Statements }

Structure of a C program

Keil Product Downloads http://www.keil.com/download/product/

Trang 6

Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/

Using Keil

• Select a device for Target ‘Target 1′: seek

for ATMEL, then select AT89C51,AT89S52, …

• You will be asked whether to

‘copy standard

8051 startup code‘ click No

12

Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/

Trang 7

Using Keil

• Click File, New

• Click ‘File, Save as’ and chose a file name for your source code ending with the letter ‘.c’,

‘code.c’ for example, and click save

to group…‘,

•Right-click on Target 1, click Options for target ‘Target 1′, then under the

‘Output‘ tab, check the box ‘crate HEX file‘

14

Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/

Trang 8

Using Keil

• Compile your source code, and correct eventual syntax errors:

‘rebuild all targets’

Trang 9

Keil Variable Extensions

• Declaration of the types of memory

idata internal RAM (indirect addressing)

bdata bit addressable internal RAM

xdata external data memory

Trang 10

Description: The variable will be stored in bit addressable memory of controller.

Example:

unsigned char bdata x ;

//each bit of the variable x can be accessed //as follows

x ^ 1 = 1 ; //1st bit of variable x is set

x ^ 0 = 0 ; //0th bit of variable x is cleared

Trang 11

Description: This keyword is used to store a constant variable in code memory Lets say you have a big string which is not going to change anywhere in program

Wasting ram for such string will be foolish thing So instead we will make use of the keyword "code" as shown in example below.

• A good understanding of C data types for 8051 can help programmers to create smaller hex files

Trang 12

Standard Types

• Unsigned char is an 8-bit data type in the range of 0 –

255 (00 – FFH) and is one of the most widely used data types for the 8051.

Data type Bytes Range

unsigned char 1 0 to 255signed char 1 -128 To +127unsigned int 2 0 to 65,535signed int 2 -32,768 To +32,767unsigned long 4 0 to 4,294,967,295signed long 4 -2,147,483,648 to 2,147,483,647float 4 ± 1.175494E-38 to

Trang 15

unsigned char – Example 1

Source: Chung-Ping Young

unsigned char – Example 2

Source: Chung-Ping Young

Trang 16

unsigned char – Example 3

Source: Chung-Ping Young

signed char – Example

Source: Chung-Ping Young

Trang 17

sbit, signed int – Example

Trang 18

Description: This keyword is used to store a variable on

a defined location in ram.

Example:

unsigned char idata x _at_ 0x30 ;

// variable x will be stored at location // 0x30 in internal data memory

using

Description: This keyword is used to define register bank for a function User can specify register bank 0 to 3.

Trang 19

Description: This keyword will tells the compiler that function described is an interrupt service routine C51 compiler supports interrupt functions for 32 interrupts (0-31) Use the interrupt vector address in the following table to determine the interrupt number

Trang 20

Examples of Data Types

char code text [] = "Enter temperature:" ; unsigned long xdata MyArray [100];

float idata x, y, z;

unsigned int pdata Dimension;

unsigned char xdata vector [10] [4] [4];

char BDATA Indicator;

Lê Chí Thông

Trang 21

sum4 () { result4 = a+b;

} main () { a=2;

unsigned char const c=5;

unsigned char const c=’5’;

unsigned char const x[]="ABCDEF";

42

Lê Chí Thông

Trang 22

Operators (1)

43

+= Addition assignment operator, x+=y, is the same as x=x+y

&= Bitwise and assignment operator, x&=y, is the same as

x=x&y

& Address operator

& Bitwise and operator

^= Bitwise exclusive or assignment operator, x^=y, is the

> Greater than operator

>= Greater than or equal to operator

Lê Chí Thông

Trang 23

Operators (3)

45

<<= Left shift assignment operator, x<<=y, is the same as

x=x<<y

< Less than operator

<< Left Shift operator

<= Less than or equal to operator

&& Logical AND operator

>>= Right shift assignment, x>>=y, is the same as x=x>>y

>> Right shift operator-> Structure Pointer operation-= Subtraction assignment operator

sizeof Determines size in bytes of operand

Lê Chí Thông

Trang 24

P1 = P1 & 0xF0; //Adding '0x' before a number

//indicates that it is a hexadecimal one P1 = P1 & 240; //same effect

P1 = P1 | 0x81; //set the first and last bit of P1,

//without affecting the other

Shift Operations

Operator Description

Example:

P1 = 0x01; // After that operation, in binary, P1 =

//0000 0001 P1 = (P1 << 7); // After that operation, in binary

//P1 = 1000 0000

Trang 25

Comparison Operations

Operator Description

<=, >= Smaller than or equal to, bigger than or

Trang 26

Logical and Shift Operations – Example 2

Lê Chí Thông Source: Chung-Ping Young 51

Logical and Shift Operations – Example 3

Source: Chung-Ping Young

Trang 27

case expr2: stmt;

…[default: stmt] }

Lê Chí Thông

Statements (2)

54

return [stmt] terminates the execution of a function and

returns the value to the calling functionEx:

return (5);

return (a+b);

goto label performs an unconditional jump to the

named labelEx:

goto loop;

label: stmt; Ex:

loop: a=x+y;

Lê Chí Thông

Trang 28

Statements (3)

55

break; terminates the execution of the nearest

enclosing do, for, switch, or while statement

in which it appearscontinue; passes control to the next iteration of the

nearest enclosing do, for, or while statement

in which it appears, bypassing any remaining statements in the do, for, or while statement body

… }

Trang 30

case value_2:

… break;

[default:

… break;

}

Trang 31

Time Delay

• There are two ways to create a time delay in 8051 C – Using the 8051 timer

– Using a simple for loop

3 factors that can affect the accuracy of the delay

• The 8051 design– The number of machine cycle– The number of clock periods per machine cycle

• The crystal frequency connected to the X1 – X2 input pins

• Compiler choice– C compiler converts the C statements andfunctions to Assembly language instructions– Different compilers produce different codeLê Chí Thông 61

Time Delay – Example 1

Lê Chí Thông Source: Chung-Ping Young 62

Trang 32

Time Delay – Example 2

Lê Chí Thông Source: Chung-Ping Young 63

I/O Programming – Example 1

Lê Chí Thông Source: Chung-Ping Young 64

Trang 33

I/O Programming – Example 2

Lê Chí Thông Source: Chung-Ping Young 65

I/O Programming – Example 3

66

Source: Chung-Ping Young

Lê Chí Thông

Trang 34

I/O Programming – Example 4

Trang 35

I/O Programming – Example 6

Trang 36

I/O Programming – Example 8

Trang 37

Your turn! (switch … case)

Write an 8051 C program to read the P1.0 and P1.1 bits and send

an ASCII character to P0 according to the following table

P0='3';

break; } } //switch } //main

Trang 38

Your Turn! (Packed-BCD to ASCII)

• Write an 8051 C program to convert packed BCD 0x29 to ASCII and output the bytes to Port 1 and Port 2

Solution (Packed-BCD to ASCII)

• Write an 8051 C program to convert packed BCD 0x29 to ASCII and output the bytes to Port 1 and Port 2

#include <reg51.h>

main() {

unsigned char x,y;

unsigned char mybyte=0x29;

Trang 39

Your Turn! (ASCII to Packed-BCD)

• Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and send them to Port 1

Solution (ASCII to Packed-BCD)

• Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and send them to Port 1

#include <reg51.h>

main() { unsigned char bcdbyte;

Trang 40

Your Turn! (Binary to Decimal)

• Write an 8051 C program to convert 11111101B to decimal and send the digits to Port 0, Port 1 and Port 2

Solution (Binary to Decimal)

• Write an 8051 C program to convert 11111101B to decimal and send the digits to Port 0, Port 1 and Port 2

#include <reg51.h>

main() {

unsigned char x,bin,d1,d2,d3;

Trang 41

RAM space (1)

• Compile and single-step the following program on Keil Examine the contents of the internal RAM space to locate the ASCII values

#include <reg51.h>

void main(void) {

unsigned char mynum[]="ABCDEF"; //RAM space unsigned char z;

for (z=0;z<=6;z++) P1=mynum[z];

}

RAM space (2)

• Write, compile and single-step the following program on Keil

Examine the contents of the internal RAM space to locate the values

#include <reg51.h>

void main(void) {

unsigned char mydata[100]; //RAM space unsigned char x,z=0;

for (x=0;x<100;x++) {

z ;

mydata[x]=z;

P1=z;

} }

Trang 42

code unsigned char mynum[]="ABCDEF";

unsigned char z;

for (z=0;z<=6;z++) P1=mynum[z];

Trang 43

Lê Chí Thông 85

Trang 44

unsigned char conbyte=0x44;

unsigned char x;

ACC=conbyte;

for (x=0;x<8;x++) {

P1b0=regALSB;

ACC=ACC>>1;

} }

Your Turn! (Data Serialization)

• Write a C program to send out the value 44H serially one bit at a time via P1.0 The MSBshould go out first

Trang 45

Solution (Data Serialization)

• Write a C program to send out the value 44H serially one bit at a time via P1.0 The MSBshould go out first

unsigned char conbyte=0x44;

unsigned char x;

ACC=conbyte;

for (x=0;x<8;x++) {

P1b0=regAMSB;

ACC=ACC<<1;

} }

unsigned char x;

for (x=0;x<8;x++) {

membit=P1b0;

ACC=ACC>>1;

ACCMSB=membit;

} P2=ACC;

}

Trang 46

Your Turn! (Data Serialization 2)

• Write a C program to bring in a byte of data serially one bit at a time via P1.0 The MSBshould come in first

Solution (Data Serialization 2)

• Write a C program to bring in a byte of data serially one bit at a time via P1.0 The MSBshould come in first

unsigned char x;

for (x=0;x<8;x++) {

membit=P1b0;

ACC=ACC<<1;

regALSB=membit;

} P2=ACC;

}

Trang 47

Write a program using Timer 0 to create a 10 Hz square wave on P1.0

MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode)LOOP: MOV TH0, #HIGH(-50000); high byte of -50,000

MOV TL0, #LOW(-50000) ; low byte of -50,000

Write a program using Timer 0 to create a 10 Hz square wave on P1.0

#include <reg51.h>

sbit LED1 = P1^0;

int count;

main() { TMOD = 0x01;

Trang 48

Ex: Write a program that sets up the on-chip serial port, sets the baud rate, and waits for a character When a character is received, it

is transmitted using the putchar function

#include ”reg51.h”

#include ”stdio.h”

void main (void){

SCON = 0x50; //SCON: mode 1, 8-bit UART, enable receiver TMOD |= 0x20;//TMOD: timer 1, mode 2, 8-bit reload TH1 = -13 ; //TH1: reload value for 2400 baud

MOV R2,#10 ;number of loops MOV R0,#30H ;starting address LOOP: MOV A,@R0 ;get data

ACALL SEND ;send data INC R0 ;increase pointer DJNZ R2,LOOP ;loop 10 times SJMP DONE

SEND: JNB TI,$ ;transmit buffer empty? No:check again

CLR TI ;yes: clear flag and MOV SBUF,A ; send data

96

Lê Chí Thông

Trang 49

Transmission – C LanguageAssume a 10-byte string of data is stored in the internal RAM from the location 30H Write a program that sends this string to the 8051 serial port (1200 baud, crystal 11.0592 MHz)

unsigned char x;

for (x=0;x<8;x++) {

membit=P1b0;

ACC=ACC<<1;

regALSB=membit;

} P2=ACC;

}

Trang 50

• Mazidi, Mazidi and McKinlay, The 8051 Microcontroller and Embedded Systems: Using Assembly and C

Ngày đăng: 13/04/2023, 08:09

w