?? atmega8 timer.c
字號(hào):
/******************************************************************************
程序名稱:電機(jī)驅(qū)動(dòng)程序
運(yùn)行平臺(tái):ATMaga8 4m晶振
程序版本:2.0
程序說(shuō)明:通過(guò)按鍵 或A/D輸入來(lái)調(diào)節(jié)pw波
pc0-pc4 對(duì)應(yīng) r3-r1(pc3 作為ad輸入)
pwm 波通過(guò)pc5輸出
當(dāng)a/d輸入有效時(shí),鍵盤自動(dòng)屏蔽
鍵盤說(shuō)明
[rest] [+] [-]
[none] [none] [none ]
作者:Gorgon Meducer
最后修改:Gorgon Meducer
修改時(shí)間:2004年12月29日
******************************************************************************/
#include <iom8v.h>
#include <macros.h>
#pragma interrupt_handler Timer1_ovf:9
#pragma interrupt_handler Adc_isr:15 //
const char DispCode[]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6,
0xee,0x3e,0x9c,0x7a,0x9e,0x8e}; //數(shù)碼管譯碼表
const char DispBit[] = {0b11101111,0b11011111,0b10111111,0b01111111};//數(shù)碼管初始位值
unsigned char Mark_Line=0x80;
//定時(shí)器延時(shí)
unsigned int Timer_Count_End=0;
unsigned int Timer_Count=0;
//unsigned char PressKey_Per_n_Timer_Ovf=0; //定義按鍵時(shí)間計(jì)數(shù)器
unsigned char Press_Keys=0; //按鍵代碼
/***************************************************************************/
void Port_init(void);
void Timer0_init(void);
void Timer1_init(void);
void Timer1_ovf(void);
void Hardware_init(void);
void Delay_ms(unsigned int delay_time);
void Key_Press_Scan(void);
void adc_init(void);
void Display_Message(unsigned char Where,unsigned char Number,unsigned char delays);
/***************************************************************************/
/***************************************************************************
端口初始化
****************************************************************************/
void Port_init(void)
{
//定義c為輸入
DDRC =0b11111111;
PORTC=0b11111111;
DDRC =0b00100000; //PC5用來(lái)輸出pw波
//定義B口為輸出,用于顯示數(shù)據(jù)
DDRB=0xff;
PORTB=0xff;
//定義D口為輸出,用于顯示數(shù)據(jù)
DDRD=0xff;
PORTD=0x00;
}
/***************************************************************************
定時(shí)器0初始化
****************************************************************************/
//TIMER0 initialize - prescale:1
// desired value: 20KHz
// actual value: 20.000KHz (0.0%)
//定時(shí)器0用來(lái)產(chǎn)生PW波
void Timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x38; //set count
TCCR0 = 0x01; //start timer
}
/***************************************************************************
定時(shí)器1初始化
****************************************************************************/
//定時(shí)器1主要用來(lái)做精確延時(shí)
void Timer1_init(void)
{
//定時(shí)器1部分
TCNT1H = 0xE0;
TCNT1L = 0xC0;
TCCR1B = 0x01; //1分頻,定時(shí)1ms
}
/***************************************************************************
A/D初始化
****************************************************************************/
void adc_init(void)
{
ADCSRA =0X00; //關(guān)閉AD
ADMUX = 0b00100011; //選擇外部參考電源 ADC3 左對(duì)齊
ACSR = (1<<ACD); //關(guān)閉模擬比較器
ADCSRA = 0b10011110;//中斷允許 64分頻
}
/***************************************************************************
A/D中斷處理
****************************************************************************/
void Adc_isr(void)
{
Mark_Line=ADCH; //讀取高位數(shù)據(jù)(左對(duì)齊)
ADMUX = 0b00100011; //選擇外部參考電源 ADC3 左對(duì)齊
ADCSRA |=(1<<ADSC); //啟動(dòng)AD轉(zhuǎn)換
}
/***************************************************************************
定時(shí)器1中斷處理程序
****************************************************************************/
void Timer1_ovf(void) //1ms中斷程序,檢測(cè)主程序標(biāo)志
{
TCNT1H = 0xE0;
TCNT1L = 0xC0; //復(fù)位1ms記數(shù)器
if (Timer_Count>0)
{
Timer_Count++;
}
if (Press_Keys==2)
{
Mark_Line++;
if (Mark_Line>0xff)
{
Mark_Line=0x00;
}
}
if (Press_Keys==3)
{
Mark_Line--;
if (Mark_Line<0x01)
{
Mark_Line=0xff;
}
}
if (Press_Keys==1)
{
Mark_Line=0x80; //復(fù)位
}
if (TCNT0>Mark_Line)
{
//驅(qū)動(dòng)電機(jī)
PORTC |= 0b00100000;
}
else
{
//停止驅(qū)動(dòng)
PORTC &= 0b11011111;
}
}
/***************************************************************************
系統(tǒng)初始化
****************************************************************************/
void Hardware_init(void)
{
CLI();
Port_init();
Timer1_init();
Timer0_init();
adc_init();
TIMSK = 0x04;
SEI();
}
/***************************************************************************
延時(shí)程序
****************************************************************************/
/*void Delay_ms(unsigned int delay_time)
{
Timer_Count_End=delay_time;
Timer_Count=1; //啟動(dòng)計(jì)數(shù)器
re_delay:
if (Timer_Count<=Timer_Count_End)
{
if (TCNT0>Mark_Line)
{
//驅(qū)動(dòng)電機(jī)
PORTC |= 0b00100000;
}
else
{
//停止驅(qū)動(dòng)
PORTC &= 0b11011111;
}
goto re_delay;
}
else
{
Timer_Count_End=0;
Timer_Count=0;
}
}*/
void Delay_ms(unsigned int m) //1ms延時(shí)
{
int l,j;
for(l=0;l<m;l++)
for(j=0;j<500;j++)
{
if (TCNT0>Mark_Line)
{
//驅(qū)動(dòng)電機(jī)
PORTC |= 0b00100000;
}
else
{
//停止驅(qū)動(dòng)
PORTC &= 0b11011111;
}
}
}
/***************************************************************************
按鍵掃描程序
****************************************************************************/
void Key_Press_Scan(void) //判斷鍵值
{ //unsigned char temp=0;
//掃描第一列
Press_Keys=0;
PORTC = 0b00000111;
DDRC = 0b00110000;
//temp=PINC;
if ( (PINC&0b00000100) == 0)
{
Delay_ms(10);
if ((PINC&0b00000100) == 0)
{
Press_Keys=1;
}
}
else if ((PINC&0b00000010) ==0)
{
Delay_ms(10);
if ((PINC&0b00000010) ==0)
{
Press_Keys=2;
}
}
else if ((PINC&0b00000001) ==0)
{
Delay_ms(10);
if ((PINC&0b00000001) ==0)
{
Press_Keys=3;
}
}
/*
//掃描第二列
PORTC = 0b00010111;
DDRC = 0b00111000;
//temp=PINC;
if ( (PINC&0b00000100) == 0)
{
Delay_ms(10);
if ((PINC&0b00000100) == 0)
{
Press_Keys=4;
}
}
else if ((PINC&0b00000010) ==0)
{
Delay_ms(10);
if ((PINC&0b00000010) ==0)
{
Press_Keys=5;
}
}
else if ((PINC&0b00000001) ==0)
{
Delay_ms(10);
if ((PINC&0b00000001) ==0)
{
Press_Keys=6;
}
}*/
}
/***************************************************************************
顯示函數(shù)
****************************************************************************/
void Display_Message(unsigned char Where,unsigned char Number,unsigned char delays)
{
//在指定的位置顯示指定的數(shù)字
PORTD = DispBit[Where];
PORTB = DispCode[Number]; //調(diào)用數(shù)碼管譯碼表
Delay_ms(delays);
}
/**************************************************************************
主函數(shù)
**************************************************************************/
void main(void)
{
Hardware_init();
ADCSRA |=(1<<ADSC); //啟動(dòng)AD轉(zhuǎn)換
while(1)
{
if (TCNT0>Mark_Line)
{
//驅(qū)動(dòng)電機(jī)
PORTC |= 0b00100000;
}
else
{
//停止驅(qū)動(dòng)
PORTC &= 0b11011111;
}
Key_Press_Scan();
Display_Message(0,((Mark_Line<<4)>>4),5);
Display_Message(1,(Mark_Line>>4),5);
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -