欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2674|回復: 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);
  }
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表