📄 main.c
字号:
/***************************************
* 方波生成器 *
* 程序功能: 生成占空比可调方波 *
* 时钟频率: 外部7.3728M *
* 设 计:莫锦攀 *
* 修改日期:2007年07月26日 *
* 编译环境:ICC-AVR6.31 *
* 实验环境:M16学习板 *
* 使用端口: LCD:PC6,PC7,PA4~PA7 *
* KEY:PB0,PD2,PD3 *
* 程序说明:方波范围:28HZ~22800HZ,步进*
* 2HZ,低频时可以观测到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)//无有效按键按下
{
j = 0x00;//清除按键记录
return 0x00;//程序退出
}
if (j == 0x00) //为新按键
{j = i;//保存本次采样结果
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;//清除定时器值
TCCR0=0x6d;//快速PWM模式,匹配时OC0清零,top时置数,系统时钟1024分频 频,输出比较匹配清除定时器值
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;
} // 无分频,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();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -