báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT báo cáo thực hành kỹ thuật vi xử lý PTIT
Trang 2
HỌC VIỆN CÔNG NGHỆ BƯU CHÍNH VIỄN THÔNG
CƠ SỞ TẠI THÀNH PHỐ HỒ CHÍ MINH
BÁO CÁO MÔN KỸ THUẬT VI XỬ LÝ
TPHCM - 2011
GIẢNG VIÊN HƯỚNG DẪN: Phạm Thế Duy
SINH VIÊN THỰC HIỆN: Đinh Tuấn Hải
Lớp Đ09VTA1 MSSV: 409160010
Trang 3Lưu ý:
+ Các mạch mô phỏng được thực hiện trên Proteus 7.8 SP2 và CodeVision 2.05.0
Professional với các điều kiện được ghi chú sẵn
+ Các ghi chú trong code chỉ được ghi 1 lần, theo thứ tự xuất hiện của dòng lệnh (Có những code thừa nhưng do xuất hiện trước nên vẫn được ghi chú bình thường)
+ Các bài tập có sử dụng bộ định thời để chạy với ý nghĩa thời gian thực có thể hoạt động sai khi sử dụng trên các máy tính khác, và phải hiệu chỉnh lại cho thích hợp Các bài tập này mang ý nghĩa thuật toán, giải thuật nhiều hơn ý nghĩa chính xác về mặt thời gian Tất cả các bài đã được hiệu chỉnh để chạy chính xác trên hệ thống máy tính của cá nhân người lập trình, do vậy có thể mang tính chủ quan
TP.HCM ngày 10 tháng 10 năm 2011
Sinh viên thực hiện Đinh Tuấn Hải
Trang 4MỤC LỤC
B I 1 ĐIỀU HI N C NG T C ĐƠN V ED ĐƠN 4
B I 2 HI N TH T ÊN ED 7 Đ ẠN 17
B I 3 ĐIỀU HI N ĐỌC B N HÍ A T N HI N TH HÍ T ÊN ED 7 Đ ẠN 27
B I 4 ĐIỀU HI N HI N TH T ÊN CD 38
B I 5 ĐIỀU HI N ĐỌC ĐIỆN DC B NG ADC V HI N TH ÊN CD 51
B I 6 Đ NHIỆT Đ V HI N TH T ÊN ED 7 Đ ẠN D NG ẠCH S S NH TƯƠNG TỰ TÍCH H T NG AV 67
BÀI 7: TẠ UNG VU NG SIN TA GI C B NG DAC 83
BÀI 8: GIAO TIẾP NỐI TIẾP USART 96
BÀI 9: THỰC HIỆN ĐỒNG HỒ ĐIỆN TỬ 115
B I 10 ĐÈN TÍN HIỆU GIAO THÔNG 136
Trang 5
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3a Evaluation
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Trang 6Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
// Declare your global variables here
unsigned char phu, phu2;
Trang 7// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
Trang 8// OC0 output: Disconnected
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
Trang 9// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT1: Off
// INT2: Off
Trang 10MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00; //Cài đặt ngắt cho các bộ Timer/Counter
// USART initialization
// USART disabled
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
Trang 17PB1/T1 2 PB2/AIN0/INT2 3
PB3/AIN1/OC0 4
PB4/SS 5 PB5/MOSI 6
PB6/MISO 7
PB7/SCK 8
RESET 9
LED-YELLOW D3
LED-YELLOW D4
LED-YELLOW D5
LED-YELLOW D6
LED-YELLOW D7
LED-YELLOW D8
Trang 18
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3a Evaluation
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l
http://www.hpinfotech.com
Project : Bai thuc hanh so 2
Version :
Date : 8/22/2011
Author : Freeware, for evaluation and
non-commercial use only
Trang 19Company :
Comments: Dinh Tuan Hai - D09VTA1
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Declare your global variables here
unsigned char led[10] =
{0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b10111110,0b11100000,0b11111110,0b11110110,}; // dinh nghia cac chu so tren LED
unsigned char pos[5] = {0b00000111,0b00001011,0b00001101,0b00001110,0b00001111}; //dinh nghia vi tri chon LED tren bang LED (position)
unsigned char dis[4] = {0,0,0,0}; //dinh nghia so de hien thi tren LED (display)
Trang 20int i,j,k;
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
Trang 21// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
Trang 22// Mode: Normal top=0xFFFF
// OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
Trang 23// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
Trang 24UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
Trang 27}
}
Ghi chú:
+ Nhấn SW1 để thực hiện yêu cầu 1 (hiện 4 số cùng lúc trên bảng LED)
+ Nhấn SW2 để thực hiện yêu cầu 2 (chạy số từ 0-9 từ phải qua trái)
PB0/T0/XCK 1
PB1/T1 2 PB2/AIN0/INT2 3
PB3/AIN1/OC0 4
PB4/SS 5 PB5/MOSI 6 PB6/MISO 7 PB7/SCK 8
RESET 9
Trang 28
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3a Evaluation
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Trang 29Data Stack size : 256
//dinh nghia cac ky tu se hien tren LED
unsigned char led[16] =
{0b11111100,0b01100000,0b11011010,0b11110010,0b01100110,0b10110110,0b10111110,0b11100000,0b11111110,0b11110110, 0b11101110, 0b00111110, 0b10011100, 0b01111010, 0b10011110, 0b10001110};
//dinh nghia cac vi tri cua LED, pos[4] la khong chon vi tri nao(xoa)
unsigned char pos[5] = {0b00000111,0b00001011,0b00001101,0b00001110,0b00001111}; unsigned char dis[4] = {0,0,0,0};
int TIMVITRI(unsigned char p) //chuong trinh tim vi tri bit 0 trong p
Trang 30// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out
// State7=P State6=P State5=P State4=P State3=1 State2=1 State1=1 State0=1
PORTA=0xFF;
DDRA=0x00; //Cong A nhan tin hieu vao
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
Trang 31// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
Trang 32TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
Trang 33// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
Trang 34// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
Trang 38PB3/AIN1/OC0 4
PB4/SS 5 PB5/MOSI 6 PB6/MISO 7 PB7/SCK 8
RESET 9
Trang 39
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Trang 40Data Stack size : 256
#define data PORTD
void wait_LCD(void) //chương trình con nhằm đợi cho LCD sẵn sàng {
unsigned char tam;
Trang 43// Declare your global variables here
char line1[] = {" Dinh Tuan Hai"}; //chuỗi được hiển thị ở hàng 1
char line2[] = {" 409160010 "}; //chuỗi được hiển thị ở hàng 2
Trang 44// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
Trang 45// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0x07;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge
Trang 46// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
Trang 47// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
Trang 48// Place your code here
while (PINC.3 == 1) //Thuc hien yeu cau so 1
Trang 52
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l
http://www.hpinfotech.com
Project : Bai thuc hanh so 5
Version :
Date : 9/9/2011
Author : Dinh Tuan Hai
Company : D09VTA1 - PTIT - 409160010
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Trang 53Data Stack size : 256
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input) //chương trình con đọc ADC
{
Trang 54ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
Trang 57unsigned char i,j;
i = in & 0x0f; //lấy 4 bit thấp
Trang 58unsigned char i,j,in,tam;
unsigned int value,bit_value;
Trang 59// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
Trang 60// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out // State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
Trang 61// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
Trang 62// Analog Comparator initialization
// Analog Comparator: Off
Trang 63// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: ADC Stopped
// Only the 8 most significant bits of
// the AD conversion result are used
Trang 64set(0x0c);
set(0x02);
while (1)
{
// Place your code here
while (PINC.3 == 0) // Thuc hien yeu cau so 1
{
in = read_adc(0); //đọc ADC vào
display_hexa(in); //hiển thị mã hexa lên LCD
Trang 67PB1/T1 2 PB2/AIN0/INT2 3
PB3/AIN1/OC0 4
PB4/SS 5 PB5/MOSI 6
PB6/MISO 7
PB7/SCK 8
RESET 9
XTAL2
12 XTAL113
PD0/RXD 14PD1/TXD 15PD2/INT0 16PD3/INT1 17PD4/OC1B 18PD5/OC1A 19PD6/ICP1 20PD7/OC2 21
PC0/SCL 22PC1/SDA 23PC2/TCK 24PC3/TMS 25PC4/TDO 26PC5/TDI 27PC6/TOSC1 28PC7/TOSC2 29
AREF 32AVCC 30
0
Trang 68Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l
http://www.hpinfotech.com
Project :
Version :
Date : 9/19/2011
Author : Dinh Tuan Hai - 409160010
Company : D09VTA1 - PTIT
Comments: Bai thuc hanh so 6
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 8.000000 MHz
Memory model : Small
External RAM size : 0
Trang 69Data Stack size : 256
#define data PORTD
long int i,j=0,x;
long int time,temp;
int k;
float v,tempf;
unsigned char timedisplay[5] = {0,46,0,0,0}; //chuoi hien thi thoi gian tu nap
unsigned char voltagedisplay[5] = {0,46,0,0,0}; //chuoi hien thi dien ap tren tu
unsigned char tdisplay[4] = {0,0,0,0}; //chuoi hien thi nhiet do
char start[] = {" Starting "}; //Chuoi hien thi khi LCD khoi dong