/* * File: T1con_ver_1_6_1.c * CPU: 12LF1822 * Electrical diagram: T1cont_ver1-3 210109 * Created on 2021/01/28, 21:40 */ #include #include #include // Definition #define _XTAL_FREQ 31000 #define DATA RA0 // T1 DATA terminal #define TUNEON RA1 // Set T1 TUNE mode #define TUNE RA2 // Check T1 TUNE mode #define START RA4 // When 705 TUNE start "0", Keep "1" for TUNER on #define KEY RA5 // Key trans start "0", "1" detect when 705 on // Configuration 1 #pragma config FOSC = INTOSC // Use internal clock(INTOSC) #pragma config WDTE = OFF // No watch dog timer(OFF) #pragma config PWRTE = ON // Start program 64ms after power on(ON) #pragma config MCLRE = OFF // No use RA3 as reset terminal(OFF) #pragma config CP = OFF // No protection for program memory(OFF) #pragma config CPD = OFF // No protection for data memory(OFF) #pragma config BOREN = OFF // Power voltage watch ON(OFF) #pragma config CLKOUTEN = OFF // No output for clock signal #pragma config IESO = OFF // No use to switch int/ext clock(OFF) #pragma config FCMEN = OFF // No watch ext clock(OFF) // Configuration 2 #pragma config WRT = OFF // No protection for ptogram memory #pragma config PLLEN = OFF // No use 4times PLL clock #pragma config STVREN = ON // Reset when stock over #pragma config BORV = LO // Set BOR 1.9V #pragma config LVP = OFF // No LO voltage program // Initialize EEPROM __eeprom unsigned char EE_DATA[10] = {0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00} ; // unsigned int TIMEOUT ; unsigned char T1HIST ; // Record of T1 connection "0" = not yet, "1" = On unsigned char MODE ; // Store battery indicator mode void main() { OSCCON = 0b00000010 ; // Set internal clock 31KHzLF ANSELA = 0b00000000 ; // No use analog input TRISA = 0b00110101 ; // Set RA0,2,4,5 port as Input WPUA = 0b00000000 ; // Cancel pull up for START OPTION_REG = 0b00000000 ; // Use pull up for degital I/O // Initialization for all output port PORTA = 0b00000000 ; // set Output port RA1,5 //TUNEON for T1 Sw, KEY for Transmission of Radio WPUA4 = 1 ; // Set pull up for START if(TUNE == 0) // Checking connection of T1 {T1HIST = 1; } // If T1 is connected T1HIST=1 else {T1HIST = 0 ;} MODE = eeprom_read(0) ; if(MODE == 0) { eeprom_write(0,1) ; } else { eeprom_write(0,0) ; } // Main loop while(1) { // Check connection status of 705 TRISA5 = 1 ; // Set KEY port as Input if((KEY == 1) && (T1HIST == 1)) { //If 705 is connected, proceed next // Otherwise go to Sleep // Process for tune request from T1 if(TUNE == 1) { __delay_ms(20) ; // Wait for chattering // Measure TUNE SW on time // If it is less than 0.5 sec, go to TAP // If it is longer 25sec, forced RESET for(TIMEOUT = 0; TIMEOUT < 3500; TIMEOUT++) { __delay_ms(2) ; if(TUNE == 0) {break ;} // Count up until TUNE=0 or TIMEOUT=3500 } if(TIMEOUT >= 3000) { RESET() ;} // T1 SW on 25sec so force RESET if(TIMEOUT > 100 ) { // T1 SW on 0.5sec or more, go next // othrewise go TAP routine // Reserve DATA if(DATA == 1) { // If DATA is not HI, T1 is disconnected __delay_ms(110) ; // Wait 0.11 sec. (for reserve DATA) // Start to transmit tone TRISA5 = 0 ; // Set KEY port as Output KEY = 0 ; // KEY "0" (KEY is ON) __delay_ms(500) ; // Wait 0.5 sec. // Keep transmit while TUNE is "1". If it is continue more than 15 sec. // T1con may disconnect to T1, so exit the loop to stop transmission TIMEOUT = 0 ; while((TUNE == 1)&&(TIMEOUT < 150)) { __delay_ms(100) ; //wait 0.1 sec TIMEOUT = TIMEOUT + 1 ; } KEY = 1 ; // Set KEY "1" to turn transmission Off TRISA5 = 1 ; // Set KEY port as Input if(TIMEOUT >= 150) // If TUNE continue ON 15sec or more { T1HIST = 0 ; } // T1 connection record turns "not yet" } else{ T1HIST = 0 ; } // T1 connection record turns "not yet" } // PWR(Tap) routine else{ __delay_ms(4000); // Wait because T1 "PWR" is tapped T1HIST = 1 ; // T1 connection record turns "Connected" } } T1HIST = 1 ; // T1 connection record turns "Connected" //Process for tune request from IC-705 if(START == 0) { // If START is "0"(START is "0" at TUNER terminal) TRISA5 = 1 ; // Set KEY port as Input __delay_ms(10) ; // Wait 0.01 sec. if( KEY == 1) { TUNEON = 1 ; // TUNEON "1"(T1 is ON) __delay_ms(600) ; // Wait 0.6 sec. TUNEON = 0 ; // Set TUNEON "0"(T1 is OFF) // Check DATA port is HI, if not exit routine __delay_ms(10) ; // Wait 0.01 sec. if(DATA == 1) { // If DATA is not HI, T1 is disconnected // If use the DATA Signal to send T1, this is the position to put its routine // __delay_ms(110) ; // Wait 0.11 sec. (for reserve DATA)) TRISA5 = 0 ; // Set KEY port as Output KEY = 0 ; // KEY "0"(KEY is ON) Start tone __delay_ms(500) ; // Wait 0.5 sec. // Keep transmit while TUNE is "1". If it is continue more than 15 sec, // T1con may disconnect to T1, so exit the loop to stop transmission TIMEOUT = 0 ; while((TUNE == 1)&&(TIMEOUT < 150)) { __delay_ms(100) ; //Wait 0.1 sec TIMEOUT = TIMEOUT + 1 ; } KEY = 1 ; //Set KEY "1"(KEY is OFF) TRISA5 = 1 ; // Set KEY port as Input if(TIMEOUT >= 150) // If TUNE continue ON 25sec or more { T1HIST = 0 ; } // T1 connection record turns "not yet" } else {T1HIST = 0 ; } // T1 connection record turns "not yet" } } } // Sleep mode routine else { // Battery indicator if(TUNE == 0) { FVRCON = 0b10000010 ; // FVR 2.048V ADCON0 = 0b01111101 ; ADCON1 = 0b00000000 ; NOP() ; // Wait for ADC ADCON0bits.GO_nDONE = 1; // Dummy read process while(ADCON0bits.GO_nDONE == 0); TIMEOUT = ADRESH ; NOP() ; ADCON0bits.GO_nDONE = 1; // Read real value process while(ADCON0bits.GO_nDONE == 0); TIMEOUT = ADRESH ; if(TIMEOUT > 0b11001001){ // if Battery lower than 2.6V // Blink T1's Yellow LED TUNEON = 1 ; __delay_ms(200) ; TUNEON = 0 ; } else{ // if Battery higher than 2.6V if(MODE == 1 ) { // and MODE is 1 // Blink T1's Green LED TUNEON = 1 ; __delay_ms(600) ; TUNEON = 0 ; } } FVRCON = 0b00000000 ; // FVR: Inable ADCON0bits.ADON = 0 ; // ADC off } // Go to sleep IOCAF5 = 0 ; // Clear Interrupt flag IOCAP5 = 1 ; // Interrupt KEY from 0 to 1 IOCAN5 = 0 ; // No interrupt KEY from 1 to 0 WPUA4 = 0 ; // Cancel pull up for START TRISA4 = 0 ; // Set START port as Output START = 0 ; // To stable PA4 port, output "0" while sleep mode IOCIE = 1 ; // Allow I/O port interrupt GIE = 1 ; // Allow all interrupt // Entering sleep mode SLEEP() ; //Sleep until KEY from "0" to "1" // Resume NOP() ; GIE = 0 ; // Prohibit all interrupt IOCIE = 0 ; // Prohibit INT interrupt TRISA4 = 1 ; // Set START port as Input WPUA4 = 1 ; // Set pull up for START if(TUNE == 1) {T1HIST = 0 ;} // T1 connection record turns "not yet" else {T1HIST = 1 ;} // T1 connection record turns "connected" } } } // Main loop closed //Interrupt routine // When 705 is connected T1con, KEY input will be changed from "0" to "1", // so it will be interrupted. This routine is used to return from Sleep mode, // so just clear the flag then return void __interrupt() isr(void){ // Process for Ext interrupt IOCAF5 = 0 ; // Clear Interrupt flag }