#include <math.h>
int n=2048,n1=2048,n2=2048;
uint8_t Count=0;
uint8_t f=0;
int val=0;
void ADC_DATA_CALLBACK(uint32_t u32UserData)
{
n=n1;
n1=n2;
n2=DrvADC_GetConversionData(1);
if(n2-n>650)
{
DrvGPIO_ClrBit(E_PORT2, E_PIN7);
}
if(n-n2>650)
{
DrvGPIO_SetBit(E_PORT2, E_PIN7);
}
/* val=abs(n2-n);
if((val>650)&&(Count==0))
{
Count=1;
}
else
{
Count=0;
}
if(Count==1)
{
if(f)
{
f=0;
DrvGPIO_SetBit(E_PORT2, E_PIN7);
}
else
{
f=1;
DrvGPIO_ClrBit(E_PORT2, E_PIN7);
}
} */
DrvADC_StartConvert();
}
void time1_callback(uint32_t da)//定时器1专门作为闪烁使用
{
DrvSYS_UnlockProtectedReg();
DrvWDT_Ioctl(E_WDT_IOC_RESET_TIMER,0); //喂狗
DrvSYS_LockProtectedReg();
}
void time0_callback(uint32_t da)//定时器0专门做采样工作和刷新屏幕显示
{
}
void initsys()
{
while(DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1)!=FALSE);//使能外部12MHZ晶振
DrvSYS_SelectPLLSource(E_SYS_EXTERNAL_12M);
DrvSYS_SetPLLMode(0);//pll正常
DrvSYS_UnlockProtectedReg();//关闭寄存器保护
DrvSYS_Open(50000000);//sys=12MHZ
DrvSYS_SelectHCLKSource(2);//pll
//while( DrvSYS_SelectHCLKSource(0) !=FALSE);//HCLK=12MHZ
DrvSYS_SetClockDivider(E_SYS_HCLK_DIV , 0);//HCLK1分频
while(DrvSYS_SelectSysTickSource(0)!=FALSE);//选择系统嘀嗒时钟源为外部12MHZ晶振
DrvSYS_LockProtectedReg();
}
void initTMR0()
{
DrvSYS_SetIPClock(E_SYS_TMR0_CLK,1); //使能始终
DrvSYS_SelectIPClockSource(E_SYS_TMR0_CLKSRC,2 );//这个TIM0是接到APB总线,不可以用外部晶振,手册的是错误的,应该看下面的APBPCLK的时钟
DrvTIMER_Init();
DrvTIMER_Open(E_TMR0,1,E_PERIODIC_MODE); //100MS
DrvTIMER_EnableInt(E_TMR0);
DrvTIMER_SetTimerEvent(E_TMR0, 2, time0_callback, 1);//1s
DrvTIMER_Start(E_TMR0);
DrvSYS_SetIPClock(E_SYS_TMR1_CLK,1); //使能始终
DrvSYS_SelectIPClockSource(E_SYS_TMR1_CLKSRC,2 );//这个TIM0是接到APB总线,不可以用外部晶振,手册的是错误的,应该看下面的APBPCLK的时钟
DrvTIMER_Open(E_TMR1,10,E_PERIODIC_MODE); //100MS
DrvTIMER_EnableInt(E_TMR1);
DrvTIMER_SetTimerEvent(E_TMR1, 2, time1_callback, 1);//0.5s
DrvTIMER_Start(E_TMR1);
}
void init_ADC()
{
/*Initialize ADC*/
DrvGPIO_InitFunction(E_FUNC_ADC1 );
DrvADC_Open(ADC_SINGLE_END, ADC_CONTINUOUS_OP, 0x02, EXTERNAL_12MHZ, 59);
DrvADC_EnableADCInt(ADC_DATA_CALLBACK,0);
DrvADC_StartConvert();
}
#include <stdio.h>
#include <string.h>
#include &quot;SqQueue.h&quot;
status InitQueue(SqQueue *Q,QElemType *Q_BUF)
{
Q->base=Q_BUF;
Q->front=0;
Q->rear=0;
return OK;
}
int QueueLength(SqQueue *Q)
{
return ((Q->rear-Q->front+MAXQSIZE) % MAXQSIZE);
}
status EnQueue(SqQueue *Q,QElemType *e)
{
if(((Q->rear+1) % MAXQSIZE) == Q->front) return ERROR;
Q->base[Q->rear] = *e;
Q->rear = (Q->rear+1) % MAXQSIZE;
return OK;
}
status DeQueue(SqQueue *Q,QElemType *e)
{
if(Q->front == Q->rear) return ERROR;
*e = Q->base[Q->front] ;
Q->front = (Q->front+1) % MAXQSIZE;
return OK;
}
#define MAXQSIZE 100
#define ERROR 0
#define OK 1
#define status unsigned char
#define QElemType unsigned char
typedef struct{
QElemType *base;
unsigned char front;
unsigned char rear;
}SqQueue;
status InitQueue(SqQueue *Q,QElemType *Q_BUF);
int QueueLength(SqQueue *Q);
status EnQueue(SqQueue *Q,QElemType *e);
status DeQueue(SqQueue *Q,QElemType *e) ;复制代码 |