嵌入式——电赛2019—纸张计数(F题)

在这里插入图片描述
在这里插入图片描述

这次比赛我们使用的FDC2214。

简单的说一下吧,我们做这个比赛时写的方法,自检计数,比较最近数值。至于比赛名次省一,多了不想说了。》》》》》》。。。。。。。。。。。

main函数
#include  "systemInit.h"
#include  <uart.h>
#include  "SoftI2C.H"
#include  "SoftI2C_CFG.H"
#include  "uartGetPut.h"
#include "FDC2214.h"
#include <stdio.h>
#include  "lcd_driver.h"
#include  "ZLG7290.h"
#include  "buzzer.h"
#define  PART_LM3S1138
#include  <pin_map.h>
#include <stdlib.h>
#include<time.h>
#include  "systemInit.h"
#include  <timer.h>
#include  "KEY.H"

//  将较长的标识符定义成较短的形式
#define  SysCtlPeriEnable       SysCtlPeripheralEnable
#define  SysCtlPeriDisable      SysCtlPeripheralDisable
#define  GPIOPinTypeIn          GPIOPinTypeGPIOInput
#define  GPIOPinTypeOut         GPIOPinTypeGPIOOutput

#define  Key_1                  GPIOPinRead(GPIO_PORTE_BASE , GPIO_PIN_0)
#define  Key_2                  GPIOPinRead(GPIO_PORTE_BASE , GPIO_PIN_1)
#define  Key_3                  GPIOPinRead(GPIO_PORTD_BASE , GPIO_PIN_3)

//  定义KEY
#define  KEY_PERIPH             SYSCTL_PERIPH_GPIOG
#define  KEY_PORT               GPIO_PORTG_BASE
#define  KEY_PIN                GPIO_PIN_5

int T1 , T2 , T3 , T4 ;
unsigned long k;
int Real_Num , Step_All ;
char a[10];
int Data_Mar[100][2] ;
int Ans_Data ;
double comPare[500] = {0}  ;

void  Data_N ( int Array[][2] , int m );
void Time_Init( void ) {
  SysCtlPeriEnable(SYSCTL_PERIPH_GPIOD);                           //  使能iic所在的GPIO端口
  GPIOPinTypeIn(GPIO_PORTD_BASE, GPIO_PIN_0);                       //  设置iic所在管脚为输入
  GPIOPinTypeIn(GPIO_PORTD_BASE, GPIO_PIN_3);                       //  设置iic所在管脚为输入
  
  SysCtlPeriEnable(SYSCTL_PERIPH_GPIOE);                           //  使能iic所在的GPIO端口
  GPIOPinTypeIn(GPIO_PORTE_BASE, GPIO_PIN_0);                       //  设置iic所在管脚为输入
  GPIOPinTypeIn(GPIO_PORTE_BASE, GPIO_PIN_1);                       //  设置iic所在管脚为输入
 
  
  GPIOPinTypeOut(GPIO_PORTE_BASE, GPIO_PIN_2);                       
  GPIOPinTypeOut(GPIO_PORTE_BASE, GPIO_PIN_3);                      
  GPIOPinWrite( GPIO_PORTE_BASE, GPIO_PIN_2, 0x01<<2);                     
  GPIOPinWrite( GPIO_PORTE_BASE, GPIO_PIN_3, 0x01<<2);
  
  
  SysCtlPeriEnable(SYSCTL_PERIPH_TIMER0);                 //  使能Timer模块
  TimerConfigure(TIMER0_BASE, TIMER_CFG_16_BIT_PAIR |     //  配置Timer为16位周期定时器
                 TIMER_CFG_B_PERIODIC);
  
  TimerPrescaleSet(TIMER0_BASE, TIMER_B, 99);             //  预先进行100分频
  TimerLoadSet(TIMER0_BASE, TIMER_B, 30000);              //  设置Timer初值,定时500ms
  TimerIntEnable(TIMER0_BASE, TIMER_TIMB_TIMEOUT);        //  使能Timer超时中断
  IntEnable(INT_TIMER0B);                                 //  使能Timer中断
  IntMasterEnable();                                      //  使能处理器中断
  TimerEnable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
  
}

//  主函数(程序入口)
int main(void)
{ 
  jtagWait();                                             //  防止JTAG失效,重要!
  clockInit();                                            //  时钟初始化:晶振,6MHz
  I2C_Init();
  uartInit();
  init_lcd();
  Time_Init();
  buzzerInit();//  蜂鸣器初始化
  
  if(FDC2214_Init()==1){
    uartPuts("初始化成功\r\n");
    disp_line(0,"  初始化成功! ");
    SysCtlDelay(100 * (TheSysClock / 3000));
    GUI_ClearSCR(0);
  }
  else{
    uartPuts("初始化失败请检查系统后重启");
    disp_line(0,"  初始化失败!  ");
    disp_line(1,"请检查系统后重启");
    while(1);
  }
  
  while(1){
    if ( ( T1 == 0 ) && ( T2 == 0 ) && ( T3 == 0 ) && ( T4 == 0 ) ){
      T3 = 1 ;
      GUI_ClearSCR(0);
      disp_line(0,"实现自检功能: 1 ");
      disp_line(0,"实现自检功能: 1 ");
      disp_line(1,"测试纸张功能: 2 ");
      disp_line(2,"纸张类型测量:  3 ");
      disp_line(3,"                        ");
    }
    if ( T1 == 1 ){
      T3 = 0 ;
      TimerDisable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
      int N1 = 0 , N2 = 0 ,N3 = 0 ;
      int Step = 0 , i ; 
      while( T1 == 1 ) {
        FDC2214_GetChannelData(FDC2214_Channel_0, &k);
        Real_Num = ( int )( k / 1000.0 );
        sprintf(a,"电容量:%d\r\n", Real_Num);
        uartPuts(a); 
        sprintf(a,"电容量:%d", Real_Num);
        disp_line(3,a);
        T1 = 1 ;
        if ( Real_Num > 50000 ){                        
          GPIOPinWrite( GPIO_PORTE_BASE, GPIO_PIN_2, 0x00 );buzzerSound(2000);                                      //  蜂鸣器发出2000Hz声音
          SysCtlDelay(800 * (TheSysClock / 3000));                //  延时约800ms
        }else {                        
          GPIOPinWrite( GPIO_PORTE_BASE, GPIO_PIN_2, 0x01<<2 );
          buzzerQuiet();                                         //  蜂鸣器静音
        }
        if ( N1 == 0 ){
          N1 = 1 ;
          disp_line(0,"确认纸的值:   ");
          disp_line(1,"按键2 开始记录1          ");
          disp_line(2,"                        ");
          disp_line(3,"                        ");
        }
        
        if ( ( Key_2 == 0 ) && ( N2 == 0 ) ){
          N2 = 1 ;
          Data_Mar[Step][0] = Step  ;
          Data_Mar[Step][1] = Real_Num ;
          
          sprintf(a,"%d  %d  ", Data_Mar[Step][0] ,Data_Mar[Step][1] );
          uartPuts(a); 
          
          sprintf(a,"第%d 页记录成功    ", Step+1);
          if( Step >= 10  )
            sprintf(a,"第%d页记录成功    ", Step+1);
          uartPuts(a); 
          disp_line(1,a);
          Step ++ ;
          Data_Mar[Step][0] = Step  ;
          Step ++ ;
          sprintf(a,"第%d 页开始记录    ", Step+1);
          if( Step >= 10  )
            sprintf(a,"第%d页开始记录    ", Step+1);
          uartPuts(a); 
          disp_line(2,a);
          Step_All = Step  ;
          N3 = 1 ;
          T1 = 1 ;
        }
        if( Key_2 != 0  ){
          N2 = 0 ;
        }
        if( ( Key_1 == 0 ) && ( N3 == 1 ) ){
          disp_line(1,"自检成功          ");
          disp_line(2,"一秒后回主页面 ");
          disp_line(3,"                        ");
          
          for (i = 0  ; i < Step_All ; i ++  ){
            sprintf(a,"%d  %d  \r\n", Data_Mar[i][0] ,Data_Mar[i][1] );
            uartPuts(a); 
          }
          
          Data_N ( Data_Mar , Step_All   );
          
          for ( i = 0 ; i < Step_All ; i ++  ){
            sprintf(a,"%d %lf   \r\n", i , comPare[i] );
            uartPuts(a); 
          }
          
          T1 = 0 ;
          T2 = 0 ;
          T3 = 0 ;
          T4 = 0 ;
          N1 = 0 ;
          N3 = 0 ;
        }
      }
      
      TimerEnable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
    }else if ( T2 == 1  ){
      int N1 = 0 , N2 = 0 ,Err_1 , Err_2 , Ans , Error , N4 = 0 ;
      T3 = 0 ;
      TimerDisable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
      while( T2 == 1 ) {
        
        if ( N1 == 0 ){
          N1 = 1 ;
          sprintf(a,"最大纸的页数:%d", Step_All );
          disp_line(0,a);
          disp_line(1,"按键1 开始检测          ");
          disp_line(2,"                        ");
          disp_line(3,"                        ");
        }
        
        if ( N2 == 0 ){
          while( N2 == 0 ){
            FDC2214_GetChannelData(FDC2214_Channel_0, &k);
            Real_Num = ( int )( k / 1000.0 );
            sprintf(a,"电容量:%d\r\n", Real_Num);
            uartPuts(a); 
            N2 = 0 ;
            if(  Key_1 == 0 ){
              N2 = 1 ;
            }
          }
          
          for ( Error = 0 ; Error < 2 * Step_All ; Error  ++  ){
            if ( comPare[Error] >=  Real_Num ){
              break ;
            }
          }
          
          Err_1 = comPare[Error] - Real_Num ;
          Err_2 = Real_Num - comPare[Error - 1 ];
          
          Ans = ( ( Err_1 >= Err_2 ? ( Error - 1 ) : Error ) );
          sprintf(a,"页数为: %d张    ", ( Ans + 1 ));
          uartPuts(a); 
          disp_line(1,a);
          disp_line(3,"按键2 退出          ");
          while(1){
            if ( Key_2 == 0 ){
              N4 = 1 ;
            }
            if ( ( Key_2 != 0) && ( N4 == 1 ) ){
              T1 = 0 ;
              T2 = 0 ;
              T3 = 0 ;
              T4 = 0 ;
              break ;
              
            }
          }
        }
      }
      TimerEnable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
    }else if( T4 == 1 ){
      int K1 = 0 , K2 = 0 , K3 = 0;
      T3 = 0 ;
      TimerDisable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
      while( T4 == 1){
        
        if( K1 == 0 ){
          K1 = 1;         
          disp_line(0,"纸张类型测量:   ");
          disp_line(0,"纸张类型测量:   ");
          disp_line(1,"按键1 开始检测          ");
          disp_line(2,"                        ");
          disp_line(3,"                        ");
        }
        
        if( K2 == 0 ){
          while( K2 == 0 ){
            FDC2214_GetChannelData(FDC2214_Channel_0, &k);
            Real_Num = ( int )( k / 1000.0 );
            sprintf(a,"电容量:%d\r\n", Real_Num);
            uartPuts(a);
            disp_line(3,a);
            K2 = 0 ;
            if(  Key_1 == 0 ){
              K2 = 1 ;
            }
          }
          //卡纸:8441  信纸:7389  作业纸:7706
/*          if(){                                   
            disp_line(1,"      卡纸      ");
            disp_line(3,"按键2 退出          ");
          }
          else if(){
            disp_line(1,"      信纸      ");
            disp_line(3,"按键2 退出          ");
          }
          else if(){
            disp_line(1,"     作业纸     ");
            disp_line(3,"按键2 退出          ");
          }*/
          
          while(1){
            if ( Key_2 == 0 ){
              K3 = 1 ;
            }
            if ( ( Key_2 != 0) && ( K3 == 1 ) ){
              T1 = 0 ;
              T2 = 0 ;
              T3 = 0 ;
              T4 = 0 ;
              break ;
              
            }
          }
        } 
      } 
      TimerEnable(TIMER0_BASE, TIMER_B);                      //  使能Timer计数
    }
  }
}


void  Data_N ( int Array[][2] , int m  ){
  int  i = 0 , j = 0 , Num_B = 0  , Num_A = 0 ;
  for( i = 0 ; i <= m  ; i ++  ){
    Num_B = Array[i][0] ;
    Num_A = Array[i][1] ;
    comPare[Num_B] = Num_A ;
  }
  
  if ( comPare[0] == 0 ){
    comPare[0] = 1 ;
  }
  
  for ( j = 1 ; j < m  ; j ++ ){
    double Num_Left = 0 , Num_right = 0;
    int k1 = 0 , k2 = 0   ;
    if ( comPare[j] == 0 ){
      for ( k1 = j - 1  ; k1 >= 0 ; k1 -- ){
        if( comPare[k1] != 0 ){
          Num_Left = comPare[k1];
          break;
        }
      }
      if ( Num_Left == 0  ){
        Num_Left = 1 ;
      }
      for ( k2 = j + 1 ; k2 < 2*m ; k2 ++ ){
        if( comPare[k2] != 0 ){
          Num_right = comPare[k2] ;
          break ;
        }
      }
      if ( Num_right == 0 ){
        comPare[j] = comPare[j - 1 ] + 1 ;
      }else{
        comPare[j] = ( Num_Left + Num_right ) / 2.0  ;
      }
    }
  }
}

//  定时器的中断服务函数
void Timer0B_ISR(void)
{
  unsigned long ulStatus;
  
  ulStatus = TimerIntStatus(TIMER0_BASE, true);           //  读取中断状态
  TimerIntClear(TIMER0_BASE, ulStatus);                   //  清除中断状态,重要!
  
  if (ulStatus & TIMER_TIMB_TIMEOUT)                      //  如果是Timer超时中断
  {
    if (  Key_1 == 0 ){
      T1 = 1 ;
      T2 = 0 ;
      T4 = 0 ;
    }
    else if ( Key_2 == 0 ){
      T1 = 0 ;
      T2 = 1 ;
      T4 = 0 ;
    }
    else if ( Key_3 == 0 ){
      T1 = 0 ;
      T2 = 0 ;
      T4 = 1 ;
    }
  }
}

  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,以下是一个简单的基于 51 单片机的纸张计数器程序,你可以根据自己的需要进行修改和扩展: ``` #include <reg51.h> // 引入 51 单片机头文件 sbit sensor = P2^0; // 定义光电传感器引脚 sbit led = P1^0; // 定义 LED 引脚 int count = 0; // 定义计数器变量 void main() { while(1) { if(sensor == 0) // 如果检测到纸张 { led = 1; // 点亮 LED delay(10); // 延时一段时间 if(sensor == 0) // 再次检测纸张是否存在 { count++; // 计数器加一 while(sensor == 0); // 等待纸张离开传感器 } led = 0; // 熄灭 LED } } } void delay(int time) { int i, j; for(i = 0; i < time; i++) for(j = 0; j < 125; j++); } ``` 在这个程序中,我们使用 `sensor` 定义了光电传感器的引脚,使用 `led` 定义了 LED 的引脚,使用 `count` 定义了计数器变量。在主函数中,我们使用一个无限循环来不断检测传感器是否检测到纸张。如果检测到纸张,我们会点亮 LED 并进行一段短暂的延时。如果传感器仍然检测到纸张,就会将计数器加一,并等待纸张离开传感器。最后,我们会熄灭 LED 并继续循环。 程序中的 `delay` 函数用于实现一个简单的延时功能,我们使用它来在检测到纸张后等待一段时间以确保计数准确性。 请注意,这只是一个简单的示例程序,如果你需要更加精确的计数器程序,需要考虑更多的因素,例如传感器的精度、纸张的大小和形状等。同时,你也可以根据自己的需要进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值