402066 tn vđk eng

33 0 0
Tài liệu đã được kiểm tra trùng lặp
402066 tn vđk eng

Đ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

CDACDACIUDVHDBIAUVBNAXMV X CLXLCKALCSA;CSACSSCCJNALCDDDDDDDDDDDDDDDDDDDDDDDDĐNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

Trang 1

TON DUC THANG UNIVERSITY

FACULTY OF ELECTRICAL-ELECTRONICS ENGINEERING

DIVISION OF ELECTRONICS-TELECOMMUNICATIONS

Microcontrollers Laboratory Manual

Edited by Division of Electronics-Telecommunication (Internal only)

Trang 2

2

Unit 1 – Get familiar with the Proteus software

UNIT OBJECTIVE

 Students know how to use Proteus software to draw circuit

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

The circuit diagram.

Trang 3

3) The steps for creating simulation circuit run in Protues

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)

Trang 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

Trang 5

Step 3: Connect wires

Step 4: For running the application circuit.

Exercise 1: Insert the following circuit in Protues

To run the applicative circuit

Trang 6

6

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

instrumentation see the previous section)

Exercise 3: Draw microcontroller circuit as follows, Programming for leds Light up

RC1/T1OSI/CCP2 16RC2/CCP1 17RC3/SCK/SCL 18

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

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

RC0/T1OSO/T1CKI 15MCLR/Vpp/THV

Trang 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); }

} }

Trang 8

8

Exercise 4: 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); }

}

Trang 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);

} }

Trang 10

10

Unit 2 – interface with 7-segment LED

UNIT OBJECTIVE

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

1) Related Knowledge

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

2) Practice

Exercise 1: LED display is connected as follows, program to display the numbers from 00 to 99:

Trang 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); }

} }

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

65535

Trang 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);

Trang 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(); }

Trang 14

14

Unit 3 – Interface with matrix led

UNIT OBJECTIVE

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

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

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'

Trang 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;

} } } }

}

Application exercises:

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

Trang 16

16

Unit 4 – Interface with the LCD, LED

UNIT OBJECTIVE

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

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

74LS595Q0 15

Q1 1Q2 2Q3 3Q4 4Q5 5Q6 6Q7 7Q7' 9SH_CP11

RC1/T1OSI/CCP2 16RC2/CCP1 17RC3/SCK/SCL 18

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

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

RC0/T1OSO/T1CKI 15MCLR/Vpp/THV

X1 1

X2 2SCL6

#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

Trang 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);

Trang 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(); }

}

Application exercises:

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

display on the 7 segment Leds

Trang 19

Exercise 3: The students connect the scheme of circuit interface with LCD as follows:

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

Trang 20

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

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

set_tris_d(0); lcd_init();

Trang 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); }

Applicative Exercises:

Exercise 2: Integrated LCD module, retrieve the value from the a variable resistor voltage

displayed on the LCD

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

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

Trang 22

The structure of the keyboard is as follows:

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

value of the key press on the single LED

Trang 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);

Trang 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]; }

Trang 25

} }

return 0; }

Trang 26

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

Exercise 1: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

Trang 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); }

}

Ngày đăng: 09/05/2024, 00:44

Tài liệu cùng người dùng

Tài liệu liên quan