📄 ezlink_modem_main.c
字号:
/*
** ============================================================================
**
** FILE
** Id: EZLink_Modem_Main.c, v2.0
**
** DESCRIPTION
** EZLink Modem Demo main file
**
** COPYRIGHT
** Copyright 2006 Integration Associates Inc. All rights reserved.
**
** LIMITED USE LICENSE.
** By using this software, the user agrees to the terms of the
** following license. If the user does not agree to these terms,
** then this software should be returned within 30 days and a full
** refund of the purchase price or license fee will provided.
** Integration Associates hereby grants a license to the user on the
** following terms and conditions: The user may use, copy, modify,
** revise, translate, abridge, condense, expand, collect, compile,
** link, recast, distribute, transform or adapt this software solely
** in connection with the development of products incorporating
** integrated circuits sold by Integration Associates. Any other use
** for any other purpose is expressly prohibited with the prior written
** consent of Integration Associates.
**
** Any copy or modification made must satisfy the following conditions:
**
** 1. Both the copyright notice and this permission notice appear in all copies of the software,
** derivative works or modified versions, and any portions thereof, and that both notices
** appear in supporting documentation.
**
** 2. All copies of the software shall contain the following acknowledgement: "Portions of this
** software are used under license from Integration Associates Inc. and are copyrighted."
**
** 3 Neither the name of Integration Associates Inc. nor any of its subsidiaries may be used
** to endorse or promote products derived from this software without specific prior written
** permission.
**
** THIS SOFTWARE IS PROVIDED BY "AS IS" AND ALL WARRANTIES OF ANY KIND, INCLUDING THE IMPLIED
** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR USE, ARE EXPRESSLY DISCLAIMED. THE DEVELOPER
** SHALL NOT BE LIABLE FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
** THIS SOFTWARE MAY NOT BE USED IN PRODUCTS INTENDED FOR USE IN IMPLANTATION OR OTHER DIRECT
** LIFE SUPPORT APPLICATIONS WHERE MALFUNCTION MAY RESULT IN THE DIRECT PHYSICAL HARM OR INJURY
** TO PERSONS. ALL SUCH IS USE IS EXPRESSLY PROHIBITED.
**
** ============================================================================
*/
//processor definitions
#include <18F2520.h>
#fuses INTRC_IO,NOPROTECT,NOBROWNOUT,NOWDT,NOPUT,NOMCLR,NODEBUG,NOLVP,NOXINST,NOPBADEN
#use delay(clock=8000000) // 8 MHz internal oscillator
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7) // hw usart: 8bit/1 stop bit/no parity bit/no handshake/data rate: 9.6kbps
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
//==============================================================
// Select the frequency band and the target device
//==============================================================
//#define BAND_434
//#define BAND_868
#define BAND_915
#define IA4421
//include files
#include "EZLink_Modem_main.h"
#include "EZLink_Modem_cmd.c"
#include <string.h>
//global variable
int8 enable_eewrite;
//==============================================================
// Circular buffer handling
//==============================================================
char ReadBuffer (void)
{
char nextChar;
if (BufChar) // if the buffer is NOT empty:
{
BufChar--; // decrement the buffer
nextChar = CircBuf[o_ptr]; // get the next character
if (++o_ptr == CIRCBUF_LEN) o_ptr = 0; // step to the next character
return nextChar;
}
}
//==============================================================
// Interrupt routines
//==============================================================
#int_timer2
void MAIN_hardint(void)
{
++swit_10ms; // 10ms sw it request
}
#int_rda
void Serial_receive(void) // RS232 receive
{
CircBuf[i_ptr] = getc(); // get new character
if (++i_ptr == CIRCBUF_LEN) i_ptr = 0; // buffer is circular
if (i_ptr == o_ptr) // is the buffer full?
{
if (++o_ptr == CIRCBUF_LEN) o_ptr = 0;
}
else BufChar++; // if not, inc Nr of characters
rs232_timer = 1; // restart rs232 rx timer
}
//==============================================================
// RF settings
//==============================================================
int16 set_frq(int8 ch)
{
if( ch > FREQ_maxid )
{
ch = 0;
}
//save channel number into the EEPROM
if( enable_eewrite == 1 )
{
write_eeprom(EE_rf_ch, ch);
}
return (0xA000 + FREQ_start + ((int16)ch * FREQ_step));
}
int16 set_tx(int8 dev, int8 pwr) // set deviation (15 ... 240kHz) and relative output power: 0..7
{
if(dev < 15) dev = 15;
if( enable_eewrite == 1 )
write_eeprom(EE_rf_dev, dev);
if(pwr > 7) pwr = 7;
if( enable_eewrite == 1 )
write_eeprom(EE_rf_pwr, pwr);
if(dev == 255) dev = 90;
dev = dev / 15 - 1;
return(0x9800 + dev * 0x10 + 7 - pwr);
}
int16 set_dr(int16 dr) // set data rate (up to 32kbps)
{
int16 r;
if(dr == 65535) dr = 9600;
if(dr > 32000) dr = 32000;
if( enable_eewrite == 1 )
{
write_eeprom(EE_rf_dr, dr); // save the lower byte
write_eeprom(EE_rf_dr + 1, dr >> 8); // save the high byte to the next location
}
if(dr < 2700)
{
dr *= 8;
dr /= 100;
r = (3448 + dr / 2) / dr - 1;
return(0xC680 + r);
}
else
{
dr /= 100;
r = (3448 + dr / 2) / dr - 1;
return(0xC600 + r);
}
}
//==============================================================
// SPI interface functions
//==============================================================
// SPI interface read // read spidata from SPI
//--------------------------------------------------------------
int8 spi_read_sw(void)
{
int8 i, spidata;
output_low(SPI_SDO);
output_low(SPI_SCK);
for (i=0;i<8;i=i+1)
{
shift_left(&spidata, 1, input(SPI_SDI));
output_high(SPI_SCK);
output_low(SPI_SCK);
}
return(spidata);
}
// SPI interface write // send spidata via SPI
//--------------------------------------------------------------
void spi_write_sw(int16 spidata)
{
int8 i;
output_low(SPI_SDO);
output_low(SPI_SCK);
for (i=0;i<8;i=i+1)
{
output_bit(SPI_SDO, shift_left(&spidata, 1, 0));
output_high(SPI_SCK);
output_low(SPI_SCK);
}
output_low(SPI_SDO);
}
// Send SPI command to the rf chip // send 16-bit SPI command with NSEL control
//--------------------------------------------------------------
void send_cmd(int16 spicmd)
{
output_low(NSEL);
spi_write_sw(spicmd >> 8);
spi_write_sw(spicmd);
output_high(NSEL);
}
//==============================================================
// initialization
//==============================================================
void MAIN_init(void)
{
int16 dr;
//---- PIC init ------------------------------------------------
setup_oscillator(OSC_8MHZ | OSC_INTRC);
setup_comparator(NC_NC_NC_NC);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);
#ifdef IA4421
setup_ADC_PORTS(AN0); // AN0(PIN_A0): ARSSI
#endif
set_tris_a(0x03); //inputs: ARSSI; VDI
set_tris_b(0x0d); //inputs: FFIT, NIRQ, SDO
set_tris_c(0x8b); //inputs: T1OSO, T1OSI, UART_RX
setup_timer_2(T2_DIV_BY_4, 250, 10); // 10ms hardware interrupt (@4MHz)
enable_interrupts(INT_TIMER2);
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
enable_eewrite = 1;
output_high(NSEL); // nSEL inactive
output_high(NFFS); // nFFS inactive
output_low(SPI_SDO); // set SPI to default state
output_low(SPI_SCK); // set SPI to default state
output_high(TLED); // Blink LEDs on starup
delay_ms(50);
output_high(RLED);
delay_ms(50);
output_low(TLED);
delay_ms(50);
output_low(RLED);
opmode = 0; // default mode: data (1: command mode)
swit_10ms = 0;
i_ptr = 0;
o_ptr = 0;
BufChar = 0;
RxPacketLen = 0;
RxPacketPtr = 0;
rs232_timer = 0;
rs232_timeout = 200; // RS232 rx timeout = 1s
rs232_EOP = 13; // RS232 EOP char = <CR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -