ag6ju
Joined: 22 Jun 2021 Posts: 7 Location: Orange County Calif. USA
|
Sending Morse Code, number so on. |
Posted: Wed Jun 23, 2021 1:12 pm |
|
|
I often run my PIC at 32kHz, which is too slow clock for RS232 monitor
so I use Morse Code, send out variable and so on, to see what my program is doing.
Code: |
// CCS c compiler ver v5_104___2021_05_13
#define UI1 unsigned int1
#define UI8 unsigned int8
#define UI16 unsigned int16
#define UI32 unsigned int32
//#define UI48 unsigned int48
//#define UI64 unsigned int64
#define CW_SHORT_01 60 // pulse width in mS for morse code short pulse , normal value 60
#define CW_LONG_01 300 // pulse width in mS for morse code long pulse , normal value 300
#define CW_DELAY_01 60 // delay in mS between each dots and dits , normal value 60
#define CW_SPACE_AFTER_EACH_CHAR 3 // number of CW_DELAY_01 space after each charactor, normal value 03
// to send Beep ( DOT or DASH ) on CW, take 8 bits ineger --> number of pulses and 1 bit integer --> long -- 1, short -- 0
void beep_01( UI8 ui8_number_of_beeps_01, UI1 ui1_long_or_short_01 )
{
UI8 ui8_loop_01;
UI8 ui8_for_lp2;
for ( ui8_loop_01 = 0; ui8_loop_01 < ui8_number_of_beeps_01; ++ui8_loop_01 )
{
if ( ui1_long_or_short_01 == 1 )
{
for ( ui8_for_lp2 = 0; ui8_for_lp2 < 200; ++ui8_for_lp2 )
{
#ifdef TEST_BEEP_BUZ
output_high ( TEST_BEEP );
delay_us ( ( UI16 ) 700 );
#endif
#ifdef TEST_BEEP_SPKR
output_low ( AUD_ON );
delay_us ( ( UI16 ) 800 );
#endif
#ifdef TEST_BEEP_BUZ
output_low ( TEST_BEEP );
delay_us ( ( UI16 ) 700 );
#endif
#ifdef TEST_BEEP_SPKR
output_high ( AUD_ON );
delay_us ( ( UI16 ) 800 );
#endif
}
delay_ms ( 15 );
}
else
{
for ( ui8_for_lp2 = 0; ui8_for_lp2 < 60; ++ui8_for_lp2 )
{
#ifdef TEST_BEEP_BUZ
output_high ( TEST_BEEP );
delay_us ( ( UI16 ) 700 );
#endif
#ifdef TEST_BEEP_SPKR
output_low ( AUD_ON );
delay_us ( ( UI16 ) 800 );
#endif
#ifdef TEST_BEEP_BUZ
output_low ( TEST_BEEP );
delay_us ( ( UI16 ) 700 );
#endif
#ifdef TEST_BEEP_SPKR
output_high ( AUD_ON );
delay_us ( ( UI16 ) 800 );
#endif
}
delay_ms ( 30 );
}
output_low( TEST_BEEP );
} // for ( ui8_loop_01 = 0; ui8_loop_01 < ui8_number_of_beeps_01; ++ui8_loop_01 )
} // void short_beep_01( UI8 ui8_number_of_beeps_01 )
// to send space between charactor
void space_01( UI8 ui8_number_of_spaces_01 )
{
UI8 ui8_loop_01;
for ( ui8_loop_01 = 0; ui8_loop_01 < ui8_number_of_spaces_01; ++ui8_loop_01 )
{
delay_ms( CW_DELAY_01 ); //
} // for ( ui8_loop_01 = 0; ui8_loop_01 < ui8_number_of_spaces_01; ++ui8_loop_01 )
} // void space_01( UI8 ui8_number_of_spaces_01 )
// to call this function, put ASCII number in Capital letter , it will send out morse code.
void send_ID( UI8 ui8_ID )
{
switch( ui8_ID )
{
case 'A':
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
break;
case 'B':
beep_01 ( 1, 1 );
beep_01 ( 3, 0 );
break;
case 'C':
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
break;
case 'D':
beep_01 ( 1, 1 );
beep_01 ( 2, 0 );
break;
case 'E':
beep_01 ( 1, 0 );
break;
case 'F':
beep_01 ( 2, 0 );
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
break;
case 'G':
beep_01 ( 2, 1 );
beep_01 ( 1, 0 );
break;
case 'H':
beep_01 ( 4, 0 );
break;
case 'I':
beep_01 ( 2, 0 );
break;
case 'J':
beep_01 ( 1, 0 );
beep_01 ( 3, 1 );
break;
case 'K':
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
break;
case 'L':
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
beep_01 ( 2, 0 );
break;
case 'M':
beep_01 ( 2, 1 );
break;
case 'N':
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
break;
case 'O':
beep_01 ( 3, 1 );
break;
case 'P':
beep_01 ( 1, 0 );
beep_01 ( 2, 1 );
beep_01 ( 1, 0 );
break;
case 'Q':
beep_01 ( 2, 1 );
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
break;
case 'R':
beep_01 ( 1, 0 );
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
break;
case 'S':
beep_01 ( 3, 0 );
break;
case 'T':
beep_01 ( 1, 1 );
break;
case 'U':
beep_01 ( 2, 0 );
beep_01 ( 1, 1 );
break;
case 'V':
beep_01 ( 3, 0 );
beep_01 ( 1, 1 );
break;
case 'W':
beep_01 ( 1, 0 );
beep_01 ( 2, 1 );
break;
case 'X':
beep_01 ( 1, 1 );
beep_01 ( 2, 0 );
beep_01 ( 1, 1 );
break;
case 'Y':
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
beep_01 ( 2, 1 );
break;
case 'Z':
beep_01 ( 2, 1 );
beep_01 ( 2, 0 );
break;
case '1':
beep_01 ( 1, 0 );
beep_01 ( 4, 1 );
break;
case '2':
beep_01 ( 2, 0 );
beep_01 ( 3, 1 );
break;
case '3':
beep_01 ( 3, 0 );
beep_01 ( 2, 1 );
break;
case '4':
beep_01 ( 4, 0 );
beep_01 ( 1, 1 );
break;
case '5':
beep_01 ( 5, 0 );
break;
case '6':
beep_01 ( 1, 1 );
beep_01 ( 4, 0 );
break;
case '7':
beep_01 ( 2, 1 );
beep_01 ( 3, 0 );
break;
case '8':
beep_01 ( 3, 1 );
beep_01 ( 2, 0 );
break;
case '9':
beep_01 ( 4, 1 );
beep_01 ( 1, 0 );
break;
case '0':
beep_01 ( 5, 1 );
break;
case '-':
beep_01 ( 1, 1 );
beep_01 ( 4, 0 );
beep_01 ( 1, 1 );
break;
case '?':
beep_01 ( 2, 0 );
beep_01 ( 2, 1 );
beep_01 ( 2, 0 );
break;
case '/':
beep_01 ( 1, 1 );
beep_01 ( 2, 0 );
beep_01 ( 1, 1 );
beep_01 ( 1, 0 );
break;
case '=':
beep_01 ( 1, 1 );
beep_01 ( 3, 0 );
beep_01 ( 1, 1 );
break;
// speace
case 32:
space_01 ( CW_SPACE_AFTER_EACH_CHAR );
space_01 ( CW_SPACE_AFTER_EACH_CHAR );
break;
} // switch( ui8_ID )
space_01 ( CW_SPACE_AFTER_EACH_CHAR );
restart_wdt(); // restart Watch Dog Timer
} // void send_ID( UI8 ui8_ID )
// convert HEX number to CW code in letter 0-9, A-F
void send_cw_number_conv_hex_4bit( UI8 ui8_input )
{
UI8 ui8_temp_01, ui8_temp_02;
ui8_temp_01 = ( ui8_input & 0x0F );
if ( ui8_temp_01 <= 9 )
{
// convert number 0 - 9 to ID code
ui8_temp_02 = ui8_temp_01 + 48;
}
else
{
// convert number A - F to ID code
ui8_temp_02 = ui8_temp_01 + 55;
}
send_ID( ui8_temp_02 );
delay_ms ( 200 );
} // void send_cw_number_conv_4( UI8 ui8_input )
// convert HEX number to CW code in letter 0-9, A-F
void send_cw_number_conv_hex_8bit ( UI8 ui8_input )
{
UI8 ui8_temp_01;
ui8_temp_01 = ( UI8 ) ( ( ( UI8 ) 0xF0 & ui8_input ) / ( UI8 ) 0x10 );
send_cw_number_conv_hex_4bit ( ui8_temp_01 );
ui8_temp_01 = ( UI8 ) ( ( UI8 ) 0x0F & ui8_input );
send_cw_number_conv_hex_4bit ( ui8_temp_01 );
} // void send_cw_number_conv_8( UI8 ui8_input )
// convert 16bit HEX number to CW code in letter 0-9, A-F
void send_cw_number_conv_hex_16bit ( UI16 ui16_input )
{
UI8 ui8_temp_01;
// sending 1st byte
ui8_temp_01 = ( UI8 ) ( ( ( UI16 ) 0xFF00 & ui16_input ) / ( UI16 ) 0x0100 );
send_cw_number_conv_hex_8bit ( ui8_temp_01 );
// sending 2nd byte
ui8_temp_01 = ( UI8 ) ( ( UI16 ) 0x00FF & ui16_input );
send_cw_number_conv_hex_8bit ( ui8_temp_01 );
} // void send_cw_number_conv_16( UI16 ui16_input )
// convert 32 bit HEX number to CW code in letter 0-9, A-F
void send_cw_number_conv_hex_32bit ( UI32 ui32_input )
{
UI16 ui16_temp_01;
ui16_temp_01 = ( UI16 ) ( ( ( UI32 ) 0xFFFF0000 & ui32_input ) / ( UI32 ) 0x00010000 );
send_cw_number_conv_hex_16bit ( ui16_temp_01 );
ui16_temp_01 = ( UI16 ) ( ( ( UI32 ) 0x0000FFFF & ui32_input ) / ( UI32 ) 0x00000001 );
send_cw_number_conv_hex_16bit ( ui16_temp_01 );
} // void send_cw_number_conv_32( UI32 ui32_input )
void send_cw_number_decimal_16bit ( UI16 ui16_input )
{
UI1 ui1_detected_non_zero_digit;
UI8 ui8_digit_number;
UI8 ui8_digit [ 6 ];
UI16 ui16_10_power_dec [] = { 1, 10, 100, 1000, 10000 };
ui1_detected_non_zero_digit = 0;
//send_cw_number_conv_hex_4bit( 0x3 );
for ( ui8_digit_number = 5 ; ui8_digit_number >= 1 ; --ui8_digit_number )
{
ui8_digit [ ui8_digit_number ] = ( UI8 ) ( ui16_input / ui16_10_power_dec [ ui8_digit_number - 1 ] );
ui16_input -= ( UI16 ) ui8_digit [ ui8_digit_number ] * ui16_10_power_dec [ ui8_digit_number - 1 ];
//send_cw_number_conv_hex_4bit( ui8_digit [ ui8_digit_number ] );
} // for ( ui8_digit_number = 5 ; ui8_digit_number >= 1 ; --ui8_digit_number )
for ( ui8_digit_number = 5 ; ui8_digit_number >= 1 ; --ui8_digit_number )
{
if ( ( ui8_digit [ ui8_digit_number ] != 0 ) || ( ui1_detected_non_zero_digit == 1 ) )
{
send_cw_number_conv_hex_4bit( ui8_digit [ ui8_digit_number ] );
ui1_detected_non_zero_digit = 1; // if digits left of current digit is not 0
} // if ( ( ui8_digit [ ui8_digit_number ] != 0 ) || ( ui1_detected_non_zero_digit == 1 ) )
else
if ( ( ui8_digit_number == 1 ) && ( ui8_digit [ ui8_digit_number ] == 0 ) )
{
// if all digit is 0
send_cw_number_conv_hex_4bit( 0 );
} // else, if ( ( ui8_digit_number == 1 ) && ( ui8_digit [ ui8_digit_number ] == 0 ) )
} // for ( ui8_digit_number = 5 ; ui8_digit_number >= 1 ; --ui8_digit_number )
} // void send_cw_number_decimal_16bit ( UI16 ui16_input )
void send_cw_number_decimal_32bit ( UI32 ui32_input )
{
UI1 ui1_detected_non_zero_digit;
UI8 ui8_digit_number;
UI8 ui8_digit [ 11 ];
UI32 ui32_10_power_dec [] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
ui1_detected_non_zero_digit = 0;
//send_cw_number_conv_hex_4bit( 0x3 );
for ( ui8_digit_number = 10 ; ui8_digit_number >= 1 ; --ui8_digit_number )
{
ui8_digit [ ui8_digit_number ] = ( UI8 ) ( ui32_input / ui32_10_power_dec [ ui8_digit_number - 1 ] );
ui32_input -= ( UI16 ) ui8_digit [ ui8_digit_number ] * ui32_10_power_dec [ ui8_digit_number - 1 ];
//send_cw_number_conv_hex_4bit( ui8_digit [ ui8_digit_number ] );
} // for ( ui8_digit_number = 10 ; ui8_digit_number >= 1 ; --ui8_digit_number )
for ( ui8_digit_number = 10 ; ui8_digit_number >= 1 ; --ui8_digit_number )
{
if ( ( ui8_digit [ ui8_digit_number ] != 0 ) || ( ui1_detected_non_zero_digit == 1 ) )
{
send_cw_number_conv_hex_4bit( ui8_digit [ ui8_digit_number ] );
ui1_detected_non_zero_digit = 1; // if digits left of current digit is not 0
} // if ( ( ui8_digit [ ui8_digit_number ] != 0 ) || ( ui1_detected_non_zero_digit == 1 ) )
else
if ( ( ui8_digit_number == 1 ) && ( ui8_digit [ ui8_digit_number ] == 0 ) )
{
// if all digit is 0
send_cw_number_conv_hex_4bit( 0 );
} // else, if ( ( ui8_digit_number == 1 ) && ( ui8_digit [ ui8_digit_number ] == 0 ) )
} // for ( ui8_digit_number = 10 ; ui8_digit_number >= 1 ; --ui8_digit_number )
} // void send_cw_number_decimal_32bit ( UI16 ui16_input )
// find difference of each number and return difference
UI16 ui16_diff ( UI16 ui16_inp1, UI16 ui16_inp2 )
{
UI16 ui16_diff;
if ( ui16_inp1 >= ui16_inp2 )
{
ui16_diff = ui16_inp1 - ui16_inp2;
}
else
{
ui16_diff = ui16_inp2 - ui16_inp1;
}
return ( ui16_diff );
} //UI16 ui16_diff ( UI16 ui16_inp1, UI16 ui16_inp2 )
|
_________________ Orange County Calif. USA
http://ag6ju-ipv6.dynv6.net:5950/ |
|