📄 ad_convert_interrupt.c
字号:
//========================================================
// A/D conversion and Display
//
// 使用中断方式处理A/D转换结果
//
// Device PIC16F877
// set RA2 to an input analog channel,
// use RB0, RB1, RB2 and RB3 as I/O output pins select the led of 7-segment_led
// use PORTD as I/O output port to output the 7-seg value
// the 7_seg part is common cathode part
// 2007.5.29
//
// To do an A/D Conversion, follow these steps:
// 1. Configure the A/D module:
// Configure analog pins/voltage reference and digital I/O (ADCON1)
// Select A/D input channel (ADCON0)
// Select A/D conversion clock (ADCON0)
// Turn on A/D module (ADCON0)
// 2. Configure A/D interrupt (if desired):
// Clear ADIF bit
// Set ADIE bit
// Set PEIE bit
/// Set GIE bit
// 3. Wait the required acquisition time.
// 4. Start conversion:
// Set GO/DONE bit (ADCON0)
// 5. Wait for A/D conversion to complete by either:
// Polling for the GO/DONE bit to be cleared (interrupts disabled); OR
// Waiting for the A/D interrupt
// 6. Read A/D Result register pair (ADRESH:ADRESL),
// clear bit ADIF if required.
// 7. For the next conversion, go to step 1 or step 2 as required. The A/D conversion time per bit is
// defined as TAD.
//========================================================
#include<pic.h>
const int Dis_table[20]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x4e,0x79,0x71,0x80,0x40,0x76,0x00};
// save A/D conversion result
volatile static union AD_Res{
unsigned int AD_Value;
unsigned char AD_Tab[2];
} AD_Result;
// the three BCD code to display
volatile unsigned char num1,num2,num3;
// subroutines
void Delay10ms(void);
void interrupt AD_Process(void);
//=====================================================================
// the main program
void main(void)
{
// initialize the A/D module
ADCON0 = 0x51; //选择A/D通道为RA2,使AD 转换时钟为8tosc,打开A/D 转换器
ADCON1 = 0X81; //转换结果右移,及ADRESH寄存器的高6位为"0"
//且把RA2口设置为模拟量输入方式
//RA3接+5V参考电平
TRISA2=1; //设置RA2为输入方式
TRISB=0X00; //initialize the PORTD, set it to a output port
TRISD=0X00; //initialize the PORTD, set it to a output port
PORTB=0xff; //set RC0-RC3 to high level to disable the 7_seg_led
ADIF=0; //Clear A/D conversion complete flag bit
PEIE=1; //Enable the general perehperal interrupt
ADIE=1; //Enable A/D interrupt
GIE=1; //Enable the general interrupt
Delay10ms(); //waite the required acquisition time
ADGO=1; //start the A/D conversion
while(1)
{
//For the 7Seg_MPX4_CC part from the Proteus, must set the code for 7_seg_led display first,
//set the pin to low level to select the corresponding led to work
PORTD=Dis_table[num1]; //send the 7_seg code to PORTD
RB3=0; //set the corresponding led to work
Delay10ms();
RB3=1;
PORTD=Dis_table[num2];
RB2=0;
Delay10ms();
RB2=1;
PORTD=Dis_table[num3];
RB1=0;
Delay10ms();
RB1=1;
ADGO=1;
CLRWDT();
}
}
// end of the main program
//=====================================================================
//===========================interrupt AD_Process======================
void interrupt AD_Process(void)
{
if (!(ADIE && ADIF)) return;
CLRWDT();
ADIF=0;
AD_Result.AD_Tab[0]=ADRESL;
AD_Result.AD_Tab[1]=ADRESH;
AD_Result.AD_Value=(unsigned int)((float)AD_Result.AD_Value/3.3);
num1=(AD_Result.AD_Value%10);
num2=((AD_Result.AD_Value-num1)%100)/10;
num3=(AD_Result.AD_Value-num1-num2*10)/100;
}
//======================End if interrupt AD_Process=====================
//========================Delay10ms====================================
void Delay10ms(void)
{
unsigned int i;
for (i = 2000 ; i>0 ; --i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -