欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標題:
MEGA8紅外學習程序下載
[打印本頁]
作者:
domdom
時間:
2017-11-22 09:10
標題:
MEGA8紅外學習程序下載
紅外學習程序下載
單片機源程序如下:
///////////////////////////////////////////////////////////////////////////////////
// This is the TOP2800.c file. //
// //
// 04.05.08 Created by domdom. //
///////////////////////////////////////////////////////////////////////////////////
#include <MEGA8.h>
#include "TWI.H"
#define uchar unsigned char
#define uint unsigned int
#define M62429MicClk PORTC.2
#define M62429VolClk PORTC.1
#define M62429Data PORTC.3
#define ir_in PIND.2 //ir_in
#define swsda PINB.3 //Key data in
#define swsck PORTB.2//Key ledsck
#define swcs PORTB.1 //Key cs
#define ledsck PORTB.4//LED ledsck
#define ledsda PORTB.5//LED data
#define sel_vga0 PORTD.6
#define sel_vga1 PORTD.7
#define sel_v PORTD.3
#define sel_a PORTB.6
#define bp PORTC.0
#define up PORTB.7
#define down PORTD.5
#define power PORTB.0
#define ir_out PORTD.4
#define rd_device_add 0xa1
#define wr_device_add 0xa0
void CommSend(uchar data);//串行口發(fā)送處理子程序
uint ScanKey();//鍵盤掃描程序
void SysInit(void);
void LearnIR(void);
void LearnOK(void);
void SendIR(void);
void SendToVGA(unsigned char index);
void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume);
unsigned char EEPROM_read(unsigned int addr);
//void EEPROM_write(unsigned int addr,unsigned char data);
uchar AT24C32_W(uchar mcu_data,uint AT24C32_address);
uchar AT24C32_R(uint AT24C32_address);
void KeyProcess(uint KeyValue);
void CommandInput(void);
interrupt [USART_RXC] void comm_isr(void); //串行口中斷程序
interrupt [EXT_INT0] void int0_isr(void); //the IR Input interrupt
interrupt [TIM1_OVF] void timer1_int(void);
interrupt [TIM0_OVF] void timer0_int(void);
interrupt [TIM2_OVF] void timer2_int(void); //used for power delay off
uchar RxFlag;
uchar RxBuf;
unsigned char gPWMEn;
unsigned int T2Cnt; //used for Timer2 count
unsigned char gLen,gCount;
unsigned char IrDataIndex;
unsigned char gState,RsFlag;
unsigned char gSending;
unsigned char gTimeoutCount;
unsigned char gLearning;
unsigned char gWaitingIR;
unsigned int gIRData[224];
unsigned char SerialBuf[16]; //串口緩存
const uchar BaudCode[]={7,12,25,51,103}; // 38400(error),19200,9600,4800,2400 bps
unsigned char VolumeSel,AVCnt,AudioVolume,MicVolume,LedValue,Learncnt,LearnEnCnt,gLearningFail,PowerOffFlag,LearningIREn;
void delay(uint t)//延時
{
uint i;
for(i=0;i<t;i++);
}
void Beep(uchar operation)
{
if(operation) bp=1;
else bp=0;
}
unsigned char EEPROM_read(unsigned int addr)
{
/* Wait for completion of previous write */
while(EECR & (1<<1))
;
/* Set up address register */
EEAR = addr;
/* Start eeprom read by writing EERE */
EECR |= (1<<0);
/* Return data from data register */
return EEDR;
}
/*
void EEPROM_write(unsigned int addr,unsigned char data)
{
//Wait for completion of previous write
while(EECR & (1<<1))
;
// Set up address and data registers
EEAR = addr;
EEDR = data;
// Write logical one to EEMWE
EECR |= (1<<2);
// Start eeprom write by setting EEWE
EECR |= (1<<1);
}
*/
void LED(uchar Data)
{
uchar i;
for(i=0;i<8;i++)
{
Data<<=1;
ledsda=SREG.0;
ledsck=0;
ledsck=1;
}
}
uint ScanKey()//鍵盤掃描程序
{
uchar i;
uint KeyVal;
KeyVal=0x0000;
delay(20);
swcs=1;
delay(20);
swsck=0;
delay(20);
swsck=1;
delay(20);
swcs=0;
KeyVal=KeyVal|swsda;
for(i=0;i<15;i++)
{
swsck=0;
delay(20);
swsck=1;
delay(20);
KeyVal<<=1;
KeyVal=KeyVal|swsda;
}
swcs=1;
return(KeyVal);
}
void SysInit(void)
{
DDRD=0xFB; /*input:PD2(IR_IN),PD0(RXD) 11111011*/
PORTD=0xFB; //0xB3;
DDRB=0xf7; /*input: PB3(swsda) 11110111*/
PORTB=0xf7;
DDRC=0xff;
PORTC=0xfe; //Beep
TWBR=32; //TWI
TWSR=0;
TCCR0=1;//stop
TCNT0=192;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
TCCR2=0x00;
ASSR=0x00;//CK
TCNT2=0;
OCR2=0;
TCCR2=0;//ctc,No prescaling,No OC2,stop;
//TCCR2=9;//ctc,No prescaling,No OC2,start;
GICR= 0x00; // Enable INT1 interrupt
MCUCR=1; //INT0 any change INT1 //Low LEVEL(SW_IN)
TCCR1A=0;
//TCCR1B=2; // TIMER1 clock is xtal/8 //start Timer
TCCR1B = 0; //stop
// preset TIMER1
TCNT1=0;
// clear TIMER1 interrupts flags
TIFR=0;
// disable TIMER1 overflow interrupt
TIMSK=0x00;
/*UART初始化*/
OSCCAL=EEPROM_read(0x1ff); //對內(nèi)部RC OSC調(diào)整
// OSCCAL=0xff;
//UCSRA=0xfc;//0xfc;
UBRRH=0;
UBRRL=BaudCode[3]; //default Baud Rate=4800 (i=3)
// UBRRL=11; //19200
//UBRRL=24; //19200
// UBRRL=50; //9600
// UBRRL=102; //4800
// UBRRL=204; //1200
// UBRL=103;
UCSRB=0x98;//enable UART receiver and transmitter, and receive interrupt(RXCIE)
UCSRC=(1<<7)|(1<<2)|(1<<1);//8位數(shù)據(jù)+1位STOP位
SREG=0x80; //enable interrupt bit in sreg
// CommSend(OSCCAL);
//initial system Variable
if(AT24C32_R(0x00)==0x7e) RsFlag=1;
else RsFlag=0;
LearningIREn=0;
RxFlag=0;
up=1;
down=1;
power=0;//Open Projector
LedValue=0xff;
LedValue&=0xfe;
LedValue|=0x02; //Off the Notebook LED;
LedValue|=0x04; //off the DV LED;
LedValue&=0xf7; //On the AV LED
LedValue|=0x20; //Off the Screen Down LED;
LedValue|=0x10; //Off the Screen Up LED;
LedValue|=0x40; //Off the Mic Sel LED
// LedValue|=0x80; //Off the IR Learning Sel LED
LED(LedValue);
sel_a=0; //difference
sel_v=0;
sel_vga1=0;
sel_vga0=0;
T2Cnt=0; //used for Timer2 count
Learncnt=0;
LearnEnCnt=0;
VolumeSel=0;
AVCnt=0;
gLearning=0;
gSending=0;
gLearningFail=0;
AudioVolume=73;
PowerOffFlag=1;
MicVolume = 73;
M62429Ctr(0,0,73); //Audio
M62429Ctr(0,1,73);
M62429Ctr(1,0,73); //Mic
M62429Ctr(1,1,73);
}
/******************************************
I2C總線寫一個字節(jié)
返回0:寫成功
返回非0:寫失敗
*******************************************/
uchar AT24C32_W(uchar mcu_data,uint AT24C32_address)
{
unsigned int addr;
addr=AT24C32_address;
Start();//I2C啟動
Wait();
if(TestAck()!=START) return 1;//ACK
Write8Bit(wr_device_add);//寫I2C從器件地址和寫方式
Wait();
if(TestAck()!=MT_SLA_ACK) return 1;//ACK
Write8Bit(addr>>8);//寫24C02的ROM地址h
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;//ACK
Write8Bit(AT24C32_address&0x00FF);//寫24C02的ROM地址l
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;//ACK
Write8Bit(mcu_data);//寫數(shù)據(jù)到24C02的ROM
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;//ACK
Stop();//I2C停止
delay(2000);//延時等EEPROM寫完
return 0;
}
/******************************************
I2C總線讀一個字節(jié)
如果讀失敗也返回0
*******************************************/
uchar AT24C32_R(uint AT24C32_address)
{
unsigned char temp;
unsigned int addr;
addr=AT24C32_address;
Start();//I2C啟動
Wait();
if (TestAck()!=START) return 0;//ACK
Write8Bit(wr_device_add);//寫I2C從器件地址和寫方式
Wait();
if(TestAck()!=MT_SLA_ACK) return 0;//ACK
Write8Bit(addr>>8);//寫24C02的ROM地址h
Wait();
if(TestAck()!=MT_DATA_ACK) return 0;//ACK
Write8Bit(AT24C32_address&0x00FF);//寫24C02的ROM地址l
Wait();
if (TestAck()!=MT_DATA_ACK) return 0;
Start();//I2C重新啟動
Wait();
if (TestAck()!=RE_START) return 0;
Write8Bit(rd_device_add);//寫I2C從器件地址和讀方式
Wait();
if(TestAck()!=MR_SLA_ACK) return 0;//ACK
Twi();//啟動主I2C讀方式
Wait();
if(TestAck()!=MR_DATA_NOACK) return 0;//ACK
temp=TWDR;//讀取I2C接收數(shù)據(jù)
Stop();//I2C停止
return temp;
}
interrupt [USART_RXC] void comm_isr(void)//串行口中斷程序
{
uchar i,sreg,FunCode;
// CommSend(0xF5);
if ((UCSRA&0x80)==0x80)
{
sreg=SREG;
SREG=0x0; //SREG=0x0;
RxFlag=0;
while (!(UCSRA&0x80)); UCSRA&=~0x80;//CommSend(UDR);
if(UDR!=0xFF) goto EXT; //同步碼
while (!(UCSRA&0x80)); UCSRA&=~0x80; //功能碼
FunCode=UDR;
//CommSend(FunCode); }
if(FunCode==0x7F)
{
while (!(UCSRA&0x80)); UCSRA&=~0x80;
RxBuf=UDR;delay(0xff);//CommSend(RxBuf); //接收數(shù)據(jù)
while (!(UCSRA&0x80)); UCSRA&=~0x80;//結束碼
if(UDR==0x55) RxFlag=1;
}
else if(FunCode==0x8F)//232 databyte
{
while (!(UCSRA&0x80));SerialBuf[0]=UDR;UCSRA&=~0x80;//數(shù)據(jù)索引
while (!(UCSRA&0x80));SerialBuf[1]=UDR;UCSRA&=~0x80;//Baud Rate
while (!(UCSRA&0x80));
SerialBuf[2]=UDR;
UCSRA&=~0x80;//RsDataLength
for(i=0;i<SerialBuf[2];i++)
{
while (!(UCSRA&0x80));
SerialBuf[i+3]=UDR;
// AT24C32_W(UDR,RsDataIndex*512+3+i);UCSRA&=~0x80;//RsDataCode
}
while (!(UCSRA&0x80));
if(UDR==0x55)
{ // CommSend(SerialBuf[0]);
AT24C32_W(SerialBuf[1],SerialBuf[0]*512+1);//CommSend(SerialBuf[1]);//Baud Rate
AT24C32_W(SerialBuf[2],SerialBuf[0]*512+2);//CommSend(SerialBuf[2]);//RsDataLength
for(i=0;i<SerialBuf[2];i++)
{
//CommSend(SerialBuf[i+3]);
AT24C32_W(SerialBuf[i+3],SerialBuf[0]*512+3+i);//Write RsDataCode TO IIC
}
AT24C32_W(0x7e,0); //Rs232 Flag
RsFlag=1;
}
else AT24C32_W(0,0); UCSRA&=~0x80;//結束碼
}
else goto EXT;
}
EXT:
SREG=sreg;
// CommSend(0x77);
}
void CommSend(uchar data) //串行口發(fā)送處理子程序
{
/* UCSRA |= (1<<6);//TXC
UDR = data;
do{} while ((UCSRA&(1<<6)) == 0); */
while (!(UCSRA&(1<<5)));
UDR=data;
}
void M62429WriteByte(unsigned char VolumeSel,unsigned char i,unsigned char Data)
{
if(VolumeSel%2) M62429MicClk = 0; //Mic
else M62429VolClk = 0; //Audio
do
{
Data = Data >> 1;
M62429Data = SREG.0;
delay(2);
if(VolumeSel%2) M62429MicClk = 1;
else M62429VolClk = 1;
delay(2);
M62429Data = 0;
delay(2);
if(VolumeSel%2) M62429MicClk = 0;
else M62429VolClk = 0;
} while (--i != 0);
}
void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume)
{
unsigned char temp;
if (Volume < 84)
{
Mod = Mod & 0x03;
temp = (Volume & 0x03) | 0x0c;
Volume = Volume + 4;
Volume = (Volume & ~0x03) | Mod;
M62429WriteByte(VolumeSel,7,Volume);
M62429WriteByte(VolumeSel,3,temp);
M62429Data = 1;
delay(3);
if(VolumeSel%2) M62429MicClk = 1;
else M62429VolClk = 1;
delay(4);
if(VolumeSel%2) M62429MicClk = 0;
else M62429VolClk = 0;
M62429Data = 0;
}
}
///////////////////////////////
//IR Learn
void LearnIR(void)
{
// unsigned int i;
// for(i=0;i<192;i++)
// gIRData[i]=0;
gCount=0;
gWaitingIR=1;
gState=0;
// gTimeoutCount=0;
GICR= 0x40; // Enable INT0 interrupt
MCUCR=0x01; //toggle
SREG=0x80; //Enable interrupt bit in sreg
gLearning=1;//Must add it again
TIMSK=0x40;// enable TIMER2 overflow
TCNT2=0;
TCCR2=0x07;//start timer2/1024 電源控制帶延時
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
top2800-all-ok-1006.rar
(7.17 KB, 下載次數(shù): 7)
2017-11-22 09:09 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1