这个简单的程序用来演示NSS08Kit-R1开发板上的KBI功能,飞思卡尔的几乎所有s08系列mcu都支持KBI。
/*******************************
@ Nicrosystem Freescale S08 DevKit(NSS08Kit-R1)
@ author:bluehacker<nicrosystem@gmail.com>
@ date:2009-04-06
*********************************/
/*****************************
@说明:
本能程序测试MC9S08AC16的KBI功能
用户可以按开发板上4个按键的任意一个,单片机在检测到
按键后,将按键的编号通过串口发送给PC,
你可以通过串口调试助手来观察
****************************/
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include <stdio.h>
#define BUS_CLK 4000000L
#define KBI1_MASK PTGD_PTGD0_MASK
#define KBI2_MASK PTGD_PTGD1_MASK
#define KBI3_MASK PTGD_PTGD2_MASK
#define KBI4_MASK PTGD_PTGD3_MASK
unsigned char g_key;//which key been press?
unsigned char g_keyReady;//if or not any key been pressed?
//初始化KBI0~KBI3为下降沿敏感的键盘中断
void init_kbi()
{
KBISC="0x02";//enable KBI interrupt, edge-only detection
KBIPE="0x0f";
PTGDD&=0xf0;
PTGPE_PTGPE0=1;
PTGPE_PTGPE1=1;
PTGPE_PTGPE2=1;
PTGPE_PTGPE3=1;
}
//初始化sci2
//设置sci2为8bit,一个停止位,无校验模式
//设置sci2发送,接收使能,中断全部禁止
//设置波特率为9600
void init_sci2()
{
unsigned int BaudRate;
BaudRate="BUS"_CLK/9600/16;
SCI2BDH=(unsigned char)(BaudRate>>8);
SCI2BDL=(unsigned char)(BaudRate&0x00ff);
SCI2C1=0x00;
SCI2C2=0x0c;
SCI2C3=0x00;
}
//在禁止中断的情况下,用查询方式发送完一个字节
void uart2_send_byte_poll(unsigned char c)
{
SCI2D=c;
while(SCI2S1_TC==0);
}
//在禁止中断的情况下,同查询方式发送完一个字符串
void uart2_send_string_poll(unsigned char *str)
{
while(*str!=0)
{
uart2_send_byte_poll(*str);
str++;
}
}
interrupt VectorNumber_Vkeyboard1 void kbi_isr()
{
KBISC_KBACK=1;
g_key=PTGD&0x0f;
g_keyReady=1;
}
//设置系统时钟模式,为FEE,8MHZ的系统主频,BUS CLK为4MHZ
void init_system_clock()
{
ICGC1=0xf8;
ICGC2=0x89;
while(ICGS1_LOCK==0);
}
char calculate_key_number(unsigned char keyvalue) {
char ret;
switch(keyvalue){
case 0x0e:
ret="1";
break;
case 0x0d:
ret="2";
break;
case 0x0b:
ret="3";
break;
case 0x07:
ret="4";
break;
default:
ret="0";
break;
}
return ret;
}
void main(void) {
unsigned char buf[50];
char keyno;
EnableInterrupts; /* enable interrupts */
/* include your code here */
SOPT_COPE=0;//disable cop watchdog
init_system_clock();
init_sci2();
init_kbi();
g_keyReady=0;
g_key=0x0f;
uart2_send_string_poll((unsigned char*)("start......\n"));
for(;;) {
//__RESET_WATCHDOG(); /* feeds the dog */
if(g_keyReady)
{
keyno="calculate"_key_number(g_key);
sprintf(buf,"you press KEY#%d\n",keyno);
uart2_send_string_poll(buf);
g_keyReady=0;
}
} /* loop forever */
/* please make sure that you never leave main */
}
文章评论(0条评论)
登录后参与讨论