/************主 函 数 *************/
void main(void)
{
CLK_Config();
GPIO_Config();
TM1642_Init();
SegBufCha(888,1);
SegBufCha(888,2);
ledCtrl (6,1);
displayLED ();
Delay(1000);
ledCtrl (6,0);
ledCtrl (0,1);
ledCtrl (3,1);
SegBufon();
displayLED ();
Delay(1000);
****************************ADC1.C******************
ADC1_init();
enableInterrupts();
while(1)
{
Delay(10);
SegBufCha(ADC1_Buf[0],2);
SegBufCha(ADC1_Buf[1],1);
Key_Scan();
displayLED ();
}
}
#include "main.h"
u8 ADC_Index=0;
u32 ADC1_Buf[4];
u32 ADC1_change(u8 AIN_x) //AD信号采集函数
{
u8 i;
u16 AD_data1;
u32 ADData,AD_sum;
for(i=0;i<10;i++) //平均值滤波,好像没什么效果
{
ADC1->CSR = AIN_x&0x0F; // 选择AD转换通道
ADC1->CR1 |= 0x01; // 启动AD转换
while(!(ADC1->CSR & 0x80)); // 等待AD转换完成
AD_data1 = ADC1->DRH; // 左对齐时候高八位在DRH里面,低两位在DRL最低位
//AD_data0 = ADC1->DRL;
ADC1->CSR &= ~0x80;
AD_sum+=AD_data1;
}
AD_sum/=10;
ADData=AD_sum;
return(ADData);
}
void ADC1_init(void)
{
ADC1->CSR = 0x20; //转换结束EOC中断ENABLE
ADC1->CR1 = 0x40; //设置AD转换时钟1/8,单次 转换模式
ADC1->CR2 = 0x00; //设置数据为左对齐
ADC1->CR3 = 0x00; //不使用数据缓冲
//ADC1->TDRL = 0xff; //关闭施密特触发器
ADC1->CR1 |= 0x01; //打开AD模块电源
}
*******ADC.H*********
#ifndef __ADC_H__
#define __ADC_H__
extern u8 ADC_Index;
extern u32 ADC1_Buf[4];
extern u32 ADC1_change(u8 AIN_x);
extern void ADC1_init(void);
#endif
*****************************************
*/
INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
{ //ADC中断处理程序
//ADC_Index=(ADC_Index>3)?0
ADC_Index+1);
//ADC1_Buf[ADC_Index]=ADC1_change(ADC_Index+3);//ADC_Index=0~3;ADC_Index_0=ADC_AIN3
ADC1_Buf[2]=45;
ADC1_ClearITPendingBit(ADC1_IT_EOC);
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
}
一周热门 更多>