Mạch giao tiếp Vi điều khiển ATMEGA32 với bàn phím máy tính qua cổng SP2, nội dung nhập phím sẽ hiển thị trên 6 led ma trận 8x8.
Trang 1MẠCH GIAO TIẾP BÀN PHÍM VỚI ATMEGA32 HIỂN THỊ NỘI DUNG TRÊN LED MA TRẬN
1 Tổng quan:
Nội dung được nhập vào từ bàn phím, giao tiếp với AVR 32 qua cổng PS2, VDk giải mã và hiển thị nội dung trên 6 led ma trận 8x8 Chuong trình viết bằng Codevision
2 Hoạt động:
• Quét led thông qua IC 74LS164 để hiển thị chữ bằng phương pháp quét cột
• Sử dụng timer để chạy chữ
• Sử dụng UART đồng bộ giao tiếp với bàn phím để tạo chữ cần hiển thị Nhấn ESC và bắt đầu nhập chữ ,sau khi nhập chữ nhấn Enter để chạy chữ mới
3 Giao tiếp bàn phím qua cổng SP2:
4 Phương thức giao tiếp ( Protocol ):
Từ bàn phím tới Atmega32
Từ Atmega32 tới bàn phím
Trang 2 Mã phím:
5 Lưu đồ giải thuật
Hiển thị chữ:
Chạy chữ
Trang 36 Mạch điện:
Mạch vi điều khiển:
Mạch led:
Trang 47 Code:
/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.8d Professional
Automatic Program Generator
© Copyright 1998-2006 Pavel Haiduc, HP InfoTech s.r.l http://www.hpinfotech.com
Project : MCU project
Version : 1.1
Date : 11/29/2008
Author :
Company :
Comments:
Chip type : ATmega32L
Program type : Application
Clock frequency : 12.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 512
*****************************************************/
#include <mega32.h>
#include <delay.h>
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
unsigned char i,j,k,vitrichu;
unsigned char flash macdinh[] ={
0x39,0xD7,0xEF,0xD7,0x39,0xFF //chu X ,0x7D,0x1,0x7D,0xFF //chu I
,0x1,0xFB,0xF7,0xEF,0x1,0xFF //chu N ,0xFF,0xFF,0xFF //khoang trang
,0x1,0xFB,0xF7,0xEF,0x1,0xFF //chu N ,0x1,0xDF,0xDF,0xDF,0x1,0xFF //chu H
Trang 5,0x07,0x0DB,0xDD,0xDB,0x7,0xFF //chu A ,0x1,0xED,0xED,0xED,0xF3,0xFF //chu P ,0xFF,0xFF,0xFF //khoang trang ,0x83,0x7D,0x7D,0x7D,0x0BB,0xFF //chu C ,0x1,0xDF,0xDF,0xDF,0x1,0xFF //chu H ,0x81,0x7F,0x7F,0x7F,0x81,0xFF //chu U };
unsigned char flash bangchu[220] ={
0x07,0x0DB,0xDD,0xDB,0x7,0xFF //chu A ,0x1,0x6D,0x6D,0x6D,0x93,0xFF //chu B ,0x83,0x7D,0x7D,0x7D,0x0BB,0xFF //chu C ,0x1,0x7D,0x7D,0x7D,0x83,0xFF //chu D ,0x1,0x6D,0x6D,0x7D,0x7D,0xFF //chu E ,0x1,0xED,0xED,0xED,0xFD,0xFF //chu F ,0x83,0x7D,0x6D,0x6D,0x8B,0xFF //chu G ,0x1,0xDF,0xDF,0xDF,0x1,0xFF //chu H ,0x1,0xEF,0xD7,0xBB,0x7D,0xFF //chu K ,0x1,0x7F,0x7F,0x7F,0x7F,0xFF //chu L ,0x1,0xFB,0xF7,0xFB,0x1,0xFF //chu M ,0x1,0xFB,0xF7,0xEF,0x1,0xFF //chu N ,0x83,0x7D,0x7D,0x7D,0x83,0xFF //chu O ,0x1,0xED,0xED,0xED,0xF3,0xFF //chu P ,0x83,0x7D,0x5D,0xBD,0x43,0xFF //chu Q ,0x1,0xED,0xCD,0xAD,0x73,0xFF //chu R ,0xB3,0x6D,0x6D,0x6D,0x9B,0xFF //chu S ,0xFD,0xFD,0x1,0xFD,0xFD,0xFF //chu T ,0x81,0x7F,0x7F,0x7F,0x81,0xFF //chu U ,0xC1,0xBF,0x7F,0xBF,0xC1,0xFF //chu V ,0x39,0xD7,0xEF,0xD7,0x39,0xFF //chu X ,0xF9,0xF7,0xF,0xF7,0xF9,0xFF //chu Y ,0x3D,0x5D,0x6D,0x75,0x79,0xFF //chu Z ,0xBD,0x7D,0x81,0xFD,0xFD,0xFF //chu J ,0x1,0xBF,0xDF,0xBF,0x1,0xFF //chu W ,0x7D,0x1,0x7D,0xFF //chu I
,0x77,0x7B,0x1,0x7F,0x7F,0xFF //so 1 ,0x7B,0x3D,0x5D,0x6D,0x73,0xFF //so 2 ,0xBB,0x7D,0x6D,0x6D,0x93,0xFF //so 3 ,0xC1,0xDF,0xDF,0x7,0xDF,0xFF //so 4 ,0xB1,0x75,0x75,0x75,0x8D,0xFF //so 5 ,0x83,0x6D,0x6D,0x6D,0x9F,0xFF //so 6
Trang 6,0xFD,0xFD,0xFD,0xFD,0x1,0xFF //so 7
,0x93,0x6D,0x6D,0x6D,0x93,0xFF //so 8
,0xF3,0x6D,0x6D,0x6D,0x83,0xFF //so 9
,0x83,0x5D,0x6D,0x75,0x83,0xFF //so 0
};
unsigned int flash cdmd = 63; //chieu dai cua chu mac dinh luc dau unsigned char chu[256]; //chuoi chu can hien thi
unsigned int length; //chieu dai chu nhap moi
bit chonhapchu,breakcode;
void themchu(void)
{
for (i=length;i<length+6;i++)
{
chu[i]=bangchu[vitrichu];
vitrichu++;
if (length>=48) k++;
}
length +=6;
vitrichu=221;
}
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
char data;
data=UDR;
if (data == 0x76) //nut ESC
{
chonhapchu = 1;
TCCR0 =0;
k = 0;
for (i=0;i<length;i++) chu[i] = 0xFF; //xoa chu dang hien thi length = 0;
}
if (data == 0x5A) //nut Enter
{
TCCR0 = 4;
chonhapchu = 0;
Trang 7}
if ((chonhapchu==1)&&(breakcode==0)&&(length<250)) {
switch (data) {
case 0x1C: vitrichu=0; //chu A
break;
case 0x32: vitrichu=6; //chu B
break;
case 0x21: vitrichu=12; //chu C
break;
case 0x23: vitrichu=18; //chu D
break;
case 0x24: vitrichu=24; //chu E
break;
case 0x2B: vitrichu=30; //chu F
break;
case 0x34: vitrichu=36; //chu G
break;
case 0x33: vitrichu=42; //chu H
break;
case 0x42: vitrichu=48; //chu K
break;
case 0x4B: vitrichu=54; //chu L
break;
case 0x3A: vitrichu=60; //chu M
break;
case 0x31: vitrichu=66; //chu N
break;
case 0x44: vitrichu=72; //chu O
break;
case 0x4D: vitrichu=78; //chu P
break;
case 0x15: vitrichu=84; //chu Q
break;
case 0x2D: vitrichu=90; //chu R
break;
case 0x1B: vitrichu=96; //chu S
break;
case 0x2C: vitrichu=102; //chu T
Trang 8break;
case 0x3C: vitrichu=108; //chu U
break;
case 0x2A: vitrichu=114; //chu V
break;
case 0x22: vitrichu=120; //chu X
break;
case 0x35: vitrichu=126; //chu Y
break;
case 0x1A: vitrichu=132; //chu Z break;
case 0x3B: vitrichu=138; //chu j
break;
case 0x1D: vitrichu=144; //chu w break;
case 0x43: //chu I
{
vitrichu=150;
for (i=length;i<length+4;i++)
{
chu[i]=bangchu[vitrichu];
vitrichu++;
if (length>=48) k++;
}
length +=4;
vitrichu=221;
}
break;
case 0x29: //khoang trang for (i=length;i<length+3;i++)
{
chu[i]=0xFF;
if (length>=48) k++;
}
length +=3;
vitrichu=221;
break;
case 0x16: vitrichu=154; //so 1 break;
Trang 9case 0x1E: vitrichu=160; //so 2
break;
case 0x26: vitrichu=166; //so 3
break;
case 0x25: vitrichu=172; //so 4
break;
case 0x2E: vitrichu=178; //so 5
break;
case 0x36: vitrichu=184; //so 6
break;
case 0x3D: vitrichu=190; //so 7
break;
case 0x3E: vitrichu=196; //so 8
break;
case 0x46: vitrichu=202; //so 9
break;
case 0x45: vitrichu=208; //so 0
break;
default:
};
if (vitrichu<220) themchu();
};
if (data == 0xF0) breakcode=1; //break code else breakcode=0;
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Place your code here
j++;
if (j == 7) //j : toc do chay chu
{
k++; //k : bien de chay chu j=0;
}
if (k==length) k=-48;
}
Trang 10
#define data PORTA.0
#define clk PORTA.1
#define clr PORTA.2
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// 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 PORTA=0x00;
DDRA=0xFF;
// 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 PORTB=0x00;
DDRB=0x00;
// Port C initialization
// 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 PORTC=0x00;
DDRC=0xFF;
// 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;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
Trang 11// Clock value: 46.875 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected TCCR0=0x04;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh // OC1A output: Discon
// OC1B output: Discon
// Noise Canceler: Off
// Input Capture on Falling Edge // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh
// OC2 output: Disconnected ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
Trang 12// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Odd Parity // USART Receiver: On
// USART Transmitter: Off
// USART Mode: Sync Slave UCPOL=1
UCSRA=0x00;
UCSRB=0x90;
UCSRC=0xF7;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
k=-48;
length = cdmd;
for (i=0;i<length;i++)
{
chu[i]=macdinh[i];
};
while (1)
{
// Place your code here
clr = 0; //
clr = 1; //xoa man hinh
Trang 13for (i=0;i<48;i++)
{
clk = 0;
if (i==0)
{
data=1;
}
else
{
data=0;
};
clk = 1;
if ((i+k >=length)||(i+k<0)) PORTC = 0xFF; else PORTC = chu[i+k];
delay_us(50);
PORTC = 0xff;
};
}
}