dc.c

来自「这是关于AVR单片机学习的初步开发」· C语言 代码 · 共 81 行

C
81
字号
//Designed by Buaa Alf
//通过定时器实现了DC电机的运转,仅仅是一演示程序;
//烧写程序后,断开电源开关,闭合SW9的1、2和7、8管角,然后闭合电源开关即可看到电机的运转。
//可参考设计自己的电机控制程序。


//ICC-AVR application builder : 2005-3-31 上午 09:37:18
// Target : M8
// Crystal: 11.059Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0xFF;
 DDRB  = 0x03;
 PORTC = 0x7F; //m103 output only
 DDRC  = 0x00;
 PORTD = 0xFF;
 DDRD  = 0x00;
}

//TIMER1 initialisation - prescale:6
// WGM: 2) PWM 9bit phz correct, TOP=0x01FF
// desired value: 1Hz
// actual value: 21640Hz (100.0%)
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 TCNT1H = 0xFE; //setup
 TCNT1L = 0x01;
 
 OCR1AH = 0x00;
 OCR1AL = 0xEF;
 OCR1BH = 0x00;
 OCR1BL = 0xEF;
 
 ICR1H  = 0x01;
 ICR1L  = 0xFF;
 TCCR1A = 0x82;
 TCCR1B = 0x09; //start Timer
}

#pragma interrupt_handler timer1_compa_isr:7
void timer1_compa_isr(void)
{
 //compare occured TCNT1=OCR1A
 PORTB |=0x01; 
}

#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
 PORTB &=~0x01;
}

//call this routine to initialise all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 timer1_init();

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

//
void main(void)
{
 init_devices();
 //insert your functional code here...
 while(1);
}

⌨️ 快捷键说明

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