📄 d16p32v11.c
字号:
/***************************************
mcu:ATMega8
2007-10-18 256级七彩流水
遥控输入
16MHz
**************************************/
#include "inc.h"
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include <util/delay.h>
extern const uchar qct[];
uint16_t data0,data1;
uint8_t bitcnt;
uint8_t CaptureOK;
uint16_t ms,Sec;
uint32_t MsCnt; //ms count
/*********************************************
移位输出一个像素用一个字节表示
************************************************/
void Shift(uint da)
{
uchar i;
for(i=0;i<16;i++)
{
CLR_SCK;
if(da&0x8000)
SET_BLU;
else
CLR_BLU;
SET_SCK;
da<<=1;
}
}
/*********************************************
扫描256级灰度
************************************************/
void Scan(void)
{
uchar i,j;
uint m;
const char * n;
const uchar Mask[]={1,2,4,8,16,128,32,64,128,128,64,128,32,64,128,128,64,128,128};
const uchar OETAB[]={PIX,PIX/16,PIX/8,PIX/4,PIX/2,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX,PIX};
for(m=0;m<4596;m+=3)
{
//for(k=0;k<20;k++)
for(i=0;i<18;i++)
{
n=qct+m;
for(j=0;j<PIX;j++)
{
if(j>=OETAB[i])
SET_OE;
else
CLR_OE;
CLR_SCK;
if(pgm_read_byte(48*(j/3)+n++) & Mask[i])
SET_BLU;
else
CLR_BLU;
SET_SCK;
}
SET_LAT;
CLR_LAT;
}
}
}
/******************************
延时
******************************/
void Delay(void)
{
uint i,j;
for(i=0;i<400;i++)
for(j=0;j<1000;j++);
}
/****************************************
定时器0初始化
*******************************************/
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xfe; //set count
TCCR0 = 0x02; //无分频
TIMSK = 0x01;
}
/******************************************/
void int0_ini(void)
{
MCUCR=(1<<ISC01);//下升沿
GICR=0x40; //允许int0中断
}
/******************************************
定时器0中断服务程序
//#pragma interrupt_handler timer0_ovf_isr:10
*******************************************/
SIGNAL(SIG_OVERFLOW0)
{
TCNT0 = 0xfe;//0xe1;; //重装初值
ms++;
MsCnt++;
if(MsCnt>100000)
{
MsCnt=0;
Sec++;
if(Sec>300)Sec=0;
}
}
/******************************
红外接收中断
******************************/
SIGNAL (SIG_INTERRUPT0)
{
static uint oldFall;
uint temp,newFall;
newFall=ms;
if(newFall>oldFall)
temp=newFall-oldFall; //计算脉冲加间隔的时间
else
temp=65535-oldFall+newFall;
oldFall=newFall;
if(temp>80&& temp<130) // "0"信号1.125ms
{
temp=0;
}
else if(temp>170 && temp<220) //“1”信号2.25ms
{
temp=1;
}
else if(temp>800 && temp<1090) //重复”信号11.25ms
{
CaptureOK=1;
CLR_LED1;
return;
}
else if(temp>1100 && temp<1300) //header信号 13.5ms
{
bitcnt=0;
data0=0;
data1=0;
CLR_LED1;
return; //返回,等待下次开始接收
}
else ///干扰信号
{
return;
}
bitcnt++;
if(bitcnt<16) //开始接收前16位
{
data0=data0|(uint)temp;
data0=data0<<1;
}
else if(bitcnt==16)
{
data0=data0|(uint)temp;
}
else if(bitcnt<32) //开始接收后16位
{
data1=data1|(uint)temp;
data1=data1<<1;
}
else if(bitcnt==32) //接收完最后一位
{
data1=data1|(uint)temp;
CaptureOK=1;
}
}
/*******************************
初始化IO
****************************/
void IO_ini(void)
{
PORTB=0xff;
PORTC=0Xff;
PORTD=0Xff;
DDRD=(1<<LED1)|(1<<LED2);
DDRC=(1<<RED)|(1<<GRN)|(1<<BLU)|(1<<OE)|(1<<LAT)|(1<<SCK);
}
/****************
主程
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -