📄 iai_ezmac_plus_pic.c
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* *
* FILE NAME: IAI_EZMac_Plus_PIC.c *
* *
* DESIGNER: Kovacs, Krisztian; Lukacs, Miklos *
* PROGRAMMER: Kovacs Krisztian; *
* *
* DESCRIPTION: EZMac PIC processor dependend functions *
* *
* REVISION: 01_7 Nov 22, 2005 created by krk *
* 02_0 March 21, 2006 Finalize the HiTech porting *
* 02_12 Sept 28, 2006 Some changes to be able to compile *
* the code for PIC16 family *
* EZMac Plus: *
* 01_0 Sept 28, 2006 Create EZMac Plus from EZMac *
* *
* *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/***************************************************************************\
Find out the compiler
\***************************************************************************/
#ifdef HI_TECH_C
#define HITECH_COMPILER
#if defined (_PIC18)
#define HITECH_PIC18
#else
#define HITECH_PIC16
#endif
#else
#if defined(__PCB__) || defined(__PCM__) || defined(__PCH__) || defined(__PCW__) || defined(__PCWH__)
#define CCS_C_COMPILER
#endif
#endif
#ifdef HITECH_PIC18
#define BANK_1
#define BANK_2
#define _NEAR__ near
#endif
#ifdef HITECH_PIC16
#define BANK_1 bank1
#define BANK_2 bank2
#define _NEAR__
#endif
/*If the memory addresses are allocated manualy this definition has to be uncommented*/
#ifdef CCS_C_COMPILER
//#define FIX_MEM_ADDRESS
#endif
/*oscillator frequency*/
#ifdef DATA_RATE_9600
#define CLOCK_FREQ 4000000
#elif defined DATA_RATE_19200
#define CLOCK_FREQ 8000000
#elif defined DATA_RATE_38400
#define CLOCK_FREQ 16000000
#elif defined DATA_RATE_57600
#define CLOCK_FREQ 24000000
#elif defined DATA_RATE_115200
#define CLOCK_FREQ 48000000
#endif
/***************************************************************************\
Program start up
\***************************************************************************/
/*set processor type*/
#ifdef CCS_C_COMPILER
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This is a sample config setting, it should be
// changed according to the application Hardware!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <18F2620.h>
#pragma device *=16
#pragma device ADC=8
#endif
#include "IAI_EZMac_Plus_PIC.h"
/**************************************************************************\
Set fuses
\**************************************************************************/
#ifdef CCS_C_COMPILER
//////// Fuses: LP,XT,HS,RC,EC,EC_IO,H4,RC_IO,PROTECT,NOPROTECT,IESO,NOIESO
//////// Fuses: NOBROWNOUT,BROWNOUT,WDT1,WDT2,WDT4,WDT8,WDT16,WDT32,WDT64
//////// Fuses: WDT128,WDT,NOWDT,BORV21,BORV28,BORV43,BORV46,PUT,NOPUT,CPD
//////// Fuses: NOCPD,NOSTVREN,STVREN,NODEBUG,DEBUG,NOLVP,LVP,WRT,NOWRT,WRTD
//////// Fuses: NOWRTD,EBTR,NOEBTR,CPB,NOCPB,EBTRB,NOEBTRB,CCP2B3,CCP2C1
//////// Fuses: WRTC,NOWRTC,WRTB,NOWRTB,INTRC_IO,INTRC,FCMEN,NOFCMEN
//////// Fuses: BROWNOUT_SW,BROWNOUT_NOSL,WDT256,WDT512,WDT1024,WDT2048
//////// Fuses: WDT4096,WDT8192,WDT16384,WDT32768,XINST,NOXINST,PBADEN
//////// Fuses: NOPBADEN,LPT1OSC,NOLPT1OSC,MCLR,NOMCLR
#pragma fuses XT,nowdt,put,noprotect,nocpd,nobrownout,noieso,NODEBUG,NOLVP
#endif
#ifdef HITECH_COMPILER
//__CONFIG(OSCSDIS&HS&WDTDIS&PWRTDIS&DUNPROT&UNPROTECT&BORDIS&MCLREN);
#endif
/**************************************************************************\
HiTech compiler dependent functions
\**************************************************************************/
#ifdef HITECH_PIC18
/*handle Timer1*/
void set_timer1(int16 value)
{
TMR1H=(value>>8)&0x0FF; TMR1L=value&0x0FF;
}
#pragma inline(set_timer1)
/*handle the ADC*/
int16 read_adc(int8 mode)
{
switch(mode)
{
case ADC_START_ONLY:
{
ADON=1;
GODONE=1;
return 0;
}
case ADC_START_AND_READ:
{
ADON=1;
GODONE=1;
while(GODONE) continue;
return (((int16)ADRESH)<<8)|((int16)ADRESL);
}
case ADC_READ_ONLY:
{
while(GODONE) continue;
return (((int16)ADRESH)<<8)|((int16)ADRESL);
}
}
return 0;
}
#ifdef EZMAC_BOARD_WITH_ADC
#pragma interrupt_level 1
#endif
void setup_adc(int8 mode)
{
ADCON2=0xa0;
ADCON2|=mode;
}
#pragma inline(setup_adc)
void setup_adc_ports(int8 ports)
{
ADCON1=0x00|ports;
}
#pragma inline(setup_adc_ports)
void set_adc_channel(int8 ch)
{
ADCON0&=0xC3;
ADCON0|=(ch<<2);
}
#pragma inline(setup_adc_channel)
#endif
#ifdef HITECH_PIC16
/*Handle Timer1*/
void set_timer1(int16 value)
{
TMR1H=(value>>8)&0x0FF; TMR1L=value&0x0FF;
}
#pragma inline(set_timer1)
/*handle the ADC*/
int16 read_adc(int8 mode)
{
switch(mode)
{
case ADC_START_ONLY:
{
ADON=1;
ADGO=1;
return 0;
}
case ADC_START_AND_READ:
{
ADON=1;
ADGO=1;
while(ADGO) continue;
return (((int16)ADRESH)<<8)|((int16)ADRESL);
}
case ADC_READ_ONLY:
{
while(ADGO) continue;
return (((int16)ADRESH)<<8)|((int16)ADRESL);
}
}
return 0;
}
#ifdef EZMAC_BOARD_WITH_ADC
#pragma interrupt_level 1
#endif
void setup_adc(int8 mode)
{
ADCON0&=0x3F;
ADCON0|=mode;
}
#pragma inline(setup_adc)
void setup_adc_ports(int8 ports)
{
ADCON1=0x80|ports;
}
#pragma inline(setup_adc_ports)
void set_adc_channel(int8 ch)
{
ADCON0&=0xC0;
ADCON0|=ch;
}
#pragma inline(setup_adc_channel)
#endif
#ifdef HITECH_COMPILER
#if CLOCK_FREQ >= 12000000
#define delay_us(x) { unsigned char _dcnt; \
_dcnt = (x)*((CLOCK_FREQ)/(12000000)); \
while(--_dcnt != 0) \
continue; }
#else
#define delay_us(x) { unsigned char _dcnt; \
_dcnt = (x)/((12000000)/(CLOCK_FREQ))|1; \
while(--_dcnt != 0) \
continue; }
#endif
extern void delay_ms(unsigned char cnt);
void delay_ms(unsigned char cnt)
{
#if CLOCK_FREQ <= 2000000
do {
delay_us(996);
} while(--cnt);
#endif
#if CLOCK_FREQ > 2000000
unsigned char i;
do {
i = 4;
do {
delay_us(250);
} while(--i);
} while(--cnt);
#endif
}
#pragma inline(delay_ms)
/*handle uart*/
void init_uart(void)
{
TRISC6=0;
TRISC7=1;
BRGH = BRGH_VALUE;
SPBRG=DIVIDE;
TXEN=1;
SPEN=1;
CREN=1;
}
void putch(unsigned char ch)
{
/* output one byte */
while(!TRMT) /* set when register is empty */
continue;
TXREG = ch;
}
#ifdef EZMAC_TEST_SYSTEM
static char tx_len=0;
static char *tx_data=0;
void TX_int(void)
{
if(tx_len>0)
{
TXREG=*tx_data++;
tx_len--;
}
else
{
disable_INT_TBE;
tx_len=0;
}
}
static char buffer[32];
static char read=0;
static char write=0;
void RC_FIFO_int(void)
{
char ch;
char err=0;
if(OERR==1)
{
CREN=0;
CREN=1;
err=1;
}
if(FERR==1)
{
ch=RCREG;
err=1;
}
if(err==0)
{
ch=RCREG;
buffer[write]=ch;
write++;
write&=0x1F;
}
}
unsigned char getch(void)
{
char ch;
/* wait for one byte */
while(read==write)
continue;
ch=buffer[read];
read++;
read&=0x1F;
return ch;
}
// timer0_start(unsigned int timeout_ms) added by Balazs Scherer on 13.02.06
// maximum timeout is about 8s at 4 MHz in this configuration
void timer0_start(unsigned int timeout_ms)
{
unsigned int t_ms;
unsigned char tl,th;
disable_INT_TIMER0;
// configurating timer0
T0CON = 0x05; //
// Setting the timeout
t_ms = (unsigned int)(CLOCK_FREQ >> 18) * timeout_ms;
th = (t_ms >> 8);
tl = (unsigned char)(t_ms & 0xFF);
TMR0H = ~th;
TMR0L = ~tl;
// Starting the timer
TMR0ON = 1;
}
// timer0_stop() added by Balazs Scherer on 13.02.06
void timer0_stop(void)
{
TMR0ON = 0;
TMR0IF = 0;
}
// getch_timeout added by Balazs Scherer on 13.02.06
unsigned char getch_timeout(unsigned char* ch)
{
unsigned char error;
timer0_start(100);
/* wait for one byte or timeout*/
while( !RCIF && !TMR0IF);
if (RCIF)
{
TRISC2 = 0;
RC2 = !RC2;
error = 0;
}
else
{
error = 1;
CREN=0;
CREN=1;
}
*ch = RCREG;
timer0_stop();
return error;
}
#endif //EZMAC_TEST_SYSTEM
#endif
/**************************************************************************\
Set peripherals
\**************************************************************************/
#ifdef CCS_C_COMPILER
#pragma use delay(clock=CLOCK_FREQ) //fuses + delay sets the OSCCON also
#pragma use rs232(baud=19200,xmit=PIN_C6,rcv=PIN_C7)
#ifdef FIX_MEM_ADDRESS
#pragma reserve 0x29:0x75
#endif
#endif
/**************************************************************************\
Set port pins
\**************************************************************************/
#ifdef CCS_C_COMPILER
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -