原创 coldfire MCF52259开发板按键检测demo

2010-6-28 00:50 2885 9 9 分类: MCU/ 嵌入式

这是最近做的飞思卡尔coldfire开发板上的演示程序。用mcf52259的adc检测按键。当然这里代码只是demo,有不少不完善的地方。开发板是NSCF52259-R1。


Mon Dec 14 2009





//filename:keyscan.h


#ifndef _NICROSYSTEM_NSCF52259R1_KEYSCAN_H_
#define _NICROSYSTEM_NSCF52259R1_KEYSCAN_H_


extern volatile uint16 g_adcStatus;
void keyscan_init();
void start_adc();
void stop_adc();


#endif







//filename:keyscan.c


#include "support_common.h" /* include peripheral declarations and more */
#include "keyscan.h"
#include "pit.h"


#define DEFAULT_DELAY    0x00D0
#define ALL_ADC          0xFF
volatile uint16 g_adcStatus;


void keyscan_init()
{
 g_adcStatus=0;
 MCF_GPIO_PANPAR =ALL_ADC;
 MCF_ADC_CTRL1=0;
 MCF_ADC_CTRL1|=MCF_ADC_CTRL1_LLMTIE|MCF_ADC_CTRL1_SMODE(0X02);
 MCF_ADC_CTRL2=0X08;//4MHZ ADC clock
 MCF_ADC_ADLST1=0x7654;
 MCF_ADC_ADSDIS=0x00f0;
 /* set ADC interrupt*/
    MCF_INTC0_IMRH &= ~(MCF_INTC_IMRH_INT_MASK51 );
    MCF_INTC0_IMRL &=~( MCF_INTC_IMRL_MASKALL);
    MCF_INTC0_ICR51 =MCF_INTC_ICR_IP(7)+MCF_INTC_ICR_IL(2);
    /*set channel's low limit*/
 MCF_ADC_ADLLMT0=50;
 MCF_ADC_ADLLMT1=50;
 MCF_ADC_ADLLMT2=50;
 MCF_ADC_ADLLMT3=50;
  /* set the power-up delay in ADC */
    MCF_ADC_POWER   = DEFAULT_DELAY;   /* POWER: controls the power management of the ADC module*/
     /* wait until module is powered-up */
    while(MCF_ADC_POWER & MCF_ADC_POWER_PSTS0)  /* PSTS0 register: 0: ADC converter A is currently powered up,
               1: ADC converter A is currently powered down */
    ;         
    return;
}


void start_adc()
{
    while(MCF_ADC_POWER & MCF_ADC_POWER_PSTS0);
 MCF_ADC_CTRL1&=~MCF_ADC_CTRL1_STOP0 ;
 MCF_ADC_CTRL1|=MCF_ADC_CTRL1_START0;
}


void stop_adc()
{
 MCF_ADC_CTRL1|=MCF_ADC_CTRL1_STOP0; 
}



__declspec(interrupt:0) void adcint_isr(void)
{
 uint16 prev_status=0;
 uint16 cur_status=0;
 if (MCF_ADC_ADSTAT & MCF_ADC_ADSTAT_LLMTI)
 {
  prev_status=MCF_ADC_ADLSTAT;
  MCF_ADC_ADLSTAT =0xffff;  //clear interrupt
  MCF_ADC_ADSTAT |= MCF_ADC_ADSTAT_EOSI0;
  MCF_ADC_CTRL1&=~MCF_ADC_CTRL1_LLMTIE;
  stop_adc();
  delay_ms(80000,0,20);
  start_adc();
  //wait for adc conversion finish
  while((MCF_ADC_ADSTAT&MCF_ADC_ADSTAT_EOSI0)==0);
  cur_status=MCF_ADC_ADLSTAT;
  MCF_ADC_ADLSTAT =0xffff;  //clear interrupt
  MCF_ADC_ADSTAT |= MCF_ADC_ADSTAT_EOSI0;
 
  if(cur_status==prev_status)
  {
   g_adcStatus=cur_status; 
   stop_adc();
  }
  else
  {
   g_adcStatus=0;
   MCF_ADC_CTRL1|=MCF_ADC_CTRL1_LLMTIE;
  }
    
 }
}





下面是完整的工程文件,用freescale codewarrior for coldfire V7.1


PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
9
关闭 站长推荐上一条 /3 下一条