پاسخ : نمونه کدهایی برای XMEGA
تو فایل ضمیمه نوشته شده: 9600.
نوشته اصلی توسط طراح
USART_Baudrate_Set(&USARTC0, 71 , 0);
USART_Baudrate_Set(&USARTC0, 71 , 0);
#include "avr_compiler.h" #include "lcd_0.h" #include "TC_driver.h" #include "port_driver.h" unsigned char __flash keycodes[]={0x88,0x30,0x28,0x24,0x22,0x50,0x48,0x44,0x42,0x90,0x84,0x82}; char __flash chr[]={'0','1','2','3','4','5','6','7','8','9',' ','-'}; #define nokey_code 12 volatile unsigned char key_code=nokey_code; void main() { unsigned char old_key_code=nokey_code; unsigned char i=0; PORT_ConfigurePins(&PORTA,0xFE,false,false,PORT_OPC_PULLDOWN_gc,PORT_ISC_BOTHEDGES_gc); TC0_ConfigClockSource(&TCD0,TC_CLKSEL_DIV1_gc); TC0_SetOverflowIntLevel(&TCD0,TC_OVFINTLVL_LO_gc); TC_SetPeriod(&TCD0,39999); // 20 msec = (39999+1)/2MHZ LCDInit(); PORTD.DIRSET = PIN3_bm; // TXD0 USARTD0_CTRLC=USART_CMODE_ASYNCHRONOUS_gc|USART_PMODE_DISABLED_gc|USART_CHSIZE_8BIT_gc; USARTD0_BAUDCTRLA=12; //Baud Rate = 9600 USARTD0_CTRLB=USART_RXEN_bm|USART_TXEN_bm; PMIC_CTRL=PMIC_LOLVLEN_bm; //set low level interrupt enable __enable_interrupt(); LCDGotoXY(0,0); while(1) { if(USARTD0_STATUS & USART_RXCIF_bm) { LCDSendData(USARTD0_DATA); // display received data on LCD if(++i==16) { i=0; LCDGotoXY(0,0); } } if (key_code!= old_key_code) { old_key_code=key_code; if (key_code!=nokey_code) { while( !(USARTD0_STATUS & USART_DREIF_bm)); //wait for data register empty USARTD0_DATA=chr[key_code]; //send data to PC } } } } ISR(TCD0_OVF_vect) { unsigned char variable,i; PORT_SetDirection( &PORTA,0XE0 ); // ROWs=OUT COLUMNs=IN PORT_SetOutputValue( &PORTA,0XE0 ); // ROWs=1 delay_us(10); // delay for signal stablization variable=(PORTA_IN & 0X1E); PORT_SetDirection( &PORTA,0X1E ); // ROWs=IN COLUMNs=OUT PORT_SetOutputValue( &PORTA,0X1E ); // COLUMNs=1 delay_us(10); variable |=(PORTA_IN & 0XE0); for(i=0;i<12;i++) { if(variable==keycodes[i]) { key_code=i; break; } else key_code=nokey_code; } }
دیدگاه