Tải bản đầy đủ (.pdf) (33 trang)

402066 tn vđk eng

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (781.3 KB, 33 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b>TON DUC THANG UNIVERSITY </b>

<b>FACULTY OF ELECTRICAL-ELECTRONICS ENGINEERING</b>

<b>DIVISION OF ELECTRONICS-TELECOMMUNICATIONS</b>

<b>Microcontrollers Laboratory Manual</b>

Edited by Division of Electronics-Telecommunication (Internal only)

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

2

<b>Unit 1 – Get familiar with the Proteus software. </b>

<b>UNIT OBJECTIVE </b>

 Students know how to use Proteus software to draw circuit

 To use to simulate the circuit, microcontroller circuits as PIC16F877A

The circuit diagram.

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

<b>3) The steps for creating simulation circuit run in Protues </b>

Step 1: Get in Protues components.

Method 1: Press the P button on the keyboard. Method 2: Press icon P on interface

Terminals

(Contains resources and Gnd)

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

4

The interface appears as follows when we choose:

Step 2: Get the parts and arranged in principle diagram:

Name of device

Name of components

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

Step 3: Connect wires

Step 4: For running the application circuit.

<b>Exercise 1: Insert the following circuit in Protues </b>

To run the applicative circuit

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

6

<b>Exercise 2: Change the variable resistor R3, use the oscilloscopes to measure waves (Get in the </b>

instrumentation see the previous section)

<b>Exercise 3: </b>

Draw microcontroller circuit as follows, Programming for leds Light up.

<small>RC1/T1OSI/CCP2</small> <sup>16</sup><small>RC2/CCP1</small> <sup>17</sup><small>RC3/SCK/SCL</small> <sup>18</sup>

<small>RD0/PSP0</small> <sup>19</sup><small>RD1/PSP1</small> <sup>20</sup><small>RB7/PGD</small> <sup>40</sup><small>RB6/PGC</small> <sup>39</sup><small>RB5</small> <sup>38</sup><small>RB4</small> <sup>37</sup><small>RB3/PGM</small> <sup>36</sup><small>RB2</small> <sup>35</sup><small>RB1</small> <sup>34</sup><small>RB0/INT</small> <sup>33</sup>

<small>RD7/PSP7</small> <sup>30</sup><small>RD6/PSP6</small> <sup>29</sup><small>RD5/PSP5</small> <sup>28</sup><small>RD4/PSP4</small> <sup>27</sup><small>RD3/PSP3</small> <sup>22</sup><small>RD2/PSP2</small> <sup>21</sup><small>RC7/RX/DT</small> <sup>26</sup><small>RC6/TX/CK</small> <sup>25</sup><small>RC5/SDO</small> <sup>24</sup><small>RC4/SDI/SDA</small> <sup>23</sup><small>RA3/AN3/VREF+</small>

<small>RC0/T1OSO/T1CKI</small> <sup>15</sup><small>MCLR/Vpp/THV</small>

</div><span class="text_page_counter">Trang 7</span><div class="page_container" data-page="7">

// code for above circuit #include<16f877.h>

#fuses HS,NOWDT,PUT,NOPROTECT

#use delay (clock=4000000) // defind crystal = 4MHz int i=0;

void main() {

set_tris_c(0); while(1) {

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

switch (chon) {

case 0: output_c(0); break;

case 1: output_c(0b00000001); break; case 2: output_c(0b00000011); break; case 3: output_c(0b00000111); break; case 4: output_c(0b00001111); break; case 5: output_c(0b00011111); break; case 6: output_c(0b00111111); break; case 7: output_c(0b01111111); break; case 8: output_c(0b11111111); break; default: break;

}

delay_ms(300); }

} }

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

8

<b>Exercise 4: </b>

Draw the microcontroller circuit, Programming for leds Light as follows: D1D2D3D4D5D6D7=10101010 0101010110101010…..

#include <16F877A.h> #device *=16 ADC=10 #use delay(clock=20000000)

#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT

/* Define of PIN of IC74HC595 */ #define SHCP_PIN PIN_C0 #define DS_PIN PIN_C1 #define STCP_PIN PIN_C2 void IC_74hc595(int data) {

int i;

output_low(SHCP_PIN); for(i=0;i<=7;i++)

{

if((data & 0x80)==0) output_low(DS_PIN); else

output_high(DS_PIN); data=data<<1;

output_high(SHCP_PIN); output_low(SHCP_PIN); }

}

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

void IC_74HC595_Output() {

output_low(STCP_PIN); delay_us(5);

output_high(STCP_PIN); output_low(STCP_PIN); }

void main(void) {

while(1) {

IC_74hc595(0xAA); IC_74HC595_Output(); delay_ms(500);

IC_74hc595(0x55); IC_74HC595_Output(); delay_ms(500);

} }

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

10

<b>Unit 2 – interface with 7-segment LED </b>

<b>UNIT OBJECTIVE </b>

Students know how to:

 Create a project in Proteus.

 Students know how to use Proteus software to draw circuit  To use to simulate IN/OUT on microcontroller as PIC6F877A  Implement circuit on experimental kit

<b>1) Related Knowledge </b>

The control registers entry port

- Each port has three registers control main activities:

- The bit in the TRIS register: set foot respectively input (logic 1) or output (logic 0). - The bit in the PORT registers: Read vacuum level from the corresponding logic. - The bit in the register LAT: write logic levels corresponding to the foot.

<b>2) Practice </b>

<b>Exercise 1: </b>

<i>LED display is connected as follows, program to display the numbers from 00 to 99: </i>

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

// Program for display from 00 to 99 #include<16f877.h>

#fuses HS,NOWDT,NOPROTECT

#use delay(clock=4000000) // define crystal = 4MHz

Const char MA7D[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; void main()

{

int chuc,dv,i,z; set_tris_d(0x00); set_tris_b(0x00); while(true) {

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

Chuc=i/10; dv=i%10; z=MA7D[chuc]; Output_b(z); z=MA7D[dv]; Output_d(z); Delay_ms(300); }

} }

<b>Exercise 2: The students Students connect the circuit as shown below, writing program number </b>

65535

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

12 #include <7_seg.h>

#define Data PIN_B0 //dinh nghia chan du lieu cho 595 #define sh PIN_B1 //dinh nghia chan SH cho 595 #define st PIN_B2 //dinh nghia chan ST cho 595

//ma led 7 doan co dau cham nam o tren

unsigned char ma[10]={0x81,0xed,0xc2,0xc8,0xac,0x98,0x90,0xcd,0x80,0x88}; //ma led 7 doan co dau cham nam o duoi

unsigned char ma1[10]={0x18,0x7b,0x2c,0x29,0x4b,0x89,0x88,0x3b,0x08,0x09}; //mang chua cac so can hien thi len led 7 doan

unsigned char so[6]={0,1,2,3,4,5};

unsigned char sec, min1, hr,luu;

int1 en; //bien giup cho hieu ung chop tat dau :

//chuong trinh tach lay so de hien thi void Transform_Number(int16 value) {

so[1]=value/10000; //so hang van so[2]=(value-so[1]*10000)/1000;//so ngan so[3]=(value-so[1]*10000-so[2]*1000)/100;

so[4]=(value-so[1]*10000-so[2]*1000-so[3]*100)/10; so[5]=value%10;

}

//chuong trinh dich du lieu ra 595 void send_595(unsigned char da) {

signed char i; for(i=7;i>=0;i--) {

output_bit(Data,(da>>i)&0x01); output_bit(sh,0);

output_bit(sh,1); }

send_595(~(1<<(i+1)));

temp=((i%2)==0)? ma[so[i]]:ma1[so[i]]; //chon ma led 7 doan thich hop if(((i==1)||(i==3))&&(en==1)) temp&=0xf7; //hien dau cham ngan cach if(((i==2)||(i==4))&&(en==1)) temp&=0x7f; //hien dau cham ngan cach send_595(temp);

output_bit(st,0); output_bit(st,1); delay_ms(1);

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

send_595(0xff); send_595(0xff); output_bit(st,0); output_bit(st,1); delay_us(100); }

}

void main() {

signed char i;

// TODO: USER CODE!! set_tris_b(0x00);

while(true) {

Transform_Number(65535); //lam dau : chop tat

if(luu!=sec)en=~en; luu=sec;

//---

for(i=0;i<10;i++)display(); }

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

14

<b>Unit 3 – Interface with matrix led. </b>

<b>UNIT OBJECTIVE </b>

Students know how to:

 Create a project in Proteus and experimental kit.  Write CCS program.

 Translation and load program into the PIC microcontroller.  Run and debug programs.

<b>Exercise 1: Students draw the circuit as shown below, write the program run string "MATRIX </b>

8x16"

#include <16f877a.h>

#fuses NOLVP,NOWDT,PUT,HS #use delay(clock=20000000) #BIT PORTCbits_RC0 = 0X07.0 #BIT PORTCbits_RC1 = 0X07.1 #BIT PORTCbits_RC3 = 0X07.3 const unsigned char font[] = {

127, 127, 14, 28, 14, 127, 127, 0, //'M' 124, 126, 19, 19, 126, 124, 0, 0, //'A' 3, 65, 127, 127, 65, 3, 0, 0, //'T' 65, 127, 127, 9, 25, 127, 102, 0, //'R' 0, 65, 127, 127, 65, 0, 0, 0, //'I'

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

67, 103, 60, 24, 60, 103, 67, 0, //'X' 0, 0, 0, 0, 0, 0, 0, 0, //' ' 54, 127, 73, 73, 127, 54, 0, 0, //'8' 68, 108, 56, 16, 56, 108, 68, 0, //'x' 64, 66, 127, 127, 64, 64, 0, 0, //'1' 60, 126, 75, 73, 121, 48, 0, 0, //'6' 0, 0, 0, 0, 0, 0, 0, 0, //' ' 0, 0, 0, 0, 0, 0, 0, 0, //' ' };

void main() {

unsigned rol, delay, col; SET_TRIS_B(0x00); SET_TRIS_C(0x00); OUTPUT_B(0x00); OUTPUT_C(0x00); while(1)

{

for(rol=0;rol<112;rol++)

{ //font. 13 x 8[8x8 matrix] = 112. for(delay=0;delay<20;delay++) {

PORTCbits_RC0 = 1; PORTCbits_RC0 = 0; PORTCbits_RC1 = 1; PORTCbits_RC3 = 1; PORTCbits_RC3 = 0;

for(col=0;col<16;col++) // 16 hang va cot {

OUTPUT_B (~ font[col + rol]); delay_us(300);

PORTCbits_RC0 = 1; PORTCbits_RC0 = 0; PORTCbits_RC1 = 0; PORTCbits_RC3 = 1; PORTCbits_RC3 = 0;

} } } }

}

<b>Application exercises: </b>

<b>Exercise 2: Write a program that runs the string "LOP VIDIEUKHIEN" from right to left. Exercise 3: Write a program that runs the string "DH TON DUC THANG" from left to right. </b>

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

16

<b>Unit 4 – Interface with the LCD, LED </b>

<b>UNIT OBJECTIVE </b>

Students know how to:

 Create a project about LCD, LED in Proteus and experimental kit.  Translation and programs loaded into the PIC microcontroller. 

Write a program to Show LCD With CCS-C Language .

<b>Exercise 1:The students connect the circuit schematic as follows, it include DS1307, IC </b>

<small>74LS595Q0</small> <sup>15</sup>

<small>Q1</small> <sup>1</sup><small>Q2</small> <sup>2</sup><small>Q3</small> <sup>3</sup><small>Q4</small> <sup>4</sup><small>Q5</small> <sup>5</sup><small>Q6</small> <sup>6</sup><small>Q7</small> <sup>7</sup><small>Q7'</small> <sup>9</sup><small>SH_CP11</small>

<small>RC1/T1OSI/CCP2</small> <sup>16</sup><small>RC2/CCP1</small> <sup>17</sup><small>RC3/SCK/SCL</small> <sup>18</sup>

<small>RD0/PSP0</small> <sup>19</sup><small>RD1/PSP1</small> <sup>20</sup><small>RB7/PGD</small> <sup>40</sup><small>RB6/PGC</small> <sup>39</sup><small>RB5</small> <sup>38</sup><small>RB4</small> <sup>37</sup><small>RB3/PGM</small> <sup>36</sup><small>RB2</small> <sup>35</sup><small>RB1</small> <sup>34</sup><small>RB0/INT</small> <sup>33</sup>

<small>RD7/PSP7</small> <sup>30</sup><small>RD6/PSP6</small> <sup>29</sup><small>RD5/PSP5</small> <sup>28</sup><small>RD4/PSP4</small> <sup>27</sup><small>RD3/PSP3</small> <sup>22</sup><small>RD2/PSP2</small> <sup>21</sup><small>RC7/RX/DT</small> <sup>26</sup><small>RC6/TX/CK</small> <sup>25</sup><small>RC5/SDO</small> <sup>24</sup><small>RC4/SDI/SDA</small> <sup>23</sup><small>RA3/AN3/VREF+</small>

<small>RC0/T1OSO/T1CKI</small> <sup>15</sup><small>MCLR/Vpp/THV</small>

<small>X1</small> <sup>1</sup>

<small>X2</small> <sup>2</sup><small>SCL6</small>

#define Data PIN_B0 //dinh nghia chan du lieu cho 595 #define sh PIN_B1 //dinh nghia chan SH cho 595 #define st PIN_B2 //dinh nghia chan ST cho 595

//ma led 7 doan co dau cham nam o tren

</div><span class="text_page_counter">Trang 17</span><div class="page_container" data-page="17">

unsigned char ma[10]={0x81,0xed,0xc2,0xc8,0xac,0x98,0x90,0xcd,0x80,0x88}; //ma led 7 doan co dau cham nam o duoi

unsigned char ma1[10]={0x18,0x7b,0x2c,0x29,0x4b,0x89,0x88,0x3b,0x08,0x09}; //mang chua cac so can hien thi len led 7 doan

unsigned char so[6]={0,1,2,3,4,5}; unsigned char sec, min1, hr,luu;

int1 en; //bien giup cho hieu ung chop tat dau : //chuong trinh tach lay so de hien thi

void Transform_Time(char *sec, char *min, char *hr) {

so[0]=(*hr & 0x30) >> 4; //so chuc gio so[1]=*hr & 0x0F; //so gio

so[2]=(*min & 0xF0) >> 4;//so chuc phut so[3]=*min & 0x0F;

so[4]=(*sec & 0x70) >> 4; so[5]=*sec & 0x0F; }

//chuong trinh dich du lieu ra 595 void send_595(unsigned char da) {

signed char i; for(i=7;i>=0;i--) {

output_bit(Data,(da>>i)&0x01); output_bit(sh,0);

output_bit(sh,1); }

send_595(~(1<<(i+1)));

temp=((i%2)==0)? ma[so[i]]:ma1[so[i]]; //chon ma led 7 doan thich hop if(((i==1)||(i==3))&&(en==1)) temp&=0xf7; //hien dau cham ngan cach if(((i==2)||(i==4))&&(en==1)) temp&=0x7f; //hien dau cham ngan cach send_595(temp);

output_bit(st,0); output_bit(st,1); delay_ms(1);

</div><span class="text_page_counter">Trang 18</span><div class="page_container" data-page="18">

18

send_595(0xff); send_595(0xff); output_bit(st,0); output_bit(st,1); delay_us(100); }

}

void main() {

signed char i;

setup_adc_ports(NO_ANALOGS); setup_adc(ADC_OFF);

setup_psp(PSP_DISABLED); setup_spi(FALSE);

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); setup_timer_1(T1_DISABLED);

setup_timer_2(T2_DISABLED,0,1); setup_comparator(NC_NC_NC_NC); setup_vref(FALSE);

// TODO: USER CODE!! set_tris_b(0x00);

while(true) {

ds1307_get_time(&hr,&min1,&sec); Transform_Time(&sec,&min1,&hr); //lam dau : chop tat

if(luu!=sec)en=~en; luu=sec;

//---

for(i=0;i<10;i++)display(); }

}

<b>Application exercises: </b>

<b>Exercise 2: Write the program read the date, month and year data from the DS1307 and then </b>

display on the 7 segment Leds.

</div><span class="text_page_counter">Trang 19</span><div class="page_container" data-page="19">

<b>Exercise 3: The students connect the scheme of circuit interface with LCD as follows: </b>

Write a program to read date, month and year data from DS1307 and then display on LCD

</div><span class="text_page_counter">Trang 20</span><div class="page_container" data-page="20">

#fuses HS,NOWDT,PUT,NOPROTECT #use delay (clock = 20000000)

#include <LCD.c> int8 nhietdo=0; void main() {

set_tris_d(0); lcd_init();

</div><span class="text_page_counter">Trang 21</span><div class="page_container" data-page="21">

delay_ms(10);

setup_adc(ADC_CLOCK_INTERNAL); setup_adc_ports(AN0_AN1_AN3 ); set_adc_channel(0);

delay_us(10); while(1) {

int1 done = adc_done(); while(!done) {

done = adc_done(); }

nhietdo = read_adc(); lcd_gotoxy(1,1);

printf(lcd_putc,"nhiet do la : %d%d",nhietdo/10,nhietdo%10); }

<b>Applicative Exercises: </b>

<b>Exercise 2: Integrated LCD module, retrieve the value from the a variable resistor voltage </b>

displayed on the LCD.

<b>Exercise 3: Use of PIC ADC module to measure the temperature in the room, use the LCD to </b>

display temperature value with LCD-mode of 8 bits as follows:

</div><span class="text_page_counter">Trang 22</span><div class="page_container" data-page="22">

The structure of the keyboard is as follows:

<b>Exercise 1: Write a program to retrieve data from a keypress and then displays the binary </b>

value of the key press on the single LED.

</div><span class="text_page_counter">Trang 23</span><div class="page_container" data-page="23">

// Program for Exercise 1

#include <16F877A.h> #include <key_4x4.c>

#use delay(clock=20000000)

void main() {

unsigned int8 key; set_tris_b(0x00); key_4x4_init();

while(TRUE) {

key=get_key_4x4(); if(key)

{

output_b(key-48);

while(key)key=get_key_4x4(); }

delay_ms(10); }

#ifndef __KEY_4X4_ #define __KEY_4X4_

static unsigned char KEY_4X4[4][4]={'7','8','9','/', '4','5','6','*',

'1','2','3','-', '#','0','=','+'}; void key_4x4_init(void)

{

output_drive(ROW1); output_drive(ROW2); output_drive(ROW3); output_drive(ROW4); output_float(COL1);

</div><span class="text_page_counter">Trang 24</span><div class="page_container" data-page="24">

24 output_float(COL3);

output_float(COL4); }

int1 ButtonIsPush(void) {

output_low(ROW1); output_low(ROW2); output_low(ROW3); output_low(ROW4);

if((input(COL1)==0)|(input(COL2)==0)|(input(COL3)==0)|(input(COL4)==0)) return 1;

else return 0; }

void check_row(unsigned char i) {

output_high(ROW1); output_high(ROW2); output_high(ROW3); output_high(ROW4); if(i==0)

{

output_low(ROW1); }

else if(i==1) {

output_low(ROW2); }

else if(i==2) {

output_low(ROW3); }

else {

output_low(ROW4); }

}

unsigned char get_key_4x4() {

unsigned char i; if(ButtonIsPush()) {

delay_ms(5); if(ButtonIsPush()) {

for(i=0;i<4;i++) {

check_row(i);

if(!input(COL1)) return KEY_4X4[3-i][0]; if(!input(COL2)) return KEY_4X4[3-i][1]; if(!input(COL3)) return KEY_4X4[3-i][2]; if(!input(COL4)) return KEY_4X4[3-i][3]; }

</div><span class="text_page_counter">Trang 25</span><div class="page_container" data-page="25">

} }

return 0; }

</div><span class="text_page_counter">Trang 26</span><div class="page_container" data-page="26">

 Translation and programs loaded into the PIC microcontroller.  Use module PWM in practical application

<b>Exercise 1:</b>The students connect the circuit as below figure, write program to control the DC motor:

press the up button to accelerate Press the dw button to slow down Press stop button to stop

</div><span class="text_page_counter">Trang 27</span><div class="page_container" data-page="27">

void phim_up() {

if (!input(UP) && speed_value<1000) {

if (!input(UP)) {

delay_ms(20);

speed_value=speed_value+10; set_pwm1_duty(speed_value); delay_ms(200);

} } }

void phim_dw() {

if (!input(DW) && speed_value>0) {

if (!input(DW)) delay_ms(20);

speed_value=speed_value-10; set_pwm1_duty(speed_value); delay_ms(200);

} }

void main(){

set_tris_b(0xFF); set_tris_d(0x00);

//Timer 2 set up 124 is calculated duty resolution 500 max 1024 10bit setup_timer_2(T2_DIV_BY_16,249,1);

setup_ccp1(CCP_PWM); speed_value=0;

set_pwm1_duty(speed_value);

while(TRUE){ phim_up(); phim_dw();

if(!input(STOP) && speed_value >0) {

speed_value=0;

set_pwm1_duty(speed_value); }

}

</div>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×