简介:
本资料为AVR系列单片机竞赛设计实例程序22例PROTEUS仿真资料-AVR单片机三路PWM控制程序及仿真。
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.3 Professional
Automatic Program Generator
Project : p
Version : 1.00
Author : hgs(qrs)
Company : jlddx
Comments:
3路/8位/相位修正模式PWM输出测试程序,可在PB1、2、3(PIN15、16、17)
引脚接入3只LED发光二极管(共阴)观测亮度变化。
Chip type : ATmega8
Program type : Application
Clock frequency : 4.000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega8.h>
#include <delay.h>
#define key1 PINC.0
#define key2 PINC.1
#define key3 PINC.2
#define key4 PINC.3
#define key5 PINC.4
#define key6 PINC.5
// Declare your global variables here
void main(void)
{
unsigned char a=0;
unsigned char b=0;
unsigned char c=0;
// Declare your local variables here
// Input/Output Ports initialization
// Port B initialization
//配置为输出
PORTB=0x00;
DDRB=0xFF;
// Port C initialization
//配置为输入,并使内部上拉有效
PORTC=0xff;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
//配置为输入,高阻态
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz (产生约122.5Hz的PWM信号)
// Mode: Ph. correct PWM top=00FFh
// OC1A output: Inverted
// OC1B output: Inverted
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
//TCCR1A=0xF1; //调整输出波形相位(反相)
TCCR1A=0xA1; //调整输出波形相位(正相)
TCCR1B=0x03; // (产生约122.5Hz的PWM信号)
//TCCR1B=0x02; // (产生约980.4Hz的PWM信号)
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00; //改变此值将改变PWM1的占空比
OCR1BH=0x00;
OCR1BL=0x00; //改变此值将改变PWM2占空比
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 62.500 kHz (产生约122.5Hz的PWM信号)
// Mode: Phase correct PWM top=FFh
// OC2 output: Inverted PWM
ASSR=0x00;
TCCR2=0x64; //调整输出波形相位(正相) (产生约122.5Hz的PWM信号)
//TCCR2=0x62; //调整输出波形相位(正相) (产生约980.4Hz的PWM信号)
//TCCR2=0x74; //调整输出波形相位(反相)
TCNT2=0x00;
OCR2=0x00; //改变此值将改变PWM3的占空比
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
while (1)
{
if (key1==0) //判断PWM1+是否按下
{
delay_ms(20); //去抖动
if (key1==0) //再次判断
{
if (a!=255) //增加占空比
++a;
else //使占空比比较器最大值保持为255
a=255;
OCR1AL=a;
};
};
if (key2==0) //判断PWM1-是否按下
{
delay_ms(20); //去抖动
if (key2==0) //再次判断
{
if (a!=0) //减少占空比
--a;
else //使占空比比较器最小值保持为0
a=0;
OCR1AL=a;
};
};
//-------------------------------------------
if (key3==0) //判断PWM1+是否按下
{
delay_ms(20); //去抖动
if (key3==0) //再次判断
{
if (b!=255) //增加占空比
++b;
else //使占空比比较器最大值保持为255
b=255;
OCR1BL=b;
};
};
if (key4==0) //判断PWM1-是否按下
{
delay_ms(20); //去抖动
if (key4==0) //再次判断
{
if (b!=0) //减少占空比
--b;
else //使占空比比较器最小值保持为0
b=0;
OCR1BL=b;
};
};
//---------------------------------------------------------
if (key5==0) //判断PWM1+是否按下
{
delay_ms(20); //去抖动
if (key5==0) //再次判断
{
if (c!=255) //增加占空比
++c;
else //使占空比比较器最大值保持为255
c=255;
OCR2=c;
};
};
if (key6==0) //判断PWM1-是否按下
{
delay_ms(20); //去抖动
if (key6==0) //再次判断
{
if (c!=0) //减少占空比
--c;
else //使占空比比较器最小值保持为0
c=0;
OCR2=c;
};
};
//------------------------------------------------------------
delay_ms(40); //控制按键扫描间隔,防止按住不放时变化过快
};
}