#include "STC8GXX.h"

typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef signed int int16_t;
typedef unsigned int uint16_t;
typedef signed long int32_t;
typedef unsigned long uint32_t;
/*
    1 / 36M = 27.7 ns
    4us / 27.7ns = 144.4
    1.5us / 27.7ns = 54.1
    500ns / 27.7ns = 18.05


    1 / 35M = 28.5 ns
    4us / 28.5ns = 140.3
    1.5us / 28.5ns = 52.6
    1us / 28.5ns = 35.08
    500ns / 28.5ns = 17.5
    250ns / 28.5ns = 8.7
*/

#define PWM_WIDTH  3

#if (PWM_WIDTH == 1)
#define PWM_PERIOD_CNT      (140 - 2)
#define PWM_RELOAD_OFFSET   8
#elif (PWM_WIDTH == 2)
#define PWM_PERIOD_CNT      (52 - 2)
#define PWM_RELOAD_OFFSET   8
#elif (PWM_WIDTH == 3)
#define PWM_PERIOD_CNT      (35 - 2)
#define PWM_RELOAD_OFFSET   8
#endif 

// #define PWM_DUTY_CNT        17
#define PWM_DUTY_CNT        9

#define PWM_COUNT_RELOAD_VALUE  (256 - PWM_PERIOD_CNT + PWM_RELOAD_OFFSET)     //add offset
// #define PWM_COMPARE_VALUE       (256 - PWM_PERIOD_CNT + PWM_DUTY_CNT)
#define PWM_COMPARE_VALUE       (256 - PWM_DUTY_CNT)


#define DAT_IN_PIN_IDLE     0
#define DAT_IN_PIN_WORK     1

#define DAT_IN_PIN       P32
#define DAT_OUT_PIN      P33

#define PWM_PERIOD       4000  //1500ns  unit: ns
#define PWM_DUTY         500   //unit: ns

uint8_t pwm_sw = 0;
uint8_t pwm_counter_over = 0;
uint8_t reload_value = PWM_COUNT_RELOAD_VALUE;
uint8_t compare_value = PWM_COMPARE_VALUE;

void INT0_isr() interrupt 0
{
    CR = 0;
    P_SW1 = 0x30;
    DAT_OUT_PIN = DAT_IN_PIN_IDLE;
    CCAPM1 = 0x00;
    if(DAT_IN_PIN)      //pin-level: 0 to 1		stop
    {  
        // CR = 0;
        CH = 0;
        CL = 0;
        CF = 0;
    }
    else                //pin-level: 1 to 0  start
    {
        P_SW1 = 0x10;
        CCAPM1 = 0x42;
        CH = 0xff;
        CL = reload_value;
        CR = 1;
        CF = 0;
    }
}

void PCA_isr() interrupt 7
{
    if(CF)
    {
        CL = reload_value;
        CH = 0xff;
        CF = 0;
    }

}

void pin_init(void)
{
/*
	PxM1	PxM0
	0		0			standrad
	0		1			pull-push
	1		0			high-z
	1		1			open drain
*/

    P0M0 = 0x00;
    P0M1 = 0x00;
    P1M0 = 0x00;
    P1M1 = 0x00;
    P2M0 = 0x00;
    P2M1 = 0x00;
    P3M0 = 0x00;
    P3M1 = 0x00;
    P4M0 = 0x00;
    P4M1 = 0x00;
    P5M0 = 0x00;
    P5M1 = 0x00;

    P3M0 = 0x08;
    P3M1 = 0x00;

    P3PU = 0x04;
    P3SR = 0x00;
	
}

void pwm_init(void)
{
    //STC8G1K08A

    // P_SW1 = 0x00;                               //ECI/P5.5, CCP0/P3.2, CCP1/P3.3, CCP2/P5.4
    P_SW1 = 0x10;                               //ECI_2/P5.5, CCP0_2/P3.1, CCP1_2/P3.3, CCP2_2/P5.4
    // P_SW1 = 0x20;                               //ECI_3/P3.1, CCP0_3/P3.2, CCP1_3/P3.3, CCP2_3/P5.5
    // P_SW1 = 0x30;                               //ECI_3/x, CCP0_3/x, CCP1_3/x, CCP2_3/x

    CCON = 0x00;
    CMOD = 0x09;                                //PCA clock is system clock

    CH = 0x00;
    CL = 0x00;

    // CCAPM1 = 0x42;                              //b0100 0010 PCA 1 is pwm mode
    CCAPM1 = 0x00;

    //EPCnH = 1 && CCAPnH = 0xff, pwm output hold 0
    // PCA_PWM1 = 0x02;                            //PCA 1 [8bit-pwm]    
    // CCAP1H = 0xFF;

    //EPCnH = 0 && CCAPnH = 0x00, pwm output hold 1
    // PCA_PWM1 = 0x00;
    // CCAP1H = 0x00;

    CCAP1L = compare_value;
    CCAP1H = compare_value;
    

    // CR = 1;                                     //start pca counter
    CR = 0;
}


void main(void)
{
    pin_init();
    pwm_init();

	DAT_OUT_PIN = DAT_IN_PIN_IDLE;
	
    PT0 = 1;
    PX0 = 1; 
    IT0 = 0;
    EX0 = 1;

    EA = 1;

    while (1)
    {

    }
}