📄 main.c
字号:
//包含所需头文件
#include <avr/io.h>
#include <avr/interrupt.h>
#include "global.h"
#include "led.h"
#include "relay.h"
#include "ir.h"
#include "timer0.h"
#include <stdio.h>
#include <stdlib.h>
volatile UCHAR result[4] = {0,0,0,0};//通道状态
volatile UCHAR scan_number = 0;//通道扫描计数器
/**********************************************************************
* Function Name comparator_init
* Function Desc 比较器初始化函数
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
void comparator_init(void)
{
ACSR &= ~BIT(ACIE);//确保修改时不产生中断
ACSR = 0x0A;
}
/**********************************************************************
* Function Name SIGNAL(SIG_COMPARATOR)
* Function Desc 比较器中断服务函数
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
SIGNAL(SIG_COMPARATOR)
{
cli();
CLRBIT(ACSR,BIT(ACIE));//关闭中断
SETBIT(ACSR,BIT(ACI));
result[scan_number]++;
sei();
}
/**********************************************************************
* Function Name SIGNAL(SIG_OUTPUT_COMPARE2)
* Function Desc 定时器2比较中断复位程序
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
SIGNAL(SIG_OUTPUT_COMPARE2)
{
static UCHAR count = 0;
cli();
PORTC ^=BIT(scan_number + 0x01);
count++;
if(count == 4)
{
count = 0;
TCCR2 = 0x00;//关闭定时器
//CLRBIT(ACSR,BIT(ACIE));//关闭比较器中断
//SETBIT(ACSR,BIT(ACI));
}
sei();
}
/**********************************************************************
* Function Name SIGNAL(SIG_OUTPUT_COMPARE1A)
* Function Desc 定时器T1匹配中断A服务程序
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
cli();
comparator_init();//打开比较器中断
TCCR2 = 0x0A;//启动定时器2
sei();
}
/**********************************************************************
* Function Name SIGNAL(SIG_OUTPUT_COMPARE1B)
* Function Desc 定时器T1匹配中断B服务程序
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
SIGNAL(SIG_OUTPUT_COMPARE1B)
{
cli();
comparator_init();//打开比较器中断
TCCR2 = 0x0A;//启动定时器2
sei();
}
/**********************************************************************
* Function Name SIGNAL(SIG_OVERFLOW1)
* Function Desc 定时器T1溢出中断服务程序
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
SIGNAL(SIG_OVERFLOW1)
{
//UCHAR k;
cli();
CLRBIT(ACSR,BIT(ACIE));//关闭比较器中断
SETBIT(ACSR,BIT(ACI));
//srand((UCHAR)rand());
//k = rand()%100;
//OCR1A = 0x3E1 - k;
//OCR1B = 0x453 - k;
switch_control(scan_number,FALSE);//关闭上次扫描通道
scan_number++;
if(scan_number == 0x04)
{
scan_number = 0;
result_transact(result);//结果处理
}
switch_control(scan_number,TRUE);//打开本次扫描通道
sei();
}
/**********************************************************************
* Function Name port_init
* Function Desc 端口初始化
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
void port_init(void)
{
PORTB = 0b01111100;//初始时选通第一通道
DDRB = 0b10000111;
PORTC = 0x00;
DDRC = 0x3f;
PORTD = 0x00;
DDRD = 0b00111111;
}
/**********************************************************************
* Function Name timer1_init
* Function Desc 定时T1初始化
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
void timer1_init(void)
{
TCCR1B = 0x00;//停止定时器
TIMSK |= 0x1C;//中断允许
TCNT1H = 0x00;
TCNT1L = 0x00;//初始值
OCR1AH = 0x03;
OCR1AL = 0xE1;//匹配A值
OCR1BH = 0x04;
OCR1BL = 0x53;//匹配B值
ICR1H = 0x04;
ICR1L = 0xE1;//输入捕捉匹配值
TCCR1A = 0x02;
TCCR1B = 0x1B;//启动定时器
}
/**********************************************************************
* Function Name timer2_init
* Function Desc 定时器T2初始化
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
void timer2_init(void)
{
TCCR2 = 0x00;//停止定时器
TCNT2 = 0x00;//初始值
OCR2 = 0x7C;//匹配值
TIMSK |= BIT(OCIE2);//中断允许
//TCCR2 = 0x0A;//启动定时器
}
/**********************************************************************
* Function Name init_devices
* Function Desc 器件初始化函数
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
void init_devices(void)
{
cli(); //禁止所有中断
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
timer2_init();
timer1_init();
//comparator_init();
sei();//开全局中断
}
/**********************************************************************
* Function Name main
* Function Desc 主函数
* Return Value 无
* Parameter 无
* Version Date Editor Modification
* 1.0 2007/12/02 杨春华 做成
**********************************************************************/
int main(void)
{
led_init();
led_test();
//
//relay_test();
//
init_devices();
//relay_init();
timer0_init();
//
while(1)
{
NOP();
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -