找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1992|回复: 0
打印 上一主题 下一主题
收起左侧

avr mega8 PWM 超声发射试验程序

[复制链接]
跳转到指定楼层
楼主
ID:382220 发表于 2020-3-31 11:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// 采用硬件件定时器产生宽度可调PWM信号
//ICC-AVR application builder : 2020/3/23 10:29:05
// Target : M8
// Crystal: 11.059Mhz
//
#include <iom8v.h>
#include <macros.h>

// #define  FG1 0xFEED   // 20KHz
// #define  FG2 0x0113   //

#define  FG1 0xA99C    // 500HZ
#define  FG2 0x5664    //


unsigned char rtu0=0;
////////////////////////////////////////////////////////////////////////////////
void port_init(void)
{
PORTB = 0xE9;
DDRB  = 0x16;   // 0001 0110
PORTC = 0x7F;   // m103 output only
DDRC  = 0x00;
PORTD = 0xFF;
DDRD  = 0x00;
}
////////////////////////////////////////////////////////////////////////////////
//TIMER1 initialize - prescale:1
// WGM: 14) PWM fast, TOP=ICRn
// desired value: 20KHz
// actual value: 20.069KHz (0.2%)
void timer1_init(void)
{
TCCR1B = 0x00; // stop
TCNT1 = FG1;   // setup
//OCR1A = 247; // 90%FULL  
OCR1A = FG2>>1;
OCR1B = FG2;
ICR1  = FG2;

TCCR1A = 0x82;
TCCR1B = 0x19; //start Timer
}
////////////////////////////////////////////////////////////////////////////////
// TIMER2 initialize - prescale:1024
// WGM: Normal
// desired value: 23mSec
// actual  value: 22.963mSec (0.2%)
void timer2_init(void)
{
TCCR2 = 0x00; // stop
ASSR  = 0x00; // set async mode
TCNT2 = 0x08; // setup
OCR2  = 0xF8; //
TCCR2 = 0x07; // start
}
////////////////////////////////////////////////////////////////////////////////
#pragma interrupt_handler timer2_ovf_isr:iv_TIM2_OVF
void timer2_ovf_isr(void)
{
TCNT2 = 0x08;           // reload counter value

// if(OCR1A<FG2)OCR1A+=(FG2/100);
// else OCR1A=0;

// rtu0++;
if( rtu0>20 )
{ rtu0=0; PORTB^=0x14;} // B2
}
////////////////////////////////////////////////////////////////////////////////
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9600 (0.0%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x47; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0xD8;
}
////////////////////////////////////////////////////////////////////////////////
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
//uart has received a character in UDR
}
////////////////////////////////////////////////////////////////////////////////
#pragma interrupt_handler uart0_tx_isr:iv_USART0_TXC
void uart0_tx_isr(void)
{
//character has been transmitted
}
////////////////////////////////////////////////////////////////////////////////
//ADC initialize
// Conversion time: 75uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0x00; //select adc input 0
ACSR  = 0x80;
ADCSR = 0x86;
}
////////////////////////////////////////////////////////////////////////////////
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
timer2_init();
uart0_init();
adc_init();

MCUCR = 0x00;
GICR  = 0x00;
TIMSK = 0x40; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
////////////////////////////////////////////////////////////////////////////////


void main(void)
{

init_devices();

PORTB|=0x04;

while( 1 )
  {
  
  OCR1A=FG2/2;
  
  
   asm("NOP");asm("NOP");asm("NOP");asm("NOP");
   asm("NOP");asm("NOP");//asm("NOP");asm("NOP");
   
   // PORTB^=0x02;  //B1
   // while(1);
  }
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表