⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm.c

📁 采用Atmega16的变频式超声波发生器的源代码
💻 C
字号:
#include"main.h"

void pwm(ulong voltage)
{
#define pwm_bit 10

sbi(ddrb,6);
TCCR1A|=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM11);
//when compared the output is set to 0 
//and set to 1 when reach the top
TCCR1B|=(1<<WGM13)|(1<<WGM12)|(1<<CS10);
//13bit resolution,fast PWM,no prescaler
ICR1=(1<<pwm_bit)-1;

OCR1B=voltage*ICR1/5000;      
}




void pwm_phase(ulong frequency,ulong voltage)
{

uint prescaler;

sbi(ddrb,5);
//channel A output
TCCR1A|=(1<<COM1A1);
//when compare match, the OC1A is clear,and set when reaching the top

TCCR1B|=(1<<WGM13);
//phase and frequency correct PWM mode,TOP=ICR1;

if(frequency>121)
{
prescaler=1;
TCCR1B|=1;
}
else

if(frequency>14)
{
prescaler=8;
TCCR1B|=2;
}
else

if(frequency>3)
{
prescaler=64;
TCCR1B|=3;
}
else
{
prescaler=256;
TCCR1B|=4;
}

ICR1=(ulong) F_CPU/2/prescaler/frequency;
//voltage:13bit resolution 1/8192=0.000122

OCR1A=voltage*ICR1/5000;




}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -