#include <stdlib.h>
#include <iom16v.h>
#include <macros.h>
//interrupt????????????
typedef unsigned short int uint16_t;
/* from 50 to 950 */
uint16_t duty_cycle;
uint16_t i;
uint16_t j;
/*
#pragma interrupt_handler timer0_ovf_isr:10 //???????????????
void timer0_ovf_isr(void) // When TCNT1 overflow..........
{
// if the duty is at 95% the restart from 5%
if (duty_cycle > 950)
duty_cycle = 50;
DDRA = 0x03;
PORTA = 0x03;
// increment the duty cycle by 1 step at the time wich is
// 0.1%, in this way I will complete 100% in 2 seconds (1000 step)
duty_cycle=duty_cycle+100;
// set the level where the output pin OC1A will toggle
OCR1A = duty_cycle;
// set the level where the output pin OC1B will toggle
// this will be the inverse of OC1A level
OCR1B = 1000 - duty_cycle;
TCNT1 = 0x00;
DelayMs(20);
PORTA = 0x00;
}
*/
void DelayMs(int count) //Gives a delay of 1 Ms
{
int i;
int j;
for(i=0;i<count;i++)
{
for(j=0;j<5000;j++);
}
}
void counter_setup(void)
{
/* Pin OC1A & B set to output */
DDRD |= (1<<PD4) | (1<<PD5) |(1<<PD7);
DDRB |= (1<<PB3) ;
/* Clear OC1A & B on compare match, set it at BOTTOM.
Waveform generation mode on 14, see datasheet */
TCCR1A = (1<<COM1A1)|(1<<COM1B1)|(0<<COM1A0)|(0<<COM1B0)|(1<<WGM11);
TCCR1B = (1<<WGM13)|(1<<WGM12);
TCCR0=(1<<WGM00)|(1<<WGM01)|(1<<COM00)|(1<<COM01);
TCCR2=(1<<WGM20)|(1<<WGM21)|(1<<COM20)|(1<<COM21);
/* TOP value*/
ICR1 =500;
TCNT0 = 0x00;
TCNT2 = 0x00;
/* start with a duty of 50% */
OCR1A = 430;
duty_cycle=OCR1A;
OCR1B = 59;
OCR0 = 100; //set compare 占空比
OCR2 = 150;
/* enable interrupt on timer overflow */
//TIMSK = (1<<OCIE2)|(1<<TOIE2);
}
int main(void)
{
/* prepare everything */
counter_setup();
/* enable interrupt */
SEI();
/* start the counter with prescaler = 256 */
TCCR1B |= (1<<CS12)|(0<<CS11)|(0<<CS10);
/* start the counter with prescaler = 64 */
TCCR0 |=(0<<CS02)|(1<<CS01)|(0<<CS00);
/* start the counter with prescaler = 32 */
TCCR2 |=(0<<CS22)|(1<<CS21)|(1<<CS20);
/* Infinite loop doing nothing, everything is
handled by the IRQ routine called 500 times x second. */
for (;;)
{
if (OCR1A > 492)
OCR1A = 430;
/*if (OCR1B > 900)
OCR1B = 100;*/
DelayMs(1000);
OCR1A=OCR1A+5;
//OCR1B=OCR1B+100;
}
/* just for correct looking code, disable irq and terminate */
CLI();
return(0);
}
PWM.rar_AVR PWM_AVR PWM mega16_pwm avr mega16
版权申诉
123 浏览量
2022-09-21
08:36:25
上传
评论
收藏 1KB RAR 举报
邓凌佳
- 粉丝: 65
- 资源: 1万+
最新资源
- 南京邮电大学数学实验:熟练掌握 Matlab 软件的基本命令和操作
- 2017校招真题校园招聘真题算法题(37道)Python源码.zip
- 基于单片机protues仿真的多功能自动饮水机系统设计(仿真图、源代码、演示视频)
- 二叉树7-1-1.cpp
- android 9.0 原生模拟器 签名文件
- 技术面试最后反问面试官的话 校招面试非技术问题有哪些 非技术问题如何回答.png
- NB-IOT-BC26全网通模块Altium+ CADENCE +PADS三种格式(原理图SCH+PCB封装库)文件.zip
- 基于微信小程序开发的校园失物招领系统源码毕业设计(优质项目源码).zip
- 词向量是一种将自然语言中的单词转换为数值向量的技术,它能够捕捉词义和上下文信息
- nmap与masscan的简单使用
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈