R.J.Hamlett Guest
|
Re: #byte & C. |
Posted: Tue Jun 24, 2003 7:50 am |
|
|
:=With the preprocessor directive #byte is possible R/W directly a PIC register.
:=How is possible to use this directive ?
:=As example there is :
:=
:=#BYTE timer0 = 0x01
:=timer0 = 128;
:=
:=Whot is the number 0x01 ?
:=Do you have some real example ?
:=
:=Thank you....
With what you have written, '128', would be put into the register at address 1.
The actual meaning of this, would depend on the chip used, and for this, read the chip's data sheet.
A classic example in the past, would be to allow the AD trigger operation to be seperated from the read operation (this is now supported in the current compiler). Before this support was added, typical routines would be (addresses for a 16F87x)
#bit ADON=0x1F.2
#bit ADEN=0x1F.0
#BYTE ADCON0 = 0x1F
#BYTE ADCON1 = 0x9F
#BYTE ADRESL = 0x9E
#BYTE ADRESH = 0x1E
Then to read the AD:
value=make16(adresh,adresl);
While to trigger the AD:
ADEN=1;
ADON=1;
And to wait if it has not finished:
while (ADON) ;
Another 'classic', would be for EEPROM I/O, on the 18Fxx2, which now works, but in the past, required 'homebrew' routines for relaibility:
#byte EEADR = 0xFA9
#byte EECON1 = 0xFA6
#byte EECON2 = 0xFA7
#byte EEDATA = 0xFA8
#byte INTREG = 0xFA1
#bit GIE = 0xFF2.7
#byte INTCON = 0xFF2
int rd_eeprom(int address)
{
int temp;
int intflag;
//Send required address to chip
EEADR=address;
BIT_CLEAR(EECON1,7);
BIT_CLEAR(EECON1,6);
//save interrupt status
intflag=GIE;
//ensure interrupts are disabled
INTOFF;
//trigger read
BIT_SET(EECON1,0);
//get data
temp=EEDATA;
//re-enable interrupts if required
if (intflag) INTON;
return(temp);
}
void wrt_eeprom(int address,int value)
{
int intflag;
EEADR=address;
EEDATA=value;
BIT_CLEAR(EECON1,7);
BIT_CLEAR(EECON1,6);
BIT_SET(EECON1,2);
intflag=GIE;
INTOFF;
//send 'safety' marker to start write operation
EECON2=0x55;
EECON2=0xAA;
BIT_SET(EECON1,1);
//re-enable interrupts if required
if (intflag) INTON;
//Wait for operation to complete
while (!BIT_TEST(INTREG,4));
BIT_CLEAR(INTREG,4);
}
Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515514 |
|