AVR单片机如何学?ATMEGA16的定时/计数器的学习
扫描二维码
随时随地手机看文章
四、ATMEGA16的定时/计数器
四—(03)、PWM测试实验
手控PWM测试实验
定时器T2通过比较寄存器OCR2后从OC2脚(PD7)输出PWM信号,从000~255共分256级,对应的输出电压为0.00~5.00V
下面是图片~原理以前早就说过了~~不再赘述~~
然后就是程序:
不过从现在开始使用模块化变成了~~你懂得,为的就是用起来方便~~
这里是主函数:
//------------------------------------------------------------------------------
//手控PWM测试实验
//定时器T2通过比较寄存器OCR2后从OC2脚(PD7)输出PWM信号,从000~255共分256级,对应的输出电压为0.00~5.00V
#include"ioavr.h"
#include"intrinsics.h"
#include"lcdinit.h"
#include"delayics.h"
#include"bitics.h"
//------------------------------------------------------------------------------
uchar __flash title[]={"PWM Test"};
uchar __flash test[]={" OCR2:0000.00V"};
uchar wide;
//------------------------------------------------------------------------------
//端口初始化函数
void port_init()
{
DDRA=0XFF;
PORTA=0X00;
DDRB=0XFF;
PORTB=0X00;
DDRD=0X80;
__no_operation();
PORTD=0X70;
}
//------------------------------------------------------------------------------
//定时器2初始化函数
voidtimer2_init()
{
TCNT2=0X01;//TCNT2的初值为0X01
OCR2=0XFF;//输出比较寄存器初值为0XFF
TCCR2=0X61;//T2工作在相位修正PWM模式,没有预分频
}
//------------------------------------------------------------------------------
//芯片初始化函数
void device_init()
{
port_init();
timer2_init();
TIMSK=0X00;
}
//------------------------------------------------------------------------------
//按键S1检测函数
voids1_scan()
{
if(GET_BIT(PIND,4)==0)
{
delay_ms(2);
{
if(GET_BIT(PIND,4)==0)
{
if(wide<255)
wide++;
//delay_ms(50);
while(!GET_BIT(PIND,4));//等待松手!!!
}
}
}
}
//------------------------------------------------------------------------------
//按键S2检测函数
voids2_scan()
{
if(GET_BIT(PIND,5)==0)
{
delay_ms(2);
if(GET_BIT(PIND,5)==0)
{
if(wide>0)
wide--;
//delay_ms(50);
while(!GET_BIT(PIND,5));//等待松手~~~~
}
}
}
//------------------------------------------------------------------------------
//main
voidmain()
{
uint voltage;
device_init();
//delay_ms(40);
lcd_init();
lcd_display_chars(0x80,title,8);
lcd_display_chars(0x80+0x40,test,16);
while(1)
{
s1_scan();
s2_scan();
lcd_display_onechar(0x80+0x40+6,(wide/100)+0x30);
lcd_display_onechar(0x80+0x40+7,(wide0/10)+0x30);
lcd_display_onechar(0x80+0x40+8,(wide)+0x30);
OCR2=wide;
voltage=(uint)wide;
voltage=(voltage*196)/100;
lcd_display_onechar(0x80+0x40+11,(voltage/100)+0x30);
lcd_display_onechar(0x80+0x40+13,(voltage0/10)+0x30);
lcd_display_onechar(0x80+0x40+14,(voltage)+0x30);
}
}
LCD初始化函数:以后就一直用这个了:记住:(我也是花了时间正好的~~当时电脑还特别不给力,真他妈的想换了~~擦,不过又让我给修复好了~~爽歪歪啊)
//------------------------------------------------------------------------------
//LCD1602的初始化程序—可调用函数
#include"ioavr.h"
#include"intrinsics.h"
#include"delayics.h"
#include"bitics.h"
//------------------------------------------------------------------------------
typedef unsigned char uchar;
typedef unsigned intuint;
//------------------------------------------------------------------------------
//RS、RW、EN引脚输出高低电平的宏定义
#define lcd_rs_1SET_BIT(PORTB,0)
#define lcd_rs_0CLR_BIT(PORTB,0)
#define lcd_rw_1SET_BIT(PORTB,1)
#define lcd_rw_0CLR_BIT(PORTB,1)
#define lcd_en_1SET_BIT(PORTB,2)
#define lcd_en_0CLR_BIT(PORTB,2)
//------------------------------------------------------------------------------
#define data_port PORTA
#define busy0x80
//------------------------------------------------------------------------------
//LCD写数据函数
void lcd_write_data(uchar dat)
{
lcd_rs_1;
lcd_rw_0;
data_port=dat;
delay_ms(1);
lcd_en_1;
delay_ms(1);
lcd_en_0;
}
//------------------------------------------------------------------------------
//LCD写指令函数
voidlcd_write_command(uchar com)
{
lcd_rs_0;
lcd_rw_0;
data_port=com;
delay_ms(1);
lcd_en_1;
delay_ms(1);
lcd_en_0;
}
//------------------------------------------------------------------------------
//LCD初始化函数
void lcd_init()
{
lcd_write_command(0x01);
delay_ms(1);
lcd_write_command(0x38);
delay_ms(1);
lcd_write_command(0x0c);
delay_ms(1);
lcd_write_command(0x06);
delay_ms(1);
}
//------------------------------------------------------------------------------
//LCD显示一个字节函数
voidlcd_display_onechar(uchar add,uchar dat)
{
lcd_write_command(add);
delay_ms(1);
lcd_write_data(dat);
delay_ms(1);
}
//------------------------------------------------------------------------------
//LCD显示多个字节函数
voidlcd_display_chars(uchar add,uchar __flash *s,uchar num)
{
uchar i;
lcd_write_command(add);
for(i=0;i
#ifndef __lcdinit_h
#define __lcdinit_h
typedef unsigned char uchar;
typedef unsigned intuint;
//------------------------------------------------------------------------------
externvoidlcd_write_data(uchar dat);//LCD写数据函数
externvoidlcd_write_command(uchar com);//LCD写指令函数
externvoidlcd_init();//LCD初始化函数
externvoidlcd_display_onechar(uchar add,uchar dat);//LCD显示一个字节函数
voidlcd_display_chars(uchar add,uchar __flash *s,uchar num);//LCD显示多个字节函数
#endif
然后贴张图片~~看看我都用到了哪些程序模块~~