📄 iai_ezmac_pic.c
字号:
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* *
* FILE NAME: IAI_EZMac_PIC.h *
* *
* DESIGNER: Kovacs, Krisztian; Lukacs, Miklos *
* PROGRAMMER: Kovacs Krisztian; *
* *
* DESCRIPTION: EZMac PIC processor dependend functions *
* *
* REVISION: 01_7 Nov 22, 2005 created by krk *
* 01_7 Dec 08, 2005 Port to HiTech *
* *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/***************************************************************************\
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*/
#define CLOCK_FREQ 4000000
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Set the hardware
//Uncomment this, and set up SPI I/O definitions in EZMac_Internal.h and in IAI_EZMac_PIC.h!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#define END_HARDWARE
/***************************************************************************\
Program start up
\***************************************************************************/
/*set processor type*/
#ifdef CCS_C_COMPILER
#ifdef END_HARDWARE
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This is a sample config setting, it should be
// changed according to the application Hardware!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <16F916.h>
#pragma device *=16
#else
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// This is the debug config setting, can be
// omitted if confusing!!!
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#include <16F876.h>
#pragma device *=16
#endif
#endif
#include "IAI_EZMac_PIC.h"
/**************************************************************************\
Set fuses
\**************************************************************************/
#ifdef CCS_C_COMPILER
#ifdef END_HARDWARE
#pragma fuses INTRC_IO,nowdt,put,noprotect,nocpd,nobrownout,noieso,NOMCLR
#else
#pragma fuses hs,nowdt,noprotect,nobrownout,put,nolvp,nowrt,nocpd
#endif
#endif
#ifdef HITECH_COMPILER
#ifdef END_HARDWARE
//__CONFIG(OSCSDIS&HS&WDTDIS&PWRTDIS&DUNPROT&UNPROTECT&BORDIS&MCLREN);
#else
__CONFIG(HS&WDTDIS&DUNPROT&BORDIS&PWRTEN&LVPDIS&WRTDIS&UNPROTECT);
#endif
#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=1;
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;
TRISC5 = 0;
RC5 = !RC5;
}
//#pragma inline(putch)
// getch added by Balazs Scherer on 17.01.06
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
/**************************************************************************\
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 + -