#case
// Irsend16F1823.c BY PIC16F1823
//
// 2013.04.26 - Jurgen G Schmidt www.jgscraft.com
// 2014.12.15  UP/down方式から閾値データ送信方式へ変更
// 2015.1.24   識別コード追加、 UP/down方式プッシュボタン追加、STREAM=JGSDBGは未使用なので削除
//
// 16F1825 Send serial data via IR using DSM module to generate 38khz carrier
//
// - All ports support internal weak pullups
// - 25mA sink/source on I/O pins
//
// Compiler: CCS PCWH 4.130
// Programmer: PICkit2
// Dependencies: None
//
// + VDD -    1 14 - VSS -
// OSC1/RA5 - 2 13 - RA0/DAT
// OSC2/RA4 - 3 12 - RA1/CLK
// MCLR/RA3 - 4 11 - RA2/CCP3
// CCP1/RC5 - 5 10 - RC0/SCL/SCK
// TX/RC4 -   6  9 - RC1/SDA/SDI/CCP4
// CCP2/RC3 - 7  8 - RC2/ SDO
//
// RC4/MDOUT drives IR LED via 2N3904 transistor. For increased range you could
// use several LEDs. My T1 LED worked up to 10 feet at 2400 baud.
//
//-----------------------------------------------------------------------------
#include <16f1823.h>
#FUSES INTRC_IO //Internal RC Osc, no output
#FUSES NOWDT //No Watch Dog Timer
#FUSES NOPUT //No Power Up Timer
#FUSES NOMCLR //Engages weak pullup
#FUSES NOBROWNOUT //No brownout reset
//#FUSES CLKOUT //Check FOSC/4 on pin 3 - should be .125us
#use delay(internal=32M)
// Serial debug data via SW. INVERT so can connect directly to legacy PC serial port
//#use rs232( BAUD=9600, XMIT=PIN_C0, INVERT, STREAM=JGSDBG )//2016.1.24
// Use UART1, will modulate the 38khz carrier
#use rs232( BAUD=300, RCV=PIN_C5, XMIT=PIN_C4, STREAM=JGSDAT, ERRORS ) // default HW pins
#use fast_io(A)//2016.1.24
#use fast_io(C)//2016.1.24
#define port_a_io 0b00111011
#define port_c_io 0b00101000
//-----------------------------------------------------------------------------
//------ DSM Register Definitions
#byte MDCON = 0x39C //--Modulation control register--
#bit MDEN = MDCON.7 // DSM Enable 1=on
#bit MDOE = MDCON.6 // MDOUT Pin enable 1=on
#bit MDSLR = MDCON.5 // slew rate limiting enabled 1=on
#bit MDOPOL = MDCON.4 // output polarity 1=signal is inverted
#bit MDOUT = MDCON.3 // current value of MDOUT pin
#bit MDBIT = MDCON.0 // 1= Modulator uses high carrier source
#byte MDSRC = 0x39D //--Modulation source control register--
#bit MDMSODIS = MDSRC.7 // 1= disable output signal driving peripheral out pin
#bit MDMS3 = MDSRC.3 // Modulation source
#bit MDMS2 = MDSRC.2 // 0000 = MDBIT of MDCON
#bit MDMS1 = MDSRC.1 // 0001 = MDMIN port pin
#bit MDMS0 = MDSRC.0 // 1010 = EUSART TX output
#byte MDCARH = 0x39F //--Modulation high carrier control register--
#bit MDCHODIS = MDCARH.7 // 1= disable output signal driving peripheral out pin
#bit MDCHPOL = MDCARH.6 // output polarity 1=signal is inverted
#bit MDCHSYNC = MDCARH.5 // Modulator high carrier synchronization 1=on
#bit MDCH3 = MDCARH.3 // Data high carrier selection bits
#bit MDCH2 = MDCARH.2 // 0000 = Vss
#bit MDCH1 = MDCARH.1 // 0100 = CCP1
#bit MDCH0 = MDCARH.0 // 0001 = MDCIN1 port pin (C2 / pin 8)
// set to: 10100100
#byte MDCARL = 0x39E //--Modulation low carrier control register--
#bit MDCLODIS = MDCARL.7 // 1= disable output signal driving peripheral out pin
#bit MDCLPOL = MDCARL.6 // output polarity 1=signal is inverted
#bit MDCLSYNC = MDCARL.5 // Modulator low carrier synchronization 1=on
#bit MDCL3 = MDCARL.3 // Data low carrier selection bits
#bit MDCL2 = MDCARL.2 // 0000 = Vss
#bit MDCL1 = MDCARL.1 // 0100 = CCP1
#bit MDCL0 = MDCARL.0 // 0001 = MDCIN1 port pin (C2 / pin 8)
// set to: 0b10100000
// Test 1 - use MDMIN pin (pin 7) to toggle carrier output on MDOUT (pin 6) WORKS!!!!!
//
// MDCON = 0b11000001; // DSM enable, MDOUT enable, no slew, not inverted,modulator uses high carrier source
// MDSRC = 0b10000001; // disable peripheral, data from MDMIN pin (so we can see triggering on OSCOPE)
// MDCARH = 0b10100100; // disable peripheral, not inverted, synced, data high = CCP1 (38khz carrier)
// MDCARL = 0b10100000; // disable peripheral, not inverted, synced, data low = Vss
//
// Test 2 - use EUSART TX to drive carrier output on MDOUT (pin 6) WORKS!!!!!
//
// MDCON = 0b11000000;
// MDSRC = 0b10001010; // set source to EUSART, always disable peripheral pin
// MDCARH = 0b10000000; // the secret here is that data high = Vss
// MDCARL = 0b10000100; // and data low = CCP1 !!!
//
//-----------------------------------------------------------------------------
//------ CONSTANT definitions
//------ PINS ------
#define PB0 PIN_A5
#define PB1 PIN_A4
#define PB2 PIN_A3
#define PB3 PIN_C3//2016.1.24
#define LED PIN_C2
#define DB0 PIN_A0
#define DB1 PIN_A1
#define MINDB 5 // debounce count required for button press
//-----------------------------------------------------------------------------
//------ setups and initializations ------
//-----------------------------------------------------------------------------
int distinction,d2,d1,d0;
//int1 dip0,dip1;
int16 code,threshold;
int32 wrkmoto_32;
int32 wrkhosu_32;
void main()
{
int16 b0,b1,b2,b3;// push button debounce counters
setup_adc_ports( NO_ANALOGS); // ALWAYS, Always, always do this !!!
set_tris_a(port_a_io);
set_tris_c(port_c_io);
//----------------76543210
//port_a_pullups( 0b00110000 );
//port_c_pullups( 0b00100000 );
threshold = 224;
output_low( LED );
setup_timer_2(T2_DIV_BY_1, 210, 1 );//(4/32MHZ)×1×210×1=26.25μs→38.095KHZ
setup_ccp1( CCP_PWM );
set_pwm1_duty( 422L ); // square wave output - 50% duty
//---------76543210
MDCON = 0b11000000; // set DSM registers
MDSRC = 0b10001010;
MDCARH = 0b10000000;
MDCARL = 0b10000100;
//--------------------------------------------------------------------------
//------ Big Loop ------
while( TRUE )
{
    //dip0 = input(DB0);
    //dip1 = input(DB1);
    distinction = input_a() & 0b00000011;//下位2ビット以外は0
    distinction = distinction<<2;//
//------ get button press ------
if( input( PB0 ) == 0) {
b0++;
} else {
b0=0;
}
if( input( PB1 ) == 0) {
b1++;
} else {
b1=0;
}
if( input( PB2 ) == 0) {
b2++;
} else {
b2=0;
}
if( input( PB3 ) == 0) {
b3++;
} else {
b3=0;
}
//------ process the button press ------
// d2(7) d2(6) d2(5) d2(4) d2(3) d2(2) d2(1) d2(0) //  d1(7) d1(6) d1(5) d1(4) d1(3) d1(2) d1(1) d1(0) //  d0(7) d0(6) d0(5) d0(4) d0(3) d0(2) d0(1) d0(0)
//|-----------------------------補数------------------------------------------|--混信-----|--------------------------- threshoild ------------------------|
//thresholdに加算して送出
    if( b0 == MINDB )
{
output_high( LED );
//putc('A',JGSDBG);
threshold = threshold + 1;
if (threshold > 999 )
    {
        threshold = 999;
    }
d1 = make8(threshold,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(threshold,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);//1001 1010(9a) 0111/0110(76) 0101 1000(58).threshold=600と観測結果と一致
//fprintf(JGSDAT,"%c",d1);
}
if ( b0 > 125 )
{
output_high( LED );
threshold = threshold + 10;
if (threshold > 999 )
    {
        threshold = 999;
    }
d1 = make8(threshold,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(threshold,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
delay_ms( 300 );
}
//thresholdを減算して送出
if( b1 == MINDB ) {
output_high( LED );
if (threshold == 1 )
    {
        threshold = 1;
    }
else
    {
        threshold = threshold - 1;
    }
d1 = make8(threshold,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(threshold,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
//fprintf(JGSDAT,"%c",d1);
}
if ( b1 > 125 )
{
output_high( LED );
if (threshold >=11 )
    {
        threshold = threshold - 10;
    }
else
    {   
        threshold = 1;
    }
d1 = make8(threshold,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(threshold,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
delay_ms( 300 );
}
//+1加算固定コード送出
if( b2 == MINDB ) {
code = 1005;
output_high( LED );
d1 = make8(code,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(code,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
//fprintf(JGSDAT,"%c",d1);
}
//+10加算固定コード送出
if ( b2 > 125 )
{
code = 1013;
output_high( LED );
d1 = make8(code,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(code,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
delay_ms( 300 );
}
//-1加算固定コード送出
if( b3 == MINDB ) {
code = 1009;
output_high( LED );
d1 = make8(code,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(code,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
//fprintf(JGSDAT,"%c",d1);
}
//-10加算固定コード送出
if ( b3 > 125 )
{
code = 1017;
output_high( LED );
d1 = make8(code,1);//上位8ビットほd1へ。
d1 = distinction | d1;//d1は下位2ビットがデータなのでそのまま。distinctionは混信防止コード
d0 = make8(code,0);
wrkmoto_32 = make32(0,0,d1,d0);
wrkhosu_32 = ~wrkmoto_32;//補数(ビット反転)
wrkhosu_32 = wrkhosu_32<<12 ;
wrkmoto_32 = wrkmoto_32 | wrkhosu_32;
d2 = make8(wrkmoto_32,2);
d1 = make8(wrkmoto_32,1);
d0 = make8(wrkmoto_32,0);
fprintf(JGSDAT,"%c%c%c",d2,d1,d0);
delay_ms( 300 );
}
//
delay_ms( 10 );
output_low( LED );
}//------ end of big loop
}
//-----------------------------------------------------------------------------
//------ end of 1825Send.c ------