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

8051 c VI XỬ LÝ

7 1 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 7
Dung lượng 97,15 KB

Nội dung

Microsoft PowerPoint 8051 C ppt [Compatibility Mode] 1 Programming Timers 0 and 1 in 8051 C Mohammad Ravari http //ravari mbme ir/ 8051 Timers in C • In 8051 C we can access the timer registers TH, TL[.]

8051 Timers in C Programming Timers and in 8051 C • In 8051 C we can access the timer registers TH, TL, and TMOD directly using the reg51.h header file – See Example 9-20 • Timers and delay using mode – See Example 9-22 and Example 9-25 • Timers and delay using mode Mohammad Ravari http://ravari.mbme.ir/ Example 9-20 (1/2) – See Examples 9-23 and 9-24 – Look by yourself Example 9-20 (2/2) Write an 8051 C program to toggle bits of P1 continuously with some time delay Use Timer 0, 16-bit mode Solution: Assume XTML=11.0592MHz for the AT89C51 FFFFH-3500H+1=CB00H=51968 1.085às ì51968 56.384ms #include void T0Delay(void); void main(void) { while(1) { //repeat forever P1=0x55; T0Delay(); //time delay P1=0xAA; T0Delay(); }} void T0Delay() { TMOD=0x01; //Timer 0, Mode TL0=0x00; TH0=0x35; //initial value TR0=1; //turn on T0 while (TF==0); //text TF to roll over TR0=0; //turn off T0 TF0=0; } //clear TF0 Example 9-25 (1/3) A switch is connected to P1.7 Write an 8051 C program to monitor SW and create the following frequencies on P1.5 SW=0: 500 Hz; SW=1: 750 Hz Using Timer 0, mode Assume XTML=11.0592MHz for the AT89C51 Solution: #include sbit mybit=P1^5; sbit SW=P1^7; void T0Delay(void); Example 9-25 (3/3) void T0Delay(unsigned char c) { TMOD=0x01; //Timer 0, Mode if (c==0) { TL0=0x67; TH0=0xFC }; // FFFFH-FC67H+1=921, about 999.285µs, 500 Hz else { TL0=0x9A; TH0=0xFD }; // FFFFH-FD9AH+1=614, about 666.19µs, 750 Hz TR0=1; while (TF==0); TR0=0; TF0=0; } Example 9-25 (2/3) void main(void) { SW=1; //make P1.7 an input pin while(1) { //repeat forever mybit=~mybit; //toggle if (SW==0) T0Delay(0); //500Hz time delay else T0Delay(1); //750Hz time delay }} Time Delay with Different Chips • Although the numbers of clocks per machine cycle are vary with different versions, the frequency for the timer is always 1/12 the frequency of the crystal – To maintain compatibility with the original 8051 • The same codes put on AT89C51 and DS89C420 will generate different time delay – The factors of C compiler and MC of others instructions • The C compiler is a factor in the delay size since various 8051 C compilers generate different hex code sizes So, we still have approximate delay only – See Examples 9-21 Example 9-21 (1/3) Example 9-21 (2/3) Write an 8051 C program to toggle bits P1.5 continuously every 50 ms Use Timer 0, mode for (a) AT89C51 and (b) DS89C420 Solution: (a) Assume XTML=11.0592MHz for the AT89C51 FFFFH-4BFDH+1=B403H=46083 1.085às ì46083 ≈ 50ms #include void T0Delay(void); sbit mybit=P1^5; void main(void) { while(1) { //repeat forever mybit=~mybit; T0Delay(); }} void T0Delay() { TMOD=0x01; //Timer 0, Mode TL0=0xFD; TH0=0x4B; //initial value TR0=1; //turn on T0 while (TF0==0); //BACK: JNZ TF0,BACK TR0=0; //turn off T0 TF0=0; } //clear TF0 Example 9-21 (3/3) (b) Assume XTML=11.0592MHz for the DS89C4x0 FFFFH-4BFDH+1=B403H=46083 1.085às ì46083 50ms 8051 Counters in C • External pulses to T0 (P3.4) and T1 (P3.5) • See Examples 9-26 to 9-29 void T0Delay() { TMOD=0x01; //Timer 0, Mode TL0=0xFD; TH0=0x4B; //initial value TR0=1; //turn on T0 while (TF0==0); //BACK: JNZ TF0,BACK TR0=0; //turn off T0 TF0=0; } //clear TF0 Example 9-26 (1/2) Example 9-26 (2/2) Assume that a 1-Hz external clock is being fed into T1 (P3.5) Write a C program for counter in mode to count up and display the state of the TL1 count on P1 Start the count at 0H Solution: P1 is connected to LEDs TH1 T1 is connected to 1Hz external clock P1 TL1 P3.5 1Hz T1 LEDs #include sbit T1=P3^5; void main(void) { T1=1; //make T1 an input pin TMOD=0x60; //counter 1, mode TH1=0; //reload value while(1) { //repeat forever {TR1=1; P1=TL1;} while (TF1==0); TR1=0; TF1=0; //clear flags }} Transmitting and Receiving Data in C • Using C to program the serial port – Example 10-15 Serial Port Programming in C Example 10-15 Write an 8051 C program to transfer the letter “A” serially at 4800 baud continuously Use 8-bit data and stop bit Solution : #include void main(void) { TMOD=0x20; SCON=0x50; TH1=0xFA; //4800 baud rate TR1=1; while (1) { SBUF=‘A’; while (TI==0); TI=0; } } 8051C Interrupt Numbers Interrupt Programming in C Table 11–4 8051/52 Interrupt Numbers in C • The 8051 C compilers have extensive support for the 8051 interrupts with two major features as follows: – They assign a unique number to each of the 8051 interrupts, as shown in Table 11-4 – It can also assign a register bank to an ISR This avoids code overhead due to the pushes and pops of the R0-R7 Example 11-14 (1/3) Write an 8051 C program that continuously gets a single bit of data form P1.7 and sends it to P1.0 While simultaneously creating a square wave of 200 µs period on pin P2.5 Use timer to create the square wave Assume XTML=11.0592MHz Solution: 8051 Using Timer 0, mode LED P1.0 100µs /1.085µs =92 TH0=256-92=164=A4H Switc h P1.7 P2.5 200µ µs Example 11-14 (3/3) void main(void) { //setup Timer Interrupt TMOD=0x02; TH0=0xA4; //TH0=-92 IE=0x82; //enable interrupts for timer TR0=1; //start the timer //setup SW → IND SW=1; //make P1.7 an input pin while(1) { IND=SW; //send switch to LED }} Example 11-14 (2/3) #include sbit SW=P1^7; sbit IND=P1^0; sbit WAVE=P2^5; //Interrup subroutine for creating WAVE void timer0(void) interrupt { WAVE=~WAVE; //toggle pin } Example 11-15 (1/4) Write a C program that continuously gets a single bit of data from P1.7 and sends it to P1.0 in the main, while as same as simultaneously Ex 11-14 (a) creating a square wave of 200 µs period on pin P2.5 (b) sending letter ‘A’ to the serial port Use timer to create the 8051 square wave LED P1.0 Assume XTML=11.0592MHz Use the 9600 baud rate Switch Serial Port P1.7 P2.5 TxD 200µ µs Example 11-15 (2/4) Solution: #include sbit SW=P1^7; sbit IND=P1^0; sbit WAVE=P2^5; //Interrup subroutine for creating WAVE void timer0(void) interrupt { WAVE=~WAVE; //toggle pin } Example 11-15 (3/4) //Interrup subroutine for sending ‘A’ void serial0() interrupt { if (TI==1) { SUBF = ‘A’; //send ‘A’ TI=0; //clear interrupt } else { RI=0; //clear interrupt } } Example 11-15 (4/4) void main(void) { SW=1; //make P1.7 an input pin TH1= -3; //9600 baud rate TMOD=0x22;//mode for both timers TH0=0xA4; //TH0=-92 SCON=0x50; TR0=1; TR1=1; IE=0x92; //enable interrupts for timer while(1) { IND=SW; //send switch to LED }}

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

w