?? main._c
字號(hào):
/***************************************
* 方波生成器 *
* 程序功能: 生成占空比可調(diào)方波 *
* 時(shí)鐘頻率: 外部7.3728M *
* 設(shè) 計(jì):莫錦攀 *
* 修改日期:2007年07月26日 *
* 編譯環(huán)境:ICC-AVR6.31 *
* 實(shí)驗(yàn)環(huán)境:M16學(xué)習(xí)板 *
* 使用端口: LCD:PC6,PC7,PA4~PA7 *
* KEY:PB0,PD2,PD3 *
* 程序說(shuō)明:方波范圍:28HZ~22800HZ,步進(jìn)*
* 2HZ,低頻時(shí)可以觀測(cè)到LED閃爍*
* *
***************************************/
#include <iom16v.h>
#include "1602.h"
#include "delay.h"
#define uchar unsigned char
#define uint unsigned int
uchar get_key(void)
{
unsigned char i;
static unsigned char j;//按鍵記錄
PORTD|=0x0c;//pd2,pd3上拉電阻
DDRD&=0xf3;//pd2,pd3為輸入
if((PIND&0x04)==0)i='A';
else if((PIND&0x08)==0)i='B';
else i=0x00;
if (i == 0x00)//無(wú)有效按鍵按下
{
j = 0x00;//清除按鍵記錄
return 0x00;//程序退出
}
if (j == 0x00) //為新按鍵
{j = i;//保存本次采樣結(jié)果
delay_nms(10);//去按鍵顫抖
if((PIND&0x04)==0)i='A';
else if((PIND&0x08)==0)i='B';
else i=0x00;
if(i == j) {return i;}
}
return 0x00;
}
void init_timer0(void)
{
DDRB|=0x08;//OC0匹配輸出
TCNT0=0x00;//清除定時(shí)器值
TCCR0=0x6d;//快速PWM模式,匹配時(shí)OC0清零,top時(shí)置數(shù),系統(tǒng)時(shí)鐘1024分頻 頻,輸出比較匹配清除定時(shí)器值
OCR0=0x80;//OCR0置初值,占空比50%
}
void F_set(void)
{
static unsigned char i;
if(i!=5)i++;
else i=1;
switch(i)//頻率選擇
{
case 1:
{
TCCR0=0x6d;
LCD_write_string(6,0," 28HZ");
break;
} //1024分頻,F=28HZ
case 2:
{
TCCR0=0x6c;
LCD_write_string(6,0," 112HZ");
break;
}// 256分頻,F=112HZ
case 3:
{
TCCR0=0x6b;
LCD_write_string(6,0," 450HZ");
break;
} // 64分頻,F=450HZ
case 4:
{
TCCR0=0x6a;
LCD_write_string(6,0," 7200HZ");
break;
} // 8分頻,F=7200HZ
case 5:
{
TCCR0=0x69;
LCD_write_string(6,0,"28800HZ");
break;
} // 無(wú)分頻,F=28800HZ
default:break;
}
}
void R_set(void)
{
static unsigned char i;
if(i!=9)i++;
else i=1;
switch(i)//頻率選擇
{
case 1:
{
OCR0=0x0a;//占空比10%
LCD_write_string(13,0,"10%");
break;
}
case 2:
{
OCR0=0x14;//占空比20%
LCD_write_string(13,0,"20%");
break;
}
case 3:
{
OCR0=0x1e;//占空比30%
LCD_write_string(13,0,"30%");
break;
}
case 4:
{
OCR0=0x28;//占空比40%
LCD_write_string(13,0,"40%");
break;
}
case 5:
{
OCR0=0x80;//占空比50%
LCD_write_string(13,0,"50%");
break;
}
case 6:
{
OCR0=0x3c;//占空比60%
LCD_write_string(13,0,"60%");
break;
}
case 7:
{
OCR0=0x46;//占空比70%
LCD_write_string(13,0,"70%");
break;
}
case 8:
{
OCR0=0x50;//占空比80%
LCD_write_string(13,0,"80%");
break;
}
case 9:
{
OCR0=0x5a;//占空比90%
LCD_write_string(13,0,"90%");
break;
}
default:break;
}
}
void main(void)
{
uchar key;
LCD_init();//液晶初始化
init_timer0();
LCD_write_string(0,0,"F_out: 28HZ50%");
while(1)
{
key=get_key();
if(key=='A')F_set();
if(key=='B')R_set();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -