📄 iar9-5.c
字号:
#include <iom16.h>
#include<intrinsics.h>
#include "lcd1602_8bit.c"
uchar __flash title[]={"PWM test"};
#define uchar unsigned char
#define uint unsigned int
#define xtal 8
#define GET_BIT(x,y) (x&(1<<y))
uchar wide;
void port_init(void)
{
PORTD = 0x70;
DDRD = 0xF0;
__no_operation();
DDRD = 0x80;
}
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x01; //setup
OCR2 = 0xFF;
TCCR2 = 0x61; //start
}
//call this routine to initialize all peripherals
void init_devices(void)
{
port_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
}
void scan_s1(void)
{
if(GET_BIT(PIND,4)==0)
{
Delay_nms(10);
if(GET_BIT(PIND,4)==0)
{if(wide<255)wide++;
Delay_nms(200);}
}
}
void scan_s2(void)
{
if(GET_BIT(PIND,5)==0)
{
Delay_nms(10);
if(GET_BIT(PIND,5)==0)
{if(wide>0)wide--;
Delay_nms(200);}
}
}
void main(void)
{
uint voltage;
init_devices();
Delay_nms(400);
DDRA=0xff;PORTA=0x00;
DDRB=0xff;PORTB=0x00;
InitLcd();
ePutstr(4,0,title);
DisplayOneChar(1,1,'O');
DisplayOneChar(2,1,'C');
DisplayOneChar(3,1,'R');
DisplayOneChar(4,1,'2');
DisplayOneChar(5,1,':');
DisplayOneChar(12,1,'.');
DisplayOneChar(15,1,'V');
while(1)
{
scan_s1();
scan_s2();
DisplayOneChar(6,1,(wide/100)+0x30);
DisplayOneChar(7,1,(wide/10)%10+0x30);
DisplayOneChar(8,1,(wide%10)+0x30);
OCR2=wide;
voltage=(uint)wide;
voltage=(voltage*196)/100;
DisplayOneChar(11,1,(voltage/100)+0x30);
DisplayOneChar(13,1,(voltage/10)%10+0x30);
DisplayOneChar(14,1,(voltage%10)+0x30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -