⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hardware.c

📁 手机充电器的电路板的测试机架的测试程序,利用AVR的AD转换器测试各点的电压.
💻 C
字号:

#include "extern.h"
KEY Key;
U8 DelayCount;
const U8 ADCChannel[]={0,7,6,5,4,3,2,1,0,13,14,15,12,11,10};
void GeyKey(void);
void KeyScan(void);
void ADCInit(void);
unsigned int  ADCConvert(U8 ch);
void Rs232Init(void);
SIGNAL(SIG_OUTPUT_COMPARE0A)
{
	g.Flag.ms10=1;
    wdt_reset();
//	NorBit(PORTD,3);
	if(DelayCount) DelayCount--;
}
SIGNAL(SIG_USART_RECV)
{
	Rs232.Flag.Rx=1;
	Rs232.Ch=UDR0;
}
void Timer0Init(void)
{
	TCCR0B = 0x00; 				//stop	
	TCCR0A |=(1<<WGM01);		//CTC mode.
	TCCR0B |=(1<<CS02);			//1:256.
	SetBit(TIMSK0,OCIE0A);
	OCR0A =190;
}

void IOInit(void)
{
	PORTB = (1<<0);
 	DDRB  = 0x0E;
 	PORTC = 0x00; //m103 output only
 	DDRC  = 0x3A;
 	PORTD = (1<<5)+(1<<6)+(1<<7);
 	DDRD  = (1<<1)+(1<<2)+(1<<4)+(1<<3);//0x1C;

}
void KeyScan(void)
{
	unsigned int uint;
	U8 uc0;
	uint=ADCConvert(0);
	if(uint>70){
	    switch(Key.State){
	        case 0:
	        case 1:
	        case 2:
				Key.State++;
	            break;
		 	case 3:
				uc0=uint/205;
				if((uint%205)>60) uc0++;
				Key.Code=uc0+'0';
				Key.State++;
		        break;
			default:break;

	    }
	}
	else{
		Key.State=0;
	}
}
void ADCInit(void)
{//不允许中断,分频比=1:64.
 	ADCSRA = 0x00; //disable adc
 	ADMUX = 0x00;  //select adc input 0
 	ACSR  = 0x80;
 	ADCSRB = 0x00;
 	ADCSRA = 0xC6;
}

unsigned int  ADCConvert(U8 ch)
{//启动一次转换.
	unsigned int us0;
	ADMUX=ch;
	SetBit(ADCSRA,ADSC);
	while(!ChkBit(ADCSRA,ADIF));
	us0 =ADCL;
	us0 +=((ADCH&0x03)<<8);
	
	for(us0=0;us0<100;us0++);

	SetBit(ADCSRA,ADSC);
	while(!ChkBit(ADCSRA,ADIF));
	us0 =ADCL;
	us0 +=((ADCH&0x03)<<8);
	return(us0);
}
void PutCh(U8 ch)
{
	while(!ChkBit(UCSR0A,UDRE0));
	UDR0 =ch;
}
void GetCh(void)
{
	if(!ChkBit(UCSR0A,RXC0))return;
	Rs232.Flag.Rx=1;
	Rs232.Ch=UDR0;
}

void Rs232Init(void)
{//8+N+1.
 	UCSR0C =(1<<UCSZ00)+(1<<UCSZ01);
	UBRR0L = 31;//(U8)((F_OSC/(16*BAUD)-1)%256);
	UBRR0H = 0; //(U8)((F_OSC/(16*BAUD)-1)/256);
	UCSR0B =(1<<RXEN0)+(1<<TXEN0);
}
unsigned int VoltageADC(U8 ch)
{
	
	U8 uc0;
	U16 uint[5];
	uc0=ADCChannel[ch];

	ClrBit(PORTD,PD2);
	PORTC=0;
	if(ChkBit(uc0,0)) SetBit(PORTD,2);
	if(ChkBit(uc0,1)) SetBit(PORTC,4);
	if(ChkBit(uc0,2)) SetBit(PORTC,3);
	if(ChkBit(uc0,3)) SetBit(PORTC,5);
	for(uc0=0;uc0<4;uc0++){
		uint[uc0]=ADCConvert(2);
	}
	return (uint[3]);
}
void Beep(U8 count)
{
	ClrBit(PORTC,1);
	SetBit(DDRC,1);
	while(count--){
		SetBit(PORTC,1);
		Delay(30);
		ClrBit(PORTC,1);
		Delay(30);
	}
}
void EEPromWrite(U8 Addr,U8 ucData)
{
	while(EECR&(1<<EEWE));
	EEARL=Addr;
	EEDR=ucData;
	EECR |=(1<<EEMWE);
	EECR |=(1<<EEWE);
}
U8 EEPromRead(U8 Addr)
{
	while(EECR&(1<<EEWE));
	EEARL=Addr;
	EECR |=(1<<EERE);
	return EEDR;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -