|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-1-6 20:06:19
|
显示全部楼层
好像感兴趣的人不多啊,回复的人这么少!
下面上传测试源代码,测试效果见视频。
//ICC-AVR application builder : 2011/12/1 20:30:09
// Target : m48
// Crystal: 8.0000Mhz
#include <iom48v.h>
#include <macros.h>
#define PSCL PC5 //LED+
#define PSDA PC4 //LED-
// 按键编号 引脚编号
#define P01 PC3 // 11
#define P02 PC2 // 10
#define P03 PC1 // 9
#define P04 PC0 // 8
#define P05 PD1 // 13
#define P06 PD0 // 12
#define P07 PB5 // 5 //SCK
#define P08 PB4 // 4 //MISO
#define P09 PD2 // 14
#define P10 PD3 // 15
#define P11 PB2 // 2
#define P12 PB3 // 3 //MOSI
#define P13 PD4 // 16
#define P14 PB6 // 6
#define P15 PB0 // 0
#define P16 PB1 // 1
#define P17 PB7 // 7
#define P18 PD5 // 17
#define P19 PD6 // 18
#define P20 PD7 // 19
#define on 1
#define off 0
//引脚编号=K[按键编号]
unsigned char K[21]={0,11,10,9,8,13,12,5,4,14,15,2,3,16,6,0,1,7,17,18,19};
void led(unsigned char on_off)
{
if(on_off) PORTC &= ~BIT(PSDA);
else PORTC |= BIT(PSDA);
}
void key_tst_prep(unsigned char k)
{
PORTB = 0xFF;
DDRB = 0x00;
PORTC |= 0x6F;
DDRC = 0x30;
PORTD = 0xFF;
DDRD = 0x00;
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCIFR = 0x07; //clear pin change flag2...0
PCICR = 0x07; //pin change enable
if(k<8)
{
PORTB &= ~(1<<k);
DDRB |= (1<<k);
}
else if(k<12)
{
k-=8;
PORTC &= ~(1<<k);
DDRC |= (1<<k);
}
else if(k<20)
{
k-=12;
PORTD &= ~(1<<k);
DDRD |= (1<<k);
}
}
void key_tst_start(unsigned char k)
{
PCIFR = 0x07; //clear pin change flag2...0
if(k<8)
{
PCMSK0 = (1<<k); //pin change mask 0
DDRB &= ~(1<<k);
}
else if(k<12)
{
k-=8;
PCMSK1 = (1<<k); //pin change mask 1
DDRC &= ~(1<<k);
}
else if(k<20)
{
k-=12;
PCMSK2 = (1<<k); //pin change mask 2
DDRD &= ~(1<<k);
}
TCNT0 = 0x00;
}
//4,7,8,12,14,17,19,20
unsigned char KEYNUM=20,KF[21]={0};//按键标记 1按下 0未按
//按键编号 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
unsigned char D[21]={80,26,26,26,36,26,26,60,55,26,26,26,60,26,40,26,26,40,26,40,40};//门限值
#pragma interrupt_handler key_tst:iv_PCINT0
#pragma interrupt_handler key_tst:iv_PCINT1
#pragma interrupt_handler key_tst:iv_PCINT2
void key_tst(void)
{
unsigned char i;
if(TCNT0>D[KEYNUM]) KF[KEYNUM]=on;
else KF[KEYNUM]=off;
for(i=1;i<21;i++)
{
if(KF) {KF[0]=i; break;}
else KF[0]=0;
}
led(KF[0]);
if(KEYNUM++>19) KEYNUM = 1;
}
//TIMER0 initialize - prescale:1
// WGM: Normal
// desired value: 0.125uSec
// actual value: 0.125uSec (0.0%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0xFF; //set count
TCCR0A = 0x00;
TCCR0B = 0x02; //start timer
TIMSK0 = 0x00; //timer 0 interrupt sources
}
//TIMER2 initialize - prescale:32
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer2_init(void)
{
TCCR2B = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x00; //setup
OCR2A = 0x83;
TCCR2A = 0x00;
TCCR2B = 0x02; //start
TIMSK2 = 0x03; //timer 2 interrupt sources
}
#pragma interrupt_handler timer2_compa_isr:iv_TIM2_COMPA
void timer2_compa_isr(void)
{
key_tst_start(K[KEYNUM]);
}
#pragma interrupt_handler timer2_ovf_isr:iv_TIM2_OVF
void timer2_ovf_isr(void)
{
//TCNT2 = 0x06; //reload counter value
key_tst_prep(K[KEYNUM]);
}
//TWI initialize
// bit rate:1
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x01; //set bit rate
TWSR= 0x01; //set prescale
TWAR= 0x00; //set slave address
TWCR= 0x44; //enable twi
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
key_tst_prep(K[KEYNUM]);
timer0_init();
timer2_init();
//twi_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
PRR = 0x0F; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
} |
|