?? adc.c
字號(hào):
#include <iom128.h>
#include <macros.h>
#include <math.h>
//LED 開(kāi)關(guān)定義
#define ADC_start 0x40
#define Red_LED_on PORTB&~BIT(0)
#define Red_LED_off PORTB|(1<<0)
#define Green_LED_on PORTB&(~(1<<1))
#define Green_LED_off PORTB|(1<<1)
#define Yellow_LED_on PORTB&(~(1<<2))
#define Yellow_LED_off PORTB|(1<<2)
#define Blue_LED_on PORTB&(~(1<<3))
#define Blue_LED_off PORTB|(1<<3)
unsigned int AD_sample_value;
unsigned char Type_flag = 0;
unsigned char channel_0_flag = 1;
unsigned char channel_1_flag = 1;
unsigned char channel_2_flag = 1;
unsigned char channel_3_flag = 1;
//Display
unsigned char number_baifen;
unsigned char number_shifen;
unsigned char number_ge_dot;
unsigned char Display_type;
unsigned char Display_value;
unsigned char Display_flag = 0;
unsigned char LED_table[]=
{
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F, //"9"
0x77, //"A"
0x7C, //"B"
0x39, //"C"
0x5E, //"D"
0x79, //"E"
0x71, //"F"
0x76, //"H"
0x38, //"L"
0x37, //"n"
0x3E, //"u"
0x73, //"P"
0x5C, //"o"
0x40, //"-"
0x00, //熄滅
};
//use for test;
unsigned char m;
//define Interrupt Vactor
#pragma interrupt_handler External_INT_0:2 //外部中斷0
#pragma interrupt_handler External_INT_1:3 //外部中斷1
#pragma interrupt_handler External_INT_2:4 //外部中斷2
#pragma interrupt_handler ADC_INT:22 //ADC中斷
//pre_define
void main(void);
void Port_initialize(void);
void ADC_initialize(void);
void INT_initialize(void);
void UART_initialize(void);
void Delay(void);
void External_INT_0(void);
void ADC_INT(void);
void External_INT_1(void);
void External_INT_2(void);
void LED_Display(unsigned char Channel_num,unsigned int Data);
void DATA_to_NUM(unsigned int data);
void main(void)
{
unsigned char i;
Port_initialize();
//UART_initialize();
INT_initialize();
ADC_initialize();
for (; ; )
{
switch (Type_flag)
{
/*case 0: //發(fā)送的數(shù)值
{
Display_type = 0;
Display_value = Tx_data;
}break;
*/
case 1: //發(fā)送計(jì)數(shù)
{
Display_type = 1;
//Display_value = Tx_counter;
}break;
case 2: //接收的數(shù)值
{
Display_type = 2;
//Display_value = Rx_data;
}break;
case 3: //接收計(jì)數(shù)
{
Display_type = 3;
//Display_value = Rx_counter;
}break;
default:
{
Display_type = 0x0;
}break;
}
if (Display_flag == 1)
{
//DDRB |=0xF0;
LED_Display(Display_type,AD_sample_value); //數(shù)碼管顯示
//DDRB |=0xF0;
}
else
{
LED_Display(Display_type,AD_sample_value);
}
if (channel_0_flag == 0)
{
PORTB = Red_LED_on;
for (i=0;i<30 ;i++ )
{
Delay();
}
channel_0_flag = 1;
}
else
{
PORTB = Red_LED_off;
}
if (channel_1_flag == 0)
{
PORTB = Green_LED_on;
for (i=0;i<30 ;i++ )
{
Delay();
}
channel_1_flag = 1;
}
else
{
PORTB = Green_LED_off;
}
if (channel_2_flag == 0)
{
PORTB = Yellow_LED_on;
for (i=0;i<30 ;i++ )
{
Delay();
}
channel_2_flag = 1;
}
else
{
PORTB = Yellow_LED_off;
}
if (channel_3_flag == 0)
{
PORTB = Blue_LED_on;
for (i=0;i<30 ;i++ )
{
Delay();
}
channel_3_flag = 1;
}
else
{
PORTB = Blue_LED_off;
}
}
}
void Port_initialize(void)
{
DDRA = 0xFF;
DDRB |= 0x0F;
DDRD = 0x00;
PORTB = 0xFF;
}
void ADC_initialize(void)
{
ADMUX = 0x40; //Vref = AVCC,right,channel = 0
ADCSR = 0x89; //enable ADC,not start now,not continuous,F--,clock = f/2
}
void INT_initialize(void)
{
EICRA = 0xaa; //fall edge trigger
EICRB = 0x00; //no influence
EIMSK = 0x0f;
SEI();
}
void UART_initialize(void)
{
CLI();
UBRR0H = 0x00;
UBRR0L = 0x67; //波特率9600bps
UCSR0A = 0x00;
UCSR0B = 0x98; //11011000接收與發(fā)送使能
UCSR0C = 0x26; //00100110幀格式: 8bits,1stop bit,P 校驗(yàn)
SEI();
}
void Delay(void)
{
unsigned int i;
for ( i = 0; i < 6000; i++ )
{
NOP();
}
}
void External_INT_0(void)
{
ADCSR |= ADC_start;
DDRB |=0xF0; //放在這個(gè)位置是錯(cuò)誤的,中斷回到入口之處
Display_flag = 1;
/*UCSR0B |= 0x40;
UCSR0B &= ~(0x80);*/
}
void ADC_INT(void)
{
AD_sample_value = ADC;
//PORTB = Blue_LED_on;
switch (Type_flag)
{
case 1:
{
channel_1_flag = 0;
}break;
case 2:
{
channel_2_flag = 0;
}break;
case 3:
{
channel_3_flag = 0;
}break;
default:
{
channel_0_flag = 0;
}break;
}
}
void External_INT_1(void)
{
if (Type_flag < 3 )
{
Type_flag += 1;
}
else
{
Type_flag = 0;
}
ADMUX = (ADMUX&0xf8)|Type_flag; //0x41; //
Display_flag = 0;
DDRB &=0x1F;
}
void External_INT_2(void)
{
}
void LED_Display(unsigned char Channel_num,unsigned int Data)
{
DATA_to_NUM(Data);
PORTA = LED_table[number_baifen] ;
PORTB = (PORTB|0xf0)&0x7F;
Delay();
PORTA = LED_table[number_shifen];
PORTB = (PORTB|0xf0)&0xBF;
Delay();
PORTA = LED_table[number_ge_dot] + 0x80;
PORTB = (PORTB|0xf0)&0xDF;
Delay();
PORTA = LED_table[Channel_num];
PORTB = (PORTB|0xf0)&0xEF;
Delay();
}
void DATA_to_NUM(unsigned int data)
{
unsigned int temp;
temp = (unsigned int)(((float)data)*500/1024);
number_ge_dot = temp/100;
number_shifen = (temp%100)/10;
number_baifen = (temp%100)%10;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -