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

📄 hardwarec.c

📁 变频器调试参数的程序,1602的液晶显示器,AVR芯片,可以作为参考资料学习使用
💻 C
字号:
#include "externh.h"


tagKEY Key;
tagRS232 Rs232;
volatile U16 Dly[MAX_TIMER];
volatile U8 Rs232OverTime;
unsigned int  ADCConvert(U8 ch);
void Timer0Init(void);
void ADCInit(void);

void SystemInit(void)
{
	DDRD |=(1<<2);
	DDRC |=(1<<0)|(1<<1)|(1<<2)|(1<<4)|(1<<5);
	Timer0Init();
	Rs232Init();
	ADCInit();
	EnInt();
	g.State=0;
	LCDInit();
}

void GetKey(void)
{
	Key.hzS=Key.CodeS;
	Key.CodeS=0;
}
void gSet(U8 ch)
{
	g.State=ch;
	g.Flag.Disp=1;
}

void Delay(U8 ms)
{//短
	DlyMs=ms;
	while(DlyMs) wdt_reset();
}

SIGNAL(SIG_OUTPUT_COMPARE0A)
{//20ms.
	U8 i;
	g.Flag.T0=1;
	for(i=0;i<MAX_TIMER;i++){
		if(Dly[i]) Dly[i]--;
	}
}
SIGNAL(SIG_USART_RECV)
{
	Rs232.Flag.Rx=1;
	Rs232.Ch=UDR0;
	if(Rs232.Flag.Full) return;
	switch(Rs232.State){
		case 0:
			if(Rs232.Ch!=0x55) break;
			Rs232.State=1;
			break;
		case 1:
			if(Rs232.Ch==0xAA){
				Rs232.State=2;
				Rs232.Length=24;
				Rs232.Count=0;
				break;
			}
			Rs232.State=0;
			break;
		case 2:
			RxBuf[Rs232.Count]=Rs232.Ch;
			Rs232.Count++;
			RxBuf[Rs232.Count]=0;
			Rs232.Length--;
			if(Rs232.Length) break;
			Rs232.State=0;
			Rs232.Flag.Full=1;
			break;
		default:
			Rs232.State=0;
			break;
	}
}
void Timer0Init(void)
{
//	CS02	CS01	CS00	Prescale
//	0		0		0		无时钟,T/C不工作
//	0		0		1		1:1
//	0		1		0		1:8
//	0		1		1		1:64
//	1		0		0		1:256
//	1		0		1		1:1024
//	1		1		0		时钟由T0引脚输入,下降沿触发
//	1		1		1		时钟由T0引脚输入,上升沿触发
	#define Ts		0.02	//采样周期(S)
	#define Prescale	1024
	TCCR0B = 0x00; 					//stop	
	TCCR0A |=(1<<WGM01);			//CTC mode.
	TCCR0B |=(1<<CS00)|(1<<CS02);				//Prescale.
	SetBit(TIMSK0,OCIE0A);
	OCR0A =Ts*F_CPU/Prescale;
}



void KeyScan(void)
{
	unsigned int uint;
	U8 uc0;
	uint=ADCConvert(3);
	if(uint>120){
//		PutCh(uint/256);
//		PutCh(uint%256);
	    switch(Key.State){
	        case 0:
	        case 1:
	        case 2:
				Key.State++;
	            break;
		 	case 3:
				uc0=uint/KEYPERVALUE;
				Key.CodeS=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 = BAUDL;
	UBRR0H = BAUDH; 
	UCSR0B =(1<<RXEN0)+(1<<TXEN0);   //Enable Tx and Rx.
	SetBit(UCSR0B,RXCIE0);           //Enable Rx interrupt.
	Rs232.State=0;
}

⌨️ 快捷键说明

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