禁止转载
需要注意的是,现在BP机工作频段已经被国家收回,所以自己使用手台、车台等大功率无线发射器在这个频段发射无线电波是违法的,所以需要进行发射信号的衰减,无线发射功率控制在0.1W之内是没有问题的。
单片机使用STC公司的IAP15W4K58S4,时钟频率为27MHz,使用串口1来发送数据,串口波特率为115200。支持1200和512比特速率的POCSAG编码,支持正负相位,可以发送数字信息和汉字信息(不支持混输)
对讲机我用的是宝峰UV-5R。单片机的P36接对讲机的压控振荡器输入端,P35接对讲机的PTT控制端,还要和对讲机共地(至于这三个接线端具体接在哪一个位置,网上一搜就有了)。对讲机的发射频率需要调到BP机的工作频率一样。
BP机我用摩托罗拉精英王和大顾问实验成功,如果不响可以试试切换一下相位。如果显示乱码就换一个响声/功能位。
如果就是不响,要知道哪有这么容易响?地址码、BP机工作频率、对讲机频率调到和BP机工作频率一致、对讲机不能有频偏。如果就是不响,要考虑BP机是否被改过频率,很多BP机在使用的时候都是改过频率的,或者就是对讲机有频偏,可以使用SDR(软件无线电)测试对讲机的频率。
关于如何准确获得BP机的工作频率和地址码,跳转这里:
如何获得BP机的地址码和工作频率?
开源!单片机源码和上位机源码都开源!讨厌那种一个单片机加个Si4463模块就卖你几百块的商家,虽然我知道写程序画板子也不容易,但是价格是成本的十倍着实太坑。
单片机源码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
| #include <stc15.h> #include <intrins.h>
#define TX P36 #define PTT P35 #define HIGH 1 #define LOW 0 #define NILL_DATA 0x7A89C197 #define SYNC_DATA 0x7CD215D8 #define DATA_START 12 #define TEXT_OR_NUM 11 #define SPEED 10 #define UARTBUFF_SIZE 400 #define TXBUFF_SIZE 200 #define LOWSPEED_TIMER_L 0xE7 #define LOWSPEED_TIMER_H 0x31 #define HIGHTSPEED_TIMER_L 0x1A #define HIGHTSPEED_TIMER_H 0xA8
unsigned long TxBuff[TXBUFF_SIZE] = {0}; unsigned char Tx_Num; unsigned char beep; unsigned char UartBuff[UARTBUFF_SIZE] = "#P12345674HT0001$"; unsigned int UartCount = 13; unsigned char UartTmp; bit TM0_FLAG=0; bit NewData = 0;
void UartInit(void); void Delay200ms(); unsigned long calc_bch_and_parity(unsigned long cw_e); unsigned long calc_addr(unsigned long add,unsigned char fun); void Timer0Init(); void WaitTF0(void); void Send_Num(unsigned long s); void GetAddrNumber(); void calc_NumberData(); void SendTxBuff(); void Empty_Buff(); void calc_TextData();
void UartInit(void) { SCON = 0x50; AUXR |= 0x40; AUXR &= 0xFE; TMOD &= 0x0F; TL1 = 0xC5; TH1 = 0xFF; ET1 = 0; TR1 = 1; ES = 1; EA = 1; }
void Timer0Init() { AUXR |= 0x80; TMOD &= 0xF0; if(UartBuff[SPEED] == 'L') { TL0 = LOWSPEED_TIMER_L; TH0 = LOWSPEED_TIMER_H; } if(UartBuff[SPEED] == 'H') { TL0 = HIGHTSPEED_TIMER_L; TH0 = HIGHTSPEED_TIMER_H; } ET0=1; TR0=0; EA=1; }
void UartSendString(unsigned char* ch, unsigned char n) { unsigned char i; for(i = 0; i < n; i++) { SBUF = *ch++; while(TI == 0); TI = 0; } }
void Delay200ms() { unsigned char i, j, k;
_nop_(); _nop_(); i = 21; j = 133; k = 210; do { do { while (--k); } while (--j); } while (--i); }
unsigned long calc_bch_and_parity(unsigned long cw_e) { unsigned char i; unsigned char parity = 0; unsigned long local_cw; local_cw=cw_e; for(i=1;i<=21; i++,cw_e<<=1) if (cw_e & 0x80000000) cw_e ^= 0xED200000; cw_e=cw_e&0xFFC00000; local_cw |= (cw_e >> 21); cw_e=local_cw; for(i=0; i<31; i++, cw_e<<=1) if(cw_e&0x80000000) parity++; if(parity%2) local_cw+=1; return local_cw; }
unsigned long calc_addr(unsigned long add,unsigned char fun) { unsigned long adr; unsigned long tem; Tx_Num=(unsigned char)(add&0x00000007); adr=0x00; adr=add&0xFFFFFFF8; adr=adr<<10; tem=0x00; tem=fun; tem=tem<<11; adr=adr|tem; return adr; }
void WaitTF0(void) { while(!TM0_FLAG); TM0_FLAG=0; }
void Send_Num(unsigned long s) { unsigned char n; for (n=0;n<32;n++) { if(s&0x80000000) { if(UartBuff[1] == 'N') TX = LOW; else TX= HIGH; } else { if(UartBuff[1] == 'N') TX = HIGH; else TX=LOW; } WaitTF0(); s<<=1; } }
void Empty_Buff() { unsigned int i; for(i = TEXT_OR_NUM; i < UARTBUFF_SIZE; i++) { UartBuff[i] = 0x00000000; } for(i = TEXT_OR_NUM; i < TXBUFF_SIZE; i++) { TxBuff[i] = 0x00000000; } }
void GetAddrNumber() { unsigned char i; unsigned long tem; unsigned long addr_tmp; addr_tmp = (UartBuff[2] - '0')*1000000; addr_tmp += (UartBuff[3] - '0')*100000; addr_tmp += (UartBuff[4] - '0')*10000; addr_tmp += (UartBuff[5] - '0')*1000; addr_tmp += (UartBuff[6] - '0')*100; addr_tmp += (UartBuff[7] - '0')*10; addr_tmp += (UartBuff[8] - '0'); beep = UartBuff[9] - '0';
tem=calc_addr(addr_tmp,beep); for(i = 0; i < 8; i++) { if(i == Tx_Num) TxBuff[i*2] = calc_bch_and_parity(tem); else { TxBuff[i*2] = NILL_DATA; TxBuff[i*2+1] = NILL_DATA; } } }
void calc_NumberData() { unsigned long Num_Negate[UARTBUFF_SIZE] = {0}; unsigned int i, j, k, n; unsigned char byte_tmp[10], byte_tmp_negate[10]; float TxCount = 0.0; for(i = DATA_START; i < UARTBUFF_SIZE; i++) { if(UartBuff[i] == 'A') UartBuff[i] = 0x0A; if(UartBuff[i] == 'B') UartBuff[i] = 0x0B; if(UartBuff[i] == 'C') UartBuff[i] = 0x0C; if(UartBuff[i] == 'D') UartBuff[i] = 0x0D; if(UartBuff[i] == 'E') UartBuff[i] = 0x0E; if(UartBuff[i] == 'F') UartBuff[i] = 0x0F; } n = DATA_START; for(i = DATA_START; i < UARTBUFF_SIZE; i++) { if(i % 2 == 0) UartBuff[n] = UartBuff[i] << 4; else { UartBuff[n] |= UartBuff[i] & 0x0f; n++; } } k = Tx_Num * 2 + 1; for(i = DATA_START; i < UARTBUFF_SIZE; i += 5) { byte_tmp[0] = (UartBuff[i] & 0xf0); byte_tmp[1] = (UartBuff[i] & 0x0f) << 4; byte_tmp[2] = (UartBuff[i + 1] & 0xf0); byte_tmp[3] = (UartBuff[i + 1] & 0x0f) << 4; byte_tmp[4] = (UartBuff[i + 2] & 0xf0); byte_tmp[5] = (UartBuff[i + 2] & 0x0f) << 4; byte_tmp[6] = (UartBuff[i + 3] & 0xf0); byte_tmp[7] = (UartBuff[i + 3] & 0x0f) << 4; byte_tmp[8] = (UartBuff[i + 4] & 0xf0); byte_tmp[9] = (UartBuff[i + 4] & 0x0f) << 4; for(j = 0; j < 10; j++) { for(n = 0; n < 8; n++) { byte_tmp_negate[j] <<= 1; byte_tmp_negate[j] |= (byte_tmp[j] >> n) & 0x01; } } Num_Negate[k] = 0x80000000; Num_Negate[k] |= (unsigned long)byte_tmp_negate[0] << 27; Num_Negate[k] |= (unsigned long)byte_tmp_negate[1] << 23; Num_Negate[k] |= (unsigned long)byte_tmp_negate[2] << 19; Num_Negate[k] |= (unsigned long)byte_tmp_negate[3] << 15; Num_Negate[k] |= (unsigned long)byte_tmp_negate[4] << 11; Num_Negate[k + 1] = 0x80000000; Num_Negate[k + 1] |= (unsigned long)byte_tmp_negate[5] << 27; Num_Negate[k + 1] |= (unsigned long)byte_tmp_negate[6] << 23; Num_Negate[k + 1] |= (unsigned long)byte_tmp_negate[7] << 19; Num_Negate[k + 1] |= (unsigned long)byte_tmp_negate[8] << 15; Num_Negate[k + 1] |= (unsigned long)byte_tmp_negate[9] << 11; k = k + 2; } n = Tx_Num * 2 + 1; TxCount = 0.0; for(i = Tx_Num * 2 + 1; i < UARTBUFF_SIZE; i++) { if(TxCount >= (UartCount - DATA_START - 1)) break; if(i % 16 == 0) TxBuff[n++] = SYNC_DATA; TxBuff[n++] = calc_bch_and_parity( Num_Negate[i]); TxCount += 5; if(n >= TXBUFF_SIZE || i >= UARTBUFF_SIZE) break; }
}
void calc_TextData() { unsigned long Text_Negate[UARTBUFF_SIZE] = {0}; unsigned int n, i, k, byte_tmp; float TxCount = 0.0; for(k = DATA_START; k < UARTBUFF_SIZE; k++) { byte_tmp = UartBuff[k]; UartBuff[k] = 0; for(n = 0; n < 8; n++) { UartBuff[k] <<= 1; UartBuff[k] |= (byte_tmp >> n) & 0x01; } UartBuff[k] &= 0xFE; }
k = Tx_Num * 2 + 1; for(n = DATA_START; n < UARTBUFF_SIZE; n += 20) { Text_Negate[k] = 0x80000000; Text_Negate[k] |= (unsigned long)UartBuff[n] << 23; Text_Negate[k] |= (unsigned long)UartBuff[n + 1] << 16; Text_Negate[k] |= (unsigned long)UartBuff[n + 2] << 9; Text_Negate[k] &= 0xfffff800; Text_Negate[k + 1] = 0x80000000; Text_Negate[k + 1] |= (unsigned long)UartBuff[n + 2] << 29; Text_Negate[k + 1] &= 0xC0000000; Text_Negate[k + 1] |= (unsigned long)UartBuff[n + 3] << 22; Text_Negate[k + 1] |= (unsigned long)UartBuff[n + 4] << 15; Text_Negate[k + 1] |= (unsigned long)UartBuff[n + 5] << 8; Text_Negate[k + 1] &= 0xfffff800; Text_Negate[k + 2] = 0x80000000; Text_Negate[k + 2] |= (unsigned long)UartBuff[n + 5] << 28; Text_Negate[k + 2] &= 0xE0000000; Text_Negate[k + 2] |= (unsigned long)UartBuff[n + 6] << 21; Text_Negate[k + 2] |= (unsigned long)UartBuff[n + 7] << 14; Text_Negate[k + 2] |= (unsigned long)UartBuff[n + 8] << 7; Text_Negate[k + 2] &= 0xfffff800; Text_Negate[k + 3] = 0x80000000; Text_Negate[k + 3] |= (unsigned long)UartBuff[n + 8] << 27; Text_Negate[k + 3] &= 0xF0000000; Text_Negate[k + 3] |= (unsigned long)UartBuff[n + 9] << 20; Text_Negate[k + 3] |= (unsigned long)UartBuff[n + 10] << 13; Text_Negate[k + 3] |= (unsigned long)UartBuff[n + 11] << 6; Text_Negate[k + 3] &= 0xfffff800; Text_Negate[k + 4] = 0x80000000; Text_Negate[k + 4] |= (unsigned long)UartBuff[n + 11] << 26; Text_Negate[k + 4] &= 0xF8000000; Text_Negate[k + 4] |= (unsigned long)UartBuff[n + 12] << 19; Text_Negate[k + 4] |= (unsigned long)UartBuff[n + 13] << 12; Text_Negate[k + 4] |= (unsigned long)UartBuff[n + 14] << 5; Text_Negate[k + 4] &= 0xfffff800; Text_Negate[k + 5] = 0x80000000; Text_Negate[k + 5] |= (unsigned long)UartBuff[n + 14] << 25; Text_Negate[k + 5] &= 0xFC000000; Text_Negate[k + 5] |= (unsigned long)UartBuff[n + 15] << 18; Text_Negate[k + 5] |= (unsigned long)UartBuff[n + 16] << 11; Text_Negate[k + 5] |= (unsigned long)UartBuff[n + 17] << 4; Text_Negate[k + 5] &= 0xfffff800; Text_Negate[k + 6] = 0x80000000; Text_Negate[k + 6] |= (unsigned long)UartBuff[n + 17] << 24; Text_Negate[k + 6] &= 0xFE000000; Text_Negate[k + 6] |= (unsigned long)UartBuff[n + 18] << 17; Text_Negate[k + 6] |= (unsigned long)UartBuff[n + 19] << 10; Text_Negate[k + 6] &= 0xfffff800; k += 7; } n = Tx_Num * 2 + 1; TxCount = 0.0; for(i = Tx_Num * 2 + 1; i < UARTBUFF_SIZE; i++) { if(TxCount >= (UartCount - DATA_START - 1)) break; if(i % 16 == 0) TxBuff[n++] = SYNC_DATA; TxBuff[n++] = calc_bch_and_parity( Text_Negate[i]); TxCount += 2.8571428571; if(n >= TXBUFF_SIZE || i >= UARTBUFF_SIZE) break; } }
void SendTxBuff() { unsigned int i; PTT = 0; Delay200ms(); if(UartBuff[SPEED] == 'L') { TL0 = LOWSPEED_TIMER_L; TH0 = LOWSPEED_TIMER_H; } if(UartBuff[SPEED] == 'H') { TL0 = HIGHTSPEED_TIMER_L; TH0 = HIGHTSPEED_TIMER_H; } TR0 = 1; for(i = 0; i < 18; i++) { Send_Num(0xAAAAAAAA); } Send_Num(SYNC_DATA);
for(i = 0; i < TXBUFF_SIZE; i++) { if(TxBuff[i] == 0x00000000 || TxBuff[i] == 0xffffffff) break; Send_Num(TxBuff[i]); } TX=LOW; TR0 = 0; Delay200ms(); Delay200ms(); Delay200ms(); PTT = 1;
UartSendString("OK\r\n",4); UartSendString("UartCount=",10); SBUF = UartCount / 100 + '0'; while(TI == 0); TI = 0; SBUF = (UartCount / 10) % 10 + '0'; while(TI == 0); TI = 0; SBUF = UartCount % 10 + '0'; while(TI == 0); TI = 0; UartSendString("\r\n",2); }
void main() { UartInit(); Timer0Init();
TX = 0; PTT= 1;
while(1) { if(NewData == 1 && (UartBuff[TEXT_OR_NUM] == 'N' || UartBuff[TEXT_OR_NUM] == 'T')) { GetAddrNumber(); if(UartBuff[TEXT_OR_NUM] == 'N') calc_NumberData(); if(UartBuff[TEXT_OR_NUM] == 'T') calc_TextData(); SendTxBuff(); Empty_Buff(); NewData = 0; } } }
void UART_RxData() interrupt 4 { if(RI == 1) { RI = 0; UartTmp = SBUF; if(UartTmp == '#') UartCount = 0; if(UartTmp == '$') { UartTmp = 0x00; NewData = 1; } UartBuff[UartCount] = UartTmp; UartCount++; if(UartCount >= UARTBUFF_SIZE) UartCount = 0; } }
void IntTimer0() interrupt 1 { if(UartBuff[SPEED] == 'L') { TL0 = LOWSPEED_TIMER_L; TH0 = LOWSPEED_TIMER_H; } if(UartBuff[SPEED] == 'H') { TL0 = HIGHTSPEED_TIMER_L; TH0 = HIGHTSPEED_TIMER_H; } TM0_FLAG=1; }
|
上位机源码(易语言):
上位机源码
上位机可执行二进制文件:
上位机可执行二进制文件
上位机截图:
如果你用的单片机型号和我的一样,并且对讲机没有频差的话,应该是直接可以使用的。
搞定了POCSAG编码的问题,剩下的加入GSM模块,蓝牙模块,接入公网什么的就容易多了。
PS?我是不是应该在github建一个仓库?我没用过啊~
还有,希望某些人不要太过自信