/*=== Ir Remote controler for IO-Data (abc868-18424) ===*/
#include "mcc_generated_files/mcc.h"
#define MARK() __delay_us(562)
#define SPACE_1() __delay_us(1686)
#define SPACE_0() __delay_us(562)
#define READER_1() __delay_us(8992)
#define READER_0() __delay_us(4496)
#define GYAP_0() __delay_ms(43)
#define DSM_1() (MD1CON0bits.MD1BIT = 1)
#define DSM_0() (MD1CON0bits.MD1BIT = 0)
#define IO_CUSTOM 0xE880
#define SCN_DELAY() __delay_ms(30)
#define KEY_DELAY() __delay_ms(500)
uint8_t IO[][8] ={{0x80,0xE8,0x84,0x7B,0x80,0xE8,0x10,0xCF},
{0x80,0xE8,0x84,0x7B,0x80,0xE8,0x20,0xCF},
{0x80,0xE8,0x84,0x7B,0x80,0xE8,0x30,0xCF}};
uint16_t IO2[] = {
0x1084,0x4084,0x7084,0x7024, // 1, 4, 7,DG [Md=0]
0x2084,0x5084,0x8084,0xB084, // 2, 5, 8,11
0x3084,0x6084,0x9084,0x0094, // 3, 6, 9,BS
0x3014,0x4024,0x0014,0x1014, // Mn,Lf,C+,C-[Md=1]
0x2024,0x4014,0x3024,0xD084, // Up,Cr,Dw,Rt
0x2014,0x5024,0xE004,0xF004, // Sl,Rt,V+,V-
0xE014,0x0094,0x0000,0xA024, // Vd,BS, ,Ps[Md=2]
0x9024,0x0000,0x8014,0xA014, // HD, ,Cd,In
0xD014,0x7024,0x0000,0x9014, // PC,DG, ,So
0xC014,0xF024,0xE024,0xE084, // Dp,Pr,Dc,Sb[Md=3]
0x1024,0xC024,0xD024,0x5014, // Md,Wp,Nw,Sa
0x6014,0x0084,0xF084,0x7014, // Sz,Nx,pC,Tm
0xD004,0xA084,0xC084}; // power,10,12
uint16_t nco[] = {0xFFFF,0x0043,0x0086,0x010c}; // 1ms,1s,2s,4s
uint8_t sft;
uint16_t wsft;
uint16_t keyM;
uint8_t keyP;
uint8_t IrMd = 0;
/*---- IOdata Leader ---*/
void IO_Leader(){
DSM_1(); // "Leader" Start
READER_1();
DSM_0();
READER_0();
}
/*---- IOdata stop ---*/
void IO_Stop(){
DSM_1();
MARK(); // "Stop"
DSM_0();
}
/*--- Send strings of IRcode -----*/
void Ir_Send(uint8_t *dt,uint8_t x){
for (int j = 0; j < x; j++){
sft = 0x01;
for(int i = 0; i < 8; i++){
DSM_1();
MARK();
DSM_0();
if(dt[j] & sft){
SPACE_1();
}else{
SPACE_0();
}
sft <<= 1;
}
}
}
/*------- Send IOdata code -------*/
void IO_DATA(uint8_t c){
IO_Leader(); // "Leader" Start
Ir_Send(&IO[c][0],4); // Send "Data1"
IO_Stop(); // "Stop"
GYAP_0(); // Gyap time
IO_Leader(); // "Leader" Start
Ir_Send(&IO[c][4],4); // Send "Datae2"
IO_Stop(); // Stop
}
/*-------- Send word of IRcode ------*/
void Ir_Send2(uint16_t wd){
wsft = 0x0001;
for(int i = 0; i < 16; i++){
DSM_1();
MARK();
DSM_0();
if(wd & wsft){
SPACE_1();
}else{
SPACE_0();
}
wsft <<= 1;
}
}
/*-------- Send IOdata2 IRcode ----*/
void IO_DATA2(uint8_t c){
uint16_t w = IO2[c];
uint16_t wLo=(w&0x00FF) | ((~w << 8)&0xFF00);
uint16_t wHi=((w >> 8)&0x00FF) | (~w&0xFF00);
IO_Leader(); // "Leader" Start
Ir_Send2(IO_CUSTOM); // Send "Custom"
Ir_Send2(wLo); // Send "Data1"
IO_Stop(); // stop
GYAP_0(); // Gyap time
IO_Leader(); // "Leader" Start
Ir_Send2(IO_CUSTOM); // Send "Custom"
Ir_Send2(wHi); // Send "Data2"
IO_Stop(); // stop
printf(" %04X\n\r",w); // serial print
}
uint16_t keyScan(){
uint8_t k;
uint16_t ky;
X_A0_SetLow();
SCN_DELAY();
ky = ~PORTC & 0x0F;
X_A0_SetHigh();
Y_A1_SetLow();
SCN_DELAY();
k = ~PORTC & 0x0F;
ky |= ((uint16_t)k << 4);
Y_A1_SetHigh();
Z_A2_SetLow();
SCN_DELAY();
k = ~PORTC & 0x0F;
ky |= ((uint16_t)k << 8);
Z_A2_SetHigh();
return ky;
}
uint8_t bitCount(uint16_t w){
uint16_t wsft = 1;
uint8_t i;
for (i = 0; i < 12; i++){
if(w & wsft) return (i);
wsft <<= 1;
}
return 0;
}
void Power_Mode(){
uint8_t i;
__delay_ms(10);
if(PORTAbits.RA3) return;
for(i = 0; i < 20; i++){
__delay_ms(100);
if(PORTAbits.RA3){
IrMd = (IrMd + 1) % 4;
NCO1CON = 0x00; //NCO stop();
NCO1INCH = (nco[IrMd] >> 8) & 0xFF;
NCO1INCL = nco[IrMd] & 0xFF;
NCO1CON = 0x80; //NCO start
return;
}
}
IO_DATA2(48); // Power
while(!PORTAbits.RA3) ;
}
/* ===== Main application ====== */
void main(void)
{
char chr;
SYSTEM_Initialize();
INTERRUPT_PeripheralInterruptEnable();
INTERRUPT_GlobalInterruptEnable();
X_A0_SetHigh();
Y_A1_SetHigh();
Z_A2_SetHigh();
puts("Hellow\n\r");
while (1){
if(!PORTAbits.RA3) Power_Mode();
keyM = keyScan();
if(keyM){
keyP = bitCount(keyM) + (IrMd * 12);
IO_DATA2(keyP);
KEY_DELAY();
}
if(EUSART1_is_rx_ready()){
chr = getch();
putch(chr);
switch(chr){
case '1': IO_DATA2(0); break; // ch1
case '2': IO_DATA2(4); break; // ch2
case '3': IO_DATA2(8); break; // ch3
case '4': IO_DATA2(1); break; // ch4
case '5': IO_DATA2(5); break; // ch5
case '6': IO_DATA2(9); break; // ch6
case '7': IO_DATA2(2); break; // ch7
case '8': IO_DATA2(6); break; // ch8
case '9': IO_DATA2(10); break; // ch9
case 'a': IO_DATA2(49); break; // ch10
case 'b': IO_DATA2(7); break; // ch11
case 'c': IO_DATA2(50); break; // ch12
case 'v': IO_DATA2(22); break; // Vol+
case 'l': IO_DATA2(23); break; // vol-
case 's': IO_DATA2(25); break; // BS
case 'g': IO_DATA2(33); break; // DG
}
}
}
}
/* ===== End of File ===== */
|